Hello I'am tring to send a cURL command to a Orion Context Broker via a Python script.
I am running the script on OpenWRT, so i am not able to install the requests or urllib2 library, for memory issues, additionally libraries like subprocess fail to compile. So I am using os.system() to execute the cURL command. This is the code of the script:
import sys
import os
from urllib import urlencode
sys.path.insert(0, '/usr/lib/python2.7/bridge')
from bridgeclient import BridgeClient as bridgeclient
value = bridgeclient()
header="(curl 10.130.1.228:1026/v1/updateContext -s -S --header 'Content-Type: application/json' \
--header 'Accept: application/json' -d @- | python -mjson.tool) <<EOF"
json_string="""
{
"contextElements": [
{
"type": "Room",
"isPattern": "false",
"id": "R1",
"attributes": [
{
"name": "temperature",
"type": "float",
"value": "firstValue"
},
{
"name": "pressure",
"type": "float",
"value": "secondValue"
}
]
}
],
"updateAction": "UPDATE"
}
EOF"""
while(True):
all=value.getall()
sentValue1=""
sentValue2=""
if all['Tmp'] is None:
sentValue1=all['Tmp']
else:
sentValue1="NoValue"
if all['Prs'] is None:
sentValue2=all['Prs']
else:
sentValue2="NoValue"
json_string=json_string.replace("firstValue",sentValue1)
json_string=json_string.replace("secondValue",sentValue2)
os.system(header+json_string)
If I copy and paste the command i give to os.system(), as it is in the terminal window, everything works smoothly and my Orion instance gets updated. But if i run the same command via the said script i get this response from the server:
{
"errorCode": {
"code": "400",
"details": "JSON Parse Error",
"reasonPhrase": "Bad Request"
}
}
I think is some formatting issue and i have tried everything to make it work but with no luck.
UPDATE:
I found in the contextBroker log this message:
from=10.130.1.1 | srv=pending | subsrv=<defalut> | comp=Orion |
op=AlarmMenager.cpp[405]:badInput | msg=Releasing alarm BadInput
10.130.1.1: JSON Parse Error: unspecified file(1):expected end of input
and this one:
from=10.130.1.1 | srv=pending | subsrv=<defalut> | comp=Orion |
op=AlarmMenager.cpp[405]:badInput | msg=Releasing alarm BadInput
10.130.1.1: JSON Parse Error: unspecified file(1):expected object
Both repeated for each cURL request I made.
UPDATE 2:
I managed to make subprocess.call() to work, but it gives the exact same response.
/usr/bin/pythonin yourheadervariable?pycurlif you can. However to address the issue. Useimport jsonand then instead of writing a string create yourjson_stringas a python dictionary and usejson.dumps(json_string)