-
Notifications
You must be signed in to change notification settings - Fork 1
/
udiYoManipulatorV2.py
298 lines (242 loc) · 11.7 KB
/
udiYoManipulatorV2.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
#!/usr/bin/env python3
"""
MIT License
"""
try:
import udi_interface
logging = udi_interface.LOGGER
Custom = udi_interface.Custom
except ImportError:
import logging
logging.basicConfig(level=logging.INFO)
from os import truncate
#import udi_interface
#import sys
import time
from yolinkManipulatorV2 import YoLinkManipul
class udiYoManipulator(udi_interface.Node):
from udiYolinkLib import prep_schedule, activate_schedule, update_schedule_data, node_queue, wait_for_node_done, mask2key
id = 'yomanipu'
'''
drivers = [
'GV0' = Manipulator State
'GV1' = OnDelay
'GV2' = OffDelay
'BATLVL' = BatteryLevel
'ST' = Online
]
'''
drivers = [
{'driver': 'GV0', 'value': 99, 'uom': 25},
{'driver': 'GV1', 'value': 0, 'uom': 57},
{'driver': 'GV2', 'value': 0, 'uom': 57},
{'driver': 'BATLVL', 'value': 99, 'uom': 25},
{'driver': 'GV13', 'value': 0, 'uom': 25}, #Schedule index/no
{'driver': 'GV14', 'value': 99, 'uom': 25}, # Active
{'driver': 'GV15', 'value': 99, 'uom': 25}, #start Hour
{'driver': 'GV16', 'value': 99, 'uom': 25}, #start Min
{'driver': 'GV17', 'value': 99, 'uom': 25}, #stop Hour
{'driver': 'GV18', 'value': 99, 'uom': 25}, #stop Min
{'driver': 'GV19', 'value': 0, 'uom': 25}, #days
{'driver': 'GV20', 'value': 99, 'uom': 25},
{'driver': 'ST', 'value': 0, 'uom': 25},
]
def __init__(self, polyglot, primary, address, name, yoAccess, deviceInfo):
super().__init__( polyglot, primary, address, name)
logging.debug('udiYoManipulator INIT- {}'.format(deviceInfo['name']))
self.n_queue = []
self.yoAccess = yoAccess
self.devInfo = deviceInfo
self.yoManipulator = None
self.node_ready = False
self.last_state = ''
self.timer_cleared = True
self.timer_update = 5
self.timer_expires = 0
self.onDelay = 0
self.offDelay = 0
self.schedule_selected = 0
self.valveState = 99 # needed as class c device - keep value until online again
#polyglot.subscribe(polyglot.POLL, self.poll)
polyglot.subscribe(polyglot.START, self.start, self.address)
polyglot.subscribe(polyglot.STOP, self.stop)
self.poly.subscribe(self.poly.ADDNODEDONE, self.node_queue)
# start processing events and create add our controller node
polyglot.ready()
self.poly.addNode(self, conn_status = None, rename = True)
self.wait_for_node_done()
self.node = self.poly.getNode(address)
self.adr_list = []
self.adr_list.append(address)
def start(self):
logging.info('Start - udiYoManipulator')
self.node.setDriver('ST', 0, True, True)
self.yoManipulator = YoLinkManipul(self.yoAccess, self.devInfo, self.updateStatus)
time.sleep(4)
self.yoManipulator.initNode()
time.sleep(2)
#self.node.setDriver('ST', 1, True, True)
self.yoManipulator.delayTimerCallback (self.updateDelayCountdown, self.timer_update)
self.yoManipulator.refreshSchedules()
self.node_ready = True
def stop (self):
logging.info('Stop udiYoManipulator')
self.node.setDriver('ST', 0, True, True)
self.yoManipulator.shut_down()
#if self.node:
# self.poly.delNode(self.node.address)
def checkOnline(self):
#get get info even if battery operated
self.yoManipulator.refreshDevice()
def checkDataUpdate(self):
if self.yoManipulator.data_updated():
self.updateData()
#if time.time() >= self.timer_expires - self.timer_update:
# self.node.setDriver('GV1', 0, True, False)
# self.node.setDriver('GV2', 0, True, False)
def updateData(self):
if self.node is not None:
state = self.yoManipulator.getState()
if self.yoManipulator.online:
if state.upper() == 'OPEN':
self.valveState = 1
self.node.setDriver('GV0', self.valveState , True, True)
if self.last_state != state:
self.node.reportCmd('DON')
elif state.upper() == 'CLOSED':
self.valveState = 0
self.node.setDriver('GV0', self.valveState , True, True)
if self.last_state != state:
self.node.reportCmd('DOF')
else:
self.node.setDriver('GV0', 99, True, True)
self.last_state = state
self.node.setDriver('ST', 1)
#logging.debug('Timer info : {} '. format(time.time() - self.timer_expires))
if time.time() >= self.timer_expires - self.timer_update and self.timer_expires != 0:
self.node.setDriver('GV1', 0, True, False)
self.node.setDriver('GV2', 0, True, False)
logging.debug('udiYoManipulator - getBattery: () '.format(self.yoManipulator.getBattery()))
self.node.setDriver('BATLVL', self.yoManipulator.getBattery(), True, True)
if self.yoManipulator.suspended:
self.node.setDriver('GV20', 1, True, True)
else:
self.node.setDriver('GV20', 0)
else:
self.node.setDriver('GV0', 99)
self.node.setDriver('GV1', 0)
self.node.setDriver('GV2', 0)
self.node.setDriver('BATLVL', 99)
#self.node.setDriver('ST', 0)
self.node.setDriver('GV20', 2, True, True)
self.node.setDriver('GV13', self.schedule_selected)
self.node.setDriver('GV14', 99)
self.node.setDriver('GV15', 99,True, True, 25)
self.node.setDriver('GV16', 99,True, True, 25)
self.node.setDriver('GV17', 99,True, True, 25)
self.node.setDriver('GV18', 99,True, True, 25)
self.node.setDriver('GV19', 0)
sch_info = self.yoManipulator.getScheduleInfo(self.schedule_selected)
self.update_schedule_data(sch_info, self.schedule_selected)
def updateStatus(self, data):
logging.info('updateStatus - udiYoManipulator')
self.yoManipulator.updateStatus(data)
self.updateData()
def updateDelayCountdown( self, timeRemaining):
logging.debug('Manipulator updateDelayCountDown: delays {}'.format(timeRemaining))
max_delay = 0
for delayInfo in range(0, len(timeRemaining)):
if 'ch' in timeRemaining[delayInfo]:
if timeRemaining[delayInfo]['ch'] == 1:
if 'on' in timeRemaining[delayInfo]:
self.node.setDriver('GV1', timeRemaining[delayInfo]['on'], True, False)
if max_delay < timeRemaining[delayInfo]['on']:
max_delay = timeRemaining[delayInfo]['on']
if 'off' in timeRemaining[delayInfo]:
self.node.setDriver('GV2', timeRemaining[delayInfo]['off'], True, False)
if max_delay < timeRemaining[delayInfo]['off']:
max_delay = timeRemaining[delayInfo]['off']
self.node.setDriver('GV0', self.valveState , True, True)
self.timer_expires = time.time()+max_delay
def manipuControl(self, command):
logging.info('Manipulator manipuControl')
state = int(command.get('value'))
if state == 1:
self.yoManipulator.setState('open')
self.valveState = 1
self.node.setDriver('GV0',self.valveState , True, True)
#self.node.reportCmd('DON')
elif state == 0:
self.yoManipulator.setState('closed')
self.valveState = 0
self.node.setDriver('GV0',self.valveState , True, True)
#self.node.reportCmd('DOF')
elif state == 5:
logging.info('manipuControl set Delays Executed: {} {}'.format(self.onDelay, self.offDelay))
#self.yolink.setMultiOutDelay(self.port, self.onDelay, self.offDelay)
self.node.setDriver('GV1', self.onDelay * 60, True, True)
self.node.setDriver('GV2', self.offDelay * 60 , True, True)
self.yoManipulator.setDelayList([{'on':self.onDelay, 'off':self.offDelay}])
def set_open(self, command = None):
logging.info('Manipulator - set_open')
self.yoManipulator.setState('open')
self.valveState = 1
self.node.setDriver('GV0',self.valveState , True, True)
#self.node.reportCmd('DON')
def set_close(self, command = None):
logging.info('Manipulator - set_close')
self.yoManipulator.setState('closed')
self.valveState = 0
self.node.setDriver('GV0',self.valveState , True, True)
#self.node.reportCmd('DOF')
def prepOnDelay(self, command ):
self.onDelay =int(command.get('value'))
logging.info('prepOnDelay {}'.format(self.onDelay))
#self.yoManipulator.setOnDelay(delay)
#self.node.setDriver('GV1', delay*60, True, True)
#self.node.setDriver('GV0',self.valveState , True, True)
def prepOffDelay(self, command):
logging.info('setOnDelay Executed')
self.offDelay =int(command.get('value'))
logging.info('setOnDelay Executed {}'.format(self.offDelay))
#self.yoManipulator.setOffDelay(delay)
#self.node.setDriver('GV2', delay*60, True, True)
#self.node.setDriver('GV0',self.valveState , True, True)
def update(self, command = None):
logging.info('Update Status Executed')
self.yoManipulator.refreshDevice()
def program_delays(self, command):
logging.info('Manipulator program_delays {}'.format(command))
query = command.get("query")
self.onDelay = int(query.get("ondelay.uom44"))
self.offDelay = int(query.get("offdelay.uom44"))
self.node.setDriver('GV1', self.onDelay * 60, True, True)
self.node.setDriver('GV2', self.offDelay * 60 , True, True)
self.yoManipulator.setDelayList([{'on':self.onDelay, 'off':self.offDelay}])
def lookup_schedule(self, command):
logging.info('Manipulator lookup_schedule {}'.format(command))
self.schedule_selected = int(command.get('value'))
self.yoManipulator.refreshSchedules()
def define_schedule(self, command):
logging.info('udiYoSwitch define_schedule {}'.format(command))
query = command.get("query")
self.schedule_selected, params = self.prep_schedule(query)
self.yoManipulator.setSchedule(self.schedule_selected, params)
def control_schedule(self, command):
logging.info('udiYoSwitch control_schedule {}'.format(command))
query = command.get("query")
self.activated, self.schedule_selected = self.activate_schedule(query)
self.yoSwiyoManipulatortch.activateSchedule(self.schedule_selected, self.activated)
commands = {
'UPDATE': update,
'QUERY' : update,
'DON' : set_open,
'DOF' : set_close,
'MANCTRL': manipuControl,
'ONDELAY' : prepOnDelay,
'OFFDELAY' : prepOffDelay,
'DELAY_CTRL' : program_delays,
'LOOKUP_SCH' : lookup_schedule,
'DEFINE_SCH' : define_schedule,
'CTRL_SCH' : control_schedule,
}