logger: save time on UTC

Signed-off-by: YoungSoo Shin <shinys000114@gmail.com>
This commit is contained in:
2025-12-09 16:47:48 +09:00
parent ce40257fea
commit 194474fdff

View File

@@ -3,7 +3,7 @@ import asyncio
import csv import csv
import requests import requests
import websockets import websockets
from datetime import datetime from datetime import datetime, timezone
# Import the status_pb2.py file generated by `protoc`. # Import the status_pb2.py file generated by `protoc`.
# This file must be in the same directory as logger.py. # This file must be in the same directory as logger.py.
@@ -68,7 +68,7 @@ class OdroidPowerLogger:
csv_file = open(self.output_file, 'w', newline='', encoding='utf-8') csv_file = open(self.output_file, 'w', newline='', encoding='utf-8')
csv_writer = csv.writer(csv_file) csv_writer = csv.writer(csv_file)
# Write header - matches main.js and csv_2_plot.py expectations # Write header
header = [ header = [
'timestamp', 'uptime_ms', 'timestamp', 'uptime_ms',
'vin_voltage', 'vin_current', 'vin_power', 'vin_voltage', 'vin_current', 'vin_power',
@@ -97,24 +97,22 @@ class OdroidPowerLogger:
# Process only if the payload type is 'sensor_data' # Process only if the payload type is 'sensor_data'
if status_message.WhichOneof('payload') == 'sensor_data': if status_message.WhichOneof('payload') == 'sensor_data':
sensor_data = status_message.sensor_data sensor_data = status_message.sensor_data
ts_dt = datetime.fromtimestamp(sensor_data.timestamp_ms / 1000, tz=timezone.utc)
ts_str_print = ts_dt.strftime('%Y-%m-%d %H:%M:%S UTC')
# Format timestamp to ISO format with 'Z' for UTC, matching main.js print(f"--- {ts_str_print} (Uptime: {sensor_data.uptime_ms / 1000}s) ---")
ts_dt = datetime.fromtimestamp(sensor_data.timestamp_ms / 1000)
ts_iso = ts_dt.isoformat(timespec='milliseconds') + 'Z'
# Print data for console output (can be adjusted if needed) # Print data for each channel
print(f"--- {ts_iso} (Uptime: {sensor_data.uptime_ms / 1000:.3f}s) ---")
for name, channel in [('VIN', sensor_data.vin), ('MAIN', sensor_data.main), for name, channel in [('VIN', sensor_data.vin), ('MAIN', sensor_data.main),
('USB', sensor_data.usb)]: ('USB', sensor_data.usb)]:
print( print(
f" {name:<4}: {channel.voltage:.3f} V | {channel.current:.3f} A | {channel.power:.3f} W") f" {name:<4}: {channel.voltage:5.2f} V | {channel.current:5.3f} A | {channel.power:5.2f} W")
# Write to CSV if enabled # Write to CSV if enabled
if csv_writer: if csv_writer:
# Format numerical values to 3 decimal places, matching main.js ts_iso_csv = ts_dt.isoformat(timespec='milliseconds').replace('+00:00', 'Z')
row = [ row = [
ts_iso, ts_iso_csv, sensor_data.uptime_ms,
sensor_data.uptime_ms,
f"{sensor_data.vin.voltage:.3f}", f"{sensor_data.vin.current:.3f}", f"{sensor_data.vin.power:.3f}", f"{sensor_data.vin.voltage:.3f}", f"{sensor_data.vin.current:.3f}", f"{sensor_data.vin.power:.3f}",
f"{sensor_data.main.voltage:.3f}", f"{sensor_data.main.current:.3f}", f"{sensor_data.main.power:.3f}", f"{sensor_data.main.voltage:.3f}", f"{sensor_data.main.current:.3f}", f"{sensor_data.main.power:.3f}",
f"{sensor_data.usb.voltage:.3f}", f"{sensor_data.usb.current:.3f}", f"{sensor_data.usb.power:.3f}" f"{sensor_data.usb.voltage:.3f}", f"{sensor_data.usb.current:.3f}", f"{sensor_data.usb.power:.3f}"