-
Notifications
You must be signed in to change notification settings - Fork 2
/
AA_CONSOLE.ino
364 lines (320 loc) · 11.7 KB
/
AA_CONSOLE.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//<link rel="icon" type="image/x-icon" href="/favicon.ico" />
const char CONSOLE_HTML[] PROGMEM = R"=====(
<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>ESP-ECU</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/STYLESHEET">
<script>
function helpfunctie() {
document.getElementById("help").style.display = "block";
}
function sl() {
document.getElementById("help").style.display = "none";
}
</script>
<style>
tr {height:16px !important;
font-size:15px !important;
}
li a:hover {
background-color: #333 !important;
}
#help {
background-color: #ffffff;
border: solid 2px;
display:none;
padding:4px;
width:94vw;
}
</style>
</head>
<body>
<div id='help'>
<span class='close' onclick='sl();'>×</span><h3>CONSOLE COMMANDS</h3>
<b>10;ZBT=message: </b> send a zigbee message (e.g. 2710).<br><br>
<b>10;DELETE=filename: </b> delete a file.<br><br>
<b>10;INV_REBOOT: </b> reboot an unresponsive inverter<br><br>
<b>10;HEALTH: </b> healthcheck zigbee hw/system<br><br>
<b>10;POLL=x: </b> poll inverter #x<br><br>
<b>10;INIT_N: </b> start the zigbee coordinator<br><br>
<b>10;DIAG: </b> change debug, 0=disable, 1=console, 2=serial<br><br>
<b>10;EDIT=0-AABB: </b> mark an inverter as paired<br><br>
<b>10;ERASE: </b> delete all inverter files<br><br>
<b>10;FILES: </b> show filesystem<br><br>
<b>10;TESTMQTT: </b>sends a mqtt testmessage<br><br>
<b>10;CLEAR: </b> clear console window<br><br>
</div>
<div id='msect'>
<div id='menu'>
<a href='/MENU' onclick='confirmExit()' class='close'>×</span></a>
<a href='#' onclick='helpfunctie()'>help</a>
<a><input type="text" placeholder="type here" id="tiep"></a>
</div>
<br>
<div class='divstijl' style='height:84vh; border:1px solid; padding-left:10px;'>
<table id='tekstveld'></table>
</div>
</div>
<script>
var field = document.getElementById('tekstveld');
var gateway = `${(window.location.protocol == "https:"?"wss":"ws")}://${window.location.hostname}/ws`;
var websocket;
var inputField = document.getElementById('tiep');
window.onbeforeunload = confirmExit;
function confirmExit()
{
alert("close the console?");
ws.close();
}
window.addEventListener('load', onLoad);
function initWebSocket() {
console.log('Trying to open a WebSocket connection...');
websocket = new WebSocket(gateway);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onmessage = onMessage; // <-- add this line
}
function onOpen(event) {
console.log('Connection opened');
field.insertAdjacentHTML('beforeend', "<tr><td>* * connection opened * *");
inputField.focus();
}
function onClose(event) {
console.log('Connection closed');
field.insertAdjacentHTML('beforeend', "<tr><td>* * connection closed * *");
//setTimeout(initWebSocket, 2000);
}
function onMessage(event) {
//var message = event.data;
field.insertAdjacentHTML('beforeend', "<tr><td>" + event.data );
if (field.rows.length > 20) {
var rtm = field.rows.length - 20;
for (let x=0; x<rtm; x++) { field.deleteRow(0); }
}
if (event.data == "clearWindow") {
for (let i = 0; i < 22; i++) {
field.deleteRow(0); }
}
}
function onLoad(event) {
initWebSocket();
sendEvent();
}
function sendEvent() {
inputField.addEventListener('keyup', function(happen) {
if (happen.keyCode === 13) {
happen.preventDefault();
sendData();
}
});
}
function sendData(){
var data = inputField.value;
websocket.send(data, 1);
inputField.value = "";
}
function disConnect() {
alert("close the console");
ws.close();
}
</script>
</body>
</html>
)=====";
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;
for(int i=0; i<len; i++ )
{
txBuffer[i] = data[i];
}
txBuffer[len]='\0'; // terminate the array
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT)
{
//diagNose = 2; // direct the output to ws
data[len] = 0;
if (strncasecmp(txBuffer+3,"INV_REBOOT",10) == 0) {
ws.textAll("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<br>");
ws.textAll("Reboot an inverter that stopped working.");
ws.textAll("Characteristics: not responsive, (slow blinking red led).");
ws.textAll("type REBOOT_INVERTER=x (x=inverternumber 0, 1 etc.)");
ws.textAll("DISCLAIMER: THIS HAS NOT BEEN TESTED, USE AT YOUR OWN RISK!");
return;
} else
if (strncasecmp(txBuffer+3,"POLL=",5) == 0) {
//input can be 10;POLL=0;
//ws.textAll("received " + String( (char*)data) + "<br>");
int kz = String(txBuffer[8]).toInt();
if ( kz > inverterCount-1 ) {
ws.textAll("error, no such inverter");
if ( kz == 9 ) actionFlag=48; // poll all
return;
}
ws.textAll("poll inverter " + String(kz));
iKeuze=kz;
actionFlag=47;
return;
} else
if (strncasecmp(txBuffer+3,"EDIT=",5) == 0) {
//input can be 10;EDIT=0-AABB;
//ws.textAll("received " + String( (char*)data) + "<br>");
int kz = String(txBuffer[8]).toInt();
if ( kz > inverterCount-1 ) {
ws.textAll("error, no such inverter");
return;
}
char invid[5];
for(int i=10; i<15; i++) { invid[i-10] = txBuffer[i]; }
ws.textAll("edit inverter " + String(kz));
ws.textAll("id = " + String(invid));
strncpy(Inv_Prop[kz].invID, invid, 4);
String bestand = "/Inv_Prop" + String(kz) + ".str"; // /Inv_Prop0.str
writeStruct(bestand, kz); // save in SPIFFS
return;
} else
if (strncasecmp(txBuffer+3,"HEALTH",6) == 0) {
ws.textAll("check zb system");
actionFlag=44; // perform the healthcheck
return;
} else
// ************ test mosquitto *******************************
if (strncasecmp(txBuffer+3,"TESTMQTT",8) == 0) {
ws.textAll("test mosquitto");
actionFlag=49; // perform the healthcheck
return;
} else
if (strncasecmp(txBuffer+3,"CLEAR",5) == 0) {
ws.textAll("clearWindow");
return;
} else
if (strncasecmp(txBuffer+3,"REBOOT_INVERTER=",16) == 0) {
int kz = String(txBuffer[19]).toInt();
ws.textAll("reboot inverter " + String(kz));
if ( kz > inverterCount-1 )
{
ws.textAll("error, non-excisting inverter");
return;
}
actionFlag = 34;
return;
} else
if (strncasecmp(txBuffer+3,"FILES",5) == 0) {
//we do this in the loop
ws.textAll("listing files..\n");
actionFlag = 46;
return;
} else
// ********************** zigbee test new*****************************
if (strncasecmp(txBuffer+3,"ZBT=",4) == 0) {
ws.textAll("going to send a teststring, len=" + String(len));
//we do this in the loop
actionFlag = 45;
return;
} else
if (strncasecmp(txBuffer+3,"ERASE",5) == 0) {
ws.textAll("going to delete all inverter files");
String bestand;
for(int i=0; i<50; i++)
{
String bestand = "/Inv_Prop" + String(i) + ".str";
if (SPIFFS.exists(bestand))
{
SPIFFS.remove(bestand);
ws.textAll("removed file " + bestand);
}
}
inverterCount = 0;
basisConfigsave(); // save inverterCount
ws.textAll("done");
return;
} else
if (strncasecmp(txBuffer+3,"DELETE=",7) == 0) {
//input can be 10;DELETE=filename
String bestand="";
for(int i=10; i<len+1; i++) { bestand += String(txBuffer[i]); }
ws.textAll("bestand = " + bestand);
if (SPIFFS.exists(bestand))
{
ws.textAll("going to delete file " + bestand);
SPIFFS.remove(bestand);
ws.textAll("file " + bestand + " removed!");
if(bestand.indexOf("/Inv_Prop") != -1) {
consoleOut("we deleted an inverterfile");
inverterCount -= 1;
basisConfigsave(); // save inverterCount
remove_gaps();
}
} else
{
ws.textAll("no such file");
}
return;
} else
if (strncasecmp(txBuffer+3, "DIAG",4) == 0) // normal operation
{
switch(diagNose) {
case 0:
diagNose = 1;
break;
case 1:
diagNose = 2;
break;
case 2:
diagNose = 0;
break;
}
ws.textAll("set diagnose to " + String(diagNose) );
write_eeprom();
return;
// ****************************************************************
} else
if (strncasecmp(txBuffer+3, "INIT_N",6) == 0) // normal operation
{
ws.textAll("command = " + String(txBuffer) );
actionFlag = 21;
diagNose=true;
return;
// ***************************************************************
// } else
//
// if (strncasecmp(txBuffer+3, "INIT_P",6) == 0) // pairing
// {
// ws.textAll("command = " + String(txBuffer) );
// actionFlag = 22;
#ifdef TEST
} else
if (strncasecmp(txBuffer+3, "TESTINV",7) == 0)
{
ws.textAll("command = " + String(txBuffer) );
// which = String(txBuffer[10]).toInt();
// ws.textAll("chosen = " + String(which) );
actionFlag = 122;
#endif
} else {
ws.textAll("unknown command");
}
}
}
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
void *arg, uint8_t *data, size_t len) {
//Serial.println("onEvent triggered");
switch (type) {
case WS_EVT_CONNECT:
//Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
break;
case WS_EVT_DISCONNECT:
//Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
//Serial.println("WebSocket received data");
handleWebSocketMessage(arg, data, len);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
void initWebSocket() {
ws.onEvent(onEvent);
server.addHandler(&ws);
}