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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
#
# Moistcontrol - serial messages
#
# Copyright (c) 2013 Michael Buesch <m@bues.ch>
# Licensed under the terms of the GNU General Public License version 2.
#
import time
import datetime
import configparser
from pymoistcontrol.comm import *
from pymoistcontrol.logging import *
from pymoistcontrol.util import *
class Message(SerialMessage):
# Types
MSG_LOG = 0
MSG_LOG_FETCH = 1
MSG_RTC = 2
MSG_RTC_FETCH = 3
MSG_CONTR_CONF = 4
MSG_CONTR_CONF_FETCH = 5
MSG_CONTR_POT_CONF = 6
MSG_CONTR_POT_CONF_FETCH = 7
MSG_CONTR_POT_STATE = 8
MSG_CONTR_POT_STATE_FETCH = 9
MSG_CONTR_POT_REM_STATE = 10
MSG_CONTR_POT_REM_STATE_FETCH = 11
MSG_MAN_MODE = 12
MSG_MAN_MODE_FETCH = 13
@classmethod
def fromRawMessage(cls, rawMsg):
if not rawMsg:
return None
if rawMsg.getErrorCode() != SerialMessage.COMM_ERR_OK:
msg = Message()
msg.copyHeaderFrom(rawMsg)
return msg
try:
msgId = rawMsg.payload[0]
if msgId == cls.MSG_LOG:
msg = MsgLog(logItem = LogItem.fromBytes(rawMsg.payload[1:]))
elif msgId == cls.MSG_LOG_FETCH:
msg = MsgLogFetch()
elif msgId == cls.MSG_RTC:
msg = MsgRtc(
second = clamp(rawMsg.payload[1], 0, 59),
minute = clamp(rawMsg.payload[2], 0, 59),
hour = clamp(rawMsg.payload[3], 0, 23),
day = clamp(rawMsg.payload[4], 0, 30),
month = clamp(rawMsg.payload[5], 0, 11),
year = clamp(rawMsg.payload[6], 0, 99),
day_of_week = clamp(rawMsg.payload[7], 0, 6))
elif msgId == cls.MSG_RTC_FETCH:
msg = MsgRtcFetch()
elif msgId == cls.MSG_CONTR_CONF:
msg = MsgContrConf(
flags = rawMsg.payload[1],
sensor_lowest_value = rawMsg.payload[2] |
(rawMsg.payload[3] << 8),
sensor_highest_value = rawMsg.payload[4] |
(rawMsg.payload[5] << 8))
elif msgId == cls.MSG_CONTR_CONF_FETCH:
msg = MsgContrConfFetch()
elif msgId == cls.MSG_CONTR_POT_CONF:
msg = MsgContrPotConf(
pot_number = rawMsg.payload[1],
flags = rawMsg.payload[2],
min_threshold = rawMsg.payload[3],
max_threshold = rawMsg.payload[4],
start_time = rawMsg.payload[5] |
(rawMsg.payload[6] << 8),
end_time = rawMsg.payload[7] |
(rawMsg.payload[8] << 8),
dow_on_mask = rawMsg.payload[9])
elif msgId == cls.MSG_CONTR_POT_CONF_FETCH:
msg = MsgContrPotConfFetch(pot_number = rawMsg.payload[1])
elif msgId == cls.MSG_CONTR_POT_STATE:
msg = MsgContrPotState(
pot_number = rawMsg.payload[1],
state_id = rawMsg.payload[2],
is_watering = rawMsg.payload[3],
last_measured_raw_value = rawMsg.payload[4] |
(rawMsg.payload[5] << 8),
last_measured_value = rawMsg.payload[6])
elif msgId == cls.MSG_CONTR_POT_STATE_FETCH:
msg = MsgContrPotStateFetch(pot_number = rawMsg.payload[1])
elif msgId == cls.MSG_CONTR_POT_REM_STATE:
msg = MsgContrPotRemState(
pot_number = rawMsg.payload[1],
flags = rawMsg.payload[2])
elif msgId == cls.MSG_CONTR_POT_REM_STATE_FETCH:
msg = MsgContrPotRemStateFetch(
pot_number = rawMsg.payload[1])
elif msgId == cls.MSG_MAN_MODE:
msg = MsgManMode(force_stop_watering_mask = rawMsg.payload[1],
valve_manual_mask = rawMsg.payload[2],
valve_manual_state = rawMsg.payload[3],
flags = rawMsg.payload[4])
elif msgId == cls.MSG_MAN_MODE_FETCH:
msg = MsgManModeFetch()
else:
raise Error("Unknown message ID: %d" % msgId)
msg.copyHeaderFrom(rawMsg)
return msg
except IndexError as e:
raise Error("Message length error")
def __init__(self, fc=0, serialComm=None):
SerialMessage.__init__(self, fc = fc,
serialComm = serialComm)
def getType(self):
return None
def getPayload(self):
return b""
def toText(self):
raise NotImplementedError
def fromText(self):
raise NotImplementedError
class MsgLog(Message):
def __init__(self, logItem):
self.logItem = logItem
Message.__init__(self)
def getType(self):
return self.MSG_LOG
def getPayload(self):
payload = bytes([ self.getType() ])
payload += self.logItem.getBytes()
return payload
class MsgLogFetch(Message):
def __init__(self):
Message.__init__(self, fc = Message.COMM_FC_REQ_ACK)
def getType(self):
return self.MSG_LOG_FETCH
def getPayload(self):
return bytes([ self.getType(), ])
class MsgRtc(Message):
def __init__(self,
second = 0, minute = 0, hour = 0, day = 0,
month = 0, year = 0, day_of_week = 0):
self.second = second
self.minute = minute
self.hour = hour
self.day = day
self.month = month
self.year = year
self.day_of_week = day_of_week
Message.__init__(self)
def getType(self):
return self.MSG_RTC
def getPayload(self):
return bytes([ self.getType(),
clamp(self.second, 0, 59),
clamp(self.minute, 0, 59),
clamp(self.hour, 0, 23),
clamp(self.day, 0, 30),
clamp(self.month, 0, 11),
clamp(self.year, 0, 99),
clamp(self.day_of_week, 0, 6), ])
def toText(self):
return "[RTC]\n" \
"second=%d\n" \
"minute=%d\n" \
"hour=%d\n" \
"day=%d\n" \
"month=%d\n" \
"year=%d\n" \
"day_of_week=%d\n" % \
(self.second,
self.minute,
self.hour,
self.day,
self.month,
self.year,
self.day_of_week)
def fromText(self, text):
try:
p = configparser.ConfigParser()
p.read_string(text)
self.second = clamp(p.getint("RTC", "second"),
0, 59)
self.minute = clamp(p.getint("RTC", "minute"),
0, 59)
self.hour = clamp(p.getint("RTC", "hour"),
0, 23)
self.day = clamp(p.getint("RTC", "day"),
0, 30)
self.month = clamp(p.getint("RTC", "month"),
0, 11)
self.year = clamp(p.getint("RTC", "year"),
0, 99)
self.day_of_week = clamp(p.getint("RTC", "day_of_week"),
0, 6)
except configparser.Error as e:
raise Error(str(e))
class MsgRtcFetch(Message):
def __init__(self):
Message.__init__(self, fc = Message.COMM_FC_REQ_ACK)
def getType(self):
return self.MSG_RTC_FETCH
def getPayload(self):
return bytes([ self.getType(), ])
class MsgContrConf(Message):
CONTR_FLG_ENABLE = 0x01
def __init__(self,
flags = 0,
sensor_lowest_value = 0,
sensor_highest_value = 0):
self.flags = flags
self.sensor_lowest_value = sensor_lowest_value
self.sensor_highest_value = sensor_highest_value
Message.__init__(self)
def getType(self):
return self.MSG_CONTR_CONF
def getPayload(self):
return bytes([ self.getType(),
self.flags & 0xFF,
self.sensor_lowest_value & 0xFF,
(self.sensor_lowest_value >> 8) & 0xFF,
self.sensor_highest_value & 0xFF,
(self.sensor_highest_value >> 8) & 0xFF, ])
def toText(self):
return "[GLOBAL_CONFIG]\n" \
"flags=%d\n" \
"sensor_lowest_value=%d\n" \
"sensor_highest_value=%d\n" % \
(self.flags,
self.sensor_lowest_value,
self.sensor_highest_value)
def fromText(self, text):
try:
p = configparser.ConfigParser()
p.read_string(text)
self.flags = p.getint("GLOBAL_CONFIG", "flags")
self.sensor_lowest_value = p.getint("GLOBAL_CONFIG",
"sensor_lowest_value")
self.sensor_highest_value = p.getint("GLOBAL_CONFIG",
"sensor_highest_value")
except configparser.Error as e:
raise Error(str(e))
class MsgContrConfFetch(Message):
def __init__(self):
Message.__init__(self, fc = Message.COMM_FC_REQ_ACK)
def getType(self):
return self.MSG_CONTR_CONF_FETCH
def getPayload(self):
return bytes([ self.getType(), ])
class MsgContrPotConf(Message):
POT_FLG_ENABLED = 0x01
POT_FLG_LOG = 0x02
POT_FLG_LOGVERBOSE = 0x04
@classmethod
def toTimeOfDay(cls, hours, minutes, seconds):
return (seconds + (minutes * 60) + (hours * 60 * 60)) // 2
@classmethod
def fromTimeOfDay(cls, value):
if value > cls.toTimeOfDay(23, 59, 59):
value = cls.toTimeOfDay(23, 59, 59)
value *= 2
hours = clamp(value // (60 * 60), 0, 23)
value -= hours * 60 * 60
minutes = clamp(value // 60, 0, 59)
value -= minutes * 60
seconds = clamp(value, 0, 59)
return (hours, minutes, seconds)
def __init__(self,
pot_number,
flags = 0,
min_threshold = 0,
max_threshold = 0,
start_time = 0,
end_time = 0,
dow_on_mask = 0):
self.pot_number = pot_number
self.flags = flags
self.min_threshold = min_threshold
self.max_threshold = max_threshold
self.start_time = start_time
self.end_time = end_time
self.dow_on_mask = dow_on_mask
Message.__init__(self)
def getType(self):
return self.MSG_CONTR_POT_CONF
def getPayload(self):
return bytes([ self.getType(),
self.pot_number & 0xFF,
self.flags & 0xFF,
self.min_threshold & 0xFF,
self.max_threshold & 0xFF,
self.start_time & 0xFF,
(self.start_time >> 8) & 0xFF,
self.end_time & 0xFF,
(self.end_time >> 8) & 0xFF,
self.dow_on_mask & 0xFF, ])
def toText(self):
return "[POT_%d_CONFIG]\n" \
"flags=%d\n" \
"min_threshold=%d\n" \
"max_threshold=%d\n" \
"start_time=%d\n" \
"end_time=%d\n" \
"dow_on_mask=%d\n" % \
(self.pot_number,
self.flags,
self.min_threshold,
self.max_threshold,
self.start_time,
self.end_time,
self.dow_on_mask)
def fromText(self, text):
try:
p = configparser.ConfigParser()
p.read_string(text)
self.flags = p.getint("POT_%d_CONFIG" % self.pot_number,
"flags")
self.min_threshold = p.getint("POT_%d_CONFIG" % self.pot_number,
"min_threshold")
self.max_threshold = p.getint("POT_%d_CONFIG" % self.pot_number,
"max_threshold")
self.start_time = p.getint("POT_%d_CONFIG" % self.pot_number,
"start_time")
self.end_time = p.getint("POT_%d_CONFIG" % self.pot_number,
"end_time")
self.dow_on_mask = p.getint("POT_%d_CONFIG" % self.pot_number,
"dow_on_mask")
except configparser.Error as e:
raise Error(str(e))
class MsgContrPotConfFetch(Message):
def __init__(self, pot_number):
self.pot_number = pot_number
Message.__init__(self, fc = Message.COMM_FC_REQ_ACK)
def getType(self):
return self.MSG_CONTR_POT_CONF_FETCH
def getPayload(self):
return bytes([ self.getType(),
self.pot_number & 0xFF, ])
class MsgContrPotState(Message):
def __init__(self,
pot_number,
state_id = 0,
is_watering = 0,
last_measured_raw_value = 0,
last_measured_value = 0):
self.pot_number = pot_number
self.state_id = state_id
self.is_watering = is_watering
self.last_measured_raw_value = last_measured_raw_value
self.last_measured_value = last_measured_value
Message.__init__(self)
def getType(self):
return self.MSG_CONTR_POT_STATE
def getPayload(self):
return bytes([ self.getType(),
self.pot_number & 0xFF,
self.state_id & 0xFF,
self.is_watering & 0xFF,
self.last_measured_raw_value & 0xFF,
(self.last_measured_raw_value >> 8) & 0xFF,
self.last_measured_value & 0xFF, ])
class MsgContrPotStateFetch(Message):
def __init__(self, pot_number):
self.pot_number = pot_number
Message.__init__(self, fc = Message.COMM_FC_REQ_ACK)
def getType(self):
return self.MSG_CONTR_POT_STATE_FETCH
def getPayload(self):
return bytes([ self.getType(),
self.pot_number & 0xFF, ])
class MsgContrPotRemState(Message):
POT_REMFLG_WDTRIGGER = 0x01
def __init__(self,
pot_number,
flags = 0):
self.pot_number = pot_number
self.flags = flags
Message.__init__(self)
def getType(self):
return self.MSG_CONTR_POT_REM_STATE
def getPayload(self):
return bytes([ self.getType(),
self.pot_number & 0xFF,
self.flags & 0xFF, ])
class MsgContrPotRemStateFetch(Message):
def __init__(self, pot_number):
self.pot_number = pot_number
Message.__init__(self, fc = Message.COMM_FC_REQ_ACK)
def getType(self):
return self.MSG_CONTR_POT_REM_STATE_FETCH
def getPayload(self):
return bytes([ self.getType(),
self.pot_number & 0xFF, ])
class MsgManMode(Message):
MANFLG_FREEZE_CHANGE = 1 << 0
MANFLG_FREEZE_ENABLE = 1 << 1
MANFLG_NOTIFY_CHANGE = 1 << 2
MANFLG_NOTIFY_ENABLE = 1 << 3
def __init__(self,
force_stop_watering_mask = 0,
valve_manual_mask = 0,
valve_manual_state = 0,
flags = 0):
self.force_stop_watering_mask = force_stop_watering_mask
self.valve_manual_mask = valve_manual_mask
self.valve_manual_state = valve_manual_state
self.flags = flags
Message.__init__(self)
def getType(self):
return self.MSG_MAN_MODE
def getPayload(self):
return bytes([ self.getType(),
self.force_stop_watering_mask & 0xFF,
self.valve_manual_mask & 0xFF,
self.valve_manual_state & 0xFF,
self.flags & 0xFF, ])
class MsgManModeFetch(Message):
def __init__(self):
Message.__init__(self, fc = Message.COMM_FC_REQ_ACK)
def getType(self):
return self.MSG_MAN_MODE_FETCH
def getPayload(self):
return bytes([ self.getType(), ])
|