-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.ino
116 lines (110 loc) · 2.5 KB
/
parser.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
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
/*
* ESP8266 MQTT Wifi Client to Serial Bridge with NTP
* Author: rkubera https://github.com/rkubera/
* License: MIT
*/
#include <String.h>
bool getSerialBuffer (char* buf, int &idx) {
idx = 0;
bool ret = false;
while(true) {
if(Serial.available()) {
ret = true;
char readed = Serial.read();
buf[idx] = readed;
buf[idx+1] = 0;
idx++;
if((idx+1)>=bufferSize) break;
} else break;
}
return ret;
}
void parseBuffer() {
if (abs(buffmillis-millis())>intervalMillis) {
buffmillis = millis();
String line;
int lineIdx;
int start;
lineIdx = payloadsBuffer.indexOf('\n');
if (lineIdx>0) {
start = lineIdx;
line = payloadsBuffer.substring(0,start);
payloadsBuffer = payloadsBuffer.substring(start+1);
//Serial.print("[start]");
Serial.println(line);
//Serial.println("[stop]");
}
}
}
void parseLines(char* buf) {
static String myBuff = "";
myBuff = myBuff+ buf;
int start = 0;
String line;
int lineIdx;
do {
lineIdx = myBuff.indexOf('\n', start);
if (lineIdx>-1) {
line = myBuff.substring(start, lineIdx);
start = lineIdx+1;
line.trim();
parseCommand (line);
}
else {
line = "";
}
}
while (lineIdx>-1);
if (start>0) {
myBuff = myBuff.substring(start);
}
}
void parseCommand(String line) {
int tmpIdx;
String command;
String payload;
if (line.length()>0) {
tmpIdx = line.indexOf(' ');
if (tmpIdx>-1) {
command = line.substring(0, tmpIdx);
payload = line.substring(tmpIdx+1);
}
else {
command = line;
payload = "";
}
if (command=="connect") {
connectCommand(payload);
}
else if (command=="mqttserver") {
mqttServerCommand(payload);
}
else if (command=="mqttuserpass") {
mqttUserPassCommand(payload);
}
else if (command=="subscribe") {
subscribeCommand(payload);
}
else if (command=="unsubscribe") {
unsubscribeCommand(payload);
}
else if (command=="publish") {
publishCommand(payload);
}
else if (command=="publishretained") {
publishretainedCommand(payload);
}
else if (command=="hostname") {
wifiHostnameCommand(payload);
}
else if (command=="get") {
getCommand(payload);
}
else if (command=="crc32") {
crc32Command(payload);
}
else {
sendCommand("error");
}
}
}