-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluedot.py
1526 lines (1274 loc) · 53.6 KB
/
bluedot.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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Copyright © 2021-2022 Jeff Kletsky. All Rights Reserved.
"""
import asyncio
import enum
import json
import inspect
import logging
import os
import queue
import time
import traceback
from asyncio import Task
from datetime import datetime
from operator import mul
from socket import gethostname
from struct import unpack, unpack_from, pack
from typing import Optional, Union, NamedTuple, Coroutine, Awaitable, Callable
import requests
from bleak import BleakError, BleakClient, BleakScanner
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
import paho.mqtt.client as mqtt
from bleak.exc import BleakDBusError
from paho.mqtt.client import MQTTMessage, MQTTv5, MQTT_CLEAN_START_FIRST_ONLY
import pyDE1 # for pyDE1.getLogger()
import pyDE1.shutdown_manager as sm
import pyDE1.pyde1_logging as pyde1_logging
from pyDE1.de1.c_api import API_MachineStates, API_Substates
from pyDE1.config import ConfigYAML, ConfigLoadable
from pyDE1.dispatcher.resource import DE1ModeEnum, Resource
from pyDE1.event_manager import SequencerGateName # Move out
class Config(ConfigYAML):
DEFAULT_CONFIG_FILE = '/usr/local/etc/pyde1/bluedot.conf'
def __init__(self):
super(Config, self).__init__()
self.mqtt = _MQTT()
self.http = _HTTP()
self.steam = _Steam()
self.thermometer = _Thermometer()
self.logging = _Logging()
class _MQTT(ConfigLoadable):
def __init__(self):
self.TOPIC_ROOT = 'pyDE1'
self.CLIENT_ID_PREFIX = 'pyde1-bluedot'
self.BROKER_HOSTNAME = '::'
self.BROKER_PORT = 1883
self.TRANSPORT = 'tcp'
self.KEEPALIVE = 60
self.USERNAME = None
self.PASSWORD = None
self.DEBUG = False
self.TLS = False # Set True, or rest of TLS is ignored
# See paho Client.tls_set() for details
self.TLS_CA_CERTS = None
self.TLS_CERTFILE = None
self.TLS_KEYFILE = None
self.TLS_CERT_REQS = None
self.TLS_VERSION = None
self.TLS_CIPHERS = None
class _HTTP(ConfigLoadable):
def __init__(self):
# No trailing slash
self.PYDE1_BASE_URL = 'http://localhost:1234'
class _Steam(ConfigLoadable):
def __init__(self):
self.STOP_LAG = 1.0 # 0.530 is from API call on localhost
self.SKIP_INITIAL_SECONDS = 4.0
self.MAX_SAMPLES_FOR_ESTIMATE = 5
class _Thermometer(ConfigLoadable):
def __init__(self):
self.BT_ADDRESS = '00:a0:50:aa:bb:cc'
class _Logging (pyde1_logging.ConfigLogging):
def __init__(self):
super(_Logging, self).__init__()
# NB: The log file name is matched against [a-zA-Z0-9._-]
self.LOG_FILENAME = 'bluedot.log'
self.LOGGERS = {
'Lock': 'WARNING',
'MQTTClient': 'INFO',
'root.asyncio': 'INFO',
'root.bleak': 'INFO',
}
self.formatters.STYLE = '%'
self.formatters.LOGFILE = \
'%(asctime)s %(levelname)s %(name)s: %(message)s'
self.formatters.STDERR = \
'%(levelname)s %(name)s: %(message)s'
config = Config()
#
# End of Config
#
# TODO: Make this a property of SequencerGateName
GATE_NAMES_AFTER_FLOW = (
SequencerGateName.GATE_FLOW_END.value,
SequencerGateName.GATE_FLOW_STATE_EXIT.value,
SequencerGateName.GATE_LAST_DROPS.value,
SequencerGateName.GATE_SEQUENCE_COMPLETE.value,
)
#
# The MQTT and Bluetooth listeners will queue their packets
# for later processing. While this doesn't get around the GIL,
# it does provide for more asynchronous processing.
#
# "Temporarily" repeated and modified here (again)
async def async_queue_get(from_queue: queue.Queue):
"""
Awaitable queue watcher
Returns a queue entry or None when exited
"""
loop = asyncio.get_running_loop()
done = False
data = None # For exit on shutdown
while not done and not sm.shutdown_underway.is_set():
try:
data = await loop.run_in_executor(
None,
from_queue.get, True, 1.0)
# blocking, timeout
done = True
except queue.Empty:
pass
if sm.shutdown_underway.is_set():
logger.info("Shut down async_queue_get")
return data
class LockLogger:
def __init__(self, lock: asyncio.Lock, name: str):
self._lock = lock
self._name = name
self._logger = pyDE1.getLogger(f'Lock.{self._name}')
self._checked = None
self._acquired = None
def check(self):
if self._lock.locked():
self._checked = time.time()
self._logger.warning(
f"Waiting for lock {self.call_str()}")
else:
self._checked = None
return self # Allows lock_logger = LockLogger(...).check()
def acquired(self, full_trace=False):
self._acquired = time.time()
if self._checked:
dt = (self._acquired - self._checked) * 1000
# Warning here allows setting log level to warning
# and still seeing how long the waits were
self._logger.warning(
f"Acquired lock after {dt:.0f} ms {self.call_str(full_trace)}")
else:
self._logger.info(
f"Acquired lock {self.call_str()}")
def released(self, full_trace=False):
dt = (time.time() - self._acquired) * 1000
if self._lock.locked():
self._logger.error(
f"NOT RELEASED after {dt:.0f} ms {self.call_str()}")
else:
self._logger.info(
f"Released lock after {dt:.0f} ms {self.call_str(full_trace)}")
@staticmethod
def call_str(full_trace=True):
stack = inspect.stack()[2]
retval = f"at {stack.function}:{stack.lineno}"
if full_trace:
idx = 3
while True:
try:
stack = inspect.stack()[idx]
next_caller = f"{stack.function}:{stack.lineno}"
if stack.function.startswith('_run'):
break
retval = f"{retval} < {next_caller}"
except IndexError:
break
idx += 1
return retval
# The queue will either have a BDNotification or an MQTTPacket
# BDUnit is specific to the thermometer protocol and is declared later
class BDNotification (NamedTuple):
arrival_time: float
raw_data: bytearray
temperature: float
high_alarm: float
units: "BDUnit"
alarm_byte: Union[bytearray, int]
class MQTTPacket (NamedTuple):
arrival_time: float
message: MQTTMessage
class ReconnectingBTDevice:
"""
Mix-in to manage connection, disconnection, and reconnection
"""
def __init__(self, address_or_ble_device: Union[BLEDevice, str]):
self.should_auto_reconnect = True
self._client = BleakClient(address_or_ble_device)
self._connection_lock = asyncio.Lock()
self._connection_lock_last_checked: Optional[float] = None
self._connection_task: Optional[asyncio.Task] = None
self._connection_event = asyncio.Event()
self._ready_event = asyncio.Event()
self._willful_disconnect = False
self._reconnect_count = 0 # internal state
self._reconnect_log_limit = 10 # count after which use gap_long w/o log
self._connect_timeout = 10 # seconds, default is 10
self._recheck_gap = 0 # seconds, "0" gives 10-second scans with no gap
self._recheck_gap_long = 20 # seconds (every 30 sec for 10 seconds)
self._logger = pyDE1.getLogger(self.__class__.__name__)
self._client.set_disconnected_callback(
self._create_bluetooth_disconnected_callback())
# _connection_lock handles the primary use case of wait_for_connected()
# running in "slow mode" and wanting to "try now", such as a steam
# sequence starting.
#
# It should be acquired internally in the process of:
# * Creating a wait_for_connected() task
# * Terminating an existing wait_for_connected() task
# * Explicit connection attempt and subsequent initialization
# * Explicit disconnection
#
# Assuming that this is obeyed, an in-process connection/initialization
# would not be able to be cancelled by a new wait_for_connected()
# call cancelling the previous task
@property
def address(self):
return self._client.address
@property
def logstr(self):
return "{} at {}".format(
self.__class__.__name__,
self._client.address
)
@property
def is_connected(self):
return self._client.is_connected
async def wait_for_connected(self):
return await self._connection_event.wait()
def _mark_connected(self):
# Note that "ready" needs to be handled by the subclass
self._willful_disconnect = False
self._connection_event.set()
def _mark_disconnected(self):
self._ready_event.clear()
self._connection_event.clear()
@property
def is_ready(self) -> Optional[bool]:
"""
NB: Subclass needs to manage setting and clearing _ready_event()
"""
return self._ready_event.is_set()
async def wait_for_ready(self) -> bool:
return await self._ready_event.wait()
async def connect_with_retry(self):
"""
Start or restart a task to connect when the device becomes available
Always returns "immediately", can use wait_for_connected()
or poll is_connected (or "ready" if implemented by subclass)
"""
ll = LockLogger(self._connection_lock, 'connection_lock').check()
async with self._connection_lock:
ll.acquired()
await self._connect_with_retry_have_lock()
ll.released()
async def connect_with_retry_abandon(self):
"""
Cancel any pending connect_with_retry() task
should_auto_reconnect is unmodified,
but nothing should remain that is trying to reconnect
"""
ll = LockLogger(self._connection_lock, 'connection_lock').check()
async with self._connection_lock:
ll.acquired()
await self._connect_with_retry_abandon_have_lock()
ll.released()
async def connect(self, log_attempts=False) -> bool:
"""
Make a single connection attempt. See also connect_with_retry()
Raises TimeoutError or BleakError if unable to connect.
If the device isn't available yet, <class 'bleak.exc.BleakError'>:
BleakError('Device with address 00:a0:50:aa:bb:cc was not found.')
or possibly 'Device with address {0} could not be found. ...'
Also can return "Connection was not successful! (...)"
Returns connected state as Boolean
"""
if log_attempts:
self._logger.info(
f"Connect requested for {self.logstr}")
ll = LockLogger(self._connection_lock, 'connection_lock').check()
async with self._connection_lock:
ll.acquired()
# MessageBus._message_reader() seems to take up to 150 ms
# especially when connecting.
old_duration = loop.slow_callback_duration
new_duration = 0.150
if log_attempts:
self._logger.debug(
"Changing slow_callback_duration from "
f"{old_duration:.3f} to {new_duration:.3f}")
loop.slow_callback_duration = new_duration
if not self._client.is_connected:
try:
await self._client.connect(timeout=self._connect_timeout)
finally:
if log_attempts:
self._logger.debug(
"Restoring slow_callback_duration to "
f"{old_duration:.3f}")
loop.slow_callback_duration = old_duration
if self._client.is_connected:
self._mark_connected()
self._logger.info(
f"Connected {self.logstr}, calling _after_connect()")
await self._after_connect()
self._logger.info(
f"Initialized {self.logstr}")
elif log_attempts:
self._logger.warning(
f"Failed to connect {self.logstr}")
ll.released()
return self._client.is_connected
async def disconnect(self):
# Lock helps prevent race condition where connect is in progress
# and another connect() or disconnect() is called
self._logger.info(
f"Disconnect requested for {self.logstr}")
ll = LockLogger(self._connection_lock, 'connection_lock').check()
async with self._connection_lock:
ll.acquired()
await self._connect_with_retry_abandon_have_lock()
if self._client.is_connected:
await self._before_disconnect()
self._willful_disconnect = True
await self._client.disconnect()
if self._client.is_connected:
self._logger.warning(
f"Failed to disconnect {self.logstr}")
else:
self._mark_disconnected()
self._logger.info(
f"Disconnected {self.logstr}")
ll.released()
return self._client.is_connected
async def _after_connect(self):
pass
async def _before_disconnect(self):
pass
# Internals:
async def _connect_with_retry_abandon_have_lock(self):
if self._connection_task is None:
return
if not self._connection_task.done():
self._logger.info(
f"Terminating {self._connection_task.get_name()}")
self._connection_task.cancel()
# Give it 50 ms to clean up
delay_ms = 50
try:
await asyncio.wait_for(self._connection_task, delay_ms/1000)
except asyncio.TimeoutError:
self._logger.error(
f"Not done after {delay_ms} ms, "
f"removing reference anyways: {self._connection_task}")
self._connection_task = None
async def _connect_with_retry_have_lock(self):
# Stop any already running connection task
if self._connection_task is None:
self._logger.info("Starting connect_with_retry()")
else:
self._logger.info("Retarting connect_with_retry()")
await self._connect_with_retry_abandon_have_lock()
# Create the new task
self._connection_task = asyncio.create_task(
self._connect_with_retry_inner(),
name = 'ConnectWithRetry')
self._connection_task.add_done_callback(
self._create_connection_task_done_cb())
def _create_connection_task_done_cb(self) -> Callable:
device = self
def connection_task_done_cb(task: Task):
if task.cancelled():
self._logger.info(
f"Connection task cancelled")
elif (e := task.exception()):
tbe = ''.join(
traceback.TracebackException.from_exception(e).format())
self._logger.error(
f"Connection task raised exception: {tbe}")
else:
self._logger.info(
f"Connection task completed, returned '{task.result()}'")
try:
# NB: All bets are off is exception was raised
# Potentially should not shift, but retry
# asyncio.run_coroutine_threadsafe(device._cr_shift(), loop)
asyncio.create_task(
self._cr_process_callback(ConnectivityRequest.CAPTURE))
except AttributeError:
pass
if task == device._connection_task:
device._connection_task = None
return connection_task_done_cb
# TODO: Why does this timeout so quickly on a reconnect?
# (Seems related to --experimental, see client.py line 175)
# Opened as https://github.com/hbldh/bleak/issues/713
#
# 2021-12-25 19:04:49,451 INFO BlueDOT:
# Connect requested for BlueDOT at 00:a0:50:e2:f6:49
# 2021-12-25 19:04:49,630 INFO BlueDOT:
# Device with address 00:a0:50:e2:f6:49 could not be found.
# Try increasing `timeout` value or moving the device closer.
# 2021-12-25 19:04:49,632 INFO BlueDOT:
# Connect requested for BlueDOT at 00:a0:50:e2:f6:49
# 2021-12-25 19:04:49,787 INFO BlueDOT:
# Device with address 00:a0:50:e2:f6:49 could not be found.
# Try increasing `timeout` value or moving the device closer.
async def _connect_with_retry_inner(self) -> bool:
"""
This is the task that gets run by _connect_with_retry_have_lock
"""
self._reconnect_count = 0
while not self._client.is_connected and self.should_auto_reconnect \
and not sm.shutdown_underway.is_set():
self._reconnect_count += 1
log_attempts = self._reconnect_count <= self._reconnect_log_limit
try:
# The lock should prevent this connect/initialize call
# from being inadvertently cancelled with a public method
await self.connect(log_attempts=log_attempts)
except (asyncio.TimeoutError, TimeoutError, BleakError) as e:
if isinstance(e, BleakError):
# Something of a hack, but need to distinguish
# from other BleakError conditions
# See https://github.com/hbldh/bleak/issues/527
msg: str = e.args[0]
if msg.startswith(('Device with address',
'Connection was not successful!')):
if log_attempts:
self._logger.info(e)
if msg.endswith('device closer.'):
# Remove _device_path to force scan on connect
# See https://github.com/hbldh/bleak/issues/713
self._logger.debug(
"Issue 527: path: '{}' info: '{}'".format(
self._client._device_path,
self._client._device_info,
))
self._client._device_path = None
self._client._device_info = None
else:
raise e
if self.is_connected:
break
else:
if self._reconnect_count == self._reconnect_log_limit:
self._logger.info(
"No further logging of connection attempts to "
f"{self.logstr}, continuing at "
f"{self._recheck_gap_long + self._connect_timeout} "
"sec intervals")
if self._reconnect_count < self._reconnect_log_limit:
await asyncio.sleep(self._recheck_gap)
else:
await asyncio.sleep(self._recheck_gap_long)
if not self.should_auto_reconnect and not self.is_connected:
self._logger.info(f"Reconnection disabled for {self.logstr}")
return self._client.is_connected
def _create_bluetooth_disconnected_callback(self):
device = self
def bt_disconnected_cb(client: BleakClient):
nonlocal device
# Marking here is not protected by the lock
# There's some risk of a race condition,
# marking as disconnected seems less disastrous
# than ending up with deadlock during disconnect
# or failing to reflect the change promptly.
device._mark_disconnected()
# Remove _device_path to force scan on connect
# See https://github.com/hbldh/bleak/issues/713
device._client._device_path = None
device._client._device_info = None
if not device._willful_disconnect:
device._logger.warning(
f"Unexpected disconnection from {device.logstr}")
asyncio.create_task(device._cr_process_callback(
ConnectivityRequest.RELEASE))
return bt_disconnected_cb
async def _cr_process_callback(self, cb_from: "ConnectivityRequest"):
raise NotImplementedError
class ConnectivityRequest (enum.Enum):
CAPTURE = 'capture'
RELEASE = 'release'
# class ConnectivityState (enum.IntEnum):
# DISCONNECTED = 0
# CONNECTING = 1
# WAITING = 2
# CONNECTED = 3
# INITIALIZING = 4
# READY = 5
# DISCONNECTING = 6
class STCDevice (ReconnectingBTDevice):
"""
Defines what the SteamTempController needs to manage
"""
def __init__(self, address_or_ble_device: Union[BLEDevice, str]):
super(STCDevice, self).__init__(
address_or_ble_device=address_or_ble_device)
# These end up getting run from the MQTT thread
# use asyncio here, and use either
# loop.call_soon_threadsafe(callback, *args)
# asyncio.run_coroutine_threadsafe(coro_func(), loop)
self._cr_lock = asyncio.Lock()
self._cr_queue = asyncio.Queue()
self._cr_now = ConnectivityRequest.RELEASE
self._cr_next: Optional[ConnectivityRequest] = None
self._cr_then: Optional[ConnectivityRequest] = None
self._cr_task = asyncio.create_task(self._cr_queue_watcher(),
name='CRQueueWatcher')
def capture(self):
# This gets called from the MQTT thread
loop.call_soon_threadsafe(
self._cr_queue.put_nowait,
ConnectivityRequest.CAPTURE)
def release(self):
# This gets called from the MQTT thread
loop.call_soon_threadsafe(
self._cr_queue.put_nowait,
ConnectivityRequest.RELEASE)
async def at_steaming_start(self):
"""
Gets called when entering a steam sequence.
At completion, device should be providing updates to the queue
appropriate for rate-of-rise estimation
"""
raise NotImplementedError
async def at_steaming_end(self):
"""
Gets called after leaving a steam sequence.
At completion, device may continue to provide updates, but not required
"""
raise NotImplementedError
async def _cr_queue_watcher(self):
"""
Runs as a task and manages incoming ConnectionRequest from queue
"""
logger = pyDE1.getLogger('CRQueue')
logger.info('Starting CRQueueWatcher')
while not sm.shutdown_underway.is_set():
request = await self._cr_queue.get()
logger.debug(f"CRQueueWatcher got {request}")
ll = LockLogger(self._cr_lock, 'CaptureRelease').check()
async with self._cr_lock:
ll.acquired()
self._cr_add_request_have_lock(request=request)
ll.released()
async def _start_capture(self):
self._logger.info("_start_capture() called")
self.should_auto_reconnect = True
if not self.is_connected:
# Start looking
await self.connect_with_retry()
# connection_task_done_cb does self._clock_down
elif not self._ready_event.is_set():
self._logger.info("_start_capture() is calling _after_connect()")
await self._after_connect()
async def _start_release(self):
self._logger.info("_start_release() called")
self.should_auto_reconnect = False
if self.is_connected:
await self.disconnect()
# ----- New implementation -----
async def _cr_process_callback(self,cb_from: ConnectivityRequest):
logger = pyDE1.getLogger('CRManage')
logger.debug(
f"Process {cb_from.name} callback entry {self._cr_status_string()}")
ll = LockLogger(self._cr_lock, 'CaptureRelease').check()
async with self._cr_lock:
ll.acquired()
if cb_from == ConnectivityRequest.CAPTURE:
assert self._cr_next == ConnectivityRequest.CAPTURE, \
f"CAPTURE callback from {self._cr_status_string()}"
if not self.is_connected:
logger.warning(
f"{self.logstr} is disconnected after CAPTURE")
# Make state consistent
self._cr_next = ConnectivityRequest.RELEASE
self._cr_shift_have_lock()
elif cb_from == ConnectivityRequest.RELEASE \
and self._willful_disconnect:
assert self._cr_next == ConnectivityRequest.RELEASE, \
f"RELEASE callback from {self._cr_status_string()}"
if self.is_connected:
logger.warning(
f"{self.logstr} is connected after RELEASE")
# Make state consistent
self._cr_next = ConnectivityRequest.CAPTURE
self._cr_shift_have_lock()
# Written for clarity, effectively an "else" clause
elif cb_from == ConnectivityRequest.RELEASE \
and not self._willful_disconnect:
# unexpected disconnect
assert not self.is_connected, \
"Unexpected disconnect while connected {}".format(
self._cr_status_string())
if self._cr_now != ConnectivityRequest.CAPTURE:
logger.error(
"Logic puzzle, unexpected disconnect while {}".format(
self._cr_status_string()
)
)
logger.debug(
"Changing 'now' to RELEASE from unexpected disconnect")
# Don't shift, as next is still pending
self._cr_now = ConnectivityRequest.RELEASE
# With the lock, we're "next", so no need to queue
if self.should_auto_reconnect \
and not sm.shutdown_underway.is_set():
logger.info("Requesting reconnection by adding CAPTURE")
self._cr_add_request_have_lock(ConnectivityRequest.CAPTURE)
else:
logger.error(
"Logic error fallthrough for "
f"{cb_from}, willful: {self._willful_disconnect}")
logger.debug(
f"Process {cb_from.name} callback exit {self._cr_status_string()}")
def _cr_shift_have_lock(self):
logger = pyDE1.getLogger('CRManage')
self._cr_now = self._cr_next
self._cr_next = self._cr_then
self._cr_then = None
logger.debug(
f"Shifted to {self._cr_status_string()}")
# As making state consistent might have caused a duplicate
if self._cr_next == self._cr_now:
self._cr_now = self._cr_next
self._cr_next = self._cr_then
self._cr_then = None
logger.debug(
f"Reduced to {self._cr_status_string()}")
if self._cr_next:
self._cr_start_action_sync()
def _cr_start_action_sync(self):
if self._cr_next == ConnectivityRequest.CAPTURE:
asyncio.create_task(self._start_capture())
elif self._cr_next == ConnectivityRequest.RELEASE:
asyncio.create_task(self._start_release())
def _cr_status_string(self):
now = 'None'
next = 'None'
then = 'None'
if self._cr_now:
now = self._cr_now.name
if self._cr_next is not None:
next = self._cr_next.name
if self._cr_then is not None:
then = self._cr_then.name
return '{}, {}, {}'.format(now, next, then)
def _cr_add_request_have_lock(self, request: ConnectivityRequest):
logger = pyDE1.getLogger('CRManage')
logger.debug(f"Adding {request.name} to {self._cr_status_string()}")
# What is the final state before adding?
terminal = self._cr_now
if self._cr_next:
terminal = self._cr_next
if self._cr_then:
terminal = self._cr_then
if request == terminal:
start_action = False
elif self._cr_next is None:
logger.info(f"Setting next to {request.name}")
self._cr_next = request
self._cr_then = None
start_action = True
elif self._cr_next == terminal:
logger.info(f"Setting then to {request.name}")
self._cr_then = request
start_action = False
if self._cr_next == ConnectivityRequest.CAPTURE \
and request == ConnectivityRequest.RELEASE:
logger.info("Aborting CAPTURE in favor of RELEASE")
asyncio.create_task(self.connect_with_retry_abandon())
elif self._cr_next == request:
logger.info(
f"Next is already {request.name}, clearing then")
self._cr_then = None
start_action = False
else:
logger.error(
f"Logic error adding {request} to {self._cr_status_string()}")
start_action = False
logger.debug(
f"Updated to {self._cr_status_string()}, start: {start_action}")
if start_action:
self._cr_start_action_sync()
# Sigh, the messiness of nested classes
class BDUnit(enum.IntFlag):
C = 0
F = 1
@property
def freezing(self):
if self == BDUnit.C:
return 0
else:
return 32
class BlueDOT (STCDevice):
def __init__(self, address_or_ble_device: Union[BLEDevice, str],
event_queue: queue.Queue):
super(BlueDOT, self).__init__(
address_or_ble_device=address_or_ble_device)
self._event_queue = event_queue
self._last_time = time.time()
# CUUID locks
self._interval_lock = asyncio.Lock()
self._updates_lock = asyncio.Lock()
self._high_alarm_lock = asyncio.Lock()
self._units_lock = asyncio.Lock()
# NB: This is not threadsafe, pick up from queue
self._first_update: Optional[BDNotification] = None
self._have_high_alarm = asyncio.Event()
def _create_notification_callback(self) -> Callable:
bluedot = self
def bluedot_notification_cb(sender: int, data: bytearray):
nonlocal bluedot
now = time.time()
dt = now - bluedot._last_time
bluedot._last_time = now
byte_0 = data[0:1].hex()
(current, high_alarm) = unpack_from('<ii', data, offset=1)
(units_byte,) = unpack_from('B', data, offset=11)
byte_12 = data[12:13].hex()
bluedot_mac = data[13:19]
alarm_byte = data[19:20]
ustr = BDUnit(units_byte).name
self._logger.debug(
f"{dt:0.3f} {current} {high_alarm} {ustr} "
f"{byte_0} {byte_12} {alarm_byte.hex()}")
bluedot._event_queue.put_nowait(
BDNotification(
arrival_time=now,
raw_data=data,
temperature=current,
high_alarm=high_alarm,
units=BDUnit(units_byte),
alarm_byte=alarm_byte,
)
)
return bluedot_notification_cb
# _service_uuid = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
_interval_cuuid = '60721D99-6698-4EEC-8E0A-50D0C37F17B9'
_updates_cuuid = '783F2991-23E0-4BDC-AC16-78601BD84B39'
_high_alarm_cuuid = 'DE0415CF-D54A-4EA4-A58F-C7AA07F79BAA'
_low_alarm_cuuid = 'BFDBEB45-11A3-4406-BCB4-ED7C6F939FBC'
_units_cuuid = 'C86CF012-5C33-48AA-82D1-84A57C908CA0'
async def set_updates_on(self, state=True):
if not self._client.is_connected:
self._logger.warning(
f"Not connected, can't set_updates_on({state})")
return
ll = LockLogger(self._updates_lock, 'updates_lock').check()
async with self._updates_lock:
ll.acquired()
if state:
await self._client.start_notify(
self._updates_cuuid,
self._create_notification_callback())
else:
await self._client.stop_notify(
self._updates_cuuid)
self._logger.info(f"Notification enabled: {state}")
ll.released()
# TODO: Should catch not-connected exceptions in
# set_update_rate and set_high_alarm
# (as well as any new set_*)
#
# To some extent, not a big problem if run as a service
# as long as the app exits and gets restarted.
# That's about what would happen with a handler anyways.
async def set_update_rate(self, period: Union[int, float]):
# tb = "".join(traceback.format_stack(limit=5))
# self._logger.debug(tb)
if not self._client.is_connected:
self._logger.warning(
f"Not connected, can't set_update_rate({period})")
return
ll = LockLogger(self._interval_lock, 'interval_lock').check()
async with self._interval_lock:
ll.acquired()
val = int(round(period))
await self._client.write_gatt_char(
self._interval_cuuid, pack('B', val))
self._logger.info(f"Notification period set to {val}")
ll.released()
async def set_high_alarm(self, temperature: Union[int, float]):
if not self._client.is_connected:
self._logger.warning(
f"Not connected, can't set_high_alarm({temperature})")
return
ll = LockLogger(self._high_alarm_lock, 'high_alarm_lock').check()
async with self._high_alarm_lock:
ll.acquired()
val = int(round(temperature))
await self._client.write_gatt_char(
self._high_alarm_cuuid, pack('B', val))
self._logger.info(f"High alarm set: {val}")
ll.released()
async def go_slow(self):
await self.set_update_rate(60)
async def go_fast(self):
await self.set_update_rate(1)
async def _after_connect(self):
# logger.debug(
# "after_connect() "
# f"is_connected: {self.is_connected}, "
# f"is_ready: {self.is_ready} "
# )
# for task in asyncio.all_tasks():
# logger.debug(task)
try:
self._have_high_alarm.clear()
self._first_update = None
await self.set_updates_on()
await asyncio.sleep(1) # Is this needed, and, if so, when?
await self.set_update_rate(1)
wait_for_first = 2.5 # sec
try:
await asyncio.wait_for(
self._have_high_alarm.wait(),
timeout=wait_for_first)
self._first_update: BDNotification
old_ha = self._first_update.high_alarm
new_ha = self._first_update.units.freezing
await self.set_high_alarm(new_ha)
await asyncio.sleep(1.0)
await self.set_high_alarm(old_ha)
except asyncio.TimeoutError:
self._logger.warning(
f"Did not get update within {wait_for_first:.1f} sec, "
"no audio alarm for connection.")
pass
await self.go_slow()
except BleakDBusError as e:
self._logger.exception("in after_connect()", exc_info=e)
self._ready_event.set()
async def _before_disconnect(self):
self._ready_event.clear()
await self.go_slow()
await self.set_updates_on(state=False)
async def at_steaming_start(self):
await self.go_fast()