diff --git a/doc/devices_classes_branch.txt b/doc/devices_classes_branch.txt index 8c954df..d22347d 100644 --- a/doc/devices_classes_branch.txt +++ b/doc/devices_classes_branch.txt @@ -409,23 +409,30 @@ TODO NEXT DONE -* provide a way that a device class instance can have a registered callback - that passes the message that has just been handled, but without loosing - the reading update. The main purpose of this is for logging and notification - that new data has arrived (for event driven handling instead of polling) - - - - * 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 -* provide an option in the router to turn on or off auto device registration - on unknown message (poor mans discovery) +---- -* Make auto registration also add an auto route +* implement auto device registration in the router (poor mans discovery) + when a message comes from an unknown sensorid, + build a device class instance wrapper for it + put it in the registry + knit it to the router + then call it's receive handler + +* make the above auto device registration optional with a config flag in router + +* Write discovery properly as a service in Registry + When an unknown device is found, add it to the registry + if the discovery process is enabled, and route it's address. ---- @@ -435,16 +442,12 @@ ---- -* Write discovery properly as a service in Registry - When an unknown device is found, add it to the registry - if the discovery process is enabled, and route it's address. +Consider whether there is a need for a general update notify +mechanism - perhaps could have a message sequence counter that +you poll, and if it is higher than last time, you know the +device state has updated (independently of using the +when_updated callback) ----- - -* look at if we need some when_x_updated() methods on device - classes, even just a generic callback that is called when - data has been updated. Consider if there are multiple - consumers of this data (multiple callback targets) ---- diff --git a/src/energenie/Registry.py b/src/energenie/Registry.py index 0179871..503d457 100644 --- a/src/energenie/Registry.py +++ b/src/energenie/Registry.py @@ -232,6 +232,7 @@ def __init__(self, name): self.name = name # probably FSK or OOK self.routes = {} # key(tuple of ids) -> value(device class instance) + self.unknown_cb = None def add(self, address, instance): """Add this device instance to the routing table""" @@ -248,17 +249,28 @@ else: # unknown address self.handle_unknown(address, payload) + def when_unknown(self, callback): + """Register a callback for unknown messages""" + #NOTE: this is the main hook point for auto discovery and registration + self.unknown_cb = callback + def handle_unknown(self, address, payload): - #TODO: route to something that handles unknown addresses, e.g. discovery agent - #discovery agent could be configured by overriding handle_unknown at construction time, below - print("unknown address: %s" % address) - print("ignored payload: %s" % payload) + if self.unknown_cb != None: + self.unknown_cb(address, payload) + else: + # Default action is just a debug message, and drop the payload + 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. + +#TODO: Name is not completely representative of function. +# This is the Energenie 433.92MHz with OpenThings fsk_router = Router("fsk") + #OOK receive not yet written +#It will be used to be able to learn codes from Energenie legacy hand remotes ##ook_router = Router("ook") - - # END diff --git a/src/monitor_mihome.py b/src/monitor_mihome.py index 9f811cb..86fc073 100644 --- a/src/monitor_mihome.py +++ b/src/monitor_mihome.py @@ -23,22 +23,20 @@ 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 + # This is a useful way to add data logging on a per-device basis def new_data(self, message): - print("new data for %s" % self) + print("\nnew data for %s" % self) message.dump() Logger.logMessage(message) purple.when_updated(new_data) # Override the default unknown handler, so we can show data from unregistered devices - def unk(address, message): - print("Unknown device:%s" % str(hex(address[2]))) + # 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() - #TODO: add device to registry and to fsk_router table - #note: requires auto class create from product_id to be working first - Logger.logMessage(message) - energenie.fsk_router.handle_unknown = unk - #TODO: Provide a better callback registration scheme - ##energenie.fsk_router.when_unknown(unk) + energenie.fsk_router.when_unknown(unknown) try: while True: