Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
matteocarnelos committed May 3, 2022
2 parents 0a46185 + 7baf7fa commit c214ff9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
7 changes: 2 additions & 5 deletions Automation_Custom_Script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dietpi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/sailtrack/sailtrack.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ username = mosquitto
password = dietpi

[log]
format = [%(name)s] [%(levelname)s] %(message)s
format = [%(levelname)s] %(message)s
2 changes: 1 addition & 1 deletion rootfs/etc/telegraf/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ omit_hostname = true
password = "${MQTT_PASSWORD}"
data_format = "json"
[[inputs.mqtt_consumer.topic_parsing]]
topic = "#"
topic = "+/+"
measurement = "_/measurement"
12 changes: 7 additions & 5 deletions sailtrack/sailtrack-status
Original file line number Diff line number Diff line change
Expand Up @@ -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

# ------------------------------------------------------------------- #

Expand Down Expand Up @@ -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)
Expand All @@ -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({
Expand All @@ -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)
42 changes: 34 additions & 8 deletions sailtrack/sailtrack-timesync
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

0 comments on commit c214ff9

Please sign in to comment.