-
Notifications
You must be signed in to change notification settings - Fork 2
/
landroid2domoticz.js
74 lines (64 loc) · 2.63 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
const config = require('./config'); // Read configuration
// Import project modules
const Landroids = require('./landroid');
const LandroidState = Landroids.LandroidState;
const Domoticz = require('./domoticz');
const AlertLevel = Domoticz.AlertLevel;
const domoticz = new Domoticz(config);
const mqttBrokerUrl = config.mqttBrokerUrl;
if(mqttBrokerUrl && mqttBrokerUrl.indexOf("//") > 0) {
let address = mqttBrokerUrl.substring(mqttBrokerUrl.indexOf("//") + 2);
let 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");
const landroids = new Landroids(config);
domoticz.connect(function () { // Connect to MQTT
console.log("Scheduling polling");
landroids.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!");
}
});
});
});