-
Notifications
You must be signed in to change notification settings - Fork 1
/
udiYolinkLib.py
367 lines (320 loc) · 12.1 KB
/
udiYolinkLib.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
365
366
367
#!/usr/bin/env python3
"""
Polyglot TEST v3 node server
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
import json
import math
def save_cmd_state(self, cmd_state):
logging.debug('save_cmd_state {} - {}'.format(cmd_state, self.address))
temp = {}
temp['cmd_state'] = cmd_state
try:
with open(str(self.address)+'.json', 'w') as file:
json.dump(temp, file)
except IOError as e:
logging.error('An error occurred saving command state: {}'.format(e))
def save_cmd_struct(self, cmd_struct):
logging.debug('save_cmd_struct {} - {}'.format(cmd_struct, self.address))
try:
with open(str(self.address)+'.json', 'w') as file:
json.dump(cmd_struct, file)
except IOError as e:
logging.error('An error occurred saving command state: {}'.format(e))
def retrieve_cmd_state(self):
logging.debug('retrieve_cmd_state - {}'.format(self.address))
try:
with open(str(self.address)+'.json', 'r') as file:
temp = json.load(file)
self.cmd_state = temp['cmd_state']
except FileNotFoundError:
self.cmd_state = 0
self.save_cmd_state(self.cmd_state)
logging.debug('retrieve_cmd_state - state = {}'.format(self.cmd_state))
return(self.cmd_state)
def retrieve_cmd_struct(self):
logging.debug('retrieve_cmd_state - {}'.format(self.address))
try:
with open(str(self.address)+'.json', 'r') as file:
temp_struct = json.load(file)
except FileNotFoundError:
temp_struct = {}
logging.debug('retrieve_cmd_state - state = {}'.format(temp_struct))
return(temp_struct)
def node_queue(self, data):
self.n_queue.append(data['address'])
def wait_for_node_done(self):
while len(self.n_queue) == 0:
time.sleep(0.1)
self.n_queue.pop()
def my_setDriver(self, key, value, Unit=None):
logging.debug('my_setDriver : {key} {value} {Unit}')
try:
if value is None:
logging.debug('None value passed = seting 99, UOM 25')
self.node.setDriver(key, 99, True, True, 25)
else:
if Unit:
self.node.setDriver(key, value, True, True, Unit)
else:
self.node.setDriver(key, value)
except ValueError: #A non number was passed
self.node.setDriver(key, 99, True, True, 25)
def mask2key (self, mask):
logging.debug('mask2key : {}'.format(mask))
return(int(round(math.log2(mask),0)))
def daysToMask (yolink, dayList):
daysValue = 0
i = 0
for day in yolink.daysOfWeek:
if day in dayList:
daysValue = daysValue + pow(2,i)
i = i+1
return(daysValue)
def maskToDays(yolink, daysValue):
daysList = []
for i in range(0,7):
mask = pow(2,i)
if (daysValue & mask) != 0 :
daysList.append(yolink.daysOfWeek[i])
return(daysList)
def bool2Nbr (self, data):
if data is True:
return(1)
elif data is False:
return(0)
else:
return(99)
def bool2ISY (self, data):
if data is True:
return(1)
elif data is False:
return(0)
else:
return(99)
def bool2nbr(self, type):
if type is True:
return (1)
elif type is False:
return(0)
else:
return(99)
def state2Nbr(self, val):
if val == 'normal':
return(0)
elif val == 'alert':
return(1)
else:
return(99)
def isy_value(self, value):
if value == None:
return (99)
else:
return(value)
def daylist2bin(self, daylist):
sum = 0
if 'sun' in daylist:
sum = sum + 1
if 'mon' in daylist:
sum = sum + 2
if 'tue' in daylist:
sum = sum + 4
if 'wed' in daylist:
sum = sum + 8
if 'thu' in daylist:
sum = sum + 16
if 'fri' in daylist:
sum = sum + 32
if 'sat' in daylist:
sum = sum + 64
return(sum)
def prep_schedule(self, query):
logging.debug('prep_schedule {} '.format(query))
params = {}
onH = 25
onM = 0
onS = 0
offH = 25
offM = 0
offS = 0
include_sec = False
#query = command.get("query")
if 'port.uom25' in query:
port = int(query.get('port.uom25'))-1
params['ch'] = port
schedule_selected = int(query.get('index.uom25'))
tmp = int(query.get('active.uom25'))
activated = (tmp == 1)
if 'onH.uom19' in query:
onH = int(query.get('onH.uom19'))
if 'onM.uom44' in query:
onM = int(query.get('onM.uom44'))
if 'offH.uom19' in query:
offH = int(query.get('offH.uom19'))
if 'offM.uom44' in query:
offM = int(query.get('offM.uom44'))
if 'onS.' in query:
include_sec = True
if 'onS.uom57' in query:
onS = int(query.get('onS.uom57'))
else:
onS = 0
if 'offS.' in query:
include_sec = True
if 'offS.uom57' in query:
offS = int(query.get('onS.uom57'))
else:
offS = 0
binDays = int(query.get('bindays.uom25'))
params['index'] = str(schedule_selected )
params['isValid'] = activated
params['on'] = str(onH)+':'+str(onM)
params['off'] = str(offH)+':'+str(offM)
if include_sec:
params['on'] = params['on'] + ':' + str(onS)
params['off'] = params['off'] + ':' + str(offS)
params['week'] = binDays
#self.yolink.setSchedule(self.schedule_selected, params)
return(schedule_selected, params)
def activate_schedule(self, query):
logging.info('activate_schedule {}'.format(query))
#query = command.get("query")
schedule_selected = int(query.get('index.uom25'))
tmp = int(query.get('active.uom25'))
activated = (tmp == 1)
#self.yolink.activateSchedule(schedule_selected, activated)
return(activated, schedule_selected)
def check_name_in_drivers(self, name):
logging.debug('check_name_in_drivers: {}'.format(name))
found = False
for drv in enumerate(self.node.drivers):
logging.debug('check_name_in_drivers: {}'.format(drv))
if drv['driver'] == name:
found = True
return(found)
def update_schedule_data(self, sch_info, selected_schedule):
logging.info('update_schedule_data {}'.format(sch_info))
def check_name_in_drivers( name):
found = False
for indx, drv in enumerate(self.node.drivers):
if drv['driver'] == name:
found = True
return(found)
return(found)
if sch_info:
if 'ch' in sch_info:
self.node.setDriver('GV12', int(sch_info['ch']))
self.node.setDriver('GV13', selected_schedule)
if sch_info['isValid']:
self.node.setDriver('GV14', 1)
else:
self.node.setDriver('GV14', 0)
timestr = sch_info['on']
timelist = timestr.split(':')
if len(timelist) == 2:
hour = int(timelist[0])
minute = int(timelist[1])
if hour == 25:
self.node.setDriver('GV15', 98,True, True, 25)
self.node.setDriver('GV16', 98,True, True, 25)
else:
self.node.setDriver('GV15', int(hour),True, True, 19)
self.node.setDriver('GV16', int(minute),True, True, 44)
elif len(timelist) == 3:
hour = int(timelist[0])
minute = int(timelist[1])
second = int(timelist[2])
if hour == 25:
self.node.setDriver('GV15', 98,True, True, 25)
self.node.setDriver('GV16', 98,True, True, 25)
self.node.setDriver('GV10', 98,True, True, 25)
else:
self.node.setDriver('GV15', hour,True, True, 19)
self.node.setDriver('GV16', minute,True, True, 44)
self.node.setDriver('GV10', second,True, True, 57)
timestr = sch_info['off']
logging.debug('timestr : {}'.format(timestr))
timelist = timestr.split(':')
if len(timelist) == 2:
hour = int(timelist[0])
minute = int(timelist[1])
if hour == 25:
self.node.setDriver('GV17', 98,True, True, 25)
self.node.setDriver('GV18', 98,True, True, 25)
else:
self.node.setDriver('GV17', int(hour),True, True, 19)
self.node.setDriver('GV18', int(minute),True, True, 44)
elif len(timelist) == 3:
hour = int(timelist[0])
minute = int(timelist[1])
second = int(timelist[2])
if hour == 25:
self.node.setDriver('GV17', 98,True, True, 25)
self.node.setDriver('GV18', 98,True, True, 25)
self.node.setDriver('GV11', 98,True, True, 25)
else:
self.node.setDriver('GV17', hour,True, True, 19)
self.node.setDriver('GV18', minute,True, True, 44)
self.node.setDriver('GV11', second,True, True, 57)
self.node.setDriver('GV19', int(sch_info['week']))
else:
logging.debug('No schdule exist for the selected index')
if check_name_in_drivers('GV12'):
self.node.setDriver('GV12', 99, True, True, 25)
self.node.setDriver('GV13', selected_schedule)
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)
if check_name_in_drivers('GV10'):
self.node.setDriver('GV10', 99, True, True, 25)
self.node.setDriver('GV11', 99, True, True, 25)
def send_rel_temp_to_isy(self, temperature, stateVar):
logging.debug('convert_temp_to_isy - {}'.format(temperature))
logging.debug('ISYunit={}, Mess_unit={}'.format(self.ISY_temp_unit , self.messana_temp_unit ))
if self.ISY_temp_unit == 0: # Celsius in ISY
if self.messana_temp_unit == 'Celsius' or self.messana_temp_unit == 0:
self.node.setDriver(stateVar, round(temperature,1), True, True, 4)
else: # messana = Farenheit
self.node.setDriver(stateVar, round(temperature*5/9,1), True, True, 17)
elif self.ISY_temp_unit == 1: # Farenheit in ISY
if self.messana_temp_unit == 'Celsius' or self.messana_temp_unit == 0:
self.node.setDriver(stateVar, round((temperature*9/5),1), True, True, 4)
else:
self.node.setDriver(stateVar, round(temperature,1), True, True, 17)
else: # kelvin
if self.messana_temp_unit == 'Celsius' or self.messana_temp_unit == 0:
self.node.setDriver(stateVar, round((temperature,1), True, True, 4))
else:
self.node.setDriver(stateVar, round((temperature)*9/5,1), True, True, 17)
def send_temp_to_isy (self, temperature, stateVar):
logging.debug('convert_temp_to_isy - {}'.format(temperature))
logging.debug('ISYunit={}, Mess_unit={}'.format(self.ISY_temp_unit , self.messana_temp_unit ))
if self.ISY_temp_unit == 0: # Celsius in ISY
if self.messana_temp_unit == 'Celsius' or self.messana_temp_unit == 0:
self.node.setDriver(stateVar, round(temperature,1), True, True, 4)
else: # messana = Farenheit
self.node.setDriver(stateVar, round((temperature-32)*5/9,1), True, True, 17)
elif self.ISY_temp_unit == 1: # Farenheit in ISY
if self.messana_temp_unit == 'Celsius' or self.messana_temp_unit == 0:
self.node.setDriver(stateVar, round((temperature*9/5+32),1), True, True, 4)
else:
self.node.setDriver(stateVar, round(temperature,1), True, True, 17)
else: # kelvin
if self.messana_temp_unit == 'Celsius' or self.messana_temp_unit == 0:
self.node.setDriver(stateVar, round((temperature+273.15,1), True, True, 4))
else:
self.node.setDriver(stateVar, round((temperature+273.15-32)*9/5,1), True, True, 17)