Combine MQTT and IOT into IOTMQ

Combined MQTT and IOT into IOTMQ
Updated tests - now with 99% coverage for __init__
This commit is contained in:
Brian Martin
2019-02-18 17:44:41 -05:00
parent 9919baad27
commit 597129dda0
8 changed files with 559 additions and 532 deletions
+197 -215
View File
@@ -140,8 +140,8 @@ class EcoVacsAPI:
PORTAL_URL_FORMAT = 'https://portal-{continent}.ecouser.net/api'
USERSAPI = 'users/user.do'
IOTDEVMANAGERAPI = 'iot/devmanager.do' # IOT Device Manager - This provides control of "IOT" products via API, no longer XMPP
PRODUCTAPI = 'pim/product' # Leaving this open, the only endpoint known currently is "Product IOT Map" - pim/product/getProductIotMap - This provides a list of "IOT" products, which is assumed should use IOT API instead of XMPP
IOTDEVMANAGERAPI = 'iot/devmanager.do' # IOT Device Manager - This provides control of "IOT" products via RestAPI, some bots use this instead of XMPP
PRODUCTAPI = 'pim/product' # Leaving this open, the only endpoint known currently is "Product IOT Map" - pim/product/getProductIotMap - This provides a list of "IOT" products. Not sure what this provides the app.
REALM = 'ecouser.net'
@@ -236,40 +236,16 @@ class EcoVacsAPI:
params = {}
params.update(args)
url = (EcoVacsAPI.PORTAL_URL_FORMAT + "/" + api).format(continent=self.continent, **self.meta)
response = None
if not api == self.IOTDEVMANAGERAPI:
response = requests.post(url, json=params, verify=verify_ssl)
else:
try: #IOT Device sometimes doesnt provide a response depending on command, reduce timeout to 3 to accomodate and make requests faster
response = requests.post(url, json=params, timeout=3, verify=verify_ssl) #May think about having timeout as an arg that could be provided in the future
except requests.exceptions.ReadTimeout:
_LOGGER.debug("call to {} failed with ReadTimeout".format(function))
return {}
url = (EcoVacsAPI.PORTAL_URL_FORMAT + "/" + api).format(continent=self.continent, **self.meta)
response = requests.post(url, json=params, verify=verify_ssl)
json = response.json()
_LOGGER.debug("got {}".format(json))
if api == self.USERSAPI:
if json['result'] == 'ok':
return json
if api == self.IOTDEVMANAGERAPI:
if json['ret'] == 'ok':
return json
elif json['ret'] == 'fail':
if 'debug' in json:
if json['debug'] == 'wait for response timed out':
#TODO - Maybe handle timeout for IOT better in the future
_LOGGER.error("call to {} failed with {}".format(function, json))
return {}
else:
#TODO - Not sure if we want to raise an error yet, just return empty for now
_LOGGER.error("call to {} failed with {}".format(function, json))
return {}
#raise RuntimeError(
#"failure {} ({}) for call {} and parameters {}".format(json['error'], json['errno'], function, params))
if api.startswith(self.PRODUCTAPI):
if json['code'] == 0:
return json
@@ -287,9 +263,7 @@ class EcoVacsAPI:
'userId': self.uid,
'token': self.auth_code}
, verify_ssl=self.verify_ssl)
def getdevices(self):
return self.__call_portal_api(self.USERSAPI,'GetDeviceList', {
'userid': self.uid,
@@ -315,15 +289,33 @@ class EcoVacsAPI:
}, verify_ssl=self.verify_ssl)['data']
def SetIOTDevices(self, devices, iotproducts):
#Originally added for D900, and not actively used in code now - Not sure what the app checks the items in this list for
for device in devices: #Check if the device is part of iotProducts
device['iot_product'] = False
for iotProduct in iotproducts:
if device['class'] in iotProduct['classid']:
device['iot'] = True
device['iot_product'] = True
return devices
def SetIOTMQDevices(self, devices):
#Added for devices that utilize MQTT instead of XMPP for communication
#At this time the list is updated manually, so far only the D900 has been seen to use this
#These items were found in the Android app source by searching for "new IOTMqDevice("
iotmqdevices = [
'ls1ok3', #D900 / DE5G
# Possibly the Atmobot AA30 - qqy0di
# Possibly the Slim4 - wbueya
]
for device in devices:
device['iotmq'] = False
if device['class'] in iotmqdevices: #Check if the device is part of the list
device['iotmq'] = True
return devices
def devices(self):
return self.SetIOTDevices(self.getdevices(), self.getiotProducts())
return self.SetIOTMQDevices(self.getdevices())
@staticmethod
def md5(text):
@@ -396,45 +388,40 @@ class VacBot():
self.lifespanEvents = EventEmitter()
self.errorEvents = EventEmitter()
#Set none for clients to start
self.mqtt = None
self.iot = None
#Set none for clients to start
self.xmpp = None
self.iotmq = None
if vacuum['iot']:
self.iot = EcoVacsIOT(user, domain, resource, secret, continent, vacuum, verify_ssl=verify_ssl)
self.iot.subscribe_to_ctls(self._handle_ctl)
self.mqtt = EcoVacsMQTT(user, domain, resource, secret, continent, vacuum, server_address)
self.mqtt.subscribe_to_ctls(self._handle_ctl)
#self.xmpp = EcoVacsXMPP(user, domain, resource, secret, continent, vacuum, server_address)
#Uncomment line to allow unencrypted plain auth
#self.xmpp['feature_mechanisms'].unencrypted_plain = True
#self.xmpp.subscribe_to_ctls(self._handle_ctl)
else:
if not vacuum['iotmq']:
self.xmpp = EcoVacsXMPP(user, domain, resource, secret, continent, vacuum, server_address)
#Uncomment line to allow unencrypted plain auth
#self.xmpp['feature_mechanisms'].unencrypted_plain = True
self.xmpp.subscribe_to_ctls(self._handle_ctl)
self.xmpp.subscribe_to_ctls(self._handle_ctl)
else:
self.iotmq = EcoVacsIOTMQ(user, domain, resource, secret, continent, vacuum, server_address, verify_ssl=verify_ssl)
self.iotmq.subscribe_to_ctls(self._handle_ctl)
#self.xmpp = EcoVacsXMPP(user, domain, resource, secret, continent, vacuum, server_address)
#Uncomment line to allow unencrypted plain auth
#self.xmpp['feature_mechanisms'].unencrypted_plain = True
#self.xmpp.subscribe_to_ctls(self._handle_ctl)
def connect_and_wait_until_ready(self):
if not self.vacuum['iot']:
if not self.vacuum['iotmq']:
self.xmpp.connect_and_wait_until_ready()
self.xmpp.schedule('Ping', 30, lambda: self.send_ping(), repeat=True)
else:
self.mqtt.connect_and_wait_until_ready()
self.mqtt.schedule(30, self.send_ping)
self.iotmq.connect_and_wait_until_ready()
self.iotmq.schedule(30, self.send_ping)
#self.xmpp.connect_and_wait_until_ready()
if self._monitor:
# Do a first ping, which will also fetch initial statuses if the ping succeeds
self.send_ping()
if not self.vacuum['iot']:
if not self.vacuum['iotmq']:
self.xmpp.schedule('Components', 3600, lambda: self.refresh_components(), repeat=True)
else:
self.mqtt.schedule(3600,self.refresh_components)
self.iotmq.schedule(3600,self.refresh_components)
def _handle_ctl(self, ctl):
method = '_handle_' + ctl['event']
@@ -472,7 +459,7 @@ class VacBot():
type = event['type']
try:
type = CLEAN_MODE_FROM_ECOVACS[type]
if self.vacuum['iot']: #Was able to parse additional status from the IOT, may apply to XMPP too
if self.vacuum['iotmq']: #Was able to parse additional status from the IOTMQ, may apply to XMPP too
statustype = event['st']
statustype = CLEAN_ACTION_FROM_ECOVACS[statustype]
if statustype == CLEAN_ACTION_STOP or statustype == CLEAN_ACTION_PAUSE:
@@ -533,10 +520,10 @@ class VacBot():
_LOGGER.debug("*** charge_status = " + self.charge_status)
def _vacuum_address(self):
if self.vacuum['iot']:
return self.vacuum['did']
if not self.vacuum['iotmq']:
return self.vacuum['did'] + '@' + self.vacuum['class'] + '.ecorobot.net/atom'
else:
return self.vacuum['did'] + '@' + self.vacuum['class'] + '.ecorobot.net/atom'
return self.vacuum['did'] #IOTMQ only uses the did
@property
def is_charging(self) -> bool:
@@ -548,10 +535,10 @@ class VacBot():
def send_ping(self):
try:
if not self.vacuum['iot']:
if not self.vacuum['iotmq']:
self.xmpp.send_ping(self._vacuum_address())
elif self.vacuum['iot']:
if not self.mqtt.send_ping():
elif self.vacuum['iotmq']:
if not self.iotmq.send_ping():
raise RuntimeError()
#self.xmpp.send_ping(EcoVacsAPI.REALM) #IOT vacuums are using the realm instead
@@ -612,26 +599,23 @@ class VacBot():
self.refresh_components()
def send_command(self, action):
if not self.vacuum['iot']:
if not self.vacuum['iotmq']:
self.xmpp.send_command(action.to_xml(), self._vacuum_address())
else:
#IOT issues commands via restAPI, and listens on MQTT for status updates
self.iot.send_command(action, self._vacuum_address()) #IOT devices need the full action for additional parsing
#IOTMQ issues commands via RestAPI, and listens on MQTT for status updates
self.iotmq.send_command(action, self._vacuum_address()) #IOTMQ devices need the full action for additional parsing
def run(self, action):
self.send_command(action)
def disconnect(self, wait=False):
if not self.vacuum['iot']:
if not self.vacuum['iotmq']:
self.xmpp.disconnect(wait=wait)
else:
self.mqtt._disconnect()
#self.xmpp.disconnect(wait=wait)
self.iotmq._disconnect()
#self.xmpp.disconnect(wait=wait)
#This is used by EcoVacsIOT, EcoVacsXMPP, and EcoVacsMQTT for _ctl_to_dict
#This is used by EcoVacsIOTMQ and EcoVacsXMPP for _ctl_to_dict
def RepresentsInt(stringvar):
try:
int(stringvar)
@@ -639,117 +623,19 @@ def RepresentsInt(stringvar):
except ValueError:
return False
class EcoVacsIOT():
def __init__(self, user, domain, resource, secret, continent, vacuum, verify_ssl=True):
self.uid = user
self.domain = domain
self.resource = resource
self.secret = secret
self.continent = continent
self.vacuum = vacuum
self.api = EcoVacsAPI
self.api.continent = continent
self.api.meta = {}
self.ctl_subscribers = []
self.ready_flag = Event()
self.verify_ssl = str_to_bool(verify_ssl)
#TODO: Determine what to do with IOT connect and wait, or scrap
# def connect_and_wait_until_ready(self):
# self.connect(EcoVacsAPI._EcoVacsAPI__call_portal_api())
# self.process()
# self.wait_until_ready()
def send_command(self, action, recipient):
if action.name == "Clean": #For handling Clean when action not specified (i.e. CLI)
action.args['clean']['act'] = CLEAN_ACTION_TO_ECOVACS['start'] #Inject a start action
c = self._wrap_command(action, recipient)
_LOGGER.debug('Sending command {0}'.format(c))
self._handle_ctl(action,
self.api._EcoVacsAPI__call_portal_api(self.api, self.api.IOTDEVMANAGERAPI,'',c ,verify_ssl=self.verify_ssl )
)
def _wrap_command(self, cmd, recipient):
#Remove the td from ctl xml for RestAPI
payloadxml = cmd.to_xml()
payloadxml.attrib.pop("td")
return {
'auth': {
'realm': EcoVacsAPI.REALM,
'resource': self.resource,
'token': self.secret,
'userid': self.uid,
'with': 'users',
},
"cmdName": cmd.name,
"payload": ET.tostring(payloadxml).decode(),
"payloadType": "x",
"td": "q",
"toId": recipient,
"toRes": self.vacuum['resource'],
"toType": self.vacuum['class']
}
def subscribe_to_ctls(self, function):
self.ctl_subscribers.append(function)
def _handle_ctl(self, action, message):
if not message == {}:
resp = self._ctl_to_dict(action, message['resp'])
if resp is not None:
for s in self.ctl_subscribers:
s(resp)
def _ctl_to_dict(self, action, xmlstring):
xml = ET.fromstring(xmlstring)
xmlchild = xml.getchildren()
if len(xmlchild) > 0:
result = xmlchild[0].attrib.copy()
#Fix for difference in XMPP vs IOT response
#Depending on the report will use the tag and add "report" to fit the mold of sucks library
if xmlchild[0].tag == "clean":
result['event'] = "CleanReport"
elif xmlchild[0].tag == "charge":
result['event'] = "ChargeState"
elif xmlchild[0].tag == "battery":
result['event'] = "BatteryInfo"
else: #Default back to replacing Get from the api cmdName
result['event'] = action.name.replace("Get","",1)
else:
result = xml.attrib.copy()
result['event'] = action.name.replace("Get","",1)
if 'ret' in result: #Handle errors as needed
if result['ret'] == 'fail':
if action.name == "Charge": #So far only seen this with Charge, when already docked
result['event'] = "ChargeState"
for key in result:
if not RepresentsInt(result[key]): #Fix to handle negative int values
result[key] = stringcase.snakecase(result[key])
return result
class EcoVacsMQTT(ClientMQTT):
def __init__(self, user, domain, resource, secret, continent, vacuum, server_address=None ):
class EcoVacsIOTMQ(ClientMQTT):
def __init__(self, user, domain, resource, secret, continent, vacuum, server_address=None, verify_ssl=True):
ClientMQTT.__init__(self)
self.ctl_subscribers = []
self.user = user
self.domain = str(domain).split(".")[0] #MQTT is using domain without tld extension
self.resource = resource
self.secret = secret
self.continent = continent
self.vacuum = vacuum
self.scheduler = sched.scheduler(time.time, time.sleep)
self.scheduler_thread = threading.Thread(target=self.scheduler.run, daemon=True, name="mqtt_schedule_thread")
self.verify_ssl = str_to_bool(verify_ssl)
if server_address is None:
self.hostname = ('mq-{}.ecouser.net'.format(self.continent))
@@ -768,6 +654,28 @@ class EcoVacsMQTT(ClientMQTT):
self.ready_flag = Event()
def connect_and_wait_until_ready(self):
#self._on_log = self.on_log #This provides more logging than needed, even for debug
self._on_message = self._handle_ctl_mqtt
self._on_connect = self.on_connect
#TODO: This is pretty insecure and accepts any cert, maybe actually check?
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
self.tls_set_context(ssl_ctx)
self.tls_insecure_set(True)
self.connect(self.hostname, self.port)
self.loop_start()
self.wait_until_ready()
def subscribe_to_ctls(self, function):
self.ctl_subscribers.append(function)
#def subscribe_to_ctls_mqtt(self, function):
# self.ctl_subscribers.append(function)
def _disconnect(self):
self.disconnect() #disconnect mqtt connection
self.scheduler.empty() #Clear schedule queue
@@ -798,19 +706,122 @@ class EcoVacsMQTT(ClientMQTT):
#def on_log(self, client, userdata, level, buf): #This is very noisy and verbose
# _LOGGER.debug("EcoVacsMQTT Log: {} ".format(buf))
def send_ping(self):
_LOGGER.debug("*** MQTT sending ping ***")
rc = self._send_simple_command(MQTTPublish.paho.PINGREQ)
if rc == MQTTPublish.paho.MQTT_ERR_SUCCESS:
return True
else:
return False
def subscribe_to_ctls(self, function):
self.ctl_subscribers.append(function)
def send_command(self, action, recipient):
if action.name == "Clean": #For handling Clean when action not specified (i.e. CLI)
action.args['clean']['act'] = CLEAN_ACTION_TO_ECOVACS['start'] #Inject a start action
c = self._wrap_command(action, recipient)
_LOGGER.debug('Sending command {0}'.format(c))
self._handle_ctl_api(action,
self.__call_iotdevmanager_api(c ,verify_ssl=self.verify_ssl )
)
def _wrap_command(self, cmd, recipient):
#Remove the td from ctl xml for RestAPI
payloadxml = cmd.to_xml()
payloadxml.attrib.pop("td")
return {
'auth': {
'realm': EcoVacsAPI.REALM,
'resource': self.resource,
'token': self.secret,
'userid': self.user,
'with': 'users',
},
"cmdName": cmd.name,
"payload": ET.tostring(payloadxml).decode(),
"payloadType": "x",
"td": "q",
"toId": recipient,
"toRes": self.vacuum['resource'],
"toType": self.vacuum['class']
}
def _handle_ctl(self, client, userdata, message):
def __call_iotdevmanager_api(self, args, verify_ssl=True):
_LOGGER.debug("calling iotdevmanager api with {}".format(args))
params = {}
params.update(args)
url = (EcoVacsAPI.PORTAL_URL_FORMAT + "/iot/devmanager.do").format(continent=self.continent)
response = None
try: #The RestAPI sometimes doesnt provide a response depending on command, reduce timeout to 3 to accomodate and make requests faster
response = requests.post(url, json=params, timeout=3, verify=verify_ssl) #May think about having timeout as an arg that could be provided in the future
except requests.exceptions.ReadTimeout:
_LOGGER.debug("call to iotdevmanager failed with ReadTimeout")
return {}
json = response.json()
if json['ret'] == 'ok':
return json
elif json['ret'] == 'fail':
if 'debug' in json:
if json['debug'] == 'wait for response timed out':
#TODO - Maybe handle timeout for IOT better in the future
_LOGGER.error("call to iotdevmanager failed with {}".format(json))
return {}
else:
#TODO - Not sure if we want to raise an error yet, just return empty for now
_LOGGER.error("call to iotdevmanager failed with {}".format(json))
return {}
#raise RuntimeError(
#"failure {} ({}) for call {} and parameters {}".format(json['error'], json['errno'], function, params))
def _handle_ctl_api(self, action, message):
if not message == {}:
resp = self._ctl_to_dict_api(action, message['resp'])
if resp is not None:
for s in self.ctl_subscribers:
s(resp)
def _ctl_to_dict_api(self, action, xmlstring):
xml = ET.fromstring(xmlstring)
xmlchild = xml.getchildren()
if len(xmlchild) > 0:
result = xmlchild[0].attrib.copy()
#Fix for difference in XMPP vs API response
#Depending on the report will use the tag and add "report" to fit the mold of sucks library
if xmlchild[0].tag == "clean":
result['event'] = "CleanReport"
elif xmlchild[0].tag == "charge":
result['event'] = "ChargeState"
elif xmlchild[0].tag == "battery":
result['event'] = "BatteryInfo"
else: #Default back to replacing Get from the api cmdName
result['event'] = action.name.replace("Get","",1)
else:
result = xml.attrib.copy()
result['event'] = action.name.replace("Get","",1)
if 'ret' in result: #Handle errors as needed
if result['ret'] == 'fail':
if action.name == "Charge": #So far only seen this with Charge, when already docked
result['event'] = "ChargeState"
for key in result:
if not RepresentsInt(result[key]): #Fix to handle negative int values
result[key] = stringcase.snakecase(result[key])
return result
def _handle_ctl_mqtt(self, client, userdata, message):
#_LOGGER.debug("EcoVacs MQTT Received Message on Topic: {} - Message: {}".format(message.topic, str(message.payload.decode("utf-8"))))
as_dict = self._ctl_to_dict(message.topic, str(message.payload.decode("utf-8")))
as_dict = self._ctl_to_dict_mqtt(message.topic, str(message.payload.decode("utf-8")))
if as_dict is not None:
for s in self.ctl_subscribers:
s(as_dict)
def _ctl_to_dict(self, topic, xmlstring):
def _ctl_to_dict_mqtt(self, topic, xmlstring):
#I haven't seen the need to fall back to data within the topic (like we do with IOT rest call actions), but it is here in case of future need
xml = ET.fromstring(xmlstring) #Convert from string to xml (like IOT rest calls), other than this it is similar to XMPP
@@ -846,37 +857,9 @@ class EcoVacsMQTT(ClientMQTT):
if not RepresentsInt(result[key]) and ',' not in result[key]:
result[key] = stringcase.snakecase(result[key])
return result
def send_ping(self):
_LOGGER.debug("*** MQTT sending ping ***")
rc = self._send_simple_command(MQTTPublish.paho.PINGREQ)
if rc == MQTTPublish.paho.MQTT_ERR_SUCCESS:
return True
else:
return False
return result
def connect_and_wait_until_ready(self):
#self._on_log = self.on_log #This provides more logging than needed, even for debug
self._on_message = self._handle_ctl
self._on_connect = self.on_connect
#TODO: This is pretty insecure and accepts any cert, maybe actually check?
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
self.tls_set_context(ssl_ctx)
self.tls_insecure_set(True)
self.connect(self.hostname, self.port)
self.loop_start()
self.wait_until_ready()
class EcoVacsXMPP(ClientXMPP):
def __init__(self, user, domain, resource, secret, continent, vacuum, server_address=None ):
ClientXMPP.__init__(self, user + '@' + domain, '0/' + resource + '/' + secret)
@@ -952,7 +935,7 @@ class EcoVacsXMPP(ClientXMPP):
return q
def _my_address(self):
if not self.vacuum['iot']:
if not self.vacuum['iotmq']:
return self.user + '@' + self.domain + '/' + self.boundjid.resource
else:
return self.user + '@' + self.domain + '/' + self.resource
@@ -964,8 +947,7 @@ class EcoVacsXMPP(ClientXMPP):
_LOGGER.debug("*** sending ping ***")
q.send()
def connect_and_wait_until_ready(self):
def connect_and_wait_until_ready(self):
self.connect(self.server_address)
self.process()
self.wait_until_ready()
@@ -1017,9 +999,9 @@ class VacBotCommand:
return rtnobject
class Clean(VacBotCommand):
def __init__(self, mode='auto', speed='normal', iot=False, action='start',terminal=False, **kwargs):
def __init__(self, mode='auto', speed='normal', iotmq=False, action='start',terminal=False, **kwargs):
if kwargs == {}:
if not iot:
if not iotmq:
super().__init__('Clean', {'clean': {'type': CLEAN_MODE_TO_ECOVACS[mode], 'speed': FAN_SPEED_TO_ECOVACS[speed]}})
else:
super().__init__('Clean', {'clean': {'type': CLEAN_MODE_TO_ECOVACS[mode], 'speed': FAN_SPEED_TO_ECOVACS[speed],'act': CLEAN_ACTION_TO_ECOVACS[action]}})