async def handle_devmanager_botcommand(self, request): try: json_body = json.loads(await request.text()) confserverlog.debug("BotCommand: {}".format(json_body)) randomid = "".join(random.sample(string.ascii_letters, 6)) if "toId" in json_body: # Its a command bot = bumper.bot_get(json_body["toId"]) if bot["company"] == "eco-ng" and bot["mqtt_connection"] == True: retcmd = await self.helperbot.send_command(json_body, randomid) body = retcmd confserverlog.debug( "\r\n POST: {} \r\n Response: {}".format(json_body, body) ) return web.json_response(body) else: # No response, send error back confserverlog.error( "No bots with DID: {} connected to MQTT".format( json_body["toId"] ) ) body = {"id": randomid, "errno": bumper.ERR_COMMON, "ret": "fail"} return web.json_response(body) else: if "td" in json_body: # Seen when doing initial wifi config if json_body["td"] == "PollSCResult": body = {"ret": "ok"} return web.json_response(body) except Exception as e: confserverlog.exception("{}".format(e))
def test_bot_db(): bumper.db = "tests/tmp.db" # Set db location for testing bumper.bot_add("sn_123", "did_123", "dev_123", "res_123", "co_123") assert_true(bumper.bot_get("did_123")) # Test that bot was added to db bumper.bot_set_nick("did_123", "nick_123") assert_equals(bumper.bot_get("did_123")["nick"], "nick_123") # Test that nick was added to bot bumper.bot_set_mqtt("did_123", True) assert_true(bumper.bot_get("did_123") ["mqtt_connection"]) # Test that mqtt was set True for bot bumper.bot_set_xmpp("did_123", True) assert_true(bumper.bot_get("did_123") ["xmpp_connection"]) # Test that xmpp was set True for bot bumper.bot_remove("did_123") assert_false(bumper.bot_get("did_123")) # Test that bot is no longer in db
async def handle_RemoveBot(self, request): try: did = request.match_info.get("did", "") bumper.bot_remove(did) if bumper.bot_get(did): return web.json_response({"status": "failed to remove bot"}) else: return web.json_response( {"status": "successfully removed bot"}) except Exception as e: confserverlog.exception("{}".format(e)) pass
async def handle_devmanager_botcommand(self, request): try: json_body = json.loads(await request.text()) randomid = "".join(random.sample(string.ascii_letters, 4)) did = "" if "toId" in json_body: # Its a command did = json_body["toId"] if did != "": bot = bumper.bot_get(did) if bot["company"] == "eco-ng": retcmd = await bumper.mqtt_helperbot.send_command( json_body, randomid ) body = retcmd logging.debug("Send Bot - {}".format(json_body)) logging.debug("Bot Response - {}".format(body)) return web.json_response(body) else: # No response, send error back logging.error( "No bots with DID: {} connected to MQTT".format( json_body["toId"] ) ) body = { "id": randomid, "errno": 500, "ret": "fail", "debug": "wait for response timed out", } return web.json_response(body) else: if "td" in json_body: # Seen when doing initial wifi config if json_body["td"] == "PollSCResult": body = {"ret": "ok"} return web.json_response(body) if json_body["td"] == "HasUnreadMsg": # EcoVacs Home body = {"ret": "ok", "unRead": False} return web.json_response(body) if json_body["td"] == "PreWifiConfig": # EcoVacs Home body = {"ret":"ok"} return web.json_response(body) except Exception as e: logging.exception("{}".format(e))
async def on_broker_client_disconnected(self, client_id): didsplit = str(client_id).split("@") bot = bumper.bot_get(didsplit[0]) if bot: bumper.bot_set_mqtt(bot["did"], False) return clientresource = didsplit[1].split("/")[1] client = bumper.client_get(clientresource) if client: bumper.client_set_mqtt(client["resource"], False) return
def _handle_bind(self, xml): try: bot = bumper.bot_get(self.uid) if bot: bumper.bot_set_xmpp(bot["did"], True) client = bumper.client_get(self.clientresource) if client: bumper.client_set_xmpp(client["resource"], True) clientbindxml = xml.getchildren() clientresourcexml = clientbindxml[0].getchildren() if self.devclass: # its a bot self.name = "XMPP_Client_{}_{}".format(self.uid, self.devclass) self.bumper_jid = "{}@{}.ecorobot.net/atom".format( self.uid, self.devclass ) xmppserverlog.debug("new bot {}".format(self.uid)) res = '<iq type="result" id="{}"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>{}</jid></bind></iq>'.format( xml.get("id"), self.bumper_jid ) elif len(clientresourcexml) > 0: self.clientresource = clientresourcexml[0].text self.name = "XMPP_Client_{}".format(self.clientresource) self.bumper_jid = "{}@{}/{}".format( self.uid, XMPPServer.server_id, self.clientresource ) xmppserverlog.debug( "new client {} using resource {}".format( self.uid, self.clientresource ) ) res = '<iq type="result" id="{}"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>{}</jid></bind></iq>'.format( xml.get("id"), self.bumper_jid ) else: self.name = "XMPP_Client_{}_{}".format(self.uid, self.address) self.bumper_jid = "{}@{}".format(self.uid, XMPPServer.server_id) xmppserverlog.debug("new client {}".format(self.uid)) res = '<iq type="result" id="{}"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>{}</jid></bind></iq>'.format( xml.get("id"), self.bumper_jid ) self._set_state("BIND") self.send(res) except Exception as e: xmppserverlog.exception("{}".format(e))
def _disconnect(self): try: bot = bumper.bot_get(self.uid) if bot: bumper.bot_set_xmpp(bot["did"], False) client = bumper.client_get(self.clientresource) if client: bumper.client_set_xmpp(client["resource"], False) self.transport.close() except Exception as e: xmppserverlog.error("{}".format(e))
async def on_broker_client_disconnected(self, client_id): try: didsplit = str(client_id).split("@") bot = bumper.bot_get(didsplit[0]) if bot: bumper.bot_set_mqtt(bot["did"], False) #clientuserid = didsplit[0] clientresource = didsplit[1].split("/")[1] client = bumper.client_get(clientresource) if client: bumper.client_set_mqtt(client["resource"], False) except Exception as e: mqttserverlog.exception("{}".format(e))
async def handle_lg_log(self, request): # EcoVacs Home try: json_body = json.loads(await request.text()) randomid = "".join(random.sample(string.ascii_letters, 6)) did = json_body["did"] botdetails = bumper.bot_get(did) if botdetails: if not "cmdName" in json_body: if "td" in json_body: json_body["cmdName"] = json_body["td"] if not "toId" in json_body: json_body["toId"] = did if not "toType" in json_body: json_body["toType"] = botdetails["class"] if not "toRes" in json_body: json_body["toRes"] = botdetails["resource"] if not "payloadType" in json_body: json_body["payloadType"] = "x" if not "payload" in json_body: #json_body["payload"] = "" if json_body["td"] == "GetCleanLogs": json_body["td"] = "q" json_body["payload"] = '<ctl count="30"/>' if did != "": bot = bumper.bot_get(did) if bot["company"] == "eco-ng": retcmd = await bumper.mqtt_helperbot.send_command( json_body, randomid) body = retcmd logging.debug("Send Bot - {}".format(json_body)) logging.debug("Bot Response - {}".format(body)) logs = [] logsroot = ET.fromstring(retcmd["resp"]) if logsroot.attrib["ret"] == "ok": cleanlogs = logsroot.getchildren() for l in cleanlogs: cleanlog = { "ts": l.attrib['s'], "area": l.attrib['a'], "last": l.attrib['l'], "cleanType": l.attrib['t'], #imageUrl allows for providing images of cleanings, something to look into later #"imageUrl": "https://localhost:8007", } logs.append(cleanlog) body = { "ret": "ok", "logs": logs, } else: body = {"ret": "ok", "logs": []} logging.debug("lg logs return: {}".format( json.dumps(body))) return web.json_response(body) else: # No response, send error back logging.error( "No bots with DID: {} connected to MQTT".format( json_body["toId"])) body = { "id": randomid, "errno": bumper.ERR_COMMON, "ret": "fail" } return web.json_response(body) except Exception as e: logging.exception("{}".format(e))