Commit 21641479 authored by magho's avatar magho

changed bluetooth order read function

parent 85065f9f
......@@ -2,11 +2,11 @@ import os
import subprocess
import re
import gatt
import parse
from PyQt5 import QtCore, QtGui, QtWidgets
SERVICE_UUID = "00001523-1212-efde-1523-785feabcd123"
BUTTON_UUID = "00001524-1212-efde-1523-785feabcd123"
LED_UUID = "00001525-1212-efde-1523-785feabcd123"
RX_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
TX_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
class BLEThread(QtCore.QThread):
# This signal will be emitted each time the BLE manager receives a button
......@@ -67,35 +67,42 @@ class ModuleDevice(gatt.Device):
def disconnect_succeeded(self):
super().disconnect_succeeded()
print("[%s] Disconnected" % (self.mac_address))
def services_resolved(self):
super().services_resolved()
print("[%s] Resolved services" % (self.mac_address))
for service in self.services:
for characteristic in service.characteristics:
if characteristic.uuid == BUTTON_UUID:
dbus_value = characteristic.read_value()
print(dbus_value)
values = [int(v) for v in dbus_value]
print(values)
order = {}
if values != None and len(values) == 4:
order['rom'] = 'OV'
order['seksjon'] = self.parent.device_mapping[self.mac_address.upper()]
order['hylle'] = values[0]
order['rad'] = values[1]
order['kolonne'] = values[2]
order['dybde'] = values[3]
self.parent.ble_order_position(order)
else:
print("Failed to fetch values")
# Always read an order when connecting
if characteristic.uuid == TX_UUID:
self.read_order(characteristic)
# DBus is taking its time getting back with a reply on self.disconnect(), which blocks
# the whole process, so we offload this work to a different process.
# TODO: Find a nicer way of doing this.
#self.disconnect()
subprocess.Popen(["/usr/bin/bt-device", "--remove=%s" % self.mac_address])
def read_order(self, characteristic):
# Enable notifications before getting values
characteristic.enable_notifications()
dbus_value = characteristic.read_value()
# Parse characteristic value into order
order_str = ''.join([str(v) for v in dbus_value])
order_format = "S{seksjon}H{hylle}R{rad}C{kolonne}D{dybde}"
order_result = parse.parse(order_format, order_str)
if order_result:
order = order_result.named
order['rom'] = 'OV'
# Send order
self.parent.ble_order_position(order)
else:
error_str = "Failed to parse order '{}' from module.".format(order_str)
print(error_str)
self.parent.parent.show_error(error_str)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment