Join_ack working on discovery mode autojoin
1 parent cafacd0 commit 447832a2109365b56c50f0a8deaff4647c3f68e2
@David Whale David Whale authored on 25 May 2016
Showing 5 changed files
View
8
doc/devices_classes_branch.txt
 
--------------------------------------------------------------------------------
TODO NEXT
 
Code up join auto and join ask, and get tests passing.
joinauto with join_ack is working
 
Code up and test join_autoask
 
 
 
 
---- PERSISTENT REGISTRY
View
6
src/energenie/Devices.py
return self.product_id
 
def join_ack(self):
"""Send a join-ack to the real device"""
self.send_message("join ack") # TODO
msg = OpenThings.Message(header_mfrid=MFRID_ENERGENIE, header_productid=self.product_id, header_sensorid=self.device_id)
msg[OpenThings.PARAM_JOIN] = {"wr":False, "typeid":OpenThings.Value.UINT, "length":0}
self.send_message(msg)
 
#def handle_message(self, payload):
#override for any specific handling
 
View
2
■■■
src/energenie/OpenThings.py
if "paramid" in rec:
paramid = rec["paramid"]
if paramid == key:
return rec
raise RuntimeError("no paramid found for %s" % str(hex(key)))
raise KeyError("no paramid found for %s" % str(hex(key)))
 
 
def __setitem__(self, key, value):
"""set the header or the recs to the provided value"""
View
55
src/energenie/Registry.py
from lifecycle import *
 
import time
try:
import Devices # python 2
# Python 2
import Devices
import OpenThings
except ImportError:
from . import Devices # python 3
 
import Devices
# Python 3
from . import Devices
from . import OpenThings
 
 
directory = {}
 
@deprecated
print("message rejected from:%s" % (str(address)))
# default action is to drop message
# override this method if you want special processing
 
def accept_device(self, address, message):
def accept_device(self, address, message, forward=True):
print("accept_device:%s" % str(address))
# At moment, intentionally assume everything is mfrid=Energenie
product_id = address[1]
device_id = address[2]
self.registry.add(ci, "auto_%s_%s" % (str(hex(product_id)), str(hex(device_id))))
self.router.add(address, ci)
 
# Finally, forward the first message to the new device class instance
print("**** routing first message to class instance")
ci.incoming_message(message)
if forward:
print("**** routing first message to class instance")
ci.incoming_message(message)
 
##self.registry.list()
##self.router.list()
return ci # The new device class instance that we created
 
 
class AutoDiscovery(Discovery):
"""A discovery agent that auto adds unknown devices"""
def __init__(self, registry, router):
Discovery.__init__(self, registry, router)
 
def unknown_device(self, address, message):
print("TODO: unknown device auto join %s" % str(address))
# if it is not a join req
# route to unhandled message handler
# if it is a join req
# accept the device
# send join ack back to device (using new device class instance?)
pass #TODO
print("unknown device auto join %s" % str(address))
 
#TODO: need to make this work with correct meta methods
##if not OpenThings.PARAM_JOIN in message:
try:
j = message[OpenThings.PARAM_JOIN]
except KeyError:
j = None
 
if j == None: # not a join
self.unknown_device(address, message)
else: # it is a join
# but don't forward the join request as it will be malformed with no value
ci = self.accept_device(address, message, forward=False)
ci.join_ack() # Ask new class instance to send a join_ack back to physical device
#TODO: MiHomeDevice needs this join_ack() added to it
 
 
class JoinConfirmedDiscovery(Discovery):
"""A discovery agent that looks for join requests, and auto adds"""
View
src/energenie/Registry_test.py