wifi mesh control and watchclock-dog

Renamed wifi-mesh-control and now checks for meshtasticd to be running.
Also removed wants from service to prevent starting meshtasticd.
Added femto-watchclock-dog that checks for large time changes and restarts meshtasticd if detected to prevent a state where meshtasticd is running but not responsive on api port.
This commit is contained in:
Ruledo 2025-02-10 11:26:55 -08:00
parent fd52c30427
commit 5876ced494
4 changed files with 72 additions and 6 deletions

View file

@ -0,0 +1,12 @@
[Unit]
Description=Monitor time jumps and restart meshtasticd
After=network.target
[Service]
ExecStart=/usr/local/bin/femto-watchclock-dog.sh
Restart=always
User=root
[Install]
WantedBy=multi-user.target

View file

@ -1,11 +1,11 @@
[Unit]
Description=Meshtastic control of wifi
After=meshtasticd.service
Requires=meshtasticd.service
[Service]
ExecStart=/usr/local/bin/wifi-mesh-control.sh
ExecStart=/usr/local/bin/femto-wifi-mesh-control.sh
Restart=always
RestartSec=5
User=root
[Install]

View file

@ -0,0 +1,52 @@
#!/usr/bin/env bash
THRESHOLD=$((7 * 24 * 60 * 60)) # 1 week in seconds
LOGFILE="/var/log/time_change.log"
LAST_TIME_FILE="/tmp/last_time"
# Ensure the log file exists and is writable
if [ ! -f "$LOGFILE" ]; then
touch "$LOGFILE" || { echo "ERROR: Cannot create log file $LOGFILE"; exit 1; }
fi
if [ ! -w "$LOGFILE" ]; then
echo "ERROR: Log file $LOGFILE is not writable"
exit 1
fi
# Ensure the last time file exists and is writable
if [ ! -f "$LAST_TIME_FILE" ]; then
date +%s > "$LAST_TIME_FILE" || { echo "ERROR: Cannot create last time file $LAST_TIME_FILE"; exit 1; }
fi
if [ ! -w "$LAST_TIME_FILE" ]; then
echo "ERROR: Last time file $LAST_TIME_FILE is not writable"
exit 1
fi
while true; do
NEW_TIME=$(date +%s)
# Ensure last_time file is readable before using it
if [ ! -r "$LAST_TIME_FILE" ]; then
echo "ERROR: Last time file $LAST_TIME_FILE is not readable" | tee -a "$LOGFILE"
exit 1
fi
OLD_TIME=$(cat "$LAST_TIME_FILE")
TIME_DIFF=$((NEW_TIME - OLD_TIME))
if [ "${TIME_DIFF#-}" -ge "$THRESHOLD" ]; then
echo "$(date) - Large time change detected ($TIME_DIFF seconds), restarting meshtasticd" | tee -a "$LOGFILE"
systemctl restart meshtasticd
fi
# Ensure last_time file is writable before updating it
if [ -w "$LAST_TIME_FILE" ]; then
echo "$NEW_TIME" > "$LAST_TIME_FILE"
else
echo "ERROR: Failed to write to $LAST_TIME_FILE" | tee -a "$LOGFILE"
exit 1
fi
sleep 5
done

View file

@ -17,11 +17,13 @@ get_mobile_wifi_state() {
set_mobile_wifi_state() {
local state="$1"
local meshtastic_output
if [[ "$state" == "up" ]]; then
meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled true 2>&1)
meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled true 2>&1 || true)
else
meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled false 2>&1)
meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled false 2>&1 || true)
fi
log "Set Meshtastic Wi-Fi state to $state. Output: $meshtastic_output"
}
@ -67,8 +69,8 @@ monitor_changes() {
while true; do
PID=$(ps -C meshtasticd -o pid= | tr -d ' ')
#if [[ -n "$PID" ]] && sudo lsof /dev/spidev0.0 | grep -q "$PID"; then
if [[ -n "$PID" ]] && sudo lsof /dev/spidev0.0 | grep -q "$PID" && [[ -d /sys/class/net/wlan0 ]]; then
#if [[ -n "$PID" ]] && sudo lsof /dev/spidev0.0 | grep -q "$PID" && [[ -d /sys/class/net/wlan0 ]]; then
if [[ -n "$PID" ]] && sudo lsof /dev/spidev0.0 | grep -q "$PID" && [[ -d /sys/class/net/wlan0 ]] && systemctl is-active --quiet meshtasticd; then
local current_mobile_state current_wlan_state
current_mobile_state=$(get_mobile_wifi_state)
current_wlan_state=$(cat /sys/class/net/wlan0/operstate)