-
Notifications
You must be signed in to change notification settings - Fork 3
/
LandroidSrv.js
499 lines (428 loc) · 12.9 KB
/
LandroidSrv.js
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// --------------------------------------------------------------------------------------------
// Landroid Node.js Web Server
// version 1.7
// --------------------------------------------------------------------------------------------
"use strict";
var http = require('http');
var url = require('url');
var fs = require('fs');
const worx = require('./node_modules/iobroker.worx/lib/api');
var LandroidConf = require('./LandroidConf.json');
var config;
var worxCloud;
var data;
var pingTimeout = null;
var connected = false;
var WSRunning = false;
var server;
var mowerId;
var edgeCuttingTrig = false;
var oMower;
// Get Mower ID
function getMoverId(){
var mowerId = process.argv[2]; //We only expect one value
return mowerId;
}
// Build Timestamp
function getTimestamp() {
var date = new Date();
return date.toUTCString();
}
// Start Landroid web server
function startWebServer(){
server = http.createServer(function(request, response){
console.log(getTimestamp() + " --> Landroid WebServer: Request recieved");
// Get HTTP request parts
var parts = url.parse(request.url, true);
// Get HTTP path
var path = parts.pathname;
// Get HTPP parameters
var query = parts.query;
switch(path){
case "/getMessage":
var jsonData = JSON.stringify(data);
response.writeHead(200, {"Content-Type": "text/plain"});
response.end(jsonData);
console.log(getTimestamp() + " --> Landroid WebServer: Response send");
break;
case "/startMower":
fireCmd(1,query.value,response);
break;
case "/stopMower":
fireCmd(2,query.value,response);
break;
case "/changeCfgCalendar":
fireCmd(3,query.value,response);
break;
case "/changeCfgTimeExtend":
fireCmd(4,query.value,response);
break;
case "/changeCfgArea":
fireCmd(5,query.value,response);
break;
case "/startSequences":
fireCmd(6,query.value,response);
break;
case "/changeRainDelay":
fireCmd(7,query.value,response);
break;
case "/pauseMower":
fireCmd(8,query.value,response);
break;
case "/edgeCutting":
fireCmd(9,query.value,response);
break;
default:
response.writeHead(500, {"Content-Type": "text/plain"});
response.end("Invalid path: " + path);
console.log(getTimestamp() + " --> Landroid WebServer: Invalid path received: " + path);
}
}).listen(LandroidConf[mowerId].port);
console.log(getTimestamp() + " --> Landroid WebServer: server initialized");
}
function fireCmd(cmdCode, value, response){
var cmdStatus;
var httpCode;
// Handle command
switch(cmdCode){
case 1:
cmdStatus = startMower();
break;
case 2:
cmdStatus = stopMower();
break;
case 3:
cmdStatus = changeCfgCalendar(value);
break;
case 4:
cmdStatus = changeCfgTimeExtend(value);
break;
case 5:
cmdStatus = changeCfgArea(value);
break;
case 6:
cmdStatus = startSequences(value);
break;
case 7:
cmdStatus = changeRainDelay(value);
break;
case 8:
cmdStatus = pauseMower();
break;
case 9:
cmdStatus = edgeCutting();
break;
}
// Check result
if (cmdStatus.cmdState){
httpCode = "200";
}
else {
httpCode = "500";
}
// Send HTTP response
response.writeHead(httpCode, {"Content-Type": "text/plain"});
response.end(cmdStatus.msg);
console.log(getTimestamp() + " --> " + cmdStatus.msg);
}
function startMower() {
var cmdStatus = { cmdState: false, msg: "" };
var state = (data.dat && data.dat.ls ? data.dat.ls : 0);
var error = (data.dat && data.dat.le ? data.dat.le : 0);
if ((state === 1 || state === 34) && error == 0) {
// Fire MQTT Message
worxCloud.sendMessage('{"cmd":1}', oMower.serial); //start code for mower
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "Mower has been started";
return cmdStatus;
}
else {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Can not start mover because he is not at home or he has an issue, please take a look at the mover";
return cmdStatus;
}
}
function stopMower() {
var cmdStatus = { cmdState: false, msg: "" };
var state = (data.dat && data.dat.ls ? data.dat.ls : 0);
var error = (data.dat && data.dat.le ? data.dat.le : 0);
if ((state === 7 || state === 34) && error == 0) {
// Fire MQTT Message
worxCloud.sendMessage('{"cmd":3}', oMower.serial); //"Back to home" code for mower
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "Mower has been stopped, Mower going back home";
return cmdStatus;
}
else {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Can not stop mover, because he is not mowing or he has an issue, please take a look at the mover";
return cmdStatus
}
}
function pauseMower() {
var cmdStatus = { cmdState: false, msg: "" };
// Fire MQTT Message
worxCloud.sendMessage('{"cmd":2}', oMower.serial); //pause code for mower
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "Mower has been paused";
return cmdStatus;
}
function edgeCutting() {
var cmdStatus = { cmdState: false, msg: "" };
//Edge cutting for mower
//--> Start zone training (cmd:4)
//--> wait 3 sec
//--> Pause Mower (cmd:2)
//--> wait 3 sec
//--> Stop Mower (cmd:3)
// Fire MQTT Message
worxCloud.sendMessage('{"cmd":4}', oMower.serial); //Start zone training
edgeCuttingTrig = true;
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "Edge cutting has been triggered";
return cmdStatus;
}
function changeCfgCalendar(value) {
var cmdStatus = { cmdState: false, msg: "" };
var val = value.split(",");
var message = data.cfg.sc.d; // set actual values
var dayId = val[0];
var startTime;
var workTime;
var borderCut;
try {
// Validate weekday
if (isNaN(dayId) || dayId < 0 || dayId > 6){
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Weekday ID must be between 0 (Sunday) and 6 (Saturday)";
return cmdStatus;
}
// Validate and set start time
var h = val[1].split(':')[0];
var m = val[1].split(':')[1];
if (h >= 0 && h <= 23 && m >= 0 && m <= 59) {
startTime = val[1];
}
else {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Start-Time is out of range: e.g 10:00";
return cmdStatus;
}
// Validate and set work time
if (val[2] >= 0 && val[2] <= 720) {
workTime = parseInt(val[2]);
}
else {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Work-Time is out of range: 0 min > time < 720 min.";
return cmdStatus;
}
// Validate and set border cut
if (val[3] == 1 || val[3] == 0){
borderCut = parseInt(val[3]);
}
else{
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Border cut value must be 0 or 1";
return cmdStatus;
}
}
catch (e) {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Error while setting mowers config: " + e;
return cmdStatus;
}
// Set and send new config
message[dayId][0] = startTime;
message[dayId][1] = workTime;
message[dayId][2] = borderCut;
// Fire MQTT Message
worxCloud.sendMessage('{"sc":{"d":' + JSON.stringify(message) + '}}', oMower.serial);
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "Mow time changed to: " + JSON.stringify(message);
return cmdStatus;
}
function changeCfgTimeExtend(value) {
var cmdStatus = { cmdState: false, msg: "" };
var val = parseInt(value);
var message = data.cfg.sc; // set actual values
// Validate and set Mower Time Extend
if (!isNaN(val) && val >= -100 && val <= 100) {
message.p = val;
// Fire MQTT Message
worxCloud.sendMessage('{"sc":' + JSON.stringify(message) + '}', oMower.serial);
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "MowerTimeExtend set to : " + message.p;
return cmdStatus;
}
else {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "MowerTimeExtend must be a value between -100 and 100";
return cmdStatus;
}
}
function changeCfgArea(value) {
var cmdStatus = { cmdState: false, msg: "" };
var val = value.split(",");
var message = data.cfg.mz; // set actual values
var areaID = val[0];
val[1] = parseInt(val[1]);
try {
if (!isNaN(val[1]) && val[1] >= 0 && val[1] <= 500) {
message[areaID] = val[1];
// Fire MQTT Message
worxCloud.sendMessage('{"mz":' + JSON.stringify(message) + '}', oMower.serial);
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "Change Area " + (areaID + 1) + " : " + JSON.stringify(message);
return cmdStatus;
}
else {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Area Value ist not correct, please type in a val between 0 and 500";
return cmdStatus;
}
}
catch (e) {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Error while setting mowers areas: " + e;
return cmdStatus;
}
}
function startSequences(value) {
var cmdStatus = { cmdState: false, msg: "" };
var val = value;
var seq = [];
try {
seq = JSON.parse("[" + val + "]");
for (var i = 0; i < 10; i++) {
if (seq[i] != undefined) {
seq[i] = parseInt(seq[i]);
if (isNaN(seq[i]) || seq[i] < 0 || seq[i] > 3) {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Wrong start sequence, for val " + i + " , please type in a val between 0 and 3";
return cmdStatus;
}
}
else {
// Array ist too short, filling up with start point 0
seq[i] = 0;
}
}
// Fire MQTT Message
worxCloud.sendMessage('{"mzv":' + JSON.stringify(seq) + '}', oMower.serial);
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "new Array is: " + JSON.stringify(seq);
return cmdStatus;
}
catch (e) {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "Error while setting start sequence: " + e;
return cmdStatus;
}
}
function changeRainDelay(value) {
var cmdStatus = { cmdState: false, msg: "" };
var val = parseInt(value);
var message = data.cfg.rd; // set actual values
// Validate and set Mower rain delay time in minutes
if (!isNaN(val) && val >= 0 && val <= 300) {
message = val;
// Fire MQTT Message
worxCloud.sendMessage('{"rd":' + JSON.stringify(message) + '}', oMower.serial);
// Set return status
cmdStatus.cmdState = true;
cmdStatus.msg = "MowerRainDelay set to : " + message;
return cmdStatus;
}
else {
// Set return status
cmdStatus.cmdState = false;
cmdStatus.msg = "MowerRainDelay must be a value between 0 and 300";
return cmdStatus;
}
}
function edgeCuttingHandler() {
//Check for edge cutting mode
if(!edgeCuttingTrig){
return;
}
//Get actual mower state
var state = (data.dat && data.dat.ls ? data.dat.ls : 0);
if (state === 1 || state === 3){
console.log(getTimestamp() + " --> " + "Edgecut Start section :" + state);
} else if (state === 31) {
setTimeout(function(){
console.log(getTimestamp() + " --> " + "Edcut send cmd:2");
worxCloud.sendMessage('{"cmd":2}', oMower.serial); //Pause Mower
}, 3000);
} else if (state === 34) {
setTimeout(function(){
console.log(getTimestamp() + " --> " + "Edcut send cmd:3");
worxCloud.sendMessage('{"cmd":3}', oMower.serial); //Stop Mower (go home)
edgeCuttingTrig = false;
}, 3000);
} else if (state !== 31 && state !== 34) {
edgeCuttingTrig = false;
console.log(getTimestamp() + " --> " + "Something went wrong at edgeCutting");
}
}
function main() {
worxCloud = new worx(config.email, config.pwd);
worxCloud.on('connect', worxc => {
console.log(getTimestamp() + " --> " + "sucessfully connected!");
// Start Landroid web server
startWebServer();
});
worxCloud.on('found', function (mower) {
console.log(getTimestamp() + " --> " + 'found!' + JSON.stringify(mower));
//Set mower instance
oMower = mower;
});
worxCloud.on('mqtt', (mower, mower_data) => {
console.log(getTimestamp() + " --> New data arrived");
//Get data
data = mower_data;
//Run edge cutting handler
edgeCuttingHandler();
if (!WSRunning){
if (server.address() !== null){
WSRunning = true;
console.log(getTimestamp() + " --> Landroid WebServer: server running");
}
}
});
worxCloud.on('error', err => {
console.log(getTimestamp() + " --> 'ERROR: " + err);
});
}
// Retrieve mover ID
mowerId = getMoverId();
if (mowerId){
//Get landroid config
config = LandroidConf[mowerId];
// Establishh connection to MQTT Broker
main();
}
else{
console.log("Mower ID is missing!");
}