-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·116 lines (98 loc) · 2.98 KB
/
sync.sh
File metadata and controls
executable file
·116 lines (98 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
# MatrixPortal Sync Script
# Backs up TODOs, uploads sketch, restores TODOs
ARDUINO_CLI="./bin/arduino-cli"
BOARD="adafruit:samd:adafruit_matrixportal_m4"
PORT="/dev/ttyACM0"
SKETCH_DIR="."
TODO_BACKUP="data/todos.json"
IP_CACHE=".last_device_ip"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${BLUE}MatrixPortal Sync${NC}"
echo "=================="
# Find device IP
find_device_ip() {
# Try cached IP first
if [ -f "$IP_CACHE" ]; then
IP=$(cat "$IP_CACHE")
if curl -s --connect-timeout 1 "http://$IP/api/todos" > /dev/null 2>&1; then
echo "$IP"
return 0
fi
fi
# Get IP from arduino_secrets.h
if [ -f "src/arduino_secrets.h" ]; then
IP=$(grep "#define DEVICE_IP" src/arduino_secrets.h | sed 's/.*"\(.*\)".*/\1/')
if [ ! -z "$IP" ]; then
if curl -s --connect-timeout 1 "http://$IP/api/todos" > /dev/null 2>&1; then
echo "$IP" > "$IP_CACHE"
echo "$IP"
return 0
fi
fi
fi
return 1
}
# Step 1: Backup TODOs
echo -e "${BLUE}Backing up TODOs...${NC}"
IP=$(find_device_ip)
if [ ! -z "$IP" ]; then
curl -s "http://$IP/api/todos" > "$TODO_BACKUP"
COUNT=$(grep -o '"id"' "$TODO_BACKUP" 2>/dev/null | wc -l)
echo -e "${GREEN}✓ Saved $COUNT TODOs${NC}"
else
echo -e "${YELLOW}Device not found, skipping backup${NC}"
fi
# Step 2: Compile
echo -e "${BLUE}Compiling...${NC}"
$ARDUINO_CLI compile --fqbn $BOARD $SKETCH_DIR > /dev/null 2>&1
echo -e "${GREEN}✓ Compiled${NC}"
# Step 3: Upload
echo -e "${BLUE}Uploading...${NC}"
if [ ! -e "$PORT" ]; then
echo -e "${RED}Device not connected to $PORT${NC}"
exit 1
fi
$ARDUINO_CLI upload -p $PORT --fqbn $BOARD $SKETCH_DIR > /dev/null 2>&1
echo -e "${GREEN}✓ Uploaded${NC}"
# Step 4: Wait for reboot
echo -e "${BLUE}Waiting for device...${NC}"
sleep 10
# Step 5: Restore TODOs
if [ -f "$TODO_BACKUP" ] && [ ! -z "$IP" ]; then
echo -e "${BLUE}Restoring TODOs...${NC}"
# Wait for device to be ready
for i in {1..20}; do
if curl -s --connect-timeout 1 "http://$IP/api/todos" > /dev/null 2>&1; then
break
fi
sleep 1
done
# Clear existing
curl -s "http://$IP/api/todos" | grep -o '"id":[0-9]*' | sed 's/"id"://' | while read id; do
curl -s -X DELETE "http://$IP/api/todos/$id" > /dev/null
done
# Restore each TODO
cat "$TODO_BACKUP" | python3 -c "
import json, sys
data = json.load(sys.stdin)
for todo in data.get('todos', []):
print(json.dumps({'text': todo['text'], 'done': todo['done']}))
" | while read todo; do
curl -s -X POST "http://$IP/api/todos" \
-H "Content-Type: application/json" \
-d "$todo" > /dev/null
echo -n "."
done
echo
echo -e "${GREEN}✓ TODOs restored${NC}"
fi
echo -e "${GREEN}Done!${NC}"
if [ ! -z "$IP" ]; then
echo -e "Web interface: http://$IP"
fi