forked from fredan75/worx-landroid-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
landroid.js
174 lines (152 loc) · 5.39 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
/*
This module handles the communication with the Worx Landroid robotic mower
*/
// https://github.com/najaxjs/najax
var najax = require('najax');
var LandroidState = {
CHARGING: "Charging",
CHARGING_COMPLETE: "Charging complete",
MOWING: "Mowing",
GOING_HOME: "Going home",
MANUAL_STOP: "Manual stop",
ALARM: "Alarm",
ERROR: "Error"
};
var WIRE_BOUNCED_ALARM_INDEX = 2;
var 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";
/**
* Constructor that takes configuration as arguments
* @param config
* @constructor
*/
function Landroid(config) {
this.landroidUrl = config.landroidUrl;
this.pinCode = config.pinCode;
}
Landroid.prototype.doPollStatus = function() {
console.log("About to poll Landroid at " + this.landroidUrl + " for status");
var self = this;
najax({
url: this.landroidUrl + "/jsondata.cgi",
dataType: "json", // will be "application/json" in version najax 0.2.0
username: "admin",
password: this.pinCode,
success: function(response) {
var 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: null,
totalMowingHours: null,
noOfAlarms: null
};
status.batteryPercentage = response.perc_batt;
// console.log("Response from Landroid, batteryPercentage: " + batteryPercentage);
status.workingTimePercent = response.percent_programmatore;
var totalMowingHours = parseInt(response.ore_movimento);
if(! isNaN(totalMowingHours)) {
status.totalMowingHours = totalMowingHours / 10; // Provided as 0,1 h
}
status.noOfAlarms = countAlarms(response.allarmi);
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);
},
error: function (response) {
console.error("Error communicating with Landroid!");
if(self.updateListener)
self.updateListener(null);
}
});
};
/** Get alert messages from array of flags */
function alertArrayToMessage(arr) {
var output = "";
if(arr && arr.length > 0) {
for(var 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) {
var output = 0;
if(arr) {
for(var i = 0; i < arr.length; i++) { // Length should be 32
if(i != WIRE_BOUNCED_ALARM_INDEX) { // Ignore wire bounce
output += arr[i];
}
}
}
return output;
}
/**
* 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
*/
Landroid.prototype.pollEvery = function (seconds, updateListener) {
this.updateListener = updateListener;
this.doPollStatus(); // First poll immediately
var self = this;
setInterval(function() { self.doPollStatus() }, seconds * 1000);
};
//////////////////////////////////////////////////////////////////////////////////
// These handlers are supposed to be overridden
Landroid.prototype.setError = function (error) {
console.log("Error: " + error);
};
module.exports = Landroid;
// Expose LandroidState
module.exports.LandroidState = LandroidState;