-
Notifications
You must be signed in to change notification settings - Fork 2
/
landroid.js
212 lines (185 loc) · 6.2 KB
/
landroid.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
/*
This module handles the communication with the Worx Landroid robotic mower
*/
const http = require('http');
const LandroidState = {
CHARGING: "Charging",
CHARGING_COMPLETE: "Charging complete",
MOWING: "Mowing",
GOING_HOME: "Going home",
MANUAL_STOP: "Manual stop",
ALARM: "Alarm",
ERROR: "Error"
};
const WIRE_BOUNCED_ALARM_INDEX = 2;
const ERROR_MESSAGES = [];
ERROR_MESSAGES[0] = "Blade blocked";
ERROR_MESSAGES[1] = "Repositioning error";
ERROR_MESSAGES[WIRE_BOUNCED_ALARM_INDEX] = "Wire bounced";
ERROR_MESSAGES[3] = "Blade blocked";
ERROR_MESSAGES[4] = "Outside wire";
ERROR_MESSAGES[5] = "Mower lifted";
ERROR_MESSAGES[6] = "Alarm 6";
ERROR_MESSAGES[7] = "Upside down";
ERROR_MESSAGES[8] = "Alarm 8";
ERROR_MESSAGES[9] = "Collision sensor blocked";
ERROR_MESSAGES[10] = "Mower tilted";
ERROR_MESSAGES[11] = "Charge error";
ERROR_MESSAGES[12] = "Battery error";
class Landroids {
constructor(config) {
this.localLandroids = config.localLandroids.map(localLandroid => new LocalLandroid(localLandroid));
}
pollEvery(seconds, updateListener) {
this.localLandroids.forEach(localLandroid => localLandroid.pollEvery(seconds, updateListener));
}
}
/**
* Constructor that takes configuration as arguments
* @param config
* @constructor
*/
class LocalLandroid {
constructor(config) {
this.name = config.name;
this.landroidUrl = config.url;
this.pinCode = config.pinCode;
}
doPollStatus() {
console.log("About to poll Landroid at " + this.landroidUrl + " for status");
const self = this;
http.get(this.landroidUrl + "/jsondata.cgi", {
auth: "admin:" + this.pinCode,
// method: 'POST', // TODO ?
// headers: {
// 'Content-Type': 'application/json'
// },
timeout: 10 * 1000 // 10 s
}, (res) => {
const {statusCode} = res;
let success = statusCode === 200;
if(!success) {
console.log(`HTTP status: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
// Consume response data to free up memory
res.resume();
}
res.setEncoding('utf8'); // TODO
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const response = JSON.parse(rawData);
let status = null;
if(response) {
if(!response.allarmi) { // Response is not what we expected
console.error("Make sure your pin code is correct!");
}
else {
status = {
state: null,
errorMessage: null,
batteryPercentage: response.perc_batt,
totalMowingHours: null,
noOfAlarms: countAlarms(response.allarmi),
workingTimePercent: response.percent_programmatore
};
const totalMowingHours = parseInt(response.ore_movimento);
if(!isNaN(totalMowingHours)) {
status.totalMowingHours = totalMowingHours / 10; // Provided as 0,1 h
}
if(status.noOfAlarms > 0) {
status.state = LandroidState.ALARM;
status.errorMessage = alertArrayToMessage(response.allarmi);
}
else { // There were no alarms
if(response.settaggi[14]) {
status.state = LandroidState.MANUAL_STOP;
}
else if(response.settaggi[5] && !response.settaggi[13]) {
status.state = LandroidState.CHARGING;
}
else if(response.settaggi[5] && response.settaggi[13]) {
status.state = LandroidState.CHARGING_COMPLETE;
}
else if(response.settaggi[15]) {
status.state = LandroidState.GOING_HOME;
}
else
status.state = LandroidState.MOWING;
}
}
}
else
console.error("No response!");
console.log(" Landroid status: " + JSON.stringify(status));
if(self.updateListener)
self.updateListener(status, self);
}
catch (e) { // Error JSON parsing
console.error(e.message);
}
});
}).on('error', (e) => {
console.error(`Problem with request: ${e.message}`);
console.error("Error communicating with Landroid!");
if(self.updateListener)
self.updateListener(null, self);
}).on('timeout', () => {
console.error("Timeout communicating with Landroid!");
if(self.updateListener)
self.updateListener(null, self);
});
}
/**
* Start polling the Landroid for status every n seconds. First poll will be triggered before function returns.
* @param seconds No of senconds between poll.
* @param updateListener function to be called when there are updates
*/
pollEvery(seconds, updateListener) {
this.updateListener = updateListener;
this.doPollStatus(); // First poll immediately
const self = this;
setInterval(function() { self.doPollStatus() }, seconds * 1000);
}
//////////////////////////////////////////////////////////////////////////////////
// These handlers are supposed to be overridden
setError(error) {
console.log("Error: " + error);
}
}
/** Get alert messages from array of flags */
function alertArrayToMessage(arr) {
let output = "";
if(arr && arr.length > 0) {
for(let i = 0; i < arr.length; i++) {
if(arr[i] && i != WIRE_BOUNCED_ALARM_INDEX) { // There was an alert (ignore wire bounce)
var errorMessage = ERROR_MESSAGES[i];
if(errorMessage) {
if(output) { // Not first error (empty string is falsy) - insert delimiter
output += "; ";
}
output += errorMessage;
}
}
}
}
return output;
}
/** Is there any alert in the provided array? */
function countAlarms(arr) {
let output = 0;
if(arr) {
for(let i = 0; i < arr.length; i++) { // Length should be 32
if(i != WIRE_BOUNCED_ALARM_INDEX) { // Ignore wire bounce
output += arr[i];
}
}
}
return output;
}
module.exports = Landroids;
// Expose LandroidState
module.exports.LandroidState = LandroidState;