Update wifi-mesh-control.sh

encapsulated variables, also added grep for wifi line only to meshtastic command
This commit is contained in:
joshbowyer 2025-01-06 17:29:01 -06:00 • committed by GitHub
parent f1e4f82d52
commit da0b5e59c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,29 +5,29 @@ 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 [[ "$state" == "up" ]]; then
meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled true 2>&1)
if [[ "${state}" == "up" ]]; then
meshtastic_output="$(meshtastic --host 127.0.0.1 --set network.wifi_enabled true 2>&1 | grep -i wifi)"
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 | grep -i wifi)"
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."
@ -36,27 +36,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
}
@ -70,18 +70,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
@ -92,4 +92,3 @@ monitor_changes() {
validate_wifi_state_file
sync_states
monitor_changes