Try except

Try except
This commit is contained in:
Brian Martin 2019-02-05 22:08:51 -05:00
parent 8e1ba3a475
commit eb4cb2b996
4 changed files with 456 additions and 313 deletions

View file

@ -36,17 +36,19 @@ class MQTTHelperBot():
else:
self.run_helperbot()
except:
logging.exception("Exception")
except Exception as e:
logging.error('Helperbot: {}'.format(e))
pass
def run_helperbot(self, loop):
asyncio.set_event_loop(loop)
self.Client = MQTTClient(client_id=self.client_id, config={'check_hostname':False})
loop.run_until_complete(self.start_helper_bot())
loop.run_until_complete(self.get_msg())
loop.run_forever()
try:
asyncio.set_event_loop(loop)
self.Client = MQTTClient(client_id=self.client_id, config={'check_hostname':False})
loop.run_until_complete(self.start_helper_bot())
loop.run_until_complete(self.get_msg())
loop.run_forever()
except Exception as e:
logging.error('Helperbot: {}'.format(e))
async def start_helper_bot(self):
@ -56,8 +58,11 @@ class MQTTHelperBot():
('iot/p2p/+/+/+/+/helper1/bumper/helper1/+/+/+',QOS_0),
('iot/p2p/+',QOS_0)
])
except hbmqtt.client.ClientException as ce:
logging.exception("Client exception: %s" % ce)
except Exception as e:
logging.error('Helperbot: {}'.format(e))
#except hbmqtt.client.ClientException as ce:
# logging.exception("Client exception: %s" % ce)
async def get_msg(self):
@ -78,48 +83,58 @@ class MQTTHelperBot():
cresp.append({"time": time.time() ,"topic": message.topic,"payload":str(message.data.decode("utf-8"))})
self.command_responses.set(cresp)
logging.debug("MQTT Command Response List Count: %s" %len(cresp))
except hbmqtt.client.ClientException as ce:
logging.error("Client exception: %s" % ce)
except Exception as e:
logging.error('Helperbot: {}'.format(e))
#except hbmqtt.client.ClientException as ce:
# logging.error("Client exception: %s" % ce)
async def wait_for_resp(self, requestid):
t_end = (datetime.now() + timedelta(seconds=10)).timestamp()
while time.time() < t_end:
await asyncio.sleep(0.1)
responses = self.command_responses.get()
if len(responses) > 0:
for msg in responses:
topic = str(msg['topic']).split("/")
if (topic[6] == "helper1" and topic[10] == requestid):
logging.debug('VacBot MQTT Response: Topic: %s Payload: %s' % (msg['topic'], msg['payload']))
if topic[11] == "j":
resppayload = json.loads(msg['payload'])
else:
resppayload = str(msg['payload'])
resp = {
"id": requestid,
"ret": "ok",
"resp": resppayload
}
cresp = self.command_responses.get()
cresp.remove(msg)
self.command_responses.set(cresp)
return resp
try:
t_end = (datetime.now() + timedelta(seconds=10)).timestamp()
while time.time() < t_end:
await asyncio.sleep(0.1)
responses = self.command_responses.get()
if len(responses) > 0:
for msg in responses:
topic = str(msg['topic']).split("/")
if (topic[6] == "helper1" and topic[10] == requestid):
logging.debug('VacBot MQTT Response: Topic: %s Payload: %s' % (msg['topic'], msg['payload']))
if topic[11] == "j":
resppayload = json.loads(msg['payload'])
else:
resppayload = str(msg['payload'])
resp = {
"id": requestid,
"ret": "ok",
"resp": resppayload
}
cresp = self.command_responses.get()
cresp.remove(msg)
self.command_responses.set(cresp)
return resp
return { "id": requestid, "errno": "timeout", "ret": "fail" }
return { "id": requestid, "errno": "timeout", "ret": "fail" }
except Exception as e:
logging.error('Helperbot: {}'.format(e))
async def send_command(self, cmdjson, requestid):
ttopic = "iot/p2p/{}/helper1/bumper/helper1/{}/{}/{}/q/{}/{}".format(cmdjson["cmdName"],
cmdjson["toId"], cmdjson["toType"], cmdjson["toRes"], requestid, cmdjson["payloadType"])
try:
await self.Client.publish(ttopic, str(cmdjson["payload"]).encode(),QOS_0)
except:
logging.exception("Exception at send_command")
resp = await self.wait_for_resp(requestid)
return resp
try:
ttopic = "iot/p2p/{}/helper1/bumper/helper1/{}/{}/{}/q/{}/{}".format(cmdjson["cmdName"],
cmdjson["toId"], cmdjson["toType"], cmdjson["toRes"], requestid, cmdjson["payloadType"])
try:
await self.Client.publish(ttopic, str(cmdjson["payload"]).encode(),QOS_0)
except:
logging.exception("Exception at send_command")
resp = await self.wait_for_resp(requestid)
return resp
except Exception as e:
logging.error('Helperbot: {}'.format(e))
class MQTTServer():
@ -127,25 +142,38 @@ class MQTTServer():
bumper_clients = []
bumper_bots = []
async def broker_coro(self):
broker = hbmqtt.broker.Broker(config=self.default_config)
await broker.start()
async def broker_coro(self):
try:
broker = hbmqtt.broker.Broker(config=self.default_config)
await broker.start()
except PermissionError as e:
if "bind" in e.strerror:
logging.exception("Error binding mqttserver, exiting. Try using a different hostname or IP.\r\n {}".format(e))
exit(1)
async def active_bot_listing(self):
while True:
await asyncio.sleep(5)
logging.debug('Connected bots: %s' % self.bumper_bots.get())
except Exception as e:
logging.exception('MQTTServer: {}'.format(e))
exit(1)
async def active_bot_listing(self):
try:
while True:
await asyncio.sleep(5)
logging.debug('Connected bots: %s' % self.bumper_bots.get())
except Exception as e:
logging.error('MQTTServer: {}'.format(e))
def __init__(self, address, run_async=False, bumper_bots=contextvars.ContextVar, bumper_clients=contextvars.ContextVar):
#The below adds a plugin to the hbmqtt.broker.plugins without having to futz with setup.py
distribution = pkg_resources.Distribution("hbmqtt.broker.plugins")
bumper_plugin = pkg_resources.EntryPoint.parse('bumper = bumper.mqttserver:BumperMQTTServer_Plugin', dist=distribution)
distribution._ep_map = {"hbmqtt.broker.plugins": {"bumper": bumper_plugin}}
pkg_resources.working_set.add(distribution)
self.bumper_bots = bumper_bots
self.bumper_clients = bumper_clients
try:
try:
#The below adds a plugin to the hbmqtt.broker.plugins without having to futz with setup.py
distribution = pkg_resources.Distribution("hbmqtt.broker.plugins")
bumper_plugin = pkg_resources.EntryPoint.parse('bumper = bumper.mqttserver:BumperMQTTServer_Plugin', dist=distribution)
distribution._ep_map = {"hbmqtt.broker.plugins": {"bumper": bumper_plugin}}
pkg_resources.working_set.add(distribution)
self.bumper_bots = bumper_bots
self.bumper_clients = bumper_clients
# Initialize bot server
self.default_config = {
'listeners': {
@ -185,17 +213,24 @@ class MQTTServer():
else:
self.run_server()
except Exception as e:
logging.error('MQTTServer: {}'.format(e))
except:
logging.exception("Exception")
pass
#except:
# logging.exception("Exception")
# pass
def run_server(self, loop):
asyncio.set_event_loop(loop)
loop.run_until_complete(self.broker_coro())
#loop.run_until_complete(self.active_bot_listing())
loop.run_forever()
def run_server(self, loop):
try:
asyncio.set_event_loop(loop)
loop.run_until_complete(self.broker_coro())
#loop.run_until_complete(self.active_bot_listing())
loop.run_forever()
except Exception as e:
logging.error('MQTTServer: {}'.format(e))
class BumperMQTTServer_Plugin:
def __init__(self, context):
@ -204,73 +239,81 @@ class BumperMQTTServer_Plugin:
self.clients = self.context.config['clients']
except KeyError:
self.context.logger.warning("'clients' section not found in context configuration")
except Exception as e:
logging.error('MQTTServer: {}'.format(e))
async def on_broker_client_connected(self, client_id):
logging.debug('Bumper Connection: %s connected' % client_id)
connected_bots = self.clients['connected_bots'].get()
connected_clients = self.clients['connected_clients'].get()
didsplit = str(client_id).split("@")
#If this isn't a fake user (fuid) then add as a bot
if not (str(didsplit[0]).startswith("fuid") or str(didsplit[0]).startswith("helper")):
tmpbotdetail = str(didsplit[1]).split("/")
newbot = bumper.VacBotDevice()
newbot.did = didsplit[0]
newbot.vac_bot_device_class = tmpbotdetail[0]
newbot.resource = tmpbotdetail[1]
botactive = False
for bot in connected_bots:
if bot['did'] == newbot.did:
botactive = True
if botactive == False:
connected_bots.append(newbot.asdict())
logging.info("Adding bot to list: {}".format(newbot.asdict()))
self.clients['connected_bots'].set(connected_bots)
else:
tmpuserdetail = str(didsplit[1]).split("/")
newuser = bumper.VacBotUser()
newuser.userid = didsplit[0]
newuser.realm = tmpuserdetail[0]
newuser.resource = tmpuserdetail[1]
clientactive = False
for client in connected_clients:
if client['userid'] == newuser.userid:
clientactive = True
if clientactive == False:
connected_clients.append(newuser.asdict())
logging.info("Adding client to list: {}".format(newuser.asdict()))
self.clients['connected_clients'].set(connected_clients)
try:
logging.debug('Bumper Connection: %s connected' % client_id)
connected_bots = self.clients['connected_bots'].get()
connected_clients = self.clients['connected_clients'].get()
didsplit = str(client_id).split("@")
#If this isn't a fake user (fuid) then add as a bot
if not (str(didsplit[0]).startswith("fuid") or str(didsplit[0]).startswith("helper")):
tmpbotdetail = str(didsplit[1]).split("/")
newbot = bumper.VacBotDevice()
newbot.did = didsplit[0]
newbot.vac_bot_device_class = tmpbotdetail[0]
newbot.resource = tmpbotdetail[1]
botactive = False
for bot in connected_bots:
if bot['did'] == newbot.did:
botactive = True
if botactive == False:
connected_bots.append(newbot.asdict())
logging.info("Adding bot to list: {}".format(newbot.asdict()))
self.clients['connected_bots'].set(connected_bots)
else:
tmpuserdetail = str(didsplit[1]).split("/")
newuser = bumper.VacBotUser()
newuser.userid = didsplit[0]
newuser.realm = tmpuserdetail[0]
newuser.resource = tmpuserdetail[1]
clientactive = False
for client in connected_clients:
if client['userid'] == newuser.userid:
clientactive = True
if clientactive == False:
connected_clients.append(newuser.asdict())
logging.info("Adding client to list: {}".format(newuser.asdict()))
self.clients['connected_clients'].set(connected_clients)
logging.debug('Connected Bots: %s' %self.clients['connected_bots'].get())
logging.debug('Connected Clients: %s' %self.clients['connected_clients'].get())
logging.debug('Connected Bots: %s' %self.clients['connected_bots'].get())
logging.debug('Connected Clients: %s' %self.clients['connected_clients'].get())
except Exception as e:
logging.error('MQTTServer: {}'.format(e))
async def on_broker_client_disconnected(self, client_id):
logging.debug('Bumper Connection: %s disconnected' % client_id)
connected_bots = self.clients['connected_bots'].get()
connected_clients = self.clients['connected_clients'].get()
didsplit = str(client_id).split("@")
#If the did is in the list, remove it
for bot in connected_bots:
if didsplit[0] == bot['did']:
logging.info("Removing bot from list: {}".format(bot['did']))
connected_bots.remove(bot)
self.clients['connected_bots'].set(connected_bots)
try:
logging.debug('Bumper Connection: %s disconnected' % client_id)
connected_bots = self.clients['connected_bots'].get()
connected_clients = self.clients['connected_clients'].get()
didsplit = str(client_id).split("@")
#If the did is in the list, remove it
for bot in connected_bots:
if didsplit[0] == bot['did']:
logging.info("Removing bot from list: {}".format(bot['did']))
connected_bots.remove(bot)
self.clients['connected_bots'].set(connected_bots)
logging.debug('Connected Bots: %s' %self.clients['connected_bots'].get())
logging.debug('Connected Bots: %s' %self.clients['connected_bots'].get())
for client in connected_clients:
if didsplit[0] == client['userid']:
logging.info("Removing client from list: {}".format(client['userid']))
connected_clients.remove(client)
self.clients['connected_clients'].set(connected_clients)
for client in connected_clients:
if didsplit[0] == client['userid']:
logging.info("Removing client from list: {}".format(client['userid']))
connected_clients.remove(client)
self.clients['connected_clients'].set(connected_clients)
logging.debug('Connected Clients: %s' %self.clients['connected_clients'].get())
logging.debug('Connected Clients: %s' %self.clients['connected_clients'].get())
except Exception as e:
logging.error('MQTTServer: {}'.format(e))