PowerStress client approximately works

This commit is contained in:
Kevin Hester
2024-06-28 09:40:33 -07:00
parent 119be81000
commit dabb4ea44c
4 changed files with 31 additions and 23 deletions

View File

@@ -852,14 +852,14 @@ def onConnected(interface):
qr = pyqrcode.create(url)
print(qr.terminal())
if args.slog_out:
if args.slog_out or args.power_stress:
# Setup loggers
global meter
LogSet(interface, args.slog_out if args.slog_out != 'default' else None, meter)
if args.power_stress:
stress = PowerStress(interface)
stress.run()
if args.power_stress:
stress = PowerStress(interface)
stress.run()
if args.listen:
closeNow = False

View File

@@ -47,7 +47,7 @@ class PowerStressClient:
self.node_id,
portnums_pb2.POWERSTRESS_APP,
wantAck=True,
wantResponse=False,
wantResponse=True,
onResponse=onResponse,
onResponseAckPermitted=True
)
@@ -64,7 +64,7 @@ class PowerStress:
# Send the power stress command
gotAck = False
def onResponse(packet, interface):
def onResponse(packet: dict): # pylint: disable=unused-argument
nonlocal gotAck
gotAck = True

View File

@@ -1,11 +1,11 @@
"""code logging power consumption of meshtastic devices."""
import atexit
import io
import logging
import os
import re
import threading
import io
import time
from dataclasses import dataclass
from datetime import datetime
@@ -100,10 +100,17 @@ class StructuredLogger:
"""
self.client = client
self.writer = FeatherWriter(f"{dir_path}/slog")
self.raw_file: Optional[io.TextIOWrapper] = open( # pylint: disable=consider-using-with
self.raw_file: Optional[
io.TextIOWrapper
] = open( # pylint: disable=consider-using-with
f"{dir_path}/raw.txt", "w", encoding="utf8"
)
self.listener = pub.subscribe(self._onLogMessage, TOPIC_MESHTASTIC_LOG_LINE)
# We need a closure here because the subscription API is very strict about exact arg matching
def listen_glue(line, interface): # pylint: disable=unused-argument
self._onLogMessage(line)
self.listener = pub.subscribe(listen_glue, TOPIC_MESHTASTIC_LOG_LINE)
def close(self) -> None:
"""Stop logging."""
@@ -113,9 +120,7 @@ class StructuredLogger:
self.raw_file = None # mark that we are shutting down
f.close() # Close the raw.txt file
def _onLogMessage(
self, line: str, interface: MeshInterface # pylint: disable=unused-argument
) -> None:
def _onLogMessage(self, line: str) -> None:
"""Handle log messages.
line (str): the line of log output
@@ -126,17 +131,20 @@ class StructuredLogger:
args = m.group(2)
args += " " # append a space so that if the last arg is an empty str it will still be accepted as a match
logging.debug(f"SLog {src}, reason: {args}")
d = log_defs.get(src)
if d:
r = d.format.parse(args) # get the values with the correct types
if r:
di = r.named
di["time"] = datetime.now()
self.writer.add_row(di)
else:
logging.warning(f"Failed to parse slog {line} with {d.format}")
if(src != "PM"):
logging.warning(f"Not yet handling structured log {src} (FIXME)")
else:
logging.warning(f"Unknown Structured Log: {line}")
d = log_defs.get(src)
if d:
r = d.format.parse(args) # get the values with the correct types
if r:
di = r.named
di["time"] = datetime.now()
self.writer.add_row(di)
else:
logging.warning(f"Failed to parse slog {line} with {d.format}")
else:
logging.warning(f"Unknown Structured Log: {line}")
if self.raw_file:
self.raw_file.write(line + "\n") # Write the raw log