From e233fe933e4a102f462eb5bf7eaa169b97a00504 Mon Sep 17 00:00:00 2001 From: Brian Martin Date: Tue, 1 Oct 2019 16:27:58 -0400 Subject: [PATCH] send_command json instead of str send_command json instead of str --- bumper/mqttserver.py | 13 ++++++--- tests/test_mqttserver.py | 58 +++++++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/bumper/mqttserver.py b/bumper/mqttserver.py index 6f06a23..14e7b49 100644 --- a/bumper/mqttserver.py +++ b/bumper/mqttserver.py @@ -183,9 +183,16 @@ class MQTTHelperBot: cmdjson["payloadType"], ) try: - await self.Client.publish( - ttopic, str(cmdjson["payload"]).encode(), QOS_0 - ) + if cmdjson["toType"] == "x": + await self.Client.publish( + ttopic, str(cmdjson["payload"]).encode(), QOS_0 + ) + + if cmdjson["toType"] == "j": + await self.Client.publish( + ttopic, json.dumps(cmdjson["payload"]).encode(), QOS_0 + ) + except Exception as e: helperbotlog.exception("{}".format(e)) diff --git a/tests/test_mqttserver.py b/tests/test_mqttserver.py index 0f0ccb2..c685e15 100644 --- a/tests/test_mqttserver.py +++ b/tests/test_mqttserver.py @@ -280,8 +280,9 @@ async def test_helperbot_sendcommand(): "ret": "ok", } - mqtt_helperbot.Client.disconnect() + #mqtt_helperbot.Client.disconnect() + # Test GetLifeSpan (xml command) cmdjson = { "toType": "ls1ok3", "payloadType": "x", @@ -303,11 +304,7 @@ async def test_helperbot_sendcommand(): 0.2 ) # Override wait_resp_timeout (so we don't wait 10 seconds for timeout) # Send response beforehand - msg_payload = ( - "{'id': 'testx', 'ret': 'ok', 'resp': " - "" - "}" - ) + msg_payload = "" msg_topic_name = ( "iot/p2p/GetLifeSpan/bot_serial/ls1ok3/wC3g/helper1/bumper/helper1/p/testx/q" ) @@ -318,7 +315,54 @@ async def test_helperbot_sendcommand(): commandresult = await mqtt_helperbot.send_command(cmdjson, "testx") assert commandresult == { "id": "testx", - "resp": "{'id': 'testx', 'ret': 'ok', 'resp': }", + "resp": "", + "ret": "ok", + } + + # Test json payload (OZMO950) + cmdjson = { + "toType": "ls1ok3", + "payloadType": "j", + "toRes": "wC3g", + "payload": { + "header": { + "pri": 1, + "ts": "1569380075887", + "tzm": -240, + "ver": "0.0.50" + } + }, + "td": "q", + "toId": "bot_serial", + "cmdName": "getStats", + "auth": { + "token": "us_52cb21fef8e547f38f4ec9a699a5d77e", + "resource": "IOSF53D07BA", + "userid": "fuid_tmpuser", + "with": "users", + "realm": "ecouser.net", + }, + } + + mqtt_helperbot.wait_resp_timeout_seconds = ( + 0.2 + ) # Override wait_resp_timeout (so we don't wait 10 seconds for timeout) + # Send response beforehand + msg_payload = ( + '{"body":{"code":0,"data":{"area":0,"cid":"111","start":"1569378657","time":6,"type":"auto"},"msg":"ok"},"header":{"fwVer":"1.6.4","hwVer":"0.1.1","pri":1,"ts":"1569380074036","tzm":480,"ver":"0.0.1"}}' + ) + msg_topic_name = ( + "iot/p2p/getStats/bot_serial/ls1ok3/wC3g/helper1/bumper/helper1/p/testj/j" + ) + await mqtt_helperbot.Client.publish( + msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0 + ) + + commandresult = await mqtt_helperbot.send_command(cmdjson, "testj") + + assert commandresult == { + "id": "testj", + "resp": {'body':{'code':0,'data':{'area':0,'cid':'111','start':'1569378657','time':6,'type':'auto'},'msg':'ok'},'header':{'fwVer':'1.6.4','hwVer':'0.1.1','pri':1,'ts':'1569380074036','tzm':480,'ver':'0.0.1'}}, "ret": "ok", }