sebdehne / dbus-mqtt-services

Create services on the dbus using MQTT
MIT License
9 stars 2 forks source link

crash on incomming mqtt mesaage #4

Closed RFDarter closed 9 months ago

RFDarter commented 9 months ago

hi, i gave your example a try but as soon as a mqtt message is recived it get this error:


Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3.8/site-packages/paho/mqtt/client.py", line 3452, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/usr/lib/python3.8/site-packages/paho/mqtt/client.py", line 1779, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/lib/python3.8/site-packages/paho/mqtt/client.py", line 1181, in loop
    rc = self.loop_read(max_packets)
  File "/usr/lib/python3.8/site-packages/paho/mqtt/client.py", line 1572, in loop_read
    rc = self._packet_read()
  File "/usr/lib/python3.8/site-packages/paho/mqtt/client.py", line 2310, in _packet_read
    rc = self._packet_handle()
  File "/usr/lib/python3.8/site-packages/paho/mqtt/client.py", line 2936, in _packet_handle
    return self._handle_publish()
  File "/usr/lib/python3.8/site-packages/paho/mqtt/client.py", line 3216, in _handle_publish
    self._handle_on_message(message)
  File "/usr/lib/python3.8/site-packages/paho/mqtt/client.py", line 3444, in _handle_on_message
    self.on_message(self, self._userdata, message)
  File "dbus-mqtt-services.py", line 117, in on_message
    json_data = json.loads(payload)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 66 column 3 (char 1379)

This is the mqtt message recived(confirmed using mqtt explorer)

{
  "service": "daly_bms_battery_1",
  "serviceType": "battery",
  "serviceInstance": 0,
  "dbus_data": [
    {
      "path": "/Mgmt/ProcessName",
      "value": "Daly Bms Bridge",
      "valueType": "string",
      "writeable": false
    },
    {
      "path": "/Mgmt/ProcessVersion",
      "value": "1.0",
      "valueType": "string",
      "writeable": false
    },
    {
      "path": "/Mgmt/Connection",
      "value": "Serial Uart Daly",
      "valueType": "string",
      "writeable": false
    },
    {
      "path": "/ProductId",
      "value": "0",
      "valueType": "integer",
      "writeable": false
    },
    {
      "path": "/ProductName",
      "value": "Daly Bms service",
      "valueType": "string",
      "writeable": false
    },
    {
      "path": "/FirmwareVersion",
      "value": "1.0",
      "valueType": "string",
      "writeable": false
    },
    {
      "path": "/HardwareVersion",
      "value": "1.0",
      "valueType": "string",
      "writeable": false
    },
    {
      "path": "/Connected",
      "value": "1",
      "valueType": "integer",
      "writeable": false
    },
    {
      "path": "/CustomName",
      "value": "Daly Bms service",
      "valueType": "string",
      "writeable": true
    },
    {
      "path": "/Info/BatteryLowVoltage",
      "value": "52.176",
      "valueType": "float",
      "writeable": false
    },
  ]
}
sebdehne commented 9 months ago

If you paste this JSON-message into an editor which supports JSON then you will quickly see that the last comma (,) is wrong. JSON does not support leading commas and this must be removed.

RFDarter commented 9 months ago

So maybe you would want to update your example on the README. I just used that to test it.

sebdehne commented 9 months ago

Hehe good point. Thanks

RFDarter commented 9 months ago

Maybe that would be a good change too, to prefent the service crashing when an invalid Jason is recived

    try:
        json_data = json.loads(payload)
    except json.JSONDecodeError as e:
        print("Invalid JSON syntax:", e)
        return
sebdehne commented 9 months ago

Would you mind sending it as a pull request? I can take it in. Thanks

RFDarter commented 9 months ago

Can you tell me how i can remove a service from from tzhe dbus created with your script? modifing the settings.xml and removeing the is not removing the paths from the dbus

root@raspberrypi:~# dbus -y
org.freedesktop.DBus
com.victronenergy.battery.jk_Bms
com.victronenergy.modbusclient.tcp
com.victronenergy.system
com.victronenergy.qwacs
org.freedesktop.Avahi
com.victronenergy.logger
com.victronenergy.fronius
org.bluez
debug.victronenergy.gui
com.victronenergy.platform
com.victronenergy.settings
com.victronenergy.solarcharger.ttyUSB0
com.victronenergy.adc
net.connman
fi.w1.wpa_supplicant1

com.victronenergy.battery.jk_Bms

reboot doesnt fix it eighter