Beginning bumper's new journey #4

Merged
bmartin5692 merged 37 commits from dev into master 2019-02-22 14:34:25 +01:00
4 changed files with 92 additions and 76 deletions
Showing only changes of commit d316862120 - Show all commits

View file

@ -86,6 +86,8 @@ class VacBotDevice(object):
self.name = name self.name = name
self.nick = nick self.nick = nick
self.resource = resource self.resource = resource
self.mqtt_connection = False
self.xmpp_connection = False
def asdict(self): def asdict(self):
return {"class": self.vac_bot_device_class, "company": self.company, return {"class": self.vac_bot_device_class, "company": self.company,
@ -96,6 +98,8 @@ class VacBotClient(object):
self.userid = userid self.userid = userid
self.realm = realm self.realm = realm
self.resource = token self.resource = token
self.mqtt_connection = False
self.xmpp_connection = False
def asdict(self): def asdict(self):
return {"userid": self.userid,"realm": self.realm,"resource": self.resource} return {"userid": self.userid,"realm": self.realm,"resource": self.resource}

View file

@ -356,8 +356,11 @@ class ConfServer():
elif todo == 'GetDeviceList': elif todo == 'GetDeviceList':
active_bots = self.bumper_bots.get() active_bots = self.bumper_bots.get()
bot_list = []
for bot in active_bots:
bot_list.append(bot.asdict())
body = { body = {
"devices": active_bots, "devices": bot_list,
"result": "ok", "result": "ok",
"todo": "result" "todo": "result"
} }
@ -365,8 +368,8 @@ class ConfServer():
elif todo == 'SetDeviceNick': elif todo == 'SetDeviceNick':
bots = self.bumper_bots.get() bots = self.bumper_bots.get()
for bot in bots: for bot in bots:
if postbody['did'] == bot['did']: if postbody['did'] == bot.did:
bot['nick'] = postbody['nick'] bot.nick = postbody['nick']
self.bumper_bots.set(bots) self.bumper_bots.set(bots)
body = { body = {
"result": "ok", "result": "ok",

View file

@ -225,7 +225,6 @@ class MQTTServer():
mqttserverlog.exception('{}'.format(e)) mqttserverlog.exception('{}'.format(e))
def run(self, run_async=False,): def run(self, run_async=False,):
if run_async: if run_async:
sloop = asyncio.new_event_loop() sloop = asyncio.new_event_loop()
mqttserverlog.debug("Starting MQTTServer Thread: 1") mqttserverlog.debug("Starting MQTTServer Thread: 1")
@ -236,7 +235,6 @@ class MQTTServer():
else: else:
self.run_server() self.run_server()
def run_server(self, loop): def run_server(self, loop):
logging.info("Starting MQTT Server at {}".format(self.address)) logging.info("Starting MQTT Server at {}".format(self.address))
@ -292,9 +290,16 @@ class BumperMQTTServer_Plugin:
newbot.name = username newbot.name = username
newbot.vac_bot_device_class = tmpbotdetail[0] newbot.vac_bot_device_class = tmpbotdetail[0]
newbot.resource = tmpbotdetail[1] newbot.resource = tmpbotdetail[1]
bumper_bots.append(newbot.asdict()) existingbot = False
mqttserverlog.info("new bot authenticated {}".format(newbot.asdict())) for bot in bumper_bots:
self.bumper_config['bumper_bots'].set(bumper_bots) if bot.did == newbot.did:
existingbot = True
if existingbot == False:
bumper_bots.append(newbot)
mqttserverlog.info("new bot authenticated {}".format(newbot.name))
self.bumper_config['bumper_bots'].set(bumper_bots)
authenticated = True authenticated = True
else: else:
@ -307,9 +312,16 @@ class BumperMQTTServer_Plugin:
newclient.userid = didsplit[0] newclient.userid = didsplit[0]
newclient.realm = tmpclientdetail[0] newclient.realm = tmpclientdetail[0]
newclient.resource = tmpclientdetail[1] newclient.resource = tmpclientdetail[1]
bumper_clients.append(newclient.asdict()) existingclient = False
mqttserverlog.info("new client authenticated {}".format(newclient.asdict())) for client in bumper_clients:
self.bumper_config['bumper_clients'].set(bumper_clients) if client.userid == newclient.userid:
existingclient = True
if existingclient == False:
bumper_clients.append(newclient)
mqttserverlog.info("new client authenticated {}".format(newclient.userid))
self.bumper_config['bumper_clients'].set(bumper_clients)
authenticated = True authenticated = True
else: else:
@ -323,81 +335,45 @@ class BumperMQTTServer_Plugin:
async def on_broker_client_connected(self, client_id): async def on_broker_client_connected(self, client_id):
try: try:
#mqttserverlog.debug('%s connected' % client_id)
bumper_users = self.bumper_config['bumper_users'].get() bumper_users = self.bumper_config['bumper_users'].get()
bumper_bots = self.bumper_config['bumper_bots'].get() bumper_bots = self.bumper_config['bumper_bots'].get()
bumper_clients = self.bumper_config['bumper_clients'].get() bumper_clients = self.bumper_config['bumper_clients'].get()
didsplit = str(client_id).split("@") 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 bumper_bots:
# if bot['did'] == newbot.did:
# botactive = True
#if botactive == False:
# bumper_bots.append(newbot.asdict())
mqttserverlog.debug("bot connected {}".format(newbot.did))
#self.bumper_config['bumper_bots'].set(bumper_bots)
else: for bot in bumper_bots:
tmpclientdetail = str(didsplit[1]).split("/") if didsplit[0] == bot.did:
newclient = bumper.VacBotClient() bot.mqtt_connection = True
newclient.userid = didsplit[0] mqttserverlog.info("bot connected {}".format(bot.did))
newclient.realm = tmpclientdetail[0] self.bumper_config['bumper_bots'].set(bumper_bots)
newclient.resource = tmpclientdetail[1]
#clientactive = False
#for client in bumper_clients:
# if client['userid'] == newuser.userid:
# clientactive = True
#if clientactive == False and newuser.userid != 'helper1':
#bumper_clients.append(newuser.asdict())
mqttserverlog.debug("client connected {}".format(newclient.userid))
#self.bumper['bumper_clients'].set(bumper_clients)
#mqttserverlog.debug('Connected Bots: %s' %self.clients['connected_bots'].get()) for client in bumper_clients:
#mqttserverlog.debug('Connected Clients: %s' %self.clients['connected_clients'].get()) if didsplit[0] == client.userid and client.userid != 'helper1':
client.mqtt_connection = True
mqttserverlog.info("client connected {}".format(client.userid))
self.bumper_config['bumper_clients'].set(bumper_clients)
except Exception as e: except Exception as e:
mqttserverlog.exception('{}'.format(e)) mqttserverlog.exception('{}'.format(e))
async def on_broker_client_disconnected(self, client_id): async def on_broker_client_disconnected(self, client_id):
try: try:
#mqttserverlog.debug('%s disconnected' % client_id)
bumper_users = self.bumper_config['bumper_users'].get() bumper_users = self.bumper_config['bumper_users'].get()
bumper_bots = self.bumper_config['bumper_bots'].get() bumper_bots = self.bumper_config['bumper_bots'].get()
bumper_clients = self.bumper_config['bumper_clients'].get() bumper_clients = self.bumper_config['bumper_clients'].get()
#remove_clients = self.clients['remove_clients'].get()
didsplit = str(client_id).split("@") didsplit = str(client_id).split("@")
#If the did is in the list, remove it
for bot in bumper_bots: for bot in bumper_bots:
if didsplit[0] == bot['did']: if didsplit[0] == bot.did:
mqttserverlog.info("bot disconnected {}".format(bot['did'])) bot.mqtt_connection = False
#bumper_bots.remove(bot) mqttserverlog.info("bot disconnected {}".format(bot.did))
#remove_clients.append(bot['did']) self.bumper_config['bumper_bots'].set(bumper_bots)
#self.clients['bumper_bots'].set(bumper_bots)
for client in bumper_clients: for client in bumper_clients:
if didsplit[0] == client['userid'] and client['userid'] != 'helper1': if didsplit[0] == client.userid and client.userid != 'helper1':
mqttserverlog.info("client disconnected {}".format(client['userid'])) client.mqtt_connection = False
#bumper_clients.remove(client) mqttserverlog.info("client disconnected {}".format(client.userid))
#remove_clients.append(client['userid']) self.bumper_config['bumper_clients'].set(bumper_clients)
#self.bumper_config['bumper_clients'].set(bumper_clients)
#self.clients['remove_clients'].set(remove_clients)
#mqttserverlog.debug('Connected Bots: %s' %self.clients['connected_bots'].get())
#mqttserverlog.debug('Connected Clients: %s' %self.clients['connected_clients'].get())
except Exception as e: except Exception as e:
mqttserverlog.exception('{}'.format(e)) mqttserverlog.exception('{}'.format(e))

View file

@ -67,7 +67,7 @@ class XMPPServer():
xmppserverlog.debug('starting new client with ip {}'.format(client_address[0])) xmppserverlog.debug('starting new client with ip {}'.format(client_address[0]))
thread_id = uuid.uuid4() thread_id = uuid.uuid4()
client = Client(thread_id, connection, client_address) client = Client(thread_id, connection, client_address, self.bumper_users, self.bumper_bots, self.bumper_clients)
client.setDaemon(True) client.setDaemon(True)
client.start() client.start()
self.clients.append(client) self.clients.append(client)
@ -138,7 +138,7 @@ class Client(threading.Thread):
BOT = 1 BOT = 1
CONTROLLER = 2 CONTROLLER = 2
def __init__(self, thread_id, connection, client_address): def __init__(self, thread_id, connection, client_address,bumper_users=contextvars.ContextVar, bumper_bots=contextvars.ContextVar, bumper_clients=contextvars.ContextVar):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.id = thread_id self.id = thread_id
self.name = "XMPP_Client_{}".format(client_address[0]) self.name = "XMPP_Client_{}".format(client_address[0])
@ -150,6 +150,9 @@ class Client(threading.Thread):
self.uid = "" self.uid = ""
self.log_sent_message = False #Set to true to log sends self.log_sent_message = False #Set to true to log sends
self.log_incoming_data = True #Set to true to log sends self.log_incoming_data = True #Set to true to log sends
self.bumper_users = bumper_users
self.bumper_bots = bumper_bots
self.bumper_clients = bumper_clients
xmppserverlog.debug('new client thread init for client with ip {}'.format(self.address)) xmppserverlog.debug('new client thread init for client with ip {}'.format(self.address))
@ -177,7 +180,22 @@ class Client(threading.Thread):
def _disconnect(self): def _disconnect(self):
try: try:
xmppserverlog.debug('client {} with resource {} disconnecting'.format(self.address, self.clientresource)) bumper_bots = self.bumper_bots.get()
bumper_clients = self.bumper_clients.get()
for bot in bumper_bots:
if self.uid == bot.did:
bot.xmpp_connection = False
xmppserverlog.info("bot disconnected {}".format(bot.did))
self.bumper_bots.set(bumper_bots)
for client in bumper_clients:
if self.uid == client.userid and client.userid != 'helper1':
client.xmpp_connection = False
xmppserverlog.info("client disconnected {}".format(client.userid))
self.bumper_clients.set(bumper_clients)
#xmppserverlog.debug('client {} with resource {} disconnecting'.format(self.address, self.clientresource))
self.connection.close() self.connection.close()
except Exception as e: except Exception as e:
@ -343,6 +361,22 @@ class Client(threading.Thread):
self.clientresource = aitem.text self.clientresource = aitem.text
if bumper.check_authcode(self.uid, password): if bumper.check_authcode(self.uid, password):
bumper_bots = self.bumper_bots.get()
bumper_clients = self.bumper_clients.get()
for bot in bumper_bots:
if self.uid == bot.did:
bot.xmpp_connection = True
xmppserverlog.info("bot connected {}".format(bot.did))
self.bumper_config['bumper_bots'].set(bumper_bots)
for client in bumper_clients:
if self.uid == client.userid and client.userid != 'helper1':
client.xmpp_connection = True
xmppserverlog.info("client connected {}".format(client.userid))
self.bumper_config['bumper_clients'].set(bumper_clients)
#Client authenticated, move to next state #Client authenticated, move to next state
self._set_state('INIT') self._set_state('INIT')
@ -351,9 +385,7 @@ class Client(threading.Thread):
else: else:
#Failed auth #Failed auth
self.send('<iq type="error" id="{}"><error code="401" type="auth"><not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>'.format(xml.get('id'))) self.send('<iq type="error" id="{}"><error code="401" type="auth"><not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>'.format(xml.get('id')))
except ET.ParseError as e: except ET.ParseError as e:
if "no element found" in e.msg: if "no element found" in e.msg:
@ -377,7 +409,8 @@ class Client(threading.Thread):
self.clientresource = resource self.clientresource = resource
authcode = saslauth[2] authcode = saslauth[2]
if bumper.check_authcode(self.uid, authcode): if bumper.check_authcode(self.uid, authcode):
#Send response #Send response
self.send('<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>') #Success self.send('<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>') #Success