diff --git a/doc/devices_classes_branch.txt b/doc/devices_classes_branch.txt index b60d3de..c92dca8 100644 --- a/doc/devices_classes_branch.txt +++ b/doc/devices_classes_branch.txt @@ -404,35 +404,21 @@ Design for discovery done Test cases for 5 discovery variants done and work +monitor_mihome works with a synthetic join and toggles switches -------------------------------------------------------------------------------- TODO NEXT -Add auto join semantics to monitor_mihome -(might just work with the defaults - set default to discovery_joinauto - -Change monitor_mihome so that it sends a switch on/off message on a flag -to all devices currently in the registry that have a switch - - ---- PERSISTENT REGISTRY -* Need the registry to be persistent with save and load - choose a file format that is human readable, like a config file? - mostly the registry is read, occasionally it is written. +* choose a file format for the registry that is human readable like a config file + but also easy to read and occasionally write from a program Remember that when restoring the registry in a new run, it can be told to auto-create device object variables. -* consider with registry auto-create also auto-creating, or returning, - a list of all the configured devices. This then allows an app to - later iterate through all devices and decide what to do with them, - such as displaying them all in a GUI selection list. - - (Note, Registry.devices() already gives us the list and Registry.keys() - already gives us the name keys) ---- NOTIFY, UPDATE, or DATA SEQUENCE? diff --git a/src/energenie/Devices.py b/src/energenie/Devices.py index 165aef1..9c07b56 100644 --- a/src/energenie/Devices.py +++ b/src/energenie/Devices.py @@ -496,6 +496,11 @@ def __repr__(self): return "MIHO005(%s)" % str(hex(self.device_id)) + @staticmethod + def get_join_req(deviceid): + """Get a synthetic join request from this device, for testing""" + return MiHomeDevice.get_join_req(MFRID_ENERGENIE, PRODUCTID_MIHO004, deviceid) + def handle_message(self, payload): ##print("MIHO005 new data %s" % self.device_id) for rec in payload["recs"]: diff --git a/src/energenie/Registry_test.py b/src/energenie/Registry_test.py index 431cdf2..635ff01 100644 --- a/src/energenie/Registry_test.py +++ b/src/energenie/Registry_test.py @@ -153,7 +153,7 @@ discovery_autojoin() # Poke synthetic unknown JOIN into the router and let it route to unknown handler - msg = Devices.MIHO005.get_join_req(Devices.MFRID_ENERGENIE, Devices.PRODUCTID_MIHO005, UNKNOWN_SENSOR_ID) + msg = Devices.MIHO005.get_join_req(UNKNOWN_SENSOR_ID) fsk_router.incoming_message( (Devices.MFRID_ENERGENIE, Devices.PRODUCTID_MIHO005, UNKNOWN_SENSOR_ID), msg) @@ -170,7 +170,7 @@ discovery_askjoin(no) # Poke synthetic unknown JOIN into the router and let it route to unknown handler - msg = Devices.MIHO005.get_join_req(Devices.MFRID_ENERGENIE, Devices.PRODUCTID_MIHO005, UNKNOWN_SENSOR_ID) + msg = Devices.MIHO005.get_join_req(UNKNOWN_SENSOR_ID) fsk_router.incoming_message( (Devices.MFRID_ENERGENIE, Devices.PRODUCTID_MIHO005, UNKNOWN_SENSOR_ID), msg) diff --git a/src/energenie/radio.py b/src/energenie/radio.py index d2cf295..b2afc0c 100644 --- a/src/energenie/radio.py +++ b/src/energenie/radio.py @@ -114,9 +114,9 @@ if DEBUG: print("***TX %s" % payload) - ##import OpenThings - ##if payload[0] < 20: # crude way to reject ook messages - ## print("PAYLOAD: %s" % OpenThings.decode(payload)) + import OpenThings + if payload[0] < 20: # crude way to reject ook messages + print("PAYLOAD: %s" % OpenThings.decode(payload)) # remember that the sensorId is encrypted too framelen = len(payload) diff --git a/src/monitor_mihome.py b/src/monitor_mihome.py index bb9874b..a62fcbc 100644 --- a/src/monitor_mihome.py +++ b/src/monitor_mihome.py @@ -3,12 +3,14 @@ # Monitor Energine MiHome sockets import energenie -import Logger +##import Logger import time -MY_SENSOR_ID = 0x68b # manually captured from a previous run -DUMMY_SENSOR_ID = 0x111 # for testing unknown messages -APP_DELAY = 5 +APP_DELAY = 5 +MY_SENSOR_ID = 0x68b #TESTING +UNKNOWN_SENSOR_ID = 0x111 #TESTING + +energenie.radio.DEBUG = True #----- TEST APPLICATION ------------------------------------------------------- @@ -19,9 +21,9 @@ #TESTING # Manually seed the device registry and router with a known device address - purple = energenie.Devices.MIHO005(MY_SENSOR_ID) - energenie.registry.add(purple, "purple") - energenie.fsk_router.add((energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, MY_SENSOR_ID), purple) + ##purple = energenie.Devices.MIHO005(MY_SENSOR_ID) + ##energenie.registry.add(purple, "purple") + ##energenie.fsk_router.add((energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, MY_SENSOR_ID), purple) # provide a default incoming message handler # This is useful for logging every message @@ -30,39 +32,59 @@ ## Logger.logMessage(message) ##energenie.fsk_router.when_incoming(incoming) - # 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) - message.dump() - Logger.logMessage(message) - purple.when_updated(new_data) + ##def new_data(self, message): + ## print("\nnew data for %s" % self) + ## message.dump() + ## Logger.logMessage(message) + ##purple.when_updated(new_data) + + #TESTING: build a synthetic message + report_msg = energenie.OpenThings.Message(energenie.Devices.MIHO005_REPORT) + report_msg[energenie.OpenThings.PARAM_VOLTAGE]["value"] = 240 + + #TESTING: Poke a synthetic join request coming from a device id + # With the default discovery_autojoin this should enter it into the registry + join_msg = energenie.Devices.MIHO005.get_join_req(MY_SENSOR_ID) + energenie.fsk_router.incoming_message( + (energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, MY_SENSOR_ID), join_msg + ) + + energenie.registry.list() + energenie.fsk_router.list() + + switch_state = True try: - #TESTING: build a synthetic message - msg = energenie.OpenThings.Message(energenie.Devices.MIHO005_REPORT) - msg[energenie.OpenThings.PARAM_VOLTAGE]["value"] = 240 - while True: #TESTING: Poke synthetic unknown into the router and let it route to unknown handler - print("synthetic unknown device") - msg.set(header_sensorid=DUMMY_SENSOR_ID) - energenie.fsk_router.incoming_message( - (energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, DUMMY_SENSOR_ID), msg) + ##print("synthetic unknown device") + ##report_msg.set(header_sensorid=UNKNOWN_SENSOR_ID) + ##energenie.fsk_router.incoming_message( + ## (energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, UNKNOWN_SENSOR_ID), report_msg) #TESTING: Poke synthetic known into the router and let it route to our class instance print("synthetic known device") - msg.set(header_sensorid=MY_SENSOR_ID) + report_msg.set(header_sensorid=MY_SENSOR_ID) energenie.fsk_router.incoming_message( - (energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, MY_SENSOR_ID), msg) + (energenie.Devices.MFRID_ENERGENIE, energenie.Devices.PRODUCTID_MIHO005, MY_SENSOR_ID), report_msg) #TODO: Knit with real radio # Process any received messages from the real radio ##energenie.loop() - print("voltage:%s" % purple.get_voltage()) + # For all devices in the registry, if they offer a power reading, display it + #TODO + + # For all devices in the registry, if they have a switch, toggle it + for d in energenie.registry.devices(): + if d.has_switch(): + d.set_switch(switch_state) + switch_state = not switch_state + + ##print("voltage:%s" % purple.get_voltage()) time.sleep(APP_DELAY) finally: