forked from lnbits/bitcoinswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100_config.ino
84 lines (73 loc) · 1.93 KB
/
100_config.ino
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
void configOverSerialPort() {
executeConfig();
}
void executeConfig() {
while (true) {
if (Serial.available() == 0) continue;
String data = Serial.readStringUntil('\n');
Serial.println("received: " + data);
KeyValue kv = extractKeyValue(data);
String commandName = kv.key;
if (commandName == "/config-done") {
Serial.println("/config-done");
return;
}
executeCommand(commandName, kv.value);
}
}
void executeCommand(String commandName, String commandData) {
Serial.println("executeCommand: " + commandName + " > " + commandData);
KeyValue kv = extractKeyValue(commandData);
String path = kv.key;
String data = kv.value;
if (commandName == "/file-remove") {
return removeFile(path);
}
if (commandName == "/file-append") {
return appendToFile(path, data);
}
if (commandName == "/file-read") {
Serial.println("prepare to read");
readFile(path);
Serial.println("readFile done");
return;
}
Serial.println("command unknown");
}
void removeFile(String path) {
Serial.println("removeFile: " + path);
SPIFFS.remove("/" + path);
}
void appendToFile(String path, String data) {
Serial.println("appendToFile: " + path);
File file = SPIFFS.open("/" + path, FILE_APPEND);
if (!file) {
file = SPIFFS.open("/" + path, FILE_WRITE);
}
if (file) {
file.println(data);
file.close();
}
}
void readFile(String path) {
Serial.println("readFile: " + path);
File file = SPIFFS.open("/" + path);
if (file) {
while (file.available()) {
String line = file.readStringUntil('\n');
Serial.println("/file-read " + line);
}
file.close();
}
Serial.println("");
Serial.println("/file-done");
}
KeyValue extractKeyValue(String s) {
int spacePos = s.indexOf(" ");
String key = s.substring(0, spacePos);
if (spacePos == -1) {
return {key, ""};
}
String value = s.substring(spacePos + 1, s.length());
return {key, value};
}