Skip to content
Snippets Groups Projects
Commit a135f3ca authored by jkerdreu's avatar jkerdreu
Browse files

Last ujson calls.. migrate find_devices to devices mod

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2379 b32b6428-25c9-4566-ad07-03861ab6144f
parent fe015497
Branches
No related tags found
No related merge requests found
......@@ -13,6 +13,31 @@ AQARA_ENCRYPT_IV = b'\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58\x5
logger = logging.getLogger(__name__)
def find_device_class(model):
if model in ['sensor_switch.aq3','sensor_switch.aq2','switch','86sw1']:
return Switch
if model in ['86sw2','remote.b286acn01']:
return Switch86sw2
if model == 'gateway':
return Gateway
if model == 'weather.v1':
return Weather
if model in ['motion','sensor_motion.aq2']:
return Motion
if model in ['magnet','sensor_magnet.aq2']:
return Magnet
if model == 'vibration':
return Vibration
if model == 'sensor_cube.aqgl01':
return Cube
if model == 'sensor_wleak.aq1':
return RelayController
if model == 'lumi.ctrl_dualchn':
return RelayController
return None
class AqaraDev(object):
def __init__(self,sid,model,base_addr,xaal_gw):
self.sid = sid
......@@ -214,7 +239,6 @@ class Gateway(AqaraDev):
def send(self,pkt):
"""send a the unicast datagram to the lumi GW"""
print(pkt)
if not self.ip:
logger.warning("GW IP not found yet, please wait")
return
......
......@@ -12,28 +12,6 @@ import logging
PACKAGE_NAME = 'xaal.aqara'
logger = logging.getLogger(PACKAGE_NAME)
def find_device_class(model):
if model in ['sensor_switch.aq3','sensor_switch.aq2','switch','86sw1']:
return devices.Switch
if model in ['86sw2','remote.b286acn01']:
return devices.Switch86sw2
if model == 'gateway':
return devices.Gateway
if model == 'weather.v1':
return devices.Weather
if model in ['motion','sensor_motion.aq2']:
return devices.Motion
if model in ['magnet','sensor_magnet.aq2']:
return devices.Magnet
if model == 'vibration':
return devices.Vibration
if model == 'sensor_cube.aqgl01':
return devices.Cube
if model == 'sensor_wleak.aq1':
return devices.RelayController
if model == 'lumi.ctrl_dualchn':
return devices.RelayController
return None
class GW(gevent.Greenlet):
def __init__(self,engine):
......@@ -54,7 +32,8 @@ class GW(gevent.Greenlet):
self.cfg = cfg
def add_device(self,sid,model,base_addr):
klass = find_device_class(model)
""" find the device class for model, and add it to the engine"""
klass = devices.find_device_class(model)
if klass:
dev = klass(sid,model,base_addr,self)
self.engine.add_devices(dev.devices)
......@@ -65,36 +44,39 @@ class GW(gevent.Greenlet):
return None
def setup(self):
"""Start Aqara mutlicast receiver"""
self.aqara = AqaraConnector()
def get_device(self,sid,model):
cfg = self.cfg['devices']
# Already running device ?
if sid in self.devices.keys():
return self.devices[sid]
# Already known device ?
elif sid in self.cfg['devices'].keys():
cfg = self.cfg['devices'][sid]
model_old = cfg.get('model',None)
base_addr = tools.get_uuid( cfg.get('base_addr',None) )
elif sid in cfg:
dev_cfg = cfg.get(sid)
model_old = dev_cfg.get('model',None)
base_addr = tools.get_uuid( dev_cfg.get('base_addr',None) )
dev = None
if model != model_old:
logger.warn(f"Device {sid} wrong model")
if model and base_addr:
dev = self.add_device(sid,model,base_addr)
if dev and model == 'gateway':
dev.secret = cfg.get('secret',None)
dev.secret = dev_cfg.get('secret',None)
return dev
# Still not found ? => new device
else:
base_addr = tools.get_random_uuid()
dev = self.add_device(sid,model,base_addr)
logger.warning(f"Discover new Aqara Device {model} {sid}")
if dev:
cfg = {'base_addr' : base_addr.str,'model':model}
logger.warning(f"Discover new Aqara Device {model} {sid}")
cfg = {'base_addr' : str(base_addr),'model':model}
self.cfg['devices'].update({sid:cfg})
return dev
def _run(self):
logger.info("GW ready, waiting for Aqara Hubs...")
while 1:
pkt = self.aqara.receive()
if pkt:
......
......@@ -5,18 +5,17 @@
from xaal.lib import network
import logging
import ujson
import json
logger = logging.getLogger(__name__)
class AqaraConnector:
class AqaraConnector(object):
"""
Aqara Device Report/Heartbeat connector, used only to receive event, if you want to send a command,
you must send a unicast datagram to the right Aqara GW.
Please note, this isn't the Gateway discovery
"""
def __init__(self):
self.nc = network.NetworkConnector('224.0.0.50',9898,10)
self.nc.connect()
......@@ -25,10 +24,29 @@ class AqaraConnector:
buf = self.nc.receive()
if buf:
try:
#logger.warning(buf)
return ujson.decode(buf)
return json.loads(buf)
except ValueError:
logger.debug('JSON decoder Error %s' % buf)
return None
class AqaraDiscovery:
""" Aqara Hubs discovery, not used right now"""
def __init__(self):
self.nc = network.NetworkConnector('224.0.0.50',4321,10)
self.nc.connect()
def receive(self):
buf = self.nc.receive()
if buf:
try:
#logger.warning(buf)
return json.loads(buf)
except ValueError:
logger.debug('JSON decoder Error %s' % buf)
return None
def discover(self):
h = {"cmd":"whois"}
self.nc.send(json.dumps(h).encode('utf-8'))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment