feat: persistent config

This commit is contained in:
2025-10-15 21:52:24 +02:00
parent 7a7400422e
commit 993a431310
9 changed files with 1195 additions and 29 deletions

95
ctl.sh
View File

@@ -4,6 +4,25 @@ set -e
source .env
## Spore Control Script
## Usage: ./ctl.sh <command> [options]
##
## Commands:
## build [target] - Build firmware for target (base, d1_mini, etc.)
## flash [target] - Flash firmware to device
## uploadfs [target] - Upload filesystem to device
## ota update <ip> <target> - OTA update specific node
## ota all <target> - OTA update all nodes in cluster
## cluster members - List cluster members
## node wifi <ssid> <password> [ip] - Configure WiFi on node
## monitor - Monitor serial output
##
## Examples:
## ./ctl.sh build base
## ./ctl.sh flash d1_mini
## ./ctl.sh node wifi "MyNetwork" "MyPassword"
## ./ctl.sh node wifi "MyNetwork" "MyPassword" 192.168.1.100
function info {
sed -n 's/^##//p' ctl.sh
}
@@ -69,6 +88,82 @@ function cluster {
${@:-info}
}
function node {
function wifi {
if [ $# -lt 2 ]; then
echo "Usage: $0 node wifi <ssid> <password> [node_ip]"
echo " ssid: WiFi network name"
echo " password: WiFi password"
echo " node_ip: Optional IP address (defaults to API_NODE from .env)"
return 1
fi
local ssid="$1"
local password="$2"
local node_ip="${3:-$API_NODE}"
echo "Configuring WiFi on node $node_ip..."
echo "SSID: $ssid"
# Configure WiFi using the API endpoint
response=$(curl -s -w "\n%{http_code}" -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "ssid=$ssid&password=$password" \
"http://$node_ip/api/network/wifi/config" 2>/dev/null || echo -e "\n000")
# Extract HTTP status code and response body
http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | head -n -1)
# Check if curl succeeded
if [ "$http_code" = "000" ] || [ -z "$response_body" ]; then
echo "Error: Failed to connect to node at $node_ip"
echo "Please check:"
echo " - Node is powered on and connected to network"
echo " - IP address is correct"
echo " - Node is running Spore firmware"
return 1
fi
# Check HTTP status code
if [ "$http_code" != "200" ]; then
echo "Error: HTTP $http_code - Server error"
echo "Response: $response_body"
return 1
fi
# Parse and display the response
status=$(echo "$response_body" | jq -r '.status // "unknown"')
message=$(echo "$response_body" | jq -r '.message // "No message"')
config_saved=$(echo "$response_body" | jq -r '.config_saved // false')
restarting=$(echo "$response_body" | jq -r '.restarting // false')
connected=$(echo "$response_body" | jq -r '.connected // false')
ip=$(echo "$response_body" | jq -r '.ip // "N/A"')
echo "Status: $status"
echo "Message: $message"
echo "Config saved: $config_saved"
if [ "$restarting" = "true" ]; then
echo "Restarting: true"
echo "Note: Node will restart to apply new WiFi settings"
fi
echo "Connected: $connected"
if [ "$connected" = "true" ]; then
echo "IP Address: $ip"
fi
# Return appropriate exit code
if [ "$status" = "success" ]; then
echo "WiFi configuration completed successfully!"
return 0
else
echo "WiFi configuration failed!"
return 1
fi
}
${@:-info}
}
function monitor {
pio run --target monitor
}