This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
executable file
·364 lines (290 loc) · 13.5 KB
/
agent.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
"""Zeye Agent
Usage:
agent.py run [-dD] [--device=<device>] [--config=<config>] [--user_path=<user_path>] [--daemon]
[--logfile=<logfile>]
agent.py (-h | --help)
agent.py (-v | --version)
Options:
-h --help Show this screen.
-v --version Show version.
--device=<device> SNMP controller device [default: /dev/ttyUSB0].
--config=<config> OpenZWave config path [default: ./config]
-d --debug Show debug output.
-D --full-debug Show debug and pyagentx debug output.
--daemon Run agent in daemon mode.
--logfile=<logfile> Logfile path [default: '/var/log/zeye/agent.log'].
--user_path=<user_path> Path to user config [default: '.'].
"""
from louie import dispatcher
import pyagentx
import os
import time
from docopt import docopt
import logging
import traceback
from openzwave.network import ZWaveNetwork
from openzwave.option import ZWaveOption
import re
VERSION = '0.0.1'
ROOT_OID = '1.3.6.1.4.1.47015'
logger = logging.getLogger(__name__)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# ch.setFormatter(formatter)
logger.addHandler(ch)
ROOT = os.path.abspath(os.path.dirname(__file__))
def get_oid(oid):
return '{}.{}'.format(ROOT_OID, oid)
zwave_nodes = None
zwave_sensors = {
'temperature': {},
'ultraviolet': {},
'luminance': {},
'humidity': {},
}
class ZeyeAgent(pyagentx.Agent):
def setup(self):
self.register(get_oid('2'), ZeyeInfoUpdater)
self.register(get_oid('3.1'), ZeyeZwaveNodesUpdater)
self.register(get_oid('3.2.1'), ZeyeZwaveTemperatureSensorsUpdater)
self.register(get_oid('3.2.2'), ZeyeZwaveUltravioletSensorsUpdater)
self.register(get_oid('3.2.3'), ZeyeZwaveLuminanceSensorsUpdater)
self.register(get_oid('3.2.4'), ZeyeZwaveHumiditySensorsUpdater)
class ZeyeInfoUpdater(pyagentx.Updater):
def update(self):
self.set_OCTETSTRING('1.0', VERSION)
class ZeyeZwaveNodesUpdater(pyagentx.Updater):
def _get_oid(self, index, field_index):
return '2.1.{}.{}'.format(field_index, index)
def update(self):
global zwave_nodes
if zwave_nodes is None:
return
self.set_INTEGER('1.0', len(zwave_nodes))
for i in range(1, len(zwave_nodes) + 1):
n = zwave_nodes[i - 1]
self.set_INTEGER(self._get_oid(i, 1), i)
self.set_INTEGER(self._get_oid(i, 2), n['zwaveNodeId'])
self.set_OCTETSTRING(self._get_oid(i, 3), n['zwaveNodeName'])
self.set_OCTETSTRING(self._get_oid(i, 4), n['zwaveNodeLocation'])
self.set_INTEGER(self._get_oid(i, 5), n['zwaveNodeBaud'])
self.set_INTEGER(self._get_oid(i, 6), n['zwaveNodeBattery'])
self.set_INTEGER(self._get_oid(i, 7), n['zwaveNodeAwaked'])
self.set_OCTETSTRING(self._get_oid(i, 8), n['zwaveNodeType'])
self.set_OCTETSTRING(self._get_oid(i, 9), n['zwaveNodeTypeName'])
class ZeyeZwaveTemperatureSensorsUpdater(pyagentx.Updater):
def _get_oid(self, index, field_index):
return '2.1.{}.{}'.format(field_index, index)
def update(self):
global zwave_sensors
if not zwave_sensors.get('temperature'):
return
sensors = sorted(zwave_sensors['temperature'].values(),
key=lambda x: x['zwaveTemperatureSensorNodeId'] * 10 + x['_sensorIndex'])
self.set_INTEGER('1.0', len(sensors))
for i in range(1, len(sensors) + 1):
n = sensors[i - 1]
self.set_INTEGER(self._get_oid(i, 1), i)
self.set_INTEGER(self._get_oid(i, 2), n['zwaveTemperatureSensorNodeId'])
self.set_OCTETSTRING(self._get_oid(i, 3), n['zwaveTemperatureSensorId'])
self.set_OCTETSTRING(self._get_oid(i, 4), n['zwaveTemperatureSensorName'])
self.set_OCTETSTRING(self._get_oid(i, 5), n['zwaveTemperatureSensorLocation'])
self.set_INTEGER(self._get_oid(i, 6), n['zwaveTemperatureSensorValue'])
class ZeyeZwaveUltravioletSensorsUpdater(pyagentx.Updater):
def _get_oid(self, index, field_index):
return '2.1.{}.{}'.format(field_index, index)
def update(self):
global zwave_sensors
if not zwave_sensors.get('ultraviolet'):
return
sensors = sorted(zwave_sensors['ultraviolet'].values(),
key=lambda x: x['zwaveUltravioletSensorNodeId'] * 10 + x['_sensorIndex'])
self.set_INTEGER('1.0', len(sensors))
for i in range(1, len(sensors) + 1):
n = sensors[i - 1]
self.set_INTEGER(self._get_oid(i, 1), i)
self.set_INTEGER(self._get_oid(i, 2), n['zwaveUltravioletSensorNodeId'])
self.set_OCTETSTRING(self._get_oid(i, 3), n['zwaveUltravioletSensorId'])
self.set_OCTETSTRING(self._get_oid(i, 4), n['zwaveUltravioletSensorName'])
self.set_OCTETSTRING(self._get_oid(i, 5), n['zwaveUltravioletSensorLocation'])
self.set_INTEGER(self._get_oid(i, 6), n['zwaveUltravioletSensorValue'])
class ZeyeZwaveLuminanceSensorsUpdater(pyagentx.Updater):
def _get_oid(self, index, field_index):
return '2.1.{}.{}'.format(field_index, index)
def update(self):
global zwave_sensors
if not zwave_sensors.get('luminance'):
return
sensors = sorted(zwave_sensors['luminance'].values(),
key=lambda x: x['zwaveLuminanceSensorNodeId'] * 10 + x['_sensorIndex'])
self.set_INTEGER('1.0', len(sensors))
for i in range(1, len(sensors) + 1):
n = sensors[i - 1]
self.set_INTEGER(self._get_oid(i, 1), i)
self.set_INTEGER(self._get_oid(i, 2), n['zwaveLuminanceSensorNodeId'])
self.set_OCTETSTRING(self._get_oid(i, 3), n['zwaveLuminanceSensorId'])
self.set_OCTETSTRING(self._get_oid(i, 4), n['zwaveLuminanceSensorName'])
self.set_OCTETSTRING(self._get_oid(i, 5), n['zwaveLuminanceSensorLocation'])
self.set_INTEGER(self._get_oid(i, 6), n['zwaveLuminanceSensorValue'])
class ZeyeZwaveHumiditySensorsUpdater(pyagentx.Updater):
def _get_oid(self, index, field_index):
return '2.1.{}.{}'.format(field_index, index)
def update(self):
global zwave_sensors
if not zwave_sensors.get('humidity'):
return
sensors = sorted(zwave_sensors['humidity'].values(),
key=lambda x: x['zwaveHumiditySensorNodeId'] * 10 + x['_sensorIndex'])
self.set_INTEGER('1.0', len(sensors))
for i in range(1, len(sensors) + 1):
n = sensors[i - 1]
self.set_INTEGER(self._get_oid(i, 1), i)
self.set_INTEGER(self._get_oid(i, 2), n['zwaveHumiditySensorNodeId'])
self.set_OCTETSTRING(self._get_oid(i, 3), n['zwaveHumiditySensorId'])
self.set_OCTETSTRING(self._get_oid(i, 4), n['zwaveHumiditySensorName'])
self.set_OCTETSTRING(self._get_oid(i, 5), n['zwaveHumiditySensorLocation'])
self.set_INTEGER(self._get_oid(i, 6), n['zwaveHumiditySensorValue'])
def init_loggers(debug=False, pyagentx_debug=False):
level = logging.DEBUG if pyagentx_debug else logging.INFO if debug else logging.WARNING
pyagentx_logger = logging.getLogger('pyagentx')
pyagentx_logger.setLevel(level)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s: %(message)s')
ch = logging.StreamHandler()
ch.setLevel(level)
ch.setFormatter(formatter)
pyagentx_logger.addHandler(ch)
return pyagentx_logger
def zwave_network_started(network):
print("Z-wave network started: HomeId {:08x}".format(network.home_id))
def zwave_network_failed(network):
print('Z-wave network FAILED.')
def zwave_read_nodes(network):
global zwave_nodes
nodes = sorted(network.nodes.values(), key=lambda x: x.node_id)
zwave_nodes = [{
'zwaveNodeId': n.node_id,
'zwaveNodeName': n.product_name,
'zwaveNodeLocation': n.location,
'zwaveNodeBaud': n.max_baud_rate,
'zwaveNodeBattery': n.get_battery_level() if n.get_battery_level() is not None else -1,
'zwaveNodeAwaked': n.is_awake,
'zwaveNodeType': n.product_type,
'zwaveNodeTypeName': n.type,
} for n in nodes]
for node in nodes:
zwave_read_values(network, node, node.values.values())
def zwave_read_values(network, node, values):
for value in values:
if value.genre != 'User':
continue
if re.search('Temperature', value.label, re.I):
v = value.data
if value.units == 'F':
v = (v - 32) * 5 / 9
v = int(100 * v)
zwave_sensors['temperature'][value.value_id] = {
'zwaveTemperatureSensorNodeId': node.node_id,
'zwaveTemperatureSensorId': str(value.value_id),
'zwaveTemperatureSensorName': value.label,
'zwaveTemperatureSensorLocation': node.location,
'zwaveTemperatureSensorValue': v,
'_sensorIndex': value.index,
}
elif re.search('Ultraviolet', value.label, re.I):
v = value.data
v = int(100 * v)
zwave_sensors['ultraviolet'][value.value_id] = {
'zwaveUltravioletSensorNodeId': node.node_id,
'zwaveUltravioletSensorId': str(value.value_id),
'zwaveUltravioletSensorName': value.label,
'zwaveUltravioletSensorLocation': node.location,
'zwaveUltravioletSensorValue': v,
'_sensorIndex': value.index,
}
elif re.search('Luminance', value.label, re.I):
v = value.data
v = int(100 * v)
zwave_sensors['luminance'][value.value_id] = {
'zwaveLuminanceSensorNodeId': node.node_id,
'zwaveLuminanceSensorId': str(value.value_id),
'zwaveLuminanceSensorName': value.label,
'zwaveLuminanceSensorLocation': node.location,
'zwaveLuminanceSensorValue': v,
'_sensorIndex': value.index,
}
elif re.search('Humidity', value.label, re.I):
v = value.data
v = int(100 * v)
zwave_sensors['humidity'][value.value_id] = {
'zwaveHumiditySensorNodeId': node.node_id,
'zwaveHumiditySensorId': str(value.value_id),
'zwaveHumiditySensorName': value.label,
'zwaveHumiditySensorLocation': node.location,
'zwaveHumiditySensorValue': v,
'_sensorIndex': value.index,
}
print(value)
def zwave_network_ready(network):
print("Z-wave network ready: HomeId {:08x}, {} nodes found.".format(network.home_id, network.nodes_count))
dispatcher.connect(zwave_value_update, ZWaveNetwork.SIGNAL_VALUE)
dispatcher.connect(zwave_read_nodes, ZWaveNetwork.SIGNAL_NODE_ADDED)
dispatcher.connect(zwave_read_nodes, ZWaveNetwork.SIGNAL_NODE_EVENT)
dispatcher.connect(zwave_read_nodes, ZWaveNetwork.SIGNAL_NODE_REMOVED)
zwave_read_nodes(network)
def zwave_value_update(network, node, value):
print("Z-wave Value Update: Node: {} Value: {}.".format(node, value))
zwave_read_values(network, node, [value])
def init_zwave_network(device=None, config_path=None, user_path=None, debug=False):
options = ZWaveOption(device, config_path=config_path, user_path=user_path, cmd_line="")
options.set_log_file(os.path.join(user_path, "OZW_Log.log"))
options.set_append_log_file(False)
options.set_console_output(False)
options.set_save_log_level('Info') # ('Info' if debug else 'Warning')
options.set_logging(True)
options.lock()
zwave_network = ZWaveNetwork(options, autostart=False)
# We connect to the louie dispatcher
dispatcher.connect(zwave_network_started, ZWaveNetwork.SIGNAL_NETWORK_STARTED)
dispatcher.connect(zwave_network_failed, ZWaveNetwork.SIGNAL_NETWORK_FAILED)
dispatcher.connect(zwave_network_ready, ZWaveNetwork.SIGNAL_NETWORK_READY)
return zwave_network
def start_agent(device=None, config_path=None, user_path=None, debug=False, pyagentx_debug=False):
init_loggers(debug, pyagentx_debug)
zwave_network = init_zwave_network(device, config_path, user_path, debug)
while True:
try:
zwave_network.start()
agent = ZeyeAgent()
agent.start()
except KeyboardInterrupt:
agent.stop()
zwave_network.stop()
exit(0)
except Exception:
logger.error('Unhandled error:')
logger.error(traceback.format_exc())
try:
agent.stop()
zwave_network.stop()
except Exception:
pass
logger.debug('Restarting in 3 sec...')
time.sleep(3)
if __name__ == '__main__':
arguments = docopt(__doc__)
if arguments['--version']:
print('Version: {}'.format(VERSION))
exit(0)
if arguments['run']:
if arguments['--daemon']:
from daemon import pidfile, DaemonContext
pid = pidfile.TimeoutPIDLockFile('/var/run/zeyeagent.pid', 10)
ctx = DaemonContext(working_directory='.', pidfile=pid)
ctx.open()
start_agent(
device=arguments['--device'],
config_path=arguments['--config'],
user_path=arguments['--user_path'],
debug=arguments['--debug'] or arguments['--full-debug'],
pyagentx_debug=arguments['--full-debug'])