From 7baf7fa17e7fdd0b687412b3388f7da7b9ecf8ac Mon Sep 17 00:00:00 2001 From: Matteo Carnelos Date: Tue, 3 May 2022 18:15:03 +0200 Subject: [PATCH] Changes: Remove redundant name from log format Minor adjustments in status script Fixed first boot settings Revert to lighter SSH server: Dropbear Fix topic parsing wrong setting Change timesync behavior: from boot only sync to constant adjustments --- Automation_Custom_Script.sh | 7 ++--- dietpi.txt | 2 +- rootfs/etc/sailtrack/sailtrack.conf | 2 +- rootfs/etc/telegraf/telegraf.conf | 2 +- sailtrack/sailtrack-status | 12 +++++---- sailtrack/sailtrack-timesync | 42 +++++++++++++++++++++++------ 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/Automation_Custom_Script.sh b/Automation_Custom_Script.sh index d0c9ffc..d2e8349 100644 --- a/Automation_Custom_Script.sh +++ b/Automation_Custom_Script.sh @@ -6,14 +6,10 @@ G_CONFIG_INJECT "alias poweroff=/boot/sailtrack/sailtrack-x708_softsd" "alias po # Remove unused components G_EXEC rm /etc/systemd/system/dietpi-vpn.service G_EXEC rm /etc/systemd/system/dietpi-cloudshell.service -/boot/dietpi/dietpi-software uninstall 0 # Install required packages G_AGI telegraf -G_EXEC pip3 install gpiozero -G_EXEC pip3 install paho-mqtt -G_EXEC pip3 install smbus2 -G_EXEC pip3 install timeloop +G_EXEC_OUTPUT=1 G_EXEC_OUTPUT_COL="\e[90m" G_EXEC pip3 install gpiozero paho-mqtt smbus2 timeloop # Enable services G_EXEC systemctl enable sailtrack-x708_asd @@ -26,6 +22,7 @@ G_EXEC /boot/dietpi/dietpi-services dietpi_controlled sailtrack-status G_EXEC /boot/dietpi/dietpi-services dietpi_controlled sailtrack-timesync # Configure DietPi Banner +G_EXEC touch /boot/dietpi/.dietpi-banner settings=(1 1 1 0 0 1 0 1 0 0 0 0 0 0 0 0) for i in "${!settings[@]}"; do G_CONFIG_INJECT "aENABLED\[$i]=" "aENABLED[$i]=${settings[$i]}" /boot/dietpi/.dietpi-banner diff --git a/dietpi.txt b/dietpi.txt index 086aa5f..de147e1 100644 --- a/dietpi.txt +++ b/dietpi.txt @@ -80,7 +80,7 @@ AUTO_SETUP_BACKUP_RESTORE=0 ##### Software Options ##### # SSH server choice: 0=none/custom | -1=Dropbear | -2=OpenSSH -AUTO_SETUP_SSH_SERVER_INDEX=-2 +AUTO_SETUP_SSH_SERVER_INDEX=-1 # Logging mode choice: 0=none/custom | -1=RAMlog 1h clear | -2=RAMlog 1h save clear | -3=rsyslog + logrotate AUTO_SETUP_LOGGING_INDEX=-1 diff --git a/rootfs/etc/sailtrack/sailtrack.conf b/rootfs/etc/sailtrack/sailtrack.conf index f3933b9..dc9f259 100644 --- a/rootfs/etc/sailtrack/sailtrack.conf +++ b/rootfs/etc/sailtrack/sailtrack.conf @@ -4,4 +4,4 @@ username = mosquitto password = dietpi [log] -format = [%(name)s] [%(levelname)s] %(message)s +format = [%(levelname)s] %(message)s diff --git a/rootfs/etc/telegraf/telegraf.conf b/rootfs/etc/telegraf/telegraf.conf index a61348e..1f6655c 100644 --- a/rootfs/etc/telegraf/telegraf.conf +++ b/rootfs/etc/telegraf/telegraf.conf @@ -33,5 +33,5 @@ omit_hostname = true password = "${MQTT_PASSWORD}" data_format = "json" [[inputs.mqtt_consumer.topic_parsing]] - topic = "#" + topic = "+/+" measurement = "_/measurement" diff --git a/sailtrack/sailtrack-status b/sailtrack/sailtrack-status index ce71fd6..958ac41 100755 --- a/sailtrack/sailtrack-status +++ b/sailtrack/sailtrack-status @@ -28,8 +28,8 @@ I2C_BATTERY_CAPACITY_REG = 0x04 CHARGE_PIN = 6 -MQTT_JOB_INTERVAL_SEC = 1 / MQTT_PUBLISH_FREQ_HZ -LOG_JOB_INTERVAL_SEC = 1 / LOG_PRINT_FREQ_HZ +MQTT_JOB_INTERVAL_MS = 1000 / MQTT_PUBLISH_FREQ_HZ +LOG_JOB_INTERVAL_MS = 1000 / LOG_PRINT_FREQ_HZ # ------------------------------------------------------------------- # @@ -61,6 +61,7 @@ mqtt = Client(MQTT_CLIENT_ID) mqtt.username_pw_set(config["mqtt"]["username"], config["mqtt"]["password"]) mqtt.on_publish = on_publish_callback mqtt.connect(config["mqtt"]["host"]) +mqtt.loop_start() tl = Timeloop() formatter = logging.Formatter(config.get("log", "format", raw=True)) logging.getLogger("timeloop").handlers[0].setFormatter(formatter) @@ -75,7 +76,7 @@ cpu_load = LoadAverage() disk_load = DiskUsage() -@tl.job(interval=timedelta(seconds=MQTT_JOB_INTERVAL_SEC)) +@tl.job(interval=timedelta(milliseconds=MQTT_JOB_INTERVAL_MS)) def mqtt_job(): sys.stdout = open(os.devnull, 'w') mqtt.publish("status/core", json.dumps({ @@ -95,9 +96,10 @@ def mqtt_job(): sys.stdout = sys.__stdout__ -@tl.job(interval=timedelta(seconds=LOG_JOB_INTERVAL_SEC)) +@tl.job(interval=timedelta(milliseconds=LOG_JOB_INTERVAL_MS)) def log_job(): - logger.info(f"Published messages: {published_messages}, Received messages: {received_messages}") + logger.info(f"Published messages: {published_messages}, " + f"Received messages: {received_messages}") tl.start(block=True) diff --git a/sailtrack/sailtrack-timesync b/sailtrack/sailtrack-timesync index 7f298ba..e32b499 100755 --- a/sailtrack/sailtrack-timesync +++ b/sailtrack/sailtrack-timesync @@ -4,25 +4,39 @@ import json import logging import time from configparser import ConfigParser +from datetime import timedelta from paho.mqtt.client import Client +from timeloop import Timeloop # -------------------------- Configuration -------------------------- # +TIMESYNC_DELTA_THRESHOLD = 1 +LOG_PRINT_FREQ_HZ = 0.1 + MQTT_CLIENT_ID = "sailtrack-timesync" CONFIG_FILE_PATH = "/etc/sailtrack/sailtrack.conf" +LOG_JOB_INTERVAL_MS = 1000 / LOG_PRINT_FREQ_HZ + # ------------------------------------------------------------------- # +received_epochs = 0 +time_syncs = 0 +delta = 0 + def on_message_callback(client, userdata, message): + global received_epochs + global time_syncs + global delta doc = json.loads(message.payload) if "epoch" in doc and doc["epoch"]: - logger.info(f"Received epoch: {doc['epoch']}") - time.clock_settime(time.CLOCK_REALTIME, doc["epoch"]) - logger.info("Time synced successfully!") - logger.info("Exiting...") - exit() + received_epochs += 1 + delta = doc["epoch"] - int(time.time()) + if abs(delta) > TIMESYNC_DELTA_THRESHOLD: + time.clock_settime(time.CLOCK_REALTIME, doc["epoch"]) + time_syncs += 1 config = ConfigParser() @@ -31,11 +45,23 @@ mqtt = Client(MQTT_CLIENT_ID) mqtt.username_pw_set(config["mqtt"]["username"], config["mqtt"]["password"]) mqtt.on_message = on_message_callback mqtt.connect(config["mqtt"]["host"]) +mqtt.loop_start() mqtt.subscribe("sensor/gps0") +tl = Timeloop() +formatter = logging.Formatter(config.get("log", "format", raw=True)) +logging.getLogger("timeloop").handlers[0].setFormatter(formatter) logger = logging.getLogger(MQTT_CLIENT_ID) logger.setLevel(logging.INFO) handler = logging.StreamHandler() -handler.setFormatter(logging.Formatter(config.get("log", "format", raw=True))) +handler.setFormatter(formatter) logger.addHandler(handler) -logger.info("Waiting for epoch...") -mqtt.loop_forever() + + +@tl.job(interval=timedelta(milliseconds=LOG_JOB_INTERVAL_MS)) +def log_job(): + logger.info(f"Received epochs: {received_epochs}, " + f"Time syncs: {time_syncs}, " + f"Delta: {delta}") + + +tl.start(block=True)