Skip to content

Commit 7579f78

Browse files
committed
logging improvements
1 parent 112c226 commit 7579f78

3 files changed

Lines changed: 40 additions & 24 deletions

File tree

src/artisanlib/acaia.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,18 @@ def reset_parser(self) -> None:
487487

488488

489489
def parse_info(self, data:bytes) -> None:
490-
# _log.debug('INFO MSG: %s', data)
490+
if self._logging:
491+
_log.debug('INFO MSG: %s', data)
491492

492493
if len(data)>2:
493494
self.isp_version = data[2]
494-
# _log.debug('isp_version: %s', self.isp_version)
495+
if self._logging:
496+
_log.debug('isp_version: %s', self.isp_version)
495497

496498
if len(data)>5:
497499
self.firmware = (data[3],data[4],data[5]) # main/sub/add
498-
# _log.debug('firmware: %s.%s.%s', self.firmware[0], self.firmware[1], f'{self.firmware[2]:>03}')
500+
if self._logging:
501+
_log.debug('firmware: %s.%s.%s', self.firmware[0], self.firmware[1], f'{self.firmware[2]:>03}')
499502

500503
# data[2] ISP_VERSION
501504
# if len(data)>6:
@@ -537,7 +540,8 @@ def decode_weight(self, payload:bytes) -> tuple[float|None, bool]:
537540
return None, False
538541

539542
def update_weight(self, value:float|None, stable:bool|None = False) -> None:
540-
## _log.debug("PRINT update_weight(%s,%s)", value, stable)
543+
if self._logging:
544+
_log.debug('update_weight(%s,%s)', value, stable)
541545
if value is not None and (not self.stable_only or stable):
542546
if self.repeatability > 1:
543547
# round to full 10g
@@ -548,15 +552,11 @@ def update_weight(self, value:float|None, stable:bool|None = False) -> None:
548552
value_rounded:float = float2float(value, self.decimals)
549553
if stable and value_rounded != self.stable_weight:
550554
# if value is fresh and reading is stable
551-
## _log.debug("PRINT new stable weight: %s", value_rounded)
552555
self._weight_changed_handler(value_rounded, True)
553556
self.stable_weight = value_rounded
554557
elif not stable:
555-
## _log.debug("PRINT new non-stable weight: %s", value_rounded)
556558
self._weight_changed_handler(value_rounded, False)
557559
self.stable_weight = None # non-stable weights invalidate the last stable weight to ensure a sequence of equal stable weights is reported if interleaved with non-stable weights
558-
## else:
559-
## _log.debug("PRINT stable weight ignored")
560560

561561
# returns length of consumed data or -1 on error
562562
def parse_weight_event(self, payload:bytes) -> int:
@@ -591,7 +591,8 @@ def parse_timer_event(payload:bytes) -> int:
591591
def parse_ack_event(self, payload:bytes) -> int:
592592
if len(payload) < EVENT_LEN.ACK:
593593
return -1
594-
# _log.debug('ACK EVENT')
594+
if self._logging:
595+
_log.debug('ACK EVENT')
595596
consumed_extra = self.parse_extra_weight_time_data(payload[1:])
596597
return EVENT_LEN.ACK + consumed_extra
597598

@@ -702,7 +703,8 @@ def parse_scale_events(self, payload:bytes) -> None:
702703
##
703704

704705
def parse_status(self, payload:bytes) -> None:
705-
# _log.debug('STATUS')
706+
if self._logging:
707+
_log.debug('STATUS')
706708

707709
# byte 0: message len
708710

@@ -1171,8 +1173,8 @@ def notify_callback(self, _sender:'BleakGATTCharacteristic', data:bytearray) ->
11711173
asyncio.run_coroutine_threadsafe(
11721174
self._read_queue.put(bytes(data)),
11731175
self._async_loop_thread.loop)
1174-
if self._logging:
1175-
_log.debug('received: %s',data)
1176+
# if self._logging:
1177+
# _log.debug('received: %s',data)
11761178

11771179
def send_signal(self, action:STATE_ACTION) -> None:
11781180
self.protocol.send_signal(action)

src/artisanlib/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7643,7 +7643,7 @@ def curveSimilarity(self) -> tuple[float|None, float|None]: # pylint: disable=no
76437643
try:
76447644
# we masked the -1 error values
76457645
np_etb_masked = numpy.ma.masked_equal(np_etb, -1)
7646-
np_timeB_etb_masked = numpy.ma.masked_array(np_timeB, np_etb_masked.mask) # type:ignore[operator,unused-ignore] # pylint:disable=no-member
7646+
np_timeB_etb_masked = numpy.ma.masked_array(np_timeB, np_etb_masked.mask) # type:ignore[operator,unused-ignore] # ty:ignore[call-non-callable] # pylint:disable=no-member
76477647
# ignore the masked error values on computing the interpolation and fill (especially on the left) with -1 values
76487648
interp_np_etb = numpy.interp(np_timex,np_timeB_etb_masked.compressed(),np_etb_masked.compressed(),left=-1,right=-1) # pyright:ignore[reportUnknownArgumentType] # pylint:disable=no-member
76497649

@@ -7668,7 +7668,7 @@ def curveSimilarity(self) -> tuple[float|None, float|None]: # pylint: disable=no
76687668
try:
76697669
# we masked the -1 error values
76707670
np_btb_masked = numpy.ma.masked_equal(np_btb, -1)
7671-
np_timeB_btb_masked = numpy.ma.masked_array(np_timeB, np_btb_masked.mask) # type:ignore[operator,unused-ignore] # pylint:disable=no-member
7671+
np_timeB_btb_masked = numpy.ma.masked_array(np_timeB, np_btb_masked.mask) # type:ignore[operator,unused-ignore] # ty:ignore[call-non-callable] # pylint:disable=no-member
76727672
# ignore the masked error values on computing the interpolation and fill (especially on the left) with -1 values
76737673
interp_np_btb = numpy.interp(np_timex,np_timeB_btb_masked.compressed(),np_btb_masked.compressed(),left=-1,right=-1) # pyright:ignore[reportUnknownArgumentType] # pylint:disable=no-member
76747674

src/artisanlib/scale.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ class ScaleManager(QObject):
204204

205205
def __init__(self, connected_handler:Callable[[str, str], None], disconnected_handler:Callable[[str, str], None]) -> None:
206206
super().__init__()
207+
self._logging: bool = False
208+
207209
self.connected_handler = connected_handler
208210
self.disconnected_handler = disconnected_handler
209211
self.scale1: Scale|None = None
@@ -329,6 +331,7 @@ def scan_scale1_slot(self, model:int) -> None:
329331

330332
@pyqtSlot(bool)
331333
def connect_scale1_slot(self, device_logging:bool) -> None:
334+
self._logging = device_logging
332335
if self.scale1 is not None:
333336
self.scale1.connect_scale(device_logging)
334337

@@ -386,7 +389,8 @@ def scale1_disconnected_slot(self) -> None:
386389
# weight in g
387390
@pyqtSlot(float, bool)
388391
def scale1_weight_changed_slot(self, weight:float, stable:bool) -> None:
389-
## _log.debug("PRINT scale1_weight_changed_slot(%s,%s)",weight,stable)
392+
if self._logging:
393+
_log.debug('scale1_weight_changed_slot(%s,%s)',weight,stable)
390394
toa:float = libtime.time()
391395
prev_last_weight_sent = self.scale1_last_weight_sent
392396
self.scale1_last_weight_sent = int(round(weight))
@@ -400,14 +404,17 @@ def scale1_weight_changed_slot(self, weight:float, stable:bool) -> None:
400404
self.scale1_stable_reading_timer.stop() # we stop a pending timer not to have it deliver outdated data as stable
401405
# weights marked as stable by the scale are immediately forwarded as stable weights
402406
self.scale1_stable_weight_changed_signal.emit(self.scale1_last_weight_sent)
403-
## _log.debug("PRINT stable1 emit: %s",self.scale1_last_weight_sent)
407+
if self._logging:
408+
_log.debug('stable1 emit: %s',self.scale1_last_weight_sent)
404409
else:
405-
## _log.debug("PRINT STABLE->UNSTABLE1 %s, %s", stable and (toa - self.scale1_last_stable_weight_received) > MIN_TIME_BETWEEN_STABLE_WEIGHTS, stable and (prev_last_weight_sent is None or abs(self.scale1_last_weight_sent - prev_last_weight_sent) < MIN_DELTA_BETWEEN_STABLE_WEIGHTS))
410+
if self._logging:
411+
_log.debug('STABLE->UNSTABLE1 %s, %s', stable and (toa - self.scale1_last_stable_weight_received) > MIN_TIME_BETWEEN_STABLE_WEIGHTS, stable and (prev_last_weight_sent is None or abs(self.scale1_last_weight_sent - prev_last_weight_sent) < MIN_DELTA_BETWEEN_STABLE_WEIGHTS))
406412
self.scale1_last_weight = self.scale1_last_weight_sent
407413
# fed into our stable weight timer system to ensure that the "last one" is also emitted as stable weight
408414
self.scale1_stable_reading_timer.start(STABLE_TIMER_PERIOD) # start/restart stable weight timer
409415
# non-stable weights are immediately forwarded as regular weight updates to keep the display fluid
410-
## _log.debug("PRINT unstable1 emit: %s",self.scale1_last_weight_sent)
416+
if self._logging:
417+
_log.debug('unstable1 emit: %s',self.scale1_last_weight_sent)
411418
self.scale1_weight_changed_signal.emit(self.scale1_last_weight_sent)
412419

413420
def scale1_tare_pressed_slot(self) -> None:
@@ -416,7 +423,8 @@ def scale1_tare_pressed_slot(self) -> None:
416423
@pyqtSlot()
417424
def scale1_stable_reading_timer_slot(self) -> None:
418425
if self.scale1_last_weight is not None:
419-
## _log.debug("PRINT artificial stable1 emit: %s",self.scale1_last_weight)
426+
if self._logging:
427+
_log.debug('artificial stable1 emit: %s',self.scale1_last_weight)
420428
self.scale1_stable_weight_changed_signal.emit(self.scale1_last_weight)
421429

422430
def get_scale1_last_weight(self) -> int|None:
@@ -474,6 +482,7 @@ def scan_scale2_slot(self, model:int) -> None:
474482

475483
@pyqtSlot(bool)
476484
def connect_scale2_slot(self, device_logging:bool) -> None:
485+
self._logging = device_logging
477486
if self.scale2 is not None:
478487
self.scale2.connect_scale(device_logging)
479488

@@ -532,7 +541,8 @@ def scale2_disconnected_slot(self) -> None:
532541
# weight in g
533542
@pyqtSlot(float, bool)
534543
def scale2_weight_changed_slot(self, weight:float, stable:bool) -> None:
535-
## _log.debug("PRINT scale2_weight_changed_slot(%s,%s)",weight,stable)
544+
if self._logging:
545+
_log.debug('scale2_weight_changed_slot(%s,%s)',weight,stable)
536546
toa:float = libtime.time()
537547
prev_last_weight_sent = self.scale2_last_weight_sent
538548
self.scale2_last_weight_sent = int(round(weight))
@@ -546,14 +556,17 @@ def scale2_weight_changed_slot(self, weight:float, stable:bool) -> None:
546556
self.scale2_stable_reading_timer.stop() # we stop a pending timer not to have it deliver outdated data as stable
547557
# weights marked as stable by the scale are immediately forwarded as stable weights
548558
self.scale2_stable_weight_changed_signal.emit(self.scale2_last_weight_sent)
549-
## _log.debug("PRINT stable2 emit: %s",self.scale2_last_weight_sent)
559+
if self._logging:
560+
_log.debug('PRINT stable2 emit: %s',self.scale2_last_weight_sent)
550561
else:
551-
## _log.debug("PRINT STABLE->UNSTABLE2 %s, %s", stable and (toa - self.scale2_last_stable_weight_received) > MIN_TIME_BETWEEN_STABLE_WEIGHTS, stable and (prev_last_weight_sent is None or abs(self.scale2_last_weight_sent - prev_last_weight_sent) < MIN_DELTA_BETWEEN_STABLE_WEIGHTS))
562+
if self._logging:
563+
_log.debug('STABLE->UNSTABLE2 %s, %s', stable and (toa - self.scale2_last_stable_weight_received) > MIN_TIME_BETWEEN_STABLE_WEIGHTS, stable and (prev_last_weight_sent is None or abs(self.scale2_last_weight_sent - prev_last_weight_sent) < MIN_DELTA_BETWEEN_STABLE_WEIGHTS))
552564
self.scale2_last_weight = self.scale2_last_weight_sent
553565
# fed into our stable weight timer system to ensure that the "last one" is also emitted as stable weight
554566
self.scale2_stable_reading_timer.start(STABLE_TIMER_PERIOD) # start/restart stable weight timer
555567
# non-stable weights are immediately forwarded as regular weight updates to keep the display fluid
556-
## _log.debug("PRINT unstable2 emit: %s",self.scale2_last_weight_sent)
568+
if self._logging:
569+
_log.debug('unstable2 emit: %s',self.scale2_last_weight_sent)
557570
self.scale2_weight_changed_signal.emit(self.scale2_last_weight_sent)
558571

559572
def scale2_tare_pressed_slot(self) -> None:
@@ -562,7 +575,8 @@ def scale2_tare_pressed_slot(self) -> None:
562575
@pyqtSlot()
563576
def scale2_stable_reading_timer_slot(self) -> None:
564577
if self.scale2_last_weight is not None:
565-
## _log.debug("PRINT artificial stable2 emit: %s",self.scale2_last_weight)
578+
if self._logging:
579+
_log.debug('artificial stable2 emit: %s',self.scale2_last_weight)
566580
self.scale2_stable_weight_changed_signal.emit(self.scale2_last_weight)
567581

568582
def get_scale2_last_weight(self) -> int|None:

0 commit comments

Comments
 (0)