MTrab / pyworxcloud

PyPI module for integrating with Worx Cloud devices
GNU General Public License v3.0
22 stars 20 forks source link

BUG: unhandled exception if no gps key stops MQTT data receive #213

Closed johngianni closed 2 months ago

johngianni commented 3 months ago

/Dear @MTrab , I think I've found a situation in which the JSON sent from the Positech cloud is missing the key : DATA.MODULES.4G.GPS .

if this happens an exception is thrown :

Exception in thread Thread-1 (_thread_main):
Traceback (most recent call last):
File "/usr/local/lib/python3.11/threading.py", line 1045, in _bootstrap_inner
     self.run()
   File "/usr/local/lib/python3.11/threading.py", line 982, in run
     self._target(*self._args, **self._kwargs)
   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
     self.loop_forever(retry_first_connection=True)
   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
     rc = self._loop(timeout)
          ^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1164, in _loop
     rc = self.loop_read()
          ^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1556, in loop_read
     rc = self._packet_read()
          ^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 2439, in _packet_read
     rc = self._packet_handle()
          ^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3033, in _packet_handle
     return self._handle_publish()
            ^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3330, in _handle_publish
     self._handle_on_message(message)
   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3570, in _handle_on_message
    on_message(self, self._userdata, message)
   File "/code/pyworxcloud/utils/mqtt.py", line 149, in _forward_on_message
     self._on_update(msg)
   File "/code/pyworxcloud/__init__.py", line 395, in _on_update
     self._decode_data(device)
   File "/code/pyworxcloud/__init__.py", line 475, in _decode_data
     data["dat"]["modules"]["4G"]["gps"]["coo"][0],
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^

this error seems to stop the connection to the MQTT server , thus interrupting data receive.

as you can see the library correctly issue a force update, but no data is received:

2024-05-29T05:48:22.652964462Z 2024-05-29 07:48:22,652 [mqtt.py] [ping] [DEBUG] [239] Sending '{"id": xxxxxx, "uuid": "xxxxxxxxxxxxx", "tm": "2024-05-29T07:48:22Z", "cmd": 0}' on topic 'KR/MW/KR173E/xxxxxxxxxxxx/v1/commandIn'
2024-05-29T05:48:22.652257769Z 2024-05-29 07:48:22,652 [__init__.py] [_force_refresh] [DEBUG] [341] Forcing refresh for 'RTK KR173E'
2024-05-29T05:48:22.652825783Z 2024-05-29 07:48:22,652 [__init__.py] [update] [DEBUG] [730] Trying to refresh 'xxxxxxxxxxxxx'
2024-05-29T05:48:22.652881468Z 2024-05-29 07:48:22,652 [mqtt.py] [format_message] [DEBUG] [287] Formatting message '{'cmd': 0}' to '{'id': 21893, 'uuid': 'xxxxxxxxxxxxxx', 'tm': '2024-05-29T07:48:22Z', 'cmd': 0}'

I suggest to add Key verification to the code at line 471:

# Check for extra module availability
                if "modules" in data["dat"]:
                    if "4G" in data["dat"]["modules"]:
                        if "gps" in data["dat"]["modules"]["4G"]:
                            device.gps = Location(
                                data["dat"]["modules"]["4G"]["gps"]["coo"][0],
                                data["dat"]["modules"]["4G"]["gps"]["coo"][1],
                            )