File had errors. Reverted but with 1 added error check.

The last commit had multiple errors.
Reverted but used Josh's error handler to check if meshtastic is running and responsive.
This commit is contained in:
Ruledo 2025-01-07 23:22:35 -08:00
parent 0c168d6339
commit caca167b37

View file

@ -5,33 +5,35 @@ WIFI_STATE_FILE="/etc/wifi_state.txt"
PROTO_FILE="/root/.portduino/default/prefs/config.proto"
log() {
echo "$(date) - ${1}" | tee -a "${LOG_FILE}"
echo "$(date) - $1" | tee -a "$LOG_FILE"
}
get_mobile_wifi_state() {
local proto_output
proto_output=$(cat "${PROTO_FILE}" | protoc --decode_raw | awk '/4 {/, /}/ {if ($1 == "1:") print $2}')
[[ "${proto_output}" == "1" ]] && echo "up" || echo "down"
proto_output=$(cat "$PROTO_FILE" | protoc --decode_raw | awk '/4 {/, /}/ {if ($1 == "1:") print $2}')
[[ "$proto_output" == "1" ]] && echo "up" || echo "down"
}
set_mobile_wifi_state() {
local state="${1}"
local state="$1"
local meshtastic_output
if [[ "$(meshtastic --host 127.0.0.1 --get network.wifi_enabled 2>&1 | grep -i refused)" ]] || [[ ! "$(meshtastic --host 127.0.0.1 --get network.wifi_enabled)" ]]; then
log "Error connecting to MeshtasticD. Aborting."
exit 2
fi
if [[ "${state}" == "up" ]]; then
meshtastic_output="$(meshtastic --host 127.0.0.1 --set network.wifi_enabled true 2>&1 | grep -i wifi)"
if [[ "$state" == "up" ]]; then
meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled true 2>&1)
else
meshtastic_output="$(meshtastic --host 127.0.0.1 --set network.wifi_enabled false 2>&1 | grep -i wifi)"
meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled false 2>&1)
fi
log "Set Meshtastic Wi-Fi state to ${state}. Output: ${meshtastic_output}"
log "Set Meshtastic Wi-Fi state to $state. Output: $meshtastic_output"
}
set_wlan_state() {
local state="${1}"
if [[ "${state}" == "up" ]]; then
local state="$1"
if [[ "$state" == "up" ]]; then
ip link set wlan0 up && log "Set wlan0 UP."
else
ip link set wlan0 down && log "Set wlan0 DOWN."
@ -40,27 +42,27 @@ set_wlan_state() {
validate_wifi_state_file() {
local state
state=$(cat "${WIFI_STATE_FILE}" 2>/dev/null)
if [[ "${state}" != "up" && "${state}" != "down" ]]; then
echo "up" > "${WIFI_STATE_FILE}"
state=$(cat "$WIFI_STATE_FILE" 2>/dev/null)
if [[ "$state" != "up" && "$state" != "down" ]]; then
echo "up" > "$WIFI_STATE_FILE"
log "Invalid wifi_state.txt content. Defaulting to up."
fi
}
sync_states() {
local text_state mobile_state
text_state=$(cat "${WIFI_STATE_FILE}")
text_state=$(cat "$WIFI_STATE_FILE")
mobile_state=$(get_mobile_wifi_state)
if [[ "${text_state}" != "${mobile_state}" ]]; then
set_mobile_wifi_state "${text_state}"
log "Synced mobile Wi-Fi state to ${text_state}."
if [[ "$text_state" != "$mobile_state" ]]; then
set_mobile_wifi_state "$text_state"
log "Synced mobile Wi-Fi state to $text_state."
fi
current_wlan_state=$(ip link show wlan0 | grep -q 'state UP' && echo "up" || echo "down")
if [[ "${text_state}" != "${current_wlan_state}" ]]; then
set_wlan_state "${text_state}"
log "Synced wlan0 state to ${text_state}."
if [[ "$text_state" != "$current_wlan_state" ]]; then
set_wlan_state "$text_state"
log "Synced wlan0 state to $text_state."
fi
}
@ -74,18 +76,18 @@ monitor_changes() {
current_mobile_state=$(get_mobile_wifi_state)
current_wlan_state=$(ip link show wlan0 | grep -q 'state UP' && echo "up" || echo "down")
if [[ "${current_mobile_state}" != "${previous_mobile_state}" ]]; then
log "Detected mobile Wi-Fi state change: ${previous_mobile_state} -> ${current_mobile_state}"
echo "${current_mobile_state}" > "${WIFI_STATE_FILE}"
set_wlan_state "${current_mobile_state}"
previous_mobile_state="${current_mobile_state}"
if [[ "$current_mobile_state" != "$previous_mobile_state" ]]; then
log "Detected mobile Wi-Fi state change: $previous_mobile_state -> $current_mobile_state"
echo "$current_mobile_state" > "$WIFI_STATE_FILE"
set_wlan_state "$current_mobile_state"
previous_mobile_state="$current_mobile_state"
fi
if [[ "${current_wlan_state}" != "${previous_wlan_state}" ]]; then
log "Detected wlan0 state change: ${previous_wlan_state} -> ${current_wlan_state}"
echo "${current_wlan_state}" > "${WIFI_STATE_FILE}"
set_mobile_wifi_state "${current_wlan_state}"
previous_wlan_state="${current_wlan_state}"
if [[ "$current_wlan_state" != "$previous_wlan_state" ]]; then
log "Detected wlan0 state change: $previous_wlan_state -> $current_wlan_state"
echo "$current_wlan_state" > "$WIFI_STATE_FILE"
set_mobile_wifi_state "$current_wlan_state"
previous_wlan_state="$current_wlan_state"
fi
sleep 5
@ -96,3 +98,4 @@ monitor_changes() {
validate_wifi_state_file
sync_states
monitor_changes