-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaa_api.py
More file actions
729 lines (638 loc) · 30.9 KB
/
aa_api.py
File metadata and controls
729 lines (638 loc) · 30.9 KB
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
import atexit
from flask import Flask, request, jsonify
import threading
import json
import serial
import re
import logging
import time
import argparse
import sys
from enum import Enum, IntEnum
from dataclasses import dataclass
logger = logging.getLogger(__name__)
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
CB_INACTIVITY_TIMEOUT_SECONDS = 10
class SystemType(IntEnum):
AC = 7
LIGHTS = 2
class Destination(IntEnum):
CONTROL_BOX = 1
TABLET = 3
class WeekDay(IntEnum):
SUNDAY = 64
MONDAY = 32
TUESDAY = 16
WEDNESDAY = 8
THURSDAY = 4
FRIDAY = 2
SATURDAY = 1
class FanMode(IntEnum):
off = 0
low = 1
medium = 2
high = 3
auto = 4
autoAA = 5
class AirConMode(Enum):
cool = 1
heat = 2
vent = 3
auto = 4
dry = 5
myauto = 6
class FreshAirStatus(IntEnum):
none = 0
off = 1
on = 2
class SystemState(IntEnum):
off = 0
on = 1
class ZoneState(IntEnum):
close = 0
open = 1
class UnitType(IntEnum):
daikin = 0x11
panasonic = 0x12
fujitsu = 0x13
samsungdvm = 0x19
class ActivationStatus(IntEnum):
nocode = 0
expired = 1
codeenabled = 2
class Registers(IntEnum):
GLOBAL_ZONE_CONFIG = 0x01
UNIT_TYPE_ACTIVATION_STATUS = 0x02
INDIVIDUAL_ZONE_STATE = 0x03
INDIVIDUAL_ZONE_CONFIG = 0x04
SYSTEM_STATUS = 0x05
CONTROL_BOX_2FIRMWARE_VERSION = 0x06
STATUS_ACK = 0x07
AIR_CON_ERROR = 0x08
ACTIVATION_CODE_ENTRY = 0x09
CONTROL_BOX_EXISTS_NOTIFICATION = 0x0a
SENSOR_PAIRING = 0x12
INFO_BYTE = 0x13
@dataclass(frozen=True)
class DirtyRegister():
id: str
acuid: str
register: int
zone_id: str
zone_number: int
@dataclass(frozen=True)
class RegisterUpdateFromCB():
system_type: int
destination_device: int
unit_id: str
register_id: int
data: list
running = True
# *********************************************************************
# CAN2 XML message parser / generator
# ---------------------------------------------------------------------
# Creates and parses messages of the form <U>msg</U=crc>, as used by
# the AA control box.
# *********************************************************************
class CanXmlParser:
message_regex = re.compile("(<U>[A-Za-z0-9 ]+</U=[0-9A-Za-z]{2}>)")
message_parse_regex = re.compile("<U>([A-Za-z0-9 ]+)</U=([0-9A-Za-z]{2})>")
max_buffer_size = 2048
def __init__(self):
self.data = ""
# AA CRC8 calculation (init=0x00, final=0xff, poly=0xb2)
def aacrc8(self,data: bytes):
crc = 0x00
for byte in data:
crc = crc ^ byte
for j in range(8):
if (crc & 0x01):
crc = (crc >> 1) ^ 0xb2
else:
crc = crc >> 1
return crc ^ 0xff
def pass_crc_check_fn(self,msg):
parse_result = CanXmlParser.message_parse_regex.match(msg)
if (parse_result):
msg_crc = int(parse_result.group(2), 16)
msg_content = parse_result.group(1)
calculated_crc = self.aacrc8(msg_content.encode("utf-8"))
return calculated_crc == msg_crc
else:
return False
def extract_msg_from_xml(self,msg):
parse_result = CanXmlParser.message_parse_regex.match(msg)
if (parse_result):
return parse_result.group(1)
else:
return msg
# process bytes read from the serial port; return an array of message bodies.
def process_incoming_serial_data(self, data):
self.data += data.decode("utf-8")
messages = CanXmlParser.message_regex.split(self.data)
self.data = messages[len(messages)-1]
if (len(self.data) > CanXmlParser.max_buffer_size):
self.data = self.data[CanXmlParser.max_buffer_size/2:len(self.data)-1]
if (len(messages) > 1):
return map(self.extract_msg_from_xml, filter(self.pass_crc_check_fn, messages[1:len(messages)-1]))
else:
return []
def encode_packet(self, data):
return "<U>{}</U={:02x}>".format(data, self.aacrc8(data.encode("utf-8"))).encode("utf-8")
def format_hex2(val):
return '{:02x}'.format(val)
lock = threading.Lock()
class Store:
def __init__(self):
self.db = {}
self.dirty_registers = set()
def initialise(self, filename):
self.filename = filename
try:
json_file = open(filename)
except FileNotFoundError:
self.db = { }
else:
self.db = json.load(json_file)
json_file.close()
def save(self):
global lock
try:
lock.acquire()
with open(self.filename, 'w') as outfile:
json.dump(self.db, outfile)
finally:
lock.release()
def process_json_update(self, upd):
global lock
try:
lock.acquire()
self.db = self.db | upd
self.update_dirty_registers_based_on_json_update(upd)
return "Success"
finally:
lock.release()
def build_register_update_message(self, unit_id, register, args):
zeroed_args = (args + [0,0,0,0,0,0,0])[:7]
return '{:02X}'.format(SystemType.AC) + '{:02X}'.format(Destination.CONTROL_BOX) + unit_id.lower() +'{:02X}'.format(register) + ''.join(map(format_hex2, zeroed_args))
def update_dirty_registers_based_on_json_update(self, update):
global lock
try:
lock.acquire()
# attempt to determine what has changed based on the incoming update, and mark
# the appropriate CB registers as "dirty" - ie. needing to be pushed to the CB.
for ackey in update["aircons"]:
# get the UID of the aircon from the store
acuid = self.db["aircons"][ackey]["info"]["uid"]
# determine what has changed in update for this aircon ...
# if we received an update to the aircons.acXX.info data, then flush system status and global zone config to CB
if update["aircons"][ackey]["info"]:
logger.info("Flushing system status and global zone config for "+ackey)
self.dirty_registers.update({
DirtyRegister(ackey, acuid, Registers.SYSTEM_STATUS, None, None),
DirtyRegister(ackey, acuid, Registers.GLOBAL_ZONE_CONFIG, None, None)
})
# check zones for updates and flag each individual zone as dirty if required
if update["aircons"][ackey]["zones"]:
for zonekey in update["aircons"][ackey]["zones"]:
zone_number = self.db["aircons"][ackey]["zones"][zonekey]["number"]
logger.info("Flushing zone config for "+ackey+"/"+zonekey)
self.dirty_registers.update({
DirtyRegister(ackey, acuid, Registers.INDIVIDUAL_ZONE_CONFIG, zonekey, zone_number),
DirtyRegister(ackey, acuid, Registers.INDIVIDUAL_ZONE_STATE, zonekey, zone_number)
})
if (self.db["aircons"][ackey]["zones"][zonekey]["SensorUid"]):
logger.info("Flushing sensor pairing data for zone "+ackey+"/"+zonekey)
self.dirty_registers.update({
DirtyRegister(ackey, acuid, Registers.SENSOR_PAIRING, zonekey, zone_number)
})
finally:
lock.release()
def dirty_all_registers(self):
global lock
try:
lock.acquire()
logger.info("dirtying all registers - db = " + str(type(self.db)) + "/" + str(self.db))
# attempt to determine what has changed based on the incoming update, and mark
# the appropriate CB registers as "dirty" - ie. needing to be pushed to the CB.
for ackey in self.db["aircons"]:
# get the UID of the aircon from the store
acuid = self.db["aircons"][ackey]["info"]["uid"]
# determine what has changed in update for this aircon ...
# if we received an update to the aircons.acXX.info data, then flush system status and global zone config to CB
logger.info("Flushing system status and global zone config for "+ackey)
self.dirty_registers.update({
DirtyRegister(ackey, acuid, Registers.SYSTEM_STATUS, None, None),
DirtyRegister(ackey, acuid, Registers.GLOBAL_ZONE_CONFIG, None, None)
})
# check zones for updates and flag each individual zone as dirty if required
for zonekey in self.db["aircons"][ackey]["zones"]:
zone_number = self.db["aircons"][ackey]["zones"][zonekey]["number"]
logger.info("Flushing zone config and zone state for zone "+ackey+"/"+zonekey)
self.dirty_registers.update({
DirtyRegister(ackey, acuid, Registers.INDIVIDUAL_ZONE_CONFIG, zonekey, zone_number),
DirtyRegister(ackey, acuid, Registers.INDIVIDUAL_ZONE_STATE, zonekey, zone_number)
})
if (self.db["aircons"][ackey]["zones"][zonekey]["SensorUid"]):
logger.info("Flushing sensor pairing data for zone "+ackey+"/"+zonekey)
self.dirty_registers.update({
DirtyRegister(ackey, acuid, Registers.SENSOR_PAIRING, zonekey, zone_number)
})
finally:
lock.release()
def process_register_update_from_cb(self, upd):
global lock
try:
lock.acquire()
cb_id = upd.unit_id
if (self.db["aircons"] is None):
self.db["aircons"] = {}
ac = self.find_ac_by_uid(cb_id)
if not ac:
logger.warning("Received notification from unknown control box; please follow instructions to configure control box with ID: "+cb_id)
logger.warning("Cannot proceed safely, exiting...")
sys.exit()
if (upd.register_id == Registers.CONTROL_BOX_EXISTS_NOTIFICATION.value):
logger.debug("Control box " + cb_id + " exists notification received")
elif (upd.register_id == Registers.SENSOR_PAIRING.value):
logger.debug("Control box " + cb_id + " detected sensor pairing button press: "+ 'SensorUid: {:02x}{:02x}{:02x}, Sensor Maj Rev: {:02x}'.format(upd.data[0],upd.data[1],upd.data[2], upd.data[4]))
elif (upd.register_id == Registers.CONTROL_BOX_2FIRMWARE_VERSION.value):
logger.debug("Control box firmware version notification received - firmware = v" + str(upd.data[0]) + "." + str(upd.data[1]) + "(rf v=" + str(upd.data[3]) + ")")
ac["info"]["cbFWRevMajor"] = upd.data[0]
ac["info"]["cbFWRevMinor"] = upd.data[1]
ac["info"]["unitType"] = upd.data[2]
ac["info"]["rfFWRevMajor"] = upd.data[3]
elif (upd.register_id == Registers.GLOBAL_ZONE_CONFIG.value):
logger.debug("Global zone config received")
ac["info"]["noOfZones"] = upd.data[1]
ac["info"]["noOfConstants"] = upd.data[2]
ac["info"]["constant1"] = upd.data[3]
ac["info"]["constant2"] = upd.data[4]
ac["info"]["constant3"] = upd.data[5]
ac["info"]["filterCleanStatus"] = upd.data[6]
elif (upd.register_id == Registers.SYSTEM_STATUS.value):
logger.debug("System status update received")
ac["info"]["state"] = SystemState(upd.data[0]).name
ac["info"]["mode"] = AirConMode(upd.data[1]).name
ac["info"]["fan"] = FanMode(upd.data[2]).name
ac["info"]["setTemp"] = upd.data[3] / 2.0
ac["info"]["myZone"] = upd.data[4]
ac["info"]["freshAirStatus"] = FreshAirStatus(upd.data[5]).name
ac["info"]["rfSysId"] = upd.data[6]
elif (upd.register_id == Registers.AIR_CON_ERROR.value):
error_code = bytes(upd.data).decode()
logger.error("Received error status code from AC: "+error_code)
ac["info"]["airconErrorCode"] = error_code
elif (upd.register_id == Registers.INFO_BYTE.value):
logger.debug("Received info byte update for AC -- ignoring")
elif (upd.register_id == Registers.UNIT_TYPE_ACTIVATION_STATUS.value):
logger.debug("Received activation status update for AC: "+str(upd.register_id))
ac["info"]["unitType"] = upd.data[0]
ac["info"]["activationCodeStatus"] = ActivationStatus(upd.data[1]).name
elif (upd.register_id == Registers.INDIVIDUAL_ZONE_STATE.value):
zone_num = upd.data[0]
logger.debug("Received zone state update for AC: "+str(upd.register_id)+"(zone: "+str(zone_num)+")"+" - measured temp = " + str(upd.data[4] + upd.data[5]/10.0))
zone = self.find_zone(ac, zone_num)
if (zone is None):
logger.warning("Received zone state update for zone "+str(zone_num)+" which is not configured.")
return
zone["state"] = "open" if ((upd.data[1] & 128) == 128) else "closed"
zone["value"] = upd.data[1] & 127
zone["type"] = upd.data[2]
zone["setTemp"] = upd.data[3] / 2.0
zone["measuredTemp"] = upd.data[4] + upd.data[5]/10.0
elif (upd.register_id == Registers.INDIVIDUAL_ZONE_CONFIG.value):
zone_num = upd.data[0]
logger.debug("Received zone config for AC: "+str(upd.register_id)+"(zone: "+str(zone_num)+")")
zone = self.find_zone(ac, zone_num)
if (zone is None):
logger.warning("Received zone config update for zone "+str(zone_num)+" which is not configured.")
return
zone["minDamper"] = upd.data[1]
zone["maxDamper"] = upd.data[2]
zone["motion"] = upd.data[3]
zone["motionConfig"] = upd.data[4]
zone["rssi"] = upd.data[6]
else:
print("Received update for unknown register: "+str(upd.register_id))
finally:
lock.release()
def clear_dirty_registers(self):
global lock
try:
lock.acquire()
self.dirty_registers.clear()
finally:
lock.release()
def get_dirty_register_msg(self):
global lock
try:
lock.acquire()
count = 0
to_process = set()
while (len(self.dirty_registers) > 0 and count < 5):
to_process.add(self.dirty_registers.pop())
count = count + 1
if len(to_process) >= 0:
msg = "setCAN"
for register in to_process:
if (register.register == Registers.SYSTEM_STATUS):
# `05` - CB JZ14 - System Status
#| Byte # | Description |
#| ------ | ----------- |
#| 0 | System State - On (`01`) or off (`00`) |
#| 1 | System Mode (`01`=cool,`02`=heat,`03`=vent,`04`=auto,`05`=dry,`06`=myauto) |
#| 2 | System Fan (`00`=off, `01`=low, `02`=medium, `03`=high, `04`=auto, `05`=autoAA) |
#| 3 | Set Temp (deg C * 2.0) |
#| 4 | MyZone ID (1-10, 0 = default / not enabled??) |
#| 5 | Fresh Air Status (`00`=none, `01`=off, `02`=on) |
#| 6 | RF Sys ID |
msg = msg + " " + self.build_register_update_message(
register.acuid,
register.register,
[
SystemState[self.db["aircons"][register.id]["info"]["state"]].value,
AirConMode[self.db["aircons"][register.id]["info"]["mode"]].value,
FanMode[self.db["aircons"][register.id]["info"]["fan"]].value,
int(self.db["aircons"][register.id]["info"]["setTemp"] * 2.0),
self.db["aircons"][register.id]["info"]["myZone"],
FreshAirStatus[self.db["aircons"][register.id]["info"]["freshAirStatus"]].value,
self.db["aircons"][register.id]["info"]["rfSysID"]
]
)
elif (register.register == Registers.GLOBAL_ZONE_CONFIG):
# `01` - (Tablet to CB) - Zone Config
#| Byte # | Description |
#| --- | ----------- |
#| 0 | Hex 0x11 (Decimal 17) -- not sure what this means |
#| 1 | # of zones |
#| 2 | # of constant zones (0-3) |
#| 3 | constant zone 1 |
#| 4 | constant zone 2 |
#| 5 | constant zone 3 |
#| 6 | filter clean status (00 or 01) |
msg = msg + " " + self.build_register_update_message(
register.acuid,
register.register,
[
17,
self.db["aircons"][register.id]["info"]["noOfZones"],
self.db["aircons"][register.id]["info"]["noOfConstants"],
self.db["aircons"][register.id]["info"]["constant1"],
self.db["aircons"][register.id]["info"]["constant2"],
self.db["aircons"][register.id]["info"]["constant3"],
self.db["aircons"][register.id]["info"]["filterCleanStatus"]
]
)
elif (register.register == Registers.INDIVIDUAL_ZONE_CONFIG):
# `04` - CB JZ13 - Zone Config
#| Byte # | Description |
#| --- | ----------- |
#| 0 | Zone # (`01`-`0a`) |
#| 1 | Min Damper |
#| 2 | Max Damper |
#| 3 | Motion Status (0-22) |
#| 4 | Motion Config (0, 1, 2) |
#| 5 | Motion Zone Error |
#| 6 | CB RSSI |
msg = msg + " " + self.build_register_update_message(
register.acuid,
register.register,
[
register.zone_number,
self.db["aircons"][register.id]["zones"][register.zone_id]["minDamper"],
self.db["aircons"][register.id]["zones"][register.zone_id]["maxDamper"],
0,
self.db["aircons"][register.id]["zones"][register.zone_id]["motion"],
0,
0
]
)
elif (register.register == Registers.INDIVIDUAL_ZONE_STATE):
## `03` - CB JZ11 - Zone State
#| Byte # |Description |
#| --- | ----------- |
#| 0 | Zone # (01-0a) |
#| 1 | Bit 7: 1=Zone Open, 0=Zone Closed<br>Bits 6-0: Zone Percent 0-100 |
#| 2 | Sensor Type<br>0=No Sensor, 1=RF, 2=Wired, 3=RF2CAN Booster, 4=RF_X|
#| 3 | Hex Set Temp * 2.0 (0 - 80 ==> 0-40 degrees C)|
#| 4 | Measured Temp Int Portion |
#| 5 | Measured Temp Decimal Portion (0-9) |
#| 6 | Hex 00 / Ignored |
msg = msg + " " + self.build_register_update_message(
register.acuid,
register.register,
[
register.zone_number,
0 if self.db["aircons"][register.id]["zones"][register.zone_id]["state"] == "close" else 128,
self.db["aircons"][register.id]["zones"][register.zone_id]["type"],
int(self.db["aircons"][register.id]["zones"][register.zone_id]["setTemp"] * 2.0),
0,
0,
0
]
)
elif (register.register == Registers.SENSOR_PAIRING):
# `12` - Tablet to CB: CB JZ32 - Attach sensor to zone
#
#| Byte # | Description |
#| --- | ----------- |
#| 0-2 | Sensor UID |
#| 3 | Zone # |
#| 4-6 | Hex 000000 |
sensor_id = self.db["aircons"][register.id]["zones"][register.zone_id]["SensorUid"]
msg = msg + " " + self.build_register_update_message(
register.acuid,
register.register,
[
int(sensor_id[0:2],16),
int(sensor_id[2:4],16),
int(sensor_id[4:6],16),
register.zone_number,
0,
0,
0
]
)
return msg
else:
return None
finally:
lock.release()
def find_ac_by_uid(self, cb_id):
ac = None
for key in self.db["aircons"]:
if (self.db["aircons"][key]["info"]["uid"] == cb_id):
ac = self.db["aircons"][key]
return ac
def find_zone(self, ac, zone_num):
found_zone = False
for key in ac["zones"]:
if (ac["zones"][key]["number"] == zone_num):
found_zone = True
return ac["zones"][key]
return None
# *********************************************************************
# Global Application State
# *********************************************************************
logging.info("Creating store")
store = Store()
logging.info("Reading store state from DB file")
store.initialise("./db/aircons.db.json")
# *********************************************************************
# CAN Layer: Handle serial comms with the Control Box
# *********************************************************************
class CanLayer:
def __init__(self, serialPath):
self.serialPath = serialPath
self.inactivity_timer = None
self.reset_timer()
self.reset()
self.ser = serial.Serial(serialPath, 57600, timeout=0)
self.parser = CanXmlParser()
self.can_connection_state = "DISCONNECTED"
def reset_timer(self):
if self.inactivity_timer:
self.inactivity_timer.cancel()
self.inactivity_timer = None
self.inactivity_timer = threading.Timer(CB_INACTIVITY_TIMEOUT_SECONDS, self.__inactive_connection_detected)
def __inactive_connection_detected(self):
logger.info("Inactive connected detected")
self.can_connection_state = "INACTIVE"
def reset(self):
self.outbound_message_queue = []
def shutdown(self):
self.ser.close()
self.reset()
def event_loop_tick(self):
incoming_serial_data = self.ser.read(1024)
incoming_messages = self.parser.process_incoming_serial_data(incoming_serial_data)
for incoming_message in incoming_messages:
self.process_message(incoming_message)
# Iterate through dirty registers (ie. things that have been updated by clients of the API), and queue
# the updates for synchronisation back to the CB.
def __enqueue_outbound_register_updates(self):
global store
message = store.get_dirty_register_msg()
if (message):
if (message != "setCAN"):
logger.info("Sending update message to CB: "+message)
self.outbound_message_queue.append(message)
# Process inbound message from the CB
def process_message(self, msg):
global store
self.inactivity_timer.cancel()
self.inactivity_timer = None
first_word = msg.split(" ")[0]
if (msg == "Ping"):
if (self.can_connection_state == "INACTIVE"):
logger.info("Connection re-stablished, requesting system data refresh")
self.ser.write(self.parser.encode_packet("getSystemData"))
self.can_connection_state = "WAIT_SYSTEMDATA";
elif (self.can_connection_state == "DISCONNECTED"):
logger.info("Connection Established, requesting system data")
self.ser.write(self.parser.encode_packet("getSystemData"))
self.can_connection_state = "WAIT_SYSTEMDATA";
elif (self.can_connection_state == "CONNECTED"):
if (len(self.outbound_message_queue) == 0):
self.__enqueue_outbound_register_updates()
if (len(self.outbound_message_queue) != 0):
next_msg = self.outbound_message_queue.pop(0)
self.ser.write(self.parser.encode_packet(next_msg))
elif (msg == "CAN2 in use"):
logger.debug("Received CAN2 protocol confirmation from CB. Sending flush register request. ");
# make sure first message we send is 'setCAN 0701000000600000000000000', which tells the
# CB to follow up with a full register flush.
self.outbound_message_queue.append("setCAN 0701000000600000000000000")
# don't care about lights for now
# self.outbound_message_queue.append("setCAN 0201000000000360000000000")
# self.outbound_message_queue.append("setCAN 0201000000236000000000000")
# force full flush of all DB data to the DB by marking all registers as dirty (ie. as if a user had just updated them from the API)
store.dirty_all_registers()
self.can_connection_state = "CONNECTED"
elif (first_word == "getCAN"):
# convert getCAN message into set of register updates, and forward to store
if (len(msg) > 10):
logger.debug("getCAN message meceived: "+msg)
msg_regex = re.compile('^getCAN ([0-9]+) ([0-9A-Fa-f ]+)$')
regex_match_result = msg_regex.match(msg)
if (regex_match_result):
whole = regex_match_result.group(0)
id = regex_match_result.group(1)
payload = regex_match_result.group(2)
# split getCAN message payload and extract individual register updates
register_updates = payload.split(" ")
# process each register separately; create a RegisterUpdate object and
# dispatch it to the store.
for register_update in register_updates:
if (len(register_update) == 25):
reg = RegisterUpdateFromCB(
int(register_update[0:2], 16),
int(register_update[2:4], 16),
register_update[4:9],
int(register_update[9:11],16),
[
int(register_update[11:13],16),
int(register_update[13:15],16),
int(register_update[15:17],16),
int(register_update[17:19],16),
int(register_update[19:21],16),
int(register_update[21:23],16),
int(register_update[23:25],16)
]
)
store.process_register_update_from_cb(reg)
elif (register_update != ''):
logger.warning("Received getCAN register update that was not of expected length: "+register_update+" \nfull packet: "+msg);
# save updates to store on disk
store.save()
# make sure that the next message sent from the (*cough*) "tablet" (*cough*) to CB is an ACK for the setCAN
# by injecting ackCAN response at the head of the outbound message queue.
self.outbound_message_queue.insert(0,"ackCAN 1") # ack messages go to the front of the queue
else:
logger.error("Unknown/unhandled message received: "+msg)
self.reset_timer()
# *********************************************************************
# Flask API endpoints
# *********************************************************************
app = Flask(__name__)
# Ugh, this is horrible, updating state using a GET request.
@app.route("/setAircon", methods=['GET'])
def update_aircon_settings():
update = json.loads(request.args.get("json"))
logger.info("Processing request to update aircon state with values "+str(update))
global store
data = store.db
result = store.process_json_update(update)
success = (result == "Success")
error_description = result
return jsonify(
ack=success,
reason=error_description
), 200 if success else 400, {'Content-Type': 'application/json; charset=utf-8'}
@app.route("/getSystemData", methods=['GET'])
def get_system_data():
logger.info("Processing request to get system data / current state")
global store
data = store.db
return data, 200, {'Content-Type': 'application/json; charset=utf-8'}
def graceful_shutdown():
global store
store.save()
running = False
atexit.register(graceful_shutdown)
def main_loop(serial_device):
can = CanLayer(serial_device)
while running:
can.event_loop_tick()
time.sleep(0.01)
parser = argparse.ArgumentParser(description='Monitor communications between AA Tablet and Control Box')
parser.add_argument('--device', type=str, nargs=1, required=True,
help='serial device to use when using serial interface (eg. /dev/cu.usbserial)')
args = parser.parse_args()
if __name__ == '__main__':
flask_thread = threading.Thread(target=lambda: app.run(host='0.0.0.0', port=2025, debug=True, use_reloader=False)).start()
main_loop(args.device[0])
flask_thread.join()
print("API has been shut down gracefully.")