Organize the project by domain and name analyses by their rule
Energy code moves to src/energy/ and its docs to docs/energy/; shared code (CSV loading, venv switch, browser, zoom script) goes to src/common/, and the AIMsight originals to docs/aimsight/. Analyses are no longer numbered: two Energy scripts remain, daily.py (delivered state and trend) and all_analyses.py (comparison of the four). Calculations and pages unchanged (D-026). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
77c8f24d2f
commit
8a32f4d418
44 files changed
+1757
-771
No files matched your search
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,882 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os, threading, signal, json, time, argparse, socket, re, datetime, collections
|
||||
import serial
|
||||
from .. import __version__ as _AIMBEDDED_VERSION
|
||||
from ..libs.logging import init_logger, set_logging_level, log_levels
|
||||
import subprocess
|
||||
|
||||
from ina219 import INA219, SmbusI2cDevice
|
||||
import psutil
|
||||
|
||||
try:
|
||||
import redis
|
||||
except ImportError:
|
||||
redis = None
|
||||
|
||||
|
||||
logger = init_logger(level="info")
|
||||
|
||||
|
||||
def send_at_command(ser, command, timeout=1):
|
||||
"""
|
||||
Sends an AT command to the modem and reads the response.
|
||||
"""
|
||||
ser.write((command + "\r\n").encode())
|
||||
time.sleep(timeout)
|
||||
response = ser.read_all().decode(errors="ignore").strip()
|
||||
return response
|
||||
|
||||
def map_csq_rssi(rssi):
|
||||
"""
|
||||
Maps RSSI value to dBm.
|
||||
"""
|
||||
if rssi == 0:
|
||||
return -113 # -113 dBm or less
|
||||
elif rssi == 31:
|
||||
return -51 # -51 dBm or greater
|
||||
elif 1 <= rssi <= 30:
|
||||
return -113 + (rssi * 2) # dBm
|
||||
elif rssi == 99:
|
||||
return None
|
||||
return None
|
||||
|
||||
def map_csq_ber(ber):
|
||||
"""
|
||||
Maps BER value to percentage range.
|
||||
"""
|
||||
ber_mapping = {
|
||||
0: 0.1,
|
||||
1: 0.3,
|
||||
2: 0.6,
|
||||
3: 1.2,
|
||||
4: 2.4,
|
||||
5: 5.0,
|
||||
6: 9.0,
|
||||
7: 13.0,
|
||||
99: None,
|
||||
}
|
||||
return ber_mapping.get(ber, None)
|
||||
|
||||
def parse_csq(response):
|
||||
"""
|
||||
Parses AT+CSQ response for signal quality.
|
||||
"""
|
||||
match = re.search(r'\+CSQ: (\d+),(\d+)', response)
|
||||
if match:
|
||||
rssi = int(match.group(1))
|
||||
ber = int(match.group(2))
|
||||
rssi_dbm = map_csq_rssi(rssi)
|
||||
ber_percent = map_csq_ber(ber)
|
||||
return rssi_dbm, ber_percent
|
||||
return None, None
|
||||
|
||||
def parse_cesq(response):
|
||||
"""
|
||||
Parses AT+CESQ response and interprets rsrq and rsrp.
|
||||
"""
|
||||
match = re.search(r'\+CESQ: (\d+),(\d+),(\d+),(\d+),(\d+),(\d+)', response)
|
||||
if match:
|
||||
# Extract rsrq and rsrp values (ignore first 4 values)
|
||||
rsrq = int(match.group(5))
|
||||
rsrp = int(match.group(6))
|
||||
|
||||
# Map rsrq to dB ranges
|
||||
rsrq_db = (
|
||||
-19.5 + (rsrq * 0.5) if 0 <= rsrq <= 34
|
||||
else None if rsrq == 255
|
||||
else None
|
||||
)
|
||||
|
||||
# Map rsrp to dBm ranges
|
||||
rsrp_dbm = (
|
||||
-140 + rsrp if 0 <= rsrp <= 97
|
||||
else None if rsrp == 255
|
||||
else None
|
||||
)
|
||||
|
||||
return rsrq_db, rsrp_dbm
|
||||
return None, None
|
||||
|
||||
def parse_cereg(response):
|
||||
"""
|
||||
Parses AT+CEREG? response into a registration status string.
|
||||
"""
|
||||
mapping = {0: "not_registered", 1: "home", 2: "searching",
|
||||
3: "denied", 4: "unknown", 5: "roaming"}
|
||||
match = re.search(r'\+CEREG: \d+,(\d+)', response)
|
||||
if match:
|
||||
return mapping.get(int(match.group(1)), "unknown")
|
||||
return None
|
||||
|
||||
def parse_cgact(response):
|
||||
"""
|
||||
Parses AT+CGACT? response. Returns the list of active PDP context ids.
|
||||
"""
|
||||
return [int(m.group(1)) for m in re.finditer(r'\+CGACT: (\d+),1', response)]
|
||||
|
||||
def parse_cgpaddr(response):
|
||||
"""
|
||||
Parses AT+CGPADDR=<cid> response. Returns the PDP address or None.
|
||||
"""
|
||||
match = re.search(r'\+CGPADDR: \d+,"([^"]+)"', response)
|
||||
if match and match.group(1) != "0.0.0.0":
|
||||
return match.group(1)
|
||||
return None
|
||||
|
||||
def get_network_info(response):
|
||||
"""
|
||||
Extracts network operator and technology from the response.
|
||||
"""
|
||||
match = re.search(r'\+COPS: \d,(\d),"([^"]+)",(\d+)', response)
|
||||
if match:
|
||||
mode = int(match.group(1))
|
||||
operator = match.group(2)
|
||||
technology = int(match.group(3))
|
||||
tech_mapping = {0: "GSM", 2: "UMTS", 3: "LTE", 7: "E-UTRAN"}
|
||||
return operator, tech_mapping.get(technology, "Unknown")
|
||||
return None, None
|
||||
|
||||
|
||||
class systemInfo(threading.Thread):
|
||||
# BusVoltage trend analysis: sliding window and classification deadband.
|
||||
# INA219 bus voltage LSB is 4 mV, so slopes below a few mV/min are noise.
|
||||
TREND_WINDOW_S = 60
|
||||
TREND_MIN_SAMPLES = 10
|
||||
TREND_MIN_SPAN_S = 30
|
||||
TREND_THRESHOLD_MV_MIN = 2.5
|
||||
|
||||
# Rest voltage vs state-of-charge for a 4S LiFePO4 pack (12.8 V nominal).
|
||||
# The curve is very flat between 30% and 80%, so mid-range estimates are
|
||||
# coarse and only reliable when the battery is not under charge.
|
||||
LIFEPO4_OCV_SOC = [
|
||||
(10.0, 0), (12.0, 10), (12.5, 20), (12.8, 30), (12.9, 40),
|
||||
(13.0, 50), (13.1, 60), (13.2, 70), (13.3, 80), (13.4, 90), (13.6, 100),
|
||||
]
|
||||
RUNTIME_MIN_CURRENT_MA = 50
|
||||
|
||||
# Effective series resistance [Ohm] between battery and INA219 bus
|
||||
# measurement: pack internal resistance + wiring + 100 mOhm shunt.
|
||||
# Measured 0.34 Ohm on a deployed unit via a load-step V/I regression
|
||||
# (R^2 = 0.93). Without IR compensation, load changes masquerade as
|
||||
# charge/discharge swings on LiFePO4's flat voltage curve.
|
||||
BATTERY_R_OHM_DEFAULT = 0.34
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
system_name=None,
|
||||
device=1,
|
||||
enable_logging=False,
|
||||
no_modem=False,
|
||||
refresh_rate=1,
|
||||
battery_capacity_ah=None,
|
||||
battery_r_ohm=BATTERY_R_OHM_DEFAULT,
|
||||
config_folder="/etc/aimsight",
|
||||
redis_socket=None,
|
||||
redis_host=None,
|
||||
redis_port=None,
|
||||
):
|
||||
self.config_folder = config_folder
|
||||
self.system_name = system_name
|
||||
self.device_index = device
|
||||
self.stop_service = threading.Event()
|
||||
self.event_notification = threading.Condition()
|
||||
self.refresh_rate = refresh_rate
|
||||
self.enable_logging = enable_logging
|
||||
self.modem = not no_modem
|
||||
self.power_monitor = self.init_ina(i2cBus=device)
|
||||
self.voltage_history = collections.deque()
|
||||
self.battery_state = "unknown"
|
||||
self.battery_capacity_ah = battery_capacity_ah
|
||||
self.battery_r_ohm = battery_r_ohm
|
||||
self.accumulator = None
|
||||
self.last_sync = time.time()
|
||||
self.modem_count = 0
|
||||
self.modem_resp = None
|
||||
self.network_count = 0
|
||||
self.network_resp = None
|
||||
|
||||
# Redis init (graceful degradation)
|
||||
self._redis = None
|
||||
if redis is not None:
|
||||
try:
|
||||
if redis_socket:
|
||||
self._redis = redis.Redis(unix_socket_path=redis_socket, decode_responses=False)
|
||||
else:
|
||||
self._redis = redis.Redis(
|
||||
host=redis_host or "localhost",
|
||||
port=redis_port or 6379,
|
||||
decode_responses=False,
|
||||
)
|
||||
self._redis.ping()
|
||||
logger.info("Redis connection established")
|
||||
except Exception as e:
|
||||
logger.warning("Redis not available: %s", e)
|
||||
self._redis = None
|
||||
else:
|
||||
logger.warning("redis-py not installed")
|
||||
|
||||
super().__init__()
|
||||
|
||||
def stop(self):
|
||||
logger.info("Stop instruction received. notifing acquisition thread.")
|
||||
self.stop_service.set()
|
||||
with self.event_notification:
|
||||
self.event_notification.notify()
|
||||
|
||||
def init_ina(self, shunt_ohms=0.1, i2cBus=1):
|
||||
i2c_addr = INA219.I2C_ADDR_DEFAULT
|
||||
i2c_device = SmbusI2cDevice(interface=1, address=i2c_addr)
|
||||
|
||||
ina_device = INA219(i2c_device, shunt_ohms=shunt_ohms) # Using the BBB I2C bus nb.2
|
||||
try:
|
||||
ina_device.configure(INA219.RANGE_32V, INA219.GAIN_8_320MV)
|
||||
except OSError:
|
||||
logger.fatal("INA219: Cannot communicate with peripheral %d", i2cBus)
|
||||
return ina_device
|
||||
|
||||
def _store_in_redis(self, key, data, ttl):
|
||||
"""Store data as a Redis hash with TTL. No-op if Redis is unavailable."""
|
||||
if self._redis is None:
|
||||
return
|
||||
try:
|
||||
mapping = {"_updated_at": str(time.time())}
|
||||
for k, v in data.items():
|
||||
if v is None:
|
||||
mapping[k] = ""
|
||||
elif isinstance(v, (list, tuple)):
|
||||
mapping[k] = json.dumps(v)
|
||||
else:
|
||||
mapping[k] = str(v)
|
||||
self._redis.hset(key, mapping=mapping)
|
||||
self._redis.expire(key, ttl)
|
||||
except Exception as e:
|
||||
logger.warning("Redis store failed for %s: %s", key, e)
|
||||
|
||||
def _get_gateways(self):
|
||||
"""Parse 'ip route' to build a {iface: gateway} map."""
|
||||
gateways = {}
|
||||
try:
|
||||
output = subprocess.check_output(["ip", "route"], text=True)
|
||||
for line in output.splitlines():
|
||||
# e.g. "default via 192.168.1.1 dev eth0 ..."
|
||||
parts = line.split()
|
||||
if "via" in parts and "dev" in parts:
|
||||
gw = parts[parts.index("via") + 1]
|
||||
iface = parts[parts.index("dev") + 1]
|
||||
if parts[0] == "default":
|
||||
gateways[iface] = gw
|
||||
except Exception:
|
||||
pass
|
||||
return gateways
|
||||
|
||||
def get_interface_info(self):
|
||||
"""Get network interface info (IP, netmask, gateway). Returns flat dict or None."""
|
||||
self.network_count = (self.network_count + 1) % 5
|
||||
if self.network_count > 0 and self.network_resp is not None:
|
||||
return self.network_resp
|
||||
|
||||
try:
|
||||
result = {}
|
||||
addrs = psutil.net_if_addrs()
|
||||
gateways = self._get_gateways()
|
||||
|
||||
skip_prefixes = ("lo", "docker", "br-")
|
||||
for iface, addr_list in addrs.items():
|
||||
if any(iface.startswith(p) for p in skip_prefixes):
|
||||
continue
|
||||
for addr in addr_list:
|
||||
if addr.family == socket.AF_INET:
|
||||
result[f"{iface}_ip"] = addr.address
|
||||
result[f"{iface}_netmask"] = addr.netmask
|
||||
result[f"{iface}_gateway"] = gateways.get(iface)
|
||||
break
|
||||
|
||||
self.network_resp = result if result else None
|
||||
except Exception as e:
|
||||
logger.warning("Failed to get network info: %s", e)
|
||||
|
||||
return self.network_resp
|
||||
|
||||
def publish_new_system_info(self):
|
||||
power_info = self.get_power_info()
|
||||
proc_info = self.get_proc_info()
|
||||
thermal_info = self.get_thermal_info()
|
||||
self._store_in_redis("system_info:power", power_info, 60)
|
||||
self._store_in_redis("system_info:proc", proc_info, 60)
|
||||
self._store_in_redis("system_info:temperature", thermal_info, 60)
|
||||
|
||||
modem_info = None
|
||||
if self.modem:
|
||||
modem_info = self.get_modem_info()
|
||||
if modem_info is not None:
|
||||
self._store_in_redis("system_info:modem", modem_info, 120)
|
||||
|
||||
network_info = self.get_interface_info()
|
||||
if network_info is not None:
|
||||
self._store_in_redis("system_info:network", network_info, 120)
|
||||
|
||||
if self.enable_logging:
|
||||
logger.info("Power: %s", power_info)
|
||||
logger.info("Proc : %s", proc_info)
|
||||
logger.info("Temp : %s", thermal_info)
|
||||
if self.modem:
|
||||
logger.info("Modem: %s", modem_info)
|
||||
logger.info("Net : %s", network_info)
|
||||
|
||||
def update_voltage_trend(self, voltage):
|
||||
"""
|
||||
Tracks BusVoltage over a sliding time window and classifies the
|
||||
battery state from its slope.
|
||||
Returns (slope_mv_per_min, state) with state one of "charging",
|
||||
"discharging", "stable" or "unknown".
|
||||
"""
|
||||
now = time.time()
|
||||
self.voltage_history.append((now, voltage))
|
||||
while now - self.voltage_history[0][0] > self.TREND_WINDOW_S:
|
||||
self.voltage_history.popleft()
|
||||
|
||||
n = len(self.voltage_history)
|
||||
span = self.voltage_history[-1][0] - self.voltage_history[0][0]
|
||||
if n < self.TREND_MIN_SAMPLES or span < self.TREND_MIN_SPAN_S:
|
||||
return None, "unknown"
|
||||
|
||||
# Least-squares slope of voltage vs time, converted to mV/min
|
||||
t_mean = sum(t for t, _ in self.voltage_history) / n
|
||||
v_mean = sum(v for _, v in self.voltage_history) / n
|
||||
num = sum((t - t_mean) * (v - v_mean) for t, v in self.voltage_history)
|
||||
den = sum((t - t_mean) ** 2 for t, _ in self.voltage_history)
|
||||
if den == 0:
|
||||
return None, "unknown"
|
||||
slope_mv_min = num / den * 1000 * 60
|
||||
|
||||
if slope_mv_min > self.TREND_THRESHOLD_MV_MIN:
|
||||
self.battery_state = "charging"
|
||||
elif slope_mv_min < -self.TREND_THRESHOLD_MV_MIN:
|
||||
self.battery_state = "discharging"
|
||||
else:
|
||||
self.battery_state = "stable"
|
||||
return slope_mv_min, self.battery_state
|
||||
|
||||
def battery_percent(self, voltage):
|
||||
"""
|
||||
Estimates state-of-charge [%] from pack voltage by linear
|
||||
interpolation of the LiFePO4 OCV table. While charging, the bus
|
||||
follows the charger voltage and the estimate saturates at 100%.
|
||||
"""
|
||||
table = self.LIFEPO4_OCV_SOC
|
||||
if voltage <= table[0][0]:
|
||||
return 0.0
|
||||
if voltage >= table[-1][0]:
|
||||
return 100.0
|
||||
for (v0, p0), (v1, p1) in zip(table, table[1:]):
|
||||
if voltage <= v1:
|
||||
return p0 + (p1 - p0) * (voltage - v0) / (v1 - v0)
|
||||
return 100.0
|
||||
|
||||
def get_power_info(self):
|
||||
bus_voltage = self.power_monitor.voltage()
|
||||
power_info = {"BusVoltage": bus_voltage}
|
||||
try:
|
||||
power_info.update({
|
||||
"BusCurrent": self.power_monitor.current(),
|
||||
"Power": self.power_monitor.power(),
|
||||
"ShuntVoltage": self.power_monitor.shunt_voltage(),
|
||||
})
|
||||
except Exception as exception:
|
||||
# Current out of device range with specified shunt resistor
|
||||
logger.error("INA219: %s", exception.__repr__())
|
||||
|
||||
# Compensate the IR drop (V_ocv ~ V_bus + I*R) so that trend and SoC
|
||||
# track the battery, not the load: a load step sags the bus voltage
|
||||
# and would otherwise read as a discharge, and the relaxation after
|
||||
# load release as a charge.
|
||||
current_ma = power_info.get("BusCurrent")
|
||||
if current_ma is not None:
|
||||
ocv = bus_voltage + (current_ma / 1000) * self.battery_r_ohm
|
||||
else:
|
||||
ocv = bus_voltage
|
||||
slope_mv_min, battery_state = self.update_voltage_trend(ocv)
|
||||
# SoC from the windowed mean of the compensated voltage: filters
|
||||
# INA219 quantization noise and residual load transients.
|
||||
mean_ocv = sum(v for _, v in self.voltage_history) / len(self.voltage_history)
|
||||
soc = self.battery_percent(mean_ocv)
|
||||
power_info.update({
|
||||
"BusVoltageOCV": round(ocv, 3),
|
||||
"BusVoltageTrend": round(slope_mv_min, 2) if slope_mv_min is not None else None,
|
||||
"BatteryState": battery_state,
|
||||
"BatteryPercent": round(soc, 1),
|
||||
})
|
||||
|
||||
if self.battery_capacity_ah is not None:
|
||||
remaining_ah = self.battery_capacity_ah * soc / 100
|
||||
power_info["BatteryRemainingAh"] = round(remaining_ah, 2)
|
||||
current_ma = power_info.get("BusCurrent")
|
||||
if (battery_state == "discharging" and current_ma is not None
|
||||
and current_ma > self.RUNTIME_MIN_CURRENT_MA):
|
||||
power_info["BatteryRuntime_h"] = round(remaining_ah / (current_ma / 1000), 1)
|
||||
return power_info
|
||||
|
||||
def get_thermal_info(self):
|
||||
folder_skeleton = "/sys/devices/virtual/thermal/thermal_zone{}/temp"
|
||||
thermal_info = {}
|
||||
mapping = {"A0": 0, "CPU": 1, "GPU": 2, "PLL": 3, "thermal": 5}
|
||||
for k, i in mapping.items():
|
||||
with open(folder_skeleton.format(i), "r") as fp:
|
||||
value = fp.read()
|
||||
thermal_info[k] = int(value) / 1000
|
||||
return thermal_info
|
||||
|
||||
def get_proc_info(self):
|
||||
gpu_load_file = "/sys/devices/gpu.0/load"
|
||||
with open(gpu_load_file, "r") as fp:
|
||||
value = fp.read()
|
||||
gpu_load = int(value)/10
|
||||
data_storage = psutil.disk_usage('/data')
|
||||
proc_info = {
|
||||
"gpu_load_percent": gpu_load,
|
||||
"cpu_load_percent": psutil.cpu_percent(),
|
||||
"percpu_load_percent": psutil.cpu_percent(percpu=True),
|
||||
"cpu_load_info": psutil.getloadavg(),
|
||||
"data_storage_percent": data_storage.percent,
|
||||
"data_storage_free_gb": data_storage.free / 1024**3,
|
||||
"data_storage_used_gb": data_storage.used / 1024**3,
|
||||
"data_storage_total_gb": data_storage.total / 1024**3,
|
||||
}
|
||||
return proc_info
|
||||
|
||||
def get_modem_info(self):
|
||||
self.modem_count = (self.modem_count + 1) % 10
|
||||
if self.modem_count > 0 and self.modem_resp is not None:
|
||||
return self.modem_resp
|
||||
try:
|
||||
ser = serial.Serial(
|
||||
port="/dev/ttyTHS1",
|
||||
baudrate=115200,
|
||||
timeout=1,
|
||||
write_timeout=1
|
||||
)
|
||||
if not ser.is_open:
|
||||
ser.open()
|
||||
response = send_at_command(ser, "AT+CSQ")
|
||||
rssi_dbm, ber_percent = parse_csq(response)
|
||||
response = send_at_command(ser, "AT+CESQ")
|
||||
rsrq_db, rsrp_dbm = parse_cesq(response)
|
||||
response = send_at_command(ser, "AT+COPS?")
|
||||
operator, technology = get_network_info(response)
|
||||
response = send_at_command(ser, "AT#CCID")
|
||||
match = re.search(r'#CCID: (\d+)', response)
|
||||
ccid = ""
|
||||
if match:
|
||||
ccid = match.group(1)
|
||||
# Link status and IP from the PDP context, mode-agnostic: works
|
||||
# in both PPP and ECM since it asks the modem about the cellular
|
||||
# session, not the host interface (in ECM the host only sees the
|
||||
# modem's NAT LAN address, e.g. 192.168.225.x on wwan0).
|
||||
response = send_at_command(ser, "AT+CEREG?", timeout=0.5)
|
||||
registration = parse_cereg(response)
|
||||
response = send_at_command(ser, "AT+CGACT?", timeout=0.5)
|
||||
active_cids = parse_cgact(response)
|
||||
ip = None
|
||||
if active_cids:
|
||||
response = send_at_command(ser, "AT+CGPADDR=%d" % active_cids[0], timeout=0.5)
|
||||
ip = parse_cgpaddr(response)
|
||||
link = "up" if ip is not None else "down"
|
||||
ser.close()
|
||||
except:
|
||||
# Report an explicit "down" default instead of returning the
|
||||
# cached response: stale good values would keep telling the
|
||||
# cloud the link is fine while the modem is unreachable.
|
||||
logger.exception("Error when reading modem, reporting link down")
|
||||
self.modem_resp = {
|
||||
"SIM_CCID": None,
|
||||
"rsrq_dB": None,
|
||||
"rsrp_dBm": None,
|
||||
"rssi_dbm": None,
|
||||
"ber_percent": None,
|
||||
"network_operator": None,
|
||||
"network_technology": None,
|
||||
"registration": None,
|
||||
"link": "down",
|
||||
"ip": None,
|
||||
}
|
||||
else:
|
||||
self.modem_resp = {
|
||||
"SIM_CCID": ccid,
|
||||
"rsrq_dB": rsrq_db,
|
||||
"rsrp_dBm": rsrp_dbm,
|
||||
"rssi_dbm": rssi_dbm,
|
||||
"ber_percent": ber_percent,
|
||||
"network_operator": operator,
|
||||
"network_technology": technology,
|
||||
"registration": registration,
|
||||
"link": link,
|
||||
"ip": ip,
|
||||
}
|
||||
return self.modem_resp
|
||||
|
||||
|
||||
def run(self):
|
||||
jiter_time = 0
|
||||
while "Running Indefinitely":
|
||||
if self.refresh_rate is None:
|
||||
timeout = None
|
||||
else:
|
||||
refresh_interval = 1 / self.refresh_rate
|
||||
timeout = refresh_interval - jiter_time - 0.002
|
||||
if timeout < 0:
|
||||
timeout = 0
|
||||
with self.event_notification:
|
||||
self.event_notification.wait(timeout=timeout)
|
||||
if self.stop_service.is_set():
|
||||
break
|
||||
|
||||
t0 = time.time()
|
||||
self.publish_new_system_info()
|
||||
jiter_time = time.time() - t0
|
||||
|
||||
if self.accumulator is not None:
|
||||
self.accumulator.commit()
|
||||
|
||||
if self._redis is not None:
|
||||
try:
|
||||
self._redis.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _add_redis_args(parser):
|
||||
"""Add common Redis connection arguments to a parser/subparser."""
|
||||
redis_group = parser.add_argument_group("Redis", "Redis connection options")
|
||||
redis_group.add_argument(
|
||||
"--redis-socket",
|
||||
default=os.environ.get("SYSTEM_INFO_REDIS_SOCKET"),
|
||||
help="Unix socket path (takes precedence over host:port)",
|
||||
)
|
||||
redis_group.add_argument(
|
||||
"--redis-host",
|
||||
default=os.environ.get("SYSTEM_INFO_REDIS_HOST", "localhost"),
|
||||
help="Redis host (default: localhost)",
|
||||
)
|
||||
redis_group.add_argument(
|
||||
"--redis-port",
|
||||
type=int,
|
||||
default=int(os.environ.get("SYSTEM_INFO_REDIS_PORT", 6379)),
|
||||
help="Redis port (default: 6379)",
|
||||
)
|
||||
redis_group.add_argument(
|
||||
"--redis-password",
|
||||
default=os.environ.get("SYSTEM_INFO_REDIS_PASSWORD"),
|
||||
help="Redis password",
|
||||
)
|
||||
redis_group.add_argument(
|
||||
"--redis-db",
|
||||
type=int,
|
||||
default=int(os.environ.get("SYSTEM_INFO_REDIS_DB", 0)),
|
||||
help="Redis database number (default: 0)",
|
||||
)
|
||||
redis_group.add_argument(
|
||||
"--redis-stream",
|
||||
default=os.environ.get("SYSTEM_INFO_REDIS_STREAM", "aimsight:sync"),
|
||||
help="Redis stream name (default: aimsight:sync)",
|
||||
)
|
||||
return redis_group
|
||||
|
||||
|
||||
def _redis_kwargs(args):
|
||||
"""Extract Redis connection kwargs from parsed args."""
|
||||
return {
|
||||
"socket": args.redis_socket,
|
||||
"host": args.redis_host,
|
||||
"port": args.redis_port,
|
||||
"password": args.redis_password,
|
||||
"db": args.redis_db,
|
||||
}
|
||||
|
||||
|
||||
def cmd_monitor(args):
|
||||
"""Run the system info monitor (default command)."""
|
||||
ENABLE_LOGGING = json.loads(os.environ.get("SYSTEM_INFO_ENABLE_LOGGING", "false"))
|
||||
DISABLE_MODEM = json.loads(os.environ.get("SYSTEM_INFO_DISABLE_MODEM", "false"))
|
||||
|
||||
pw = systemInfo(
|
||||
device=args.device,
|
||||
system_name=args.system_name,
|
||||
enable_logging=args.enable_logging or ENABLE_LOGGING,
|
||||
refresh_rate=args.refresh,
|
||||
no_modem=args.modem or DISABLE_MODEM,
|
||||
battery_capacity_ah=args.battery_capacity,
|
||||
battery_r_ohm=args.battery_resistance,
|
||||
redis_socket=args.redis_socket,
|
||||
redis_host=args.redis_host,
|
||||
redis_port=args.redis_port,
|
||||
)
|
||||
|
||||
signal.signal(signal.SIGTERM, lambda x, y: pw.stop())
|
||||
signal.signal(signal.SIGINT, lambda x, y: pw.stop())
|
||||
pw.start()
|
||||
while pw.is_alive():
|
||||
pw.join(1)
|
||||
|
||||
|
||||
# Per-Redis-topic sync spec: each topic maps to a list of directives, applied
|
||||
# in order against the topic's decoded hash. Directive shapes:
|
||||
#
|
||||
# {"fields": [key, ...]}
|
||||
# Heterogeneous quantities: publish each key as its own metric whose
|
||||
# data_type is the key itself.
|
||||
#
|
||||
# {"metric": name, "label": key, "fields": {redis_key: label_value, ...}}
|
||||
# Same-unit scalar series that only differ along one dimension: publish
|
||||
# every field under a single metric ``name``, distinguished by a
|
||||
# Prometheus-style ``label=label_value`` label instead of baking the
|
||||
# dimension into the metric name.
|
||||
#
|
||||
# {"metric": name, "label": key, "list": redis_key, "list_labels": [...]}
|
||||
# A list-valued field (one element per series member): publish one metric
|
||||
# ``name`` per element, labelled by ``list_labels[i]`` (or the index).
|
||||
SYNC_SPEC = {
|
||||
"power": [
|
||||
{"fields": [
|
||||
"Power", "BusCurrent", "BusVoltage", "BusVoltageOCV", "ShuntVoltage",
|
||||
"BusVoltageTrend", "BatteryState", "BatteryPercent",
|
||||
"BatteryRemainingAh", "BatteryRuntime_h",
|
||||
]},
|
||||
],
|
||||
"modem": [
|
||||
# ``ip`` is published as ip_address{interface="modem"}, see below.
|
||||
{"fields": [
|
||||
"rsrq_dB", "SIM_CCID", "rsrp_dBm", "rssi_dbm", "ber_percent",
|
||||
"network_operator", "network_technology", "registration", "link",
|
||||
]},
|
||||
],
|
||||
"temperature": [
|
||||
{"metric": "temperature_deg_c", "label": "sensor",
|
||||
"fields": {"A0": "A0", "CPU": "CPU", "GPU": "GPU", "PLL": "PLL", "thermal": "thermal"}},
|
||||
],
|
||||
"proc": [
|
||||
{"fields": ["data_storage_percent"]},
|
||||
{"metric": "data_storage_gb", "label": "state",
|
||||
"fields": {
|
||||
"data_storage_free_gb": "free",
|
||||
"data_storage_used_gb": "used",
|
||||
"data_storage_total_gb": "total",
|
||||
}},
|
||||
{"metric": "load_percent", "label": "unit",
|
||||
"fields": {"cpu_load_percent": "cpu", "gpu_load_percent": "gpu"}},
|
||||
{"metric": "cpu_load_percent", "label": "cpu", "list": "percpu_load_percent"},
|
||||
{"metric": "load_average", "label": "window", "list": "cpu_load_info",
|
||||
"list_labels": ["1m", "5m", "15m"]},
|
||||
],
|
||||
}
|
||||
|
||||
# Interface IPs published as ip_address{interface=...}, one series per link.
|
||||
# nebula1 is the Nebula overlay tun (config ``tun.dev``), only present while the
|
||||
# VPN is up; the modem IP is the cellular PDP address from the modem topic.
|
||||
IP_SOURCES = [
|
||||
("eth0", "network", "eth0_ip"),
|
||||
("nebula", "network", "nebula1_ip"),
|
||||
("modem", "modem", "ip"),
|
||||
]
|
||||
|
||||
|
||||
def _emit_directive(publisher, data, directive):
|
||||
"""Publish one directive's metrics from a decoded Redis hash.
|
||||
|
||||
Returns the number of metrics published. None-valued fields/elements and a
|
||||
missing (or non-list) ``list`` source are skipped.
|
||||
"""
|
||||
metric = directive.get("metric")
|
||||
label = directive.get("label")
|
||||
count = 0
|
||||
|
||||
list_key = directive.get("list")
|
||||
if list_key is not None:
|
||||
values = data.get(list_key)
|
||||
if not isinstance(values, list):
|
||||
return 0
|
||||
list_labels = directive.get("list_labels")
|
||||
for i, value in enumerate(values):
|
||||
if value is None:
|
||||
continue
|
||||
label_value = list_labels[i] if list_labels and i < len(list_labels) else str(i)
|
||||
publisher.metric(json.dumps(value), data_type=metric, labels={label: label_value})
|
||||
count += 1
|
||||
return count
|
||||
|
||||
if label is not None:
|
||||
for redis_key, label_value in directive["fields"].items():
|
||||
value = data.get(redis_key)
|
||||
if value is None:
|
||||
continue
|
||||
publisher.metric(json.dumps(value), data_type=metric, labels={label: label_value})
|
||||
count += 1
|
||||
return count
|
||||
|
||||
for key in directive["fields"]:
|
||||
value = data.get(key)
|
||||
if value is None:
|
||||
continue
|
||||
publisher.metric(json.dumps(value), data_type=key)
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def _module_version(*dist_names):
|
||||
"""Best-effort version of an installed distribution, or None.
|
||||
|
||||
Reads distribution metadata (does not import the package, so no heavy
|
||||
optional deps are pulled in); tries each candidate name in turn.
|
||||
"""
|
||||
try:
|
||||
from importlib.metadata import version, PackageNotFoundError
|
||||
except ImportError:
|
||||
return None
|
||||
for dist in dist_names:
|
||||
try:
|
||||
return version(dist)
|
||||
except PackageNotFoundError:
|
||||
continue
|
||||
except Exception:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def _distro_version(path="/etc/build"):
|
||||
"""Parse ``DISTRO_VERSION`` from the image build file, or None if absent."""
|
||||
try:
|
||||
with open(path) as fp:
|
||||
for line in fp:
|
||||
if line.startswith("DISTRO_VERSION"):
|
||||
_, _, value = line.partition("=")
|
||||
return value.strip().strip('"').strip("'") or None
|
||||
except OSError:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def _software_versions():
|
||||
"""Component versions for the ``software_version`` info-metric labels.
|
||||
|
||||
Missing components report "unknown" so the series keeps a stable set of
|
||||
label keys.
|
||||
"""
|
||||
return {
|
||||
"aimbedded": _AIMBEDDED_VERSION or "unknown",
|
||||
"aimsight_optical": _module_version("aimsight-optical", "aimsight_optical") or "unknown",
|
||||
"distro": _distro_version() or "unknown",
|
||||
}
|
||||
|
||||
|
||||
def _ntp_synchronized():
|
||||
"""Whether the system clock is NTP-synchronized, or None if undetermined.
|
||||
|
||||
Read live from ``timedatectl`` (an unprivileged query): a drifted clock
|
||||
breaks TLS and makes every reported timestamp suspect, so it is worth
|
||||
surfacing on its own.
|
||||
"""
|
||||
try:
|
||||
out = subprocess.check_output(
|
||||
["timedatectl", "show", "-p", "NTPSynchronized", "--value"],
|
||||
text=True, stderr=subprocess.DEVNULL, timeout=5,
|
||||
).strip().lower()
|
||||
except Exception:
|
||||
return None
|
||||
if out in ("yes", "true", "1"):
|
||||
return True
|
||||
if out in ("no", "false", "0"):
|
||||
return False
|
||||
return None
|
||||
|
||||
|
||||
def cmd_sync(args):
|
||||
"""Sync system_info snapshot from Redis to cloud via MQTT."""
|
||||
from .client import SystemInfoClient
|
||||
from ..mqtt_sync.publisher import SyncPublisher
|
||||
|
||||
redis_kw = _redis_kwargs(args)
|
||||
|
||||
with SystemInfoClient(**redis_kw) as client, \
|
||||
SyncPublisher(**redis_kw, stream=args.redis_stream) as publisher:
|
||||
snap = client.snapshot()
|
||||
if not snap:
|
||||
logger.warning("No system_info data in Redis, skipping sync")
|
||||
return
|
||||
|
||||
count = 0
|
||||
for topic, directives in SYNC_SPEC.items():
|
||||
data = snap.get(topic)
|
||||
if not data:
|
||||
continue
|
||||
for directive in directives:
|
||||
count += _emit_directive(publisher, data, directive)
|
||||
|
||||
# Interface IPs, one series per interface via an ``interface`` label.
|
||||
for label, topic, key in IP_SOURCES:
|
||||
ip = (snap.get(topic) or {}).get(key)
|
||||
if ip:
|
||||
publisher.metric(json.dumps(ip), data_type="ip_address", labels={"interface": label})
|
||||
count += 1
|
||||
|
||||
# Clock health: 1 synchronized, 0 drifted. Reported even when 0 (a
|
||||
# de-synced clock must be visible, not merely absent); omitted only
|
||||
# when it cannot be determined.
|
||||
ntp = _ntp_synchronized()
|
||||
if ntp is not None:
|
||||
publisher.metric(json.dumps(1 if ntp else 0), data_type="ntp_synchronized")
|
||||
count += 1
|
||||
|
||||
# Component versions as a Prometheus-style info metric (value 1, the
|
||||
# versions carried as labels).
|
||||
publisher.metric(json.dumps(1), data_type="software_version", labels=_software_versions())
|
||||
count += 1
|
||||
|
||||
publisher.metric(json.dumps(datetime.datetime.now().isoformat()), data_type="last_sync")
|
||||
logger.info("Synced %d system_info metrics to cloud", count + 1)
|
||||
|
||||
def main():
|
||||
SYSTEM_NAME = os.environ.get("SYSTEM_NAME", None)
|
||||
REFRESH_RATE = os.environ.get("SYSTEM_INFO_REFRESH_RATE", "1")
|
||||
|
||||
parser = argparse.ArgumentParser(description="System info collector")
|
||||
subparsers = parser.add_subparsers(dest="command")
|
||||
|
||||
# --- monitor (default) ---
|
||||
monitor_parser = subparsers.add_parser("monitor", help="Run system info monitor")
|
||||
monitor_parser.add_argument("-d", "--device", type=int, help="device index", default=1)
|
||||
monitor_parser.add_argument("-s", "--system_name", help="System name", default=SYSTEM_NAME)
|
||||
monitor_parser.add_argument("-r", "--refresh", type=float, help="Refresh rate in [Hz]", default=REFRESH_RATE)
|
||||
monitor_parser.add_argument("-l", "--enable_logging", action="store_true", help="print values")
|
||||
monitor_parser.add_argument("-m", "--modem", action="store_true", help="disable modem")
|
||||
BATTERY_CAPACITY = os.environ.get("SYSTEM_INFO_BATTERY_CAPACITY_AH")
|
||||
monitor_parser.add_argument(
|
||||
"-c", "--battery-capacity", type=float,
|
||||
default=float(BATTERY_CAPACITY) if BATTERY_CAPACITY else None,
|
||||
help="Battery capacity in Ah (7.3 or 19); enables remaining-Ah and runtime estimates",
|
||||
)
|
||||
BATTERY_R = os.environ.get("SYSTEM_INFO_BATTERY_R_OHM")
|
||||
monitor_parser.add_argument(
|
||||
"--battery-resistance", type=float,
|
||||
default=float(BATTERY_R) if BATTERY_R else systemInfo.BATTERY_R_OHM_DEFAULT,
|
||||
help="Effective series resistance battery-to-INA219 in Ohm, for IR compensation "
|
||||
"(default: %(default)s, measured via load-step V/I regression)",
|
||||
)
|
||||
_add_redis_args(monitor_parser)
|
||||
|
||||
# --- sync ---
|
||||
sync_parser = subparsers.add_parser("sync", help="Sync system_info snapshot to cloud")
|
||||
_add_redis_args(sync_parser)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.command == "sync":
|
||||
cmd_sync(args)
|
||||
else:
|
||||
# Default to monitor (handles both explicit "monitor" and no subcommand)
|
||||
if args.command is None:
|
||||
args = monitor_parser.parse_args()
|
||||
cmd_monitor(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user