From 33e97e474fa070ef1fcc93d8e4d5de5e3b5aa967 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Fri, 15 Sep 2017 22:50:54 +0300 Subject: [PATCH] Some minor fixes --- README.md | 1 + demo.py | 6 +++--- pylgbst/comms.py | 9 ++++++--- pylgbst/peripherals.py | 7 +++++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f06823c..cf96518 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Best way to start is to look at [demo.py](demo.py) file, and run it. - angled and timed movement for motors - LED color change - sensor data subscribe/unsubscribe +- battery voltage available ## Usage diff --git a/demo.py b/demo.py index 43804e8..e734721 100644 --- a/demo.py +++ b/demo.py @@ -172,9 +172,9 @@ if __name__ == '__main__': hub = MoveHub(connection) - hub.devices[PORT_BATTERY].subscribe(cb_log, 0x00, granularity=2) + #hub.devices[PORT_SOMETHING1].subscribe(cb_log, 0x00, granularity=1) sleep(10) demo_port_cd_motor(hub) - sleep(60) - hub.devices[PORT_BATTERY].unsubscribe(cb_log) + sleep(10) + #hub.devices[PORT_SOMETHING1].unsubscribe(cb_log) # demo_all(hub) diff --git a/pylgbst/comms.py b/pylgbst/comms.py index b7686ee..165eb81 100644 --- a/pylgbst/comms.py +++ b/pylgbst/comms.py @@ -16,7 +16,7 @@ from threading import Thread from pylgbst.constants import LEGO_MOVE_HUB, MSG_DEVICE_SHUTDOWN -log = logging.getLogger('transport') +log = logging.getLogger('comms') def str2hex(data): # TODO: eliminate it @@ -171,7 +171,7 @@ class DebugServer(object): self.sock.close() def _notify_dummy(self, handle, data): - log.debug("Notification from handle %s: %s", handle, binascii.hexlify(data.strip())) + log.debug("Dropped notification from handle %s: %s", handle, binascii.hexlify(data.strip())) self._check_shutdown(data) def _notify(self, conn, handle, data): @@ -291,7 +291,10 @@ class DebugServerConnection(Connection): if line: item = json.loads(line) if item['type'] == 'notification' and self.notify_handler: - self.notify_handler(item['handle'], unhexlify(item['data'])) + try: + self.notify_handler(item['handle'], unhexlify(item['data'])) + except BaseException: + log.error("Failed to notify handler: %s", traceback.format_exc()) elif item['type'] == 'response': self.incoming.append(item) else: diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index 7957379..6d241fc 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -61,7 +61,9 @@ class Peripheral(object): if callback in self._subscribers: self._subscribers.remove(callback) - if not self._subscribers: + if not self._port_subscription_mode: + log.warning("Attempt to unsubscribe while never subscribed: %s", self) + elif not self._subscribers: self._port_subscribe(self._port_subscription_mode, 0, False) self._port_subscription_mode = None @@ -273,8 +275,9 @@ class ColorDistanceSensor(Peripheral): class Battery(Peripheral): + # we know only voltage subscription from it. is it really battery or just onboard voltage? def handle_port_data(self, data): - self._notify_subscribers(unpack("