Search code examples
python-3.xglibdbus

Pydbus more notifications same time handling


I would like to receive sensor data from BLE devices by notifications using pydbus. I am using GLib with main loop Linked a part of my code:

def sensor1_handler(iface, prop_changed, prop_removed):
    if 'Value' in prop_changed:
       
        """Handle values"""
       
def sensor2_handler(iface, prop_changed, prop_removed):
    if 'Value' in prop_changed:
       """Handle values"""

sensor1=bus.get("org.bluez", "/org/bluez/hciX/dev_XX_XX_XX_XX_XX_XX/serviceYYYY/charYYYY")
sensor2=bus.get("org.bluez", "/org/bluez/hciX/dev_XX_XX_XX_XX_XX_XX/serviceYYYY/charYYYY")
sensor1.onPropertiesChanged = sensor1_handler
sensor2.onPropertiesChanged = sensor2_handler

sensor2.StartNotify()
sensor1.StartNotify()

When I would like to receive the notifications, the two signals arrive almost the same time and only one (the first arrived notification) Notification Callback function runs.

How could I solve this problem? I was thinking on a message queue.

EDIT:

sensor1 function:

def sensor1_handler(iface, prop_changed, prop_removed):
    if 'Value' in prop_changed:
        temperatureLSB = prop_changed['Value'][1]
        temperatureMSB = prop_changed['Value'][0]
        humidityLSB = prop_changed['Value'][3]
        humidityMSB = prop_changed['Value'][2]
        temperature = temperatureLSB | (temperatureMSB << 8)
        humidity = humidityLSB | (humidityMSB << 8)
        print(-45+175*(temperature/(pow(2,16)-1)))
        print(100*(humidity/(pow(2,16)-1)))

sensor2 function:

def sensor2_handler(iface, prop_changed, prop_removed):
    if 'Value' in prop_changed:
        iaqLSB = prop_changed['Value'][1]
        iaqMSB = prop_changed['Value'][0]
        iaq = iaqLSB | (iaqMSB << 8)
        print(iaq)

sensor2 data sent before sensor1 so sensor2 data arrives first. The two data arrives from the same device, I did not add the other devices yet.


Solution

  • Thank you for every help!

    After many attemptions, I've noticed the problem was on my server side. The two sensor data was sent from one device with ~10ms difference and the message sending was not in Notification mode but Indication.

    I have not noticed it before, because I have sent only 1 sensor data with 2 seconds difference but problem came when I was trying to send 2 messages almost simultaneously.

    After setting it to Notification, the server could send messages rapidly (did not need acknoledgement).

    I know this error was not caused by pydbus in the end, but by my fault. I hope that if someone finds a similar problem, they should check the sender (server) side as well.