-
Notifications
You must be signed in to change notification settings - Fork 1
/
restSmart.cpp
285 lines (275 loc) · 6.25 KB
/
restSmart.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "restSmart.h"
PropColor::PropColor(uint8_t r, uint8_t g, uint8_t b)
{
this->r = r;
this->g = g;
this->b = b;
}
uint8_t Prop::getSize()
{
switch (type)
{
case PropType::Bool:
return 1;
case PropType::Float:
return 4;
case PropType::Int:
return 4;
case PropType::Color:
return 3;
case PropType::Str:
return STRING_BUFFER;
default:
return 0;
}
}
void Prop::read()
{
#if !DISABLE_EEPROM
if (eepromSave)
{
switch (type)
{
case PropType::Bool:
*valueBool = EEPROM.readBool(startAddress);
break;
case PropType::Float:
*valueFloat = EEPROM.readFloat(startAddress);
break;
case PropType::Int:
*valueInt = EEPROM.readInt(startAddress);
break;
case PropType::Color:
(*valueColor).r = EEPROM.readByte(startAddress + 0);
(*valueColor).g = EEPROM.readByte(startAddress + 1);
(*valueColor).b = EEPROM.readByte(startAddress + 2);
break;
case PropType::Str:
*valueStr = EEPROM.readString(startAddress);
break;
default:
break;
}
}
#endif
}
void Prop::write()
{
#if !DISABLE_EEPROM
if (eepromSave)
{
switch (type)
{
case PropType::Bool:
EEPROM.writeBool(startAddress, *valueBool);
break;
case PropType::Float:
EEPROM.writeFloat(startAddress, *valueFloat);
break;
case PropType::Int:
EEPROM.writeInt(startAddress, *valueInt);
break;
case PropType::Color:
EEPROM.writeByte(startAddress + 0, (*valueColor).r);
EEPROM.writeByte(startAddress + 1, (*valueColor).g);
EEPROM.writeByte(startAddress + 2, (*valueColor).b);
break;
case PropType::Str:
EEPROM.writeString(startAddress, valueStr->length() > STRING_BUFFER ? valueStr->substring(0, STRING_BUFFER - 1) : *valueStr);
break;
default:
break;
}
EEPROM.commit();
}
#endif
}
void Prop::setFromBuffer()
{
#if RESTSMART_DEBUG
Serial.print(id);
Serial.print(" = ");
Serial.println(rxBuffer);
#endif
switch (type)
{
case PropType::Bool:
*valueBool = rxBuffer == "ON";
break;
case PropType::Float:
*valueFloat = rxBuffer.toFloat();
break;
case PropType::Int:
*valueInt = rxBuffer.toInt();
break;
case PropType::Color:
{
String buffer = "";
uint8_t channel = 0;
rxBuffer += ",";
for (uint16_t i = 0; i < rxBuffer.length(); i++)
{
if (rxBuffer[i] == ',')
{
switch (channel)
{
case 0:
(*valueColor).r = buffer.toInt();
break;
case 1:
(*valueColor).g = buffer.toInt();
break;
case 2:
(*valueColor).b = buffer.toInt();
break;
default:
break;
}
buffer = "";
channel++;
}
else
{
buffer += rxBuffer[i];
}
}
break;
}
case PropType::Str:
*valueStr = rxBuffer;
break;
default:
break;
}
write();
rxBuffer = "";
}
String Prop::stringify()
{
switch (type)
{
case PropType::Bool:
return *valueBool ? "ON" : "OFF";
break;
case PropType::Float:
return String(*valueFloat, floatPrecision);
break;
case PropType::Int:
return String(*valueInt);
break;
case PropType::Color:
return String((*valueColor).r) + "," + String((*valueColor).g) + "," + String((*valueColor).b);
case PropType::Str:
return *valueStr;
default:
return "UNDEF";
}
}
void RestSmart::updateProps()
{
uint16_t eepromCounter = eepromStart;
for (std::vector<Prop>::iterator it = props.begin(); it != props.end(); it++)
{
it->startAddress = eepromCounter;
it->read();
eepromCounter += it->getSize();
}
}
String RestSmart::stringify()
{
String json = "{";
for (std::vector<Prop>::iterator it = props.begin(); it != props.end(); it++)
{
json += "\"" + it->id + "\": " + it->stringify();
if (it < props.end() - 1)
{
json += ",";
}
}
json += "}";
return json;
}
void RestSmart::connect()
{
while (!WiFi.isConnected())
{
#if RESTSMART_DEBUG
Serial.print("WiFi connecting");
#endif
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
for (uint8_t i = 0; i < 10; i++)
{
if (WiFi.isConnected())
{
break;
}
#if RESTSMART_DEBUG
Serial.print('.');
#endif
delay(500);
}
#if RESTSMART_DEBUG
Serial.println();
#endif
}
delay(100);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
WiFi.setHostname(wifiHostname.c_str());
delay(100);
if (!MDNS.begin(wifiHostname.c_str()))
{
#if RESTSMART_DEBUG
Serial.println("MDNS Failed!");
#endif
while (1)
;
}
server.begin();
for (std::vector<Prop>::iterator it = props.begin(); it != props.end(); it++)
{
server.on(String("/get/" + it->id).c_str(), HTTP_ANY, [it](AsyncWebServerRequest *r)
{ r->send(200, "text/plain", it->stringify()); });
server.on(String("/set/" + it->id).c_str(), HTTP_GET, [this, it](AsyncWebServerRequest *r)
{
if (r->hasParam("v"))
{
it->rxBuffer = r->getParam("v")->value();
it->setFromBuffer();
r->send(200, "text/plain", "OK");
}
else
{
r->send(400, "text/plain", "Missing \"v\" parameter for value");
}
});
server.on(
String("/set/" + it->id).c_str(), HTTP_PUT, [this, it](AsyncWebServerRequest *r)
{ r->send(200, "text/plain", "OK"); },
[](AsyncWebServerRequest *r, String filename, size_t index, uint8_t *data, size_t len, bool f) {}, [it](AsyncWebServerRequest *r, uint8_t *data, size_t len, size_t ind, size_t total)
{
for (int i = 0; i < len; i++)
{
it->rxBuffer += (char)data[i];
}
if (ind + len == total)
{
it->setFromBuffer();
}
});
}
server.on("/get", HTTP_GET, [this](AsyncWebServerRequest *r)
{ r->send(200, "application/json", stringify()); });
server.onNotFound([](AsyncWebServerRequest *r)
{ r->send(404); });
MDNS.addService("http", "tcp", RESTSMART_PORT);
}
void RestSmart::loop()
{
if (!WiFi.isConnected())
{
server.end();
MDNS.end();
connect();
}
}