-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_simple.py
executable file
·285 lines (233 loc) · 8.66 KB
/
example_simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/python3
# Copyright 2016 Robert Muth <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 3
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
Simple example demonstrating basic pyzwaver concepts.
The program will run until all Nodes have been interviewed.
The flag '--curses' enables nice rendering via curses.
Progression:
* open the device
* start the controller
* wait for controller initialization
* wait for each node to be interviewed
(0_None -> 10_Discovered -> 20_Interviewed)
* terminate
"""
import argparse
import curses
import datetime
import logging
import random
import sys
import threading
import time
from pyzwaver.controller import Controller
from pyzwaver.driver import Driver, MakeSerialDevice, MessageStatsString
from pyzwaver.command_translator import CommandTranslator
from pyzwaver import command
from pyzwaver.node import Nodeset, NODE_STATE_NONE, NODE_STATE_DISCOVERED
def _NodeName(n):
return str(n) if n <= 255 else "%d.%d" % (n >> 8, n & 0xff)
class MyFormatter(logging.Formatter):
"""
Nicer logging format
"""
def __init__(self):
super(MyFormatter, self).__init__()
TIME_FMT = '%Y-%m-%d %H:%M:%S.%f'
def format(self, record):
return "%s%s %s:%s:%d %s" % (
record.levelname[0],
datetime.datetime.fromtimestamp(
record.created).strftime(MyFormatter.TIME_FMT)[:-3],
record.threadName,
record.filename,
record.lineno,
record.msg % record.args)
class TestListener(object):
"""
Demonstrates how to hook into the stream of messages
sent to the controller from other nodes
"""
def __init__(self):
pass
def put(self, n, ts, key, values):
name = "@NONE@"
if key[0] is not None:
name = command.StringifyCommand(key)
logging.warning("RECEIVED [%s]: %s - %s", _NodeName(n), name, values)
def Banner(m):
print("=" * 60)
print(m)
print("=" * 60)
class Screen(logging.Handler):
def __init__(self, stdscr, driver, controller, nodeset):
logging.Handler.__init__(self)
self.activity = ""
self.stdscr = stdscr
self.controller = controller
self.nodeset = nodeset
self.driver = driver
self.messages = []
self.h, self.w = self.stdscr.getmaxyx()
def emit(self, record):
"""Implements the looging api"""
self.messages.append(record)
def _printyx(self, y: int, x: int, lines, attr=curses.A_NORMAL):
try:
for line in lines:
self.stdscr.insstr(y, x, line, attr)
y += 1
return len(lines)
except Exception as e:
raise ValueError("%d %d (%d %d)[%s]" % (
y, x, self.h, self.w, e))
def _titleyx(self, y: int, x: int, line, w=80):
if len(line) < w:
line += " " * (w - len(line))
return self._printyx(y, x, [line], curses.A_REVERSE | curses.A_BOLD)
def Redraw(self):
self.stdscr.clear()
if curses.is_term_resized(self.h, self.w):
self.h, self.w = self.stdscr.getmaxyx()
curses.resizeterm(self.h, self.w)
i = 0
i += self._titleyx(i, 0, "CONTROLLER")
lines = str(self.controller).split("\n")
i += self._printyx(i, 0, lines)
def render_node(n):
if n == self.controller.GetNodeId():
return"node %d CONTROLLER" % n
elif n in self.controller.failed_nodes:
return"node %d FAILED" % n
elif n in self.nodeset.nodes:
node = self.nodeset.nodes[n]
return "%s %s" % (node.Name(), node.state)
else:
return "Node %d UNKNOWN" % n
i += self._titleyx(i, 0, "NODES")
nodes = set(self.controller.nodes) | self.nodeset.nodes.keys()
lines = [render_node(n) for n in nodes]
i += self._printyx(i, 0, lines)
i += self._titleyx(i, 0, "QUEUE")
lines = self.driver.OutQueueString().split("\n")
i += self._printyx(i, 0, lines)
i += self._titleyx(i, 0, "STATS")
lines = MessageStatsString(self.driver.History()).split("\n")
i += self._printyx(i, 0, lines)
i = 1
lines = [self.format(r) for r in self.messages[-self.h + 2:]]
self._printyx(i, 81, lines)
i += len(lines) + 1
self.stdscr.refresh()
def InitializeDevices(stdscr, driver, controller):
"""If stdscr is not None we use curses and must not use print
With curses the left side shows interesting info and the right
side show loggging.
"""
translator = CommandTranslator(driver)
nodeset = Nodeset(translator, controller.GetNodeId())
translator.AddListener(TestListener())
# n.InitializeExternally(CONTROLLER.props.product, CONTROLLER.props.library_type, True)
if stdscr:
screen = Screen(stdscr, driver, controller, nodeset)
logger = logging.getLogger()
logger.handlers.clear()
logger.addHandler(screen)
else:
screen = 0
logging.info("Pinging %d nodes", len(controller.nodes))
for n in controller.nodes:
translator.Ping(n, 5, False, "initial")
time.sleep(0.5)
logging.info("Waiting for all nodes to be interviewed")
all_nodes = set(controller.nodes)
ready_nodes = set([controller.GetNodeId()]) | set(controller.failed_nodes)
while len(all_nodes) > len(ready_nodes):
# we may create new pseudo nodes
for n in nodeset.nodes:
all_nodes.add(n)
by_state = {}
for node in nodeset.nodes.values():
if node.state not in by_state:
by_state[node.state] = set()
by_state[node.state].add(node.Name())
if stdscr:
screen.Redraw()
else:
for k, v in by_state.items():
print(k, v)
for n, node in nodeset.nodes.items():
node = nodeset.GetNode(n)
if node.IsInterviewed():
ready_nodes.add(n)
elif node.state == NODE_STATE_NONE:
translator.Ping(n, 3, False, "undiscovered")
elif node.state == NODE_STATE_DISCOVERED:
if driver.OutQueueSizeForNode(
n) < 10 and random.randint(0, 5) == 0:
node.RefreshStaticValues()
time.sleep(3.0)
def main():
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument(
'--serial_port',
type=str,
default="/dev/ttyUSB0",
help='The USB serial device representing the Z-Wave controller stick. ' +
'Common settings are: dev/ttyUSB0, dev/ttyACM0')
parser.add_argument('--verbosity', type=int,
default=logging.ERROR,
help='Lower numbers mean more verbosity')
parser.add_argument(
'--curses',
default=False,
action='store_true',
help='Use curses for rendering. Make sure your terminal '
'is at least 200 chars wide.')
args = parser.parse_args()
# note: this makes sure we have at least one handler
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger()
logger.setLevel(args.verbosity)
for h in logger.handlers:
h.setFormatter(MyFormatter())
logging.info("opening serial: [%s]", args.serial_port)
device = MakeSerialDevice(args.serial_port)
driver = Driver(device)
controller = Controller(driver, pairing_timeout_secs=60)
controller.Initialize()
success = controller.WaitUntilInitialized(2)
if not success:
logging.error("could not initialize controller")
driver.Terminate()
print(list(threading.enumerate()))
return 1
controller.UpdateRoutingInfo()
time.sleep(1)
Banner("Initialized Controller")
print(controller)
if args.curses:
try:
curses.wrapper(InitializeDevices, driver, controller)
except Exception as e:
Banner(str(e))
else:
InitializeDevices(None, driver, controller)
driver.Terminate()
return 0
if __name__ == "__main__":
sys.exit(main())