I have a sensor system. The sensors receive commands from me, do something and then send a response to me.
The response is like this:
seq. number | net_id | opcode_group | opcode | payloadlength | val
where I have these values delimited by a space character.
Now I want to take the last value named val
. In this part, I have all the information I want to know to elaborate the response from the sensors.
For example, I have this response for the command that wants to know the IEEE MAC address of the sensor:
In this case val
is all the fields after Length
in the response. There are not separation, but I have a sort of string.
All I have to do is to split this array/string of numbers, just knowing only the length of every field. For ex. the status is 1 byte, the MAC address 8 byte, and so on...
My code is this:
if response.error:
ret['error'] = 'Error while retrieving unregistered sensors'
else:
for line in response.body.split("\n"):
if line != "":
value = int(line.split(" ")[6])
ret['response'] = value
self.write(tornado.escape.json_encode(ret))
self.finish()
if command == 'IDENTIFY':
status = value.split(" ")[0]
IEEEAddrRemoteDev = value.split(" ")[1]
NWKAddrRemoteDev = value.split(" ")[2]
NumOfAssociatedDevice = value.split(" ")[3]
StartIndex = value.split(" ")[4]
ListOfShortAddress = value.split(" ")[5]
if status == 0x00:
ret['success'] = "The %s command has been succesfully sent! \
IEEE address: %s" % % (command.upper(), IEEEAddrRemoteDev)
self.write(tornado.escape.json_encode(ret))
elif status == 0x80:
ret['success'] = "Invalid Request Type"
self.write(tornado.escape.json_encode(ret))
elif status == 0x81:
ret['success'] = "Device Not Found"
self.write(tornado.escape.json_encode(ret))
where in the first part I take the 6th value from the entire response and I put this in the variable value
. After this I want to split this variable in every component.
For ex. this status = value.split(" ")[0]
in which way I have to split????
Thank you very much for the help!