Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #165 from doudz/dev
Browse files Browse the repository at this point in the history
v0.38.2
  • Loading branch information
doudz authored Jan 20, 2020
2 parents 9f1a862 + 03d335a commit 1093026
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 70 deletions.
8 changes: 3 additions & 5 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def test_disable_persistent(self):

def test_persistent(self):
path = os.path.join(self.test_dir, 'test_zigate.json')
backup_path = path + '.0'

result = self.zigate.load_state(path)
self.assertFalse(result)
Expand All @@ -53,15 +52,12 @@ def test_persistent(self):

self.zigate.save_state(path)
self.assertTrue(os.path.exists(path))
self.assertFalse(os.path.exists(backup_path))
self.zigate.save_state(path)
self.assertTrue(os.path.exists(backup_path))

result = self.zigate.load_state(path)
self.assertTrue(result)

os.remove(path)
os.remove(backup_path)

def test_persistent_loading(self):
data = '''{
Expand Down Expand Up @@ -725,7 +721,9 @@ def test_build_neighbours_table(self):
b'12340123456789abcdef0123456789abcdef00b622'))
table = self.zigate.build_neighbours_table(True)
self.assertEqual(table, [('0000', 'abcd', 182), ('abcd', '9876', 182),
('abcd', '1234', 182)])
('0000', '1234', 182)])
# self.assertEqual(table, [('0000', 'abcd', 182), ('abcd', '9876', 182),
# ('abcd', '1234', 182)])

def test_unsupported_attribute(self):
# some device like profalux doesn't have model identifier
Expand Down
4 changes: 2 additions & 2 deletions zigate/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import argparse
import time
from zigate import connect
logging.basicConfig()
logging.root.setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', help='Debug',
Expand Down Expand Up @@ -45,4 +44,5 @@
time.sleep(1)
except KeyboardInterrupt:
print('Interrupted by user')
z.save_state()
z.close()
11 changes: 11 additions & 0 deletions zigate/adminpanel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from zigate import version as zigate_version
from zigate.core import DeviceEncoder
from zigate.const import ADMINPANEL_PORT
import time


bottle.TEMPLATE_PATH.insert(0, os.path.join(os.path.dirname(__file__), 'views/'))
Expand All @@ -36,6 +37,10 @@ def get_url(routename, **kwargs):
url = bottle.urljoin(bottle.urljoin('/', scriptname), location)
if prefix and not redirect:
url = prefix + url
append = '?'
if '?' in url:
append = '&'
url += '{}_={}'.format(append, time.time())
return url

bottle.BaseTemplate.defaults['get_url'] = get_url
Expand Down Expand Up @@ -108,6 +113,12 @@ def permit_join():
zigate_instance.permit_join()
return bottle.redirect(get_url('index'))

@app.route('/api/led', name='api_led')
def set_led():
on = bottle.request.query.get('on', 'true') == 'true'
zigate_instance.set_led(on)
return bottle.redirect(get_url('index'))

@app.route('/api/discover/<addr>', name='api_discover')
def api_discover(addr):
zigate_instance.discover_device(addr, True)
Expand Down
2 changes: 2 additions & 0 deletions zigate/adminpanel/views/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<div style="display: inline-block; vertical-align: top;">
<h3>Actions</h3>
<a class="pure-button" href="{{get_url('api_permit_join')}}">Permit Join</a>
<a class="pure-button" href="{{get_url('api_led', on='true')}}">Led ON</a>
<a class="pure-button" href="{{get_url('api_led', on='false')}}">Led OFF</a>
</div>

<br>
Expand Down
11 changes: 4 additions & 7 deletions zigate/adminpanel/views/networkmap.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% rebase('base.tpl', subtitle='Network Map')
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://unpkg.com/vis-network@6.5.0/standalone/umd/vis-network.min.js"></script>
<script src="https://unpkg.com/vis-network@7.1.0/standalone/umd/vis-network.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

<div id="zigatenetworkmap"
Expand All @@ -27,14 +27,11 @@
physics: {
enabled: true,
stabilization: false,
barnesHut: {
"centralGravity": 2.7,
"springLength": 0,
"springConstant": 0,
"damping": 0.21,
forceAtlas2Based: {
"springLength": 100,
"avoidOverlap": 1
},
solver: 'barnesHut'
solver: 'forceAtlas2Based'
},
autoResize: true,
width: '100%',
Expand Down
3 changes: 1 addition & 2 deletions zigate/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def run(self):
if __name__ == '__main__':
from zigate.core import ZiGate
import logging
logging.basicConfig()
logging.root.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
z = ZiGate(auto_start=False)
server = Broker(z)
server.run()
129 changes: 80 additions & 49 deletions zigate/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import logging
import json
import os
from shutil import copyfile
from pydispatch import dispatcher
from .transport import (ThreadSerialConnection,
ThreadSocketConnection,
Expand Down Expand Up @@ -177,6 +176,7 @@ def __init__(self, port='auto', path='~/.zigate.json',
self._devices = {}
self._groups = {}
self._scenes = {}
self._led = True
self._neighbours_table_cache = []
self._building_neighbours_table = False
self._path = path
Expand Down Expand Up @@ -328,36 +328,24 @@ def save_state(self, path=None):
self._autosavetimer.cancel()
return
self._path = os.path.expanduser(path)
backup_path = self._path + '.0'
LOGGER.debug('Acquire Lock to save persistent file')
r = self._save_lock.acquire(True, 5)
if not r:
LOGGER.error('Failed to acquire Lock to save persistent file')
return
try:
if os.path.exists(self._path):
LOGGER.debug('File already existing, make a backup before')
copyfile(self._path, backup_path)
except Exception:
LOGGER.error('Failed to create backup, cancel saving.')
LOGGER.error(traceback.format_exc())
LOGGER.debug('Release Lock of persistent file')
self._save_lock.release()
return
try:
data = {'devices': list(self._devices.values()),
'groups': self._groups,
'scenes': self._scenes,
'neighbours_table': self._neighbours_table_cache
'neighbours_table': self._neighbours_table_cache,
'led': self._led
}
with open(self._path, 'w') as fp:
json.dump(data, fp, cls=DeviceEncoder,
sort_keys=True, indent=4, separators=(',', ': '))
except Exception:
LOGGER.error('Failed to save persistent file %s', self._path)
LOGGER.error(traceback.format_exc())
LOGGER.error('Restoring backup...')
copyfile(backup_path, self._path)
LOGGER.debug('Release Lock of persistent file')
self._save_lock.release()

Expand All @@ -368,38 +356,36 @@ def load_state(self, path=None):
LOGGER.warning('Persistent file is disabled')
return
self._path = os.path.expanduser(path)
backup_path = self._path + '.0'
files = [self._path, backup_path]
for f in files:
LOGGER.debug('Trying to load %s', f)
if not os.path.exists(f):
LOGGER.warning('Persistent file %s doesn\'t exist', f)
continue
try:
with open(f) as fp:
data = json.load(fp)
if not isinstance(data, dict): # old version
data = {'devices': data, 'groups': {}}
groups = data.get('groups', {})
for k, v in groups.items():
groups[k] = set([tuple(r) for r in v])
self._groups = groups
self._scenes = data.get('scenes', {})
self._neighbours_table_cache = data.get('neighbours_table', [])
LOGGER.debug('Load neighbours cache: %s', self._neighbours_table_cache)
devices = data.get('devices', [])
for data in devices:
try:
device = Device.from_json(data, self)
self._devices[device.addr] = device
device._create_actions()
except Exception:
LOGGER.error('Error loading device %s', data)
LOGGER.debug('Load success')
return True
except Exception:
LOGGER.error('Failed to load persistent file %s', self._path)
LOGGER.error(traceback.format_exc())
LOGGER.debug('Trying to load %s', self._path)
if not os.path.exists(self._path):
LOGGER.warning('Persistent file %s doesn\'t exist', self._path)
return False
try:
with open(self._path) as fp:
data = json.load(fp)
if not isinstance(data, dict): # old version
data = {'devices': data, 'groups': {}}
groups = data.get('groups', {})
for k, v in groups.items():
groups[k] = set([tuple(r) for r in v])
self._groups = groups
self._scenes = data.get('scenes', {})
self._led = data.get('led', True)
self._neighbours_table_cache = data.get('neighbours_table', [])
LOGGER.debug('Load neighbours cache: %s', self._neighbours_table_cache)
devices = data.get('devices', [])
for data in devices:
try:
device = Device.from_json(data, self)
self._devices[device.addr] = device
device._create_actions()
except Exception:
LOGGER.error('Error loading device %s', data)
LOGGER.debug('Load success')
return True
except Exception:
LOGGER.error('Failed to load persistent file %s', self._path)
LOGGER.error(traceback.format_exc())
LOGGER.debug('No file to load')
return False

Expand Down Expand Up @@ -441,6 +427,7 @@ def startup(self, channel=None):
self._start_event_thread()
self.load_state()
self.setup_connection()
self.set_led(self._led)
version = self.get_version()
self.set_channel(channel)
self.set_type(TYPE_COORDINATOR)
Expand Down Expand Up @@ -965,6 +952,7 @@ def set_led(self, on=True):
'''
Set Blue Led state ON/OFF
'''
self._led = on
data = struct.pack('!?', on)
return self.send_data(0x0018, data)

Expand Down Expand Up @@ -1000,7 +988,7 @@ def set_channel(self, channels=None):
'''
set channel
'''
channels = channels or [11, 14, 15, 19, 20, 24, 25]
channels = channels or [11, 14, 15, 19, 20, 24, 25, 26]
if not isinstance(channels, list):
channels = [channels]
mask = functools.reduce(lambda acc, x: acc ^ 2 ** x, channels, 0)
Expand Down Expand Up @@ -1249,7 +1237,50 @@ def build_neighbours_table(self, force=False):
LOGGER.warning('building neighbours table already started')
return self._neighbours_table_cache

def _neighbours_table(self):
def _neighbours_table(self, addr=None, nodes=None):
'''
Build neighbours table
'''
if addr is None:
addr = self.addr
if nodes is None:
nodes = []
LOGGER.debug('Search for children of %s', addr)
nodes.append(addr)
index = 0
neighbours = []
entries = 255
while index < entries:
r = self.lqi_request(addr, index, True)
if not r:
LOGGER.error('Failed to build neighbours table')
break
data = r.cleaned_data()
entries = data['entries']
for n in data['neighbours']:
# bit_field
# bit 0-1 = u2RxOnWhenIdle 0/1
# bit 2-3 = u2Relationship 0/1/2
# bit 4-5 = u2PermitJoining 0/1
# bit 6-7 = u2DeviceType 0/1/2
is_parent = n['bit_field'][2:4] == '00'
is_child = n['bit_field'][2:4] == '01'
is_router = n['bit_field'][6:8] == '01'
if is_parent:
neighbours.append((n['addr'], addr, n['lqi']))
elif is_child:
neighbours.append((addr, n['addr'], n['lqi']))
elif n['depth'] == 0:
neighbours.append((self.addr, n['addr'], n['lqi']))
if is_router and n['addr'] not in nodes:
LOGGER.debug('%s is a router, search for children', n['addr'])
n2 = self._neighbours_table(n['addr'], nodes)
if n2:
neighbours += n2
index += data['count']
return neighbours

def _neighbours_table2(self):
'''
Build neighbours table
'''
Expand Down
3 changes: 1 addition & 2 deletions zigate/firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ def download_latest(dest='/tmp'):


if __name__ == '__main__':
logging.basicConfig()
logging.root.setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)
download_latest()
3 changes: 1 addition & 2 deletions zigate/mqtt_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ def on_message(self, client, userdata, msg):


if __name__ == '__main__':
logging.basicConfig()
logging.root.setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)
import argparse
from zigate import ZiGate, ZiGateWiFi, ZiGateGPIO

Expand Down
2 changes: 1 addition & 1 deletion zigate/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
#


__version__ = '0.38.1'
__version__ = '0.38.2'

0 comments on commit 1093026

Please sign in to comment.