forked from fredan75/worx-landroid-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
landroid2domoticz.js
74 lines (64 loc) · 2.61 KB
/
landroid2domoticz.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
var config = require('./config'); // Read configuration
// Import project modules
var Landroid = require('./landroid');
var LandroidState = Landroid.LandroidState;
var Domoticz = require('./domoticz');
var AlertLevel = Domoticz.AlertLevel;
var domoticz = new Domoticz(config);
var mqttBrokerUrl = config.mqttBrokerUrl;
if(mqttBrokerUrl && mqttBrokerUrl.indexOf("//") > 0) {
var address = mqttBrokerUrl.substring(mqttBrokerUrl.indexOf("//") + 2);
var port = 1883; // Assume default MQTT port
if(address.indexOf(":") > 0) {
var colon = address.indexOf(":");
port = parseInt(address.substring(colon + 1));
address = address.substring(0, colon);
}
domoticz.initHardware("MQTT for Landroid", address, port);
}
else
console.error("Cannot automatically create Domoticz hardware for MQTT URL: " + mqttBrokerUrl);
domoticz.initDevices(function() { // Detect or auto create devices
console.info("You can now navigate to " + config.domoticzUrl + "/#/Utility to see your Landroid status");
var landroid = new Landroid(config);
domoticz.connect(function () { // Connect to MQTT
console.log("Scheduling polling");
landroid.pollEvery(60, function(status) { // Poll every 60 seconds
if(status) { // We got some data back from the Landroid
// Send data to Domoticz
domoticz.setNoOfAlarms(status.noOfAlarms);
domoticz.setBatteryPercentage(status.batteryPercentage);
domoticz.setTotalMowingHours(status.totalMowingHours);
switch (status.state) {
case LandroidState.ALARM:
domoticz.setAlert(AlertLevel.RED, status.errorMessage ? status.errorMessage : "[Alarm]");
break;
case LandroidState.CHARGING:
domoticz.setAlert(AlertLevel.GREEN, "Charging");
break;
case LandroidState.CHARGING_COMPLETE:
domoticz.setAlert(AlertLevel.GRAY, "Charging complete");
break;
case LandroidState.MOWING:
domoticz.setAlert(AlertLevel.YELLOW, "Mowing");
break;
case LandroidState.GOING_HOME:
domoticz.setAlert(AlertLevel.YELLOW, "Going home");
break;
case LandroidState.MANUAL_STOP:
domoticz.setAlert(AlertLevel.ORANGE, "Manual stop");
break;
case LandroidState.ERROR:
domoticz.setAlert(AlertLevel.ORANGE, "ERROR!");
break;
default:
console.error("Unknown state: " + status.state);
}
}
else {
domoticz.setError("Error getting update!");
console.error("Error getting update!");
}
});
});
});