diff --git a/doc/devices_classes_branch.txt b/doc/devices_classes_branch.txt index d22347d..7ec4d06 100644 --- a/doc/devices_classes_branch.txt +++ b/doc/devices_classes_branch.txt @@ -408,15 +408,12 @@ -------------------------------------------------------------------------------- TODO NEXT + DONE -* implement neater unknown message handler in the router - do it as a callback rather than overwriting the default method - - - * consider a callback in the router for new_data, for all messages regardless of if known or not (good for generic data logging) + * design a way that a MIHO005 class instance can be auto generated from a product_id field ---- diff --git a/src/energenie/Registry.py b/src/energenie/Registry.py index 503d457..8e8b6de 100644 --- a/src/energenie/Registry.py +++ b/src/energenie/Registry.py @@ -233,6 +233,7 @@ self.name = name # probably FSK or OOK self.routes = {} # key(tuple of ids) -> value(device class instance) self.unknown_cb = None + self.incoming_cb = None def add(self, address, instance): """Add this device instance to the routing table""" @@ -240,7 +241,9 @@ # address might be a string, a number, a tuple, but probably always the same for any one router self.routes[address] = instance - def handle_message(self, address, payload): + def incoming_message(self, address, payload): + if self.incoming_cb != None: + self.incoming_cb(address, payload) if address in self.routes: ci = self.routes[address] @@ -249,6 +252,9 @@ else: # unknown address self.handle_unknown(address, payload) + def when_incoming(self, callback): + self.incoming_cb = callback + def when_unknown(self, callback): """Register a callback for unknown messages""" #NOTE: this is the main hook point for auto discovery and registration @@ -262,6 +268,8 @@ print("Unknown address: %s" % str(address)) + + # Might rename these, especially when we add in other protocols # such as devices that are 868 wirefree doorbells etc. diff --git a/src/monitor_mihome.py b/src/monitor_mihome.py index 86fc073..84f2eeb 100644 --- a/src/monitor_mihome.py +++ b/src/monitor_mihome.py @@ -23,7 +23,21 @@ energenie.registry.add(purple, "purple") energenie.fsk_router.add((energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, MY_SENSOR_ID), purple) - # Register for update callbacks when a new message comes in for the purple device + # provide a default incoming message handler + # This is useful for logging every message + ##def incoming(address, message): + ## print("\nIncoming from %s" % str(address)) + ## Logger.logMessage(message) + ##energenie.fsk_router.when_incoming(incoming) + + # Provide an unknown message handler so we can show data from unregistered devices + # This is a useful hook point for later adding device discovery + def unknown(address, message): + print("\nUnknown device:%s" % str(address)) + message.dump() + energenie.fsk_router.when_unknown(unknown) + + # Register for update callbacks on a single device when a new message comes in. # This is a useful way to add data logging on a per-device basis def new_data(self, message): print("\nnew data for %s" % self) @@ -31,13 +45,6 @@ Logger.logMessage(message) purple.when_updated(new_data) - # Override the default unknown handler, so we can show data from unregistered devices - # This is a useful hook point for later adding device discovery - def unknown(address, message): - print("\nUnknown device:%s" % str(hex(address[2]))) - message.dump() - energenie.fsk_router.when_unknown(unknown) - try: while True: #TESTING: build a synthetic message @@ -46,12 +53,12 @@ #TESTING: Poke synthetic unknown into the router and let it route to unknown handler msg.set(header_sensorid=DUMMY_SENSOR_ID) - energenie.fsk_router.handle_message( + energenie.fsk_router.incoming_message( (energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, DUMMY_SENSOR_ID), msg) #TESTING: Poke synthetic known into the router and let it route to our class instance msg.set(header_sensorid=MY_SENSOR_ID) - energenie.fsk_router.handle_message( + energenie.fsk_router.incoming_message( (energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, MY_SENSOR_ID), msg) #TODO: Knit with real radio