forked from nicoduj/homebridge-deebotEcovacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeebotEcovacsAPI.js
205 lines (170 loc) · 7.33 KB
/
deebotEcovacsAPI.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
const ecovacsDeebot = require('ecovacs-deebot'),
nodeMachineId = require('node-machine-id'),
countries = ecovacsDeebot.countries,
EcoVacsAPI = ecovacsDeebot.EcoVacsAPI;
var EventEmitter = require('events');
var inherits = require('util').inherits;
module.exports = {
DeebotEcovacsAPI: DeebotEcovacsAPI,
};
function DeebotEcovacsAPI(log, platform) {
EventEmitter.call(this);
this.log = log;
this.platform = platform;
this.login = platform.login;
this.countryCode = platform.countryCode.toUpperCase();
this.device_id = EcoVacsAPI.md5(nodeMachineId.machineIdSync());
this.password_hash = EcoVacsAPI.md5(platform.password);
this.continent = countries[this.countryCode].continent.toUpperCase();
this.log('INFO - API :' + this.continent + '/' + this.countryCode);
this.api = new EcoVacsAPI(this.device_id, this.countryCode, this.continent);
this.vacbots = [];
}
DeebotEcovacsAPI.prototype = {
getDeebots: function () {
this.api
.connect(this.login, this.password_hash)
.then(() => {
this.log.debug('INFO - connected');
this.api.devices().then((devices) => {
this.log.debug('INFO - getDeebots :', JSON.stringify(devices));
for (let s = 0; s < devices.length; s++) {
let vacuum = devices[s]; // Selects the first vacuum from your account
let vacbot = this.api.getVacBot(
this.api.uid,
EcoVacsAPI.REALM,
this.api.resource,
this.api.user_access_token,
vacuum,
this.continent
);
this.vacbots.push(vacbot);
}
this.emit('deebotsDiscovered');
});
})
.catch((e) => {
// The Ecovacs API endpoint is not very stable, so
// connecting fails randomly from time to time
this.log('ERROR - Failure in connecting to ecovacs to retrieve your deebots! - ' + e);
this.emit('errorDiscoveringDeebots');
});
},
configureEvents(
vacBot,
HKBatteryService,
HKFanService,
HKSwitchOnService,
HKMotionService,
deebotAccessory
) {
var Characteristic = this.platform.api.hap.Characteristic;
vacBot.on('ready', (event) => {
this.log.debug('INFO - Vacbot ready: ' + JSON.stringify(event));
vacBot.run('GetCleanState');
vacBot.run('GetBatteryState');
vacBot.run('GetChargeState');
vacBot.run('GetCleanSpeed');
if (vacBot.orderToSend && vacBot.orderToSend !== undefined) {
this.log('INFO - sendingCommand ' + vacBot.orderToSend);
if (vacBot.orderToSend instanceof Array) {
vacBot.run.apply(vacBot, orderToSend);
} else {
vacBot.run(vacBot.orderToSend);
}
vacBot.orderToSend = undefined;
}
});
vacBot.on('BatteryInfo', (battery) => {
let batteryLevel = this.platform.getBatteryLevel(battery);
let currentValue = HKBatteryService.getCharacteristic(Characteristic.BatteryLevel).value;
this.log.debug('INFO - Battery level: %d %d %d', battery, batteryLevel, currentValue);
if (currentValue !== batteryLevel) {
HKBatteryService.getCharacteristic(Characteristic.BatteryLevel).updateValue(batteryLevel);
if (batteryLevel < 20)
HKBatteryService.getCharacteristic(Characteristic.StatusLowBattery).updateValue(1);
else HKBatteryService.getCharacteristic(Characteristic.StatusLowBattery).updateValue(0);
}
});
vacBot.on('ChargeState', (charge_status) => {
let charging = charge_status == 'charging';
let idle = charge_status == 'idle';
let returning = charge_status == 'returning';
this.log.debug('INFO - Charge status: %s %s %s', charge_status, idle, charging);
let currentValue = HKBatteryService.getCharacteristic(Characteristic.ChargingState).value;
if (currentValue !== charging) {
HKBatteryService.getCharacteristic(Characteristic.ChargingState).updateValue(charging);
}
if (deebotAccessory.publishFan && HKFanService) {
let currentOnValue = HKFanService.getCharacteristic(Characteristic.On).value;
if (charging && currentOnValue) {
HKFanService.getCharacteristic(Characteristic.On).updateValue(false);
} else if (returning && !currentOnValue) {
HKFanService.getCharacteristic(Characteristic.On).updateValue(true);
}
}
if (deebotAccessory.publishSwitch && HKSwitchOnService) {
let currentMainOnValue = HKSwitchOnService.getCharacteristic(Characteristic.On).value;
if (charging && currentMainOnValue) {
HKSwitchOnService.getCharacteristic(Characteristic.On).updateValue(false);
} else if (idle && !currentMainOnValue) {
HKSwitchOnService.getCharacteristic(Characteristic.On).updateValue(true);
}
}
});
vacBot.on('CleanReport', (clean_status) => {
this.log.debug('INFO - Clean status: %s', clean_status);
if (clean_status) {
let cleaning = clean_status != 'stop' && clean_status != 'pause' && clean_status != 'idle';
if (deebotAccessory.publishFan && HKFanService) {
let currentOnValue = HKFanService.getCharacteristic(Characteristic.On).value;
if (currentOnValue !== cleaning) {
HKFanService.getCharacteristic(Characteristic.On).updateValue(cleaning);
}
let currentDirectionValue = HKFanService.getCharacteristic(
Characteristic.RotationDirection
).value;
if (
clean_status == deebotAccessory.leftDirectionCleaningMode &&
currentDirectionValue == 0
) {
HKFanService.getCharacteristic(Characteristic.RotationDirection).updateValue(0);
} else if (
clean_status != deebotAccessory.leftDirectionCleaningMode &&
currentDirectionValue == 1
) {
HKFanService.getCharacteristic(Characteristic.RotationDirection).updateValue(1);
}
}
if (deebotAccessory.publishSwitch && HKSwitchOnService) {
let currentMainOnValue = HKSwitchOnService.getCharacteristic(Characteristic.On).value;
if (cleaning && !currentMainOnValue)
HKSwitchOnService.getCharacteristic(Characteristic.On).updateValue(true);
}
}
});
vacBot.on('CleanSpeed', (clean_speed) => {
if (deebotAccessory.publishFan && HKFanService) {
let currentSpeedValue = HKFanService.getCharacteristic(Characteristic.RotationSpeed).value;
let deebotSpeed = this.platform.getCleanSpeed(currentSpeedValue);
this.log.debug('INFO - Clean speed : %s - %s', clean_speed, deebotSpeed);
if (deebotSpeed !== clean_speed) {
let newSpeed = this.platform.getFanSpeed(clean_speed);
HKFanService.getCharacteristic(Characteristic.RotationSpeed).updateValue(newSpeed);
}
}
});
vacBot.on('Error', (error_message) => {
this.log.debug('INFO - Error from deebot : %s ', error_message);
if (deebotAccessory.publishMotionDetector && HKMotionService) {
if (error_message)
HKMotionService.getCharacteristic(Characteristic.MotionDetected).updateValue(true);
}
});
vacBot.on('message', (message) => {
this.log.debug('INFO - Message from deebot : %s ', message);
});
if (!vacBot.is_ready) vacBot.connect_and_wait_until_ready();
},
};
inherits(DeebotEcovacsAPI, EventEmitter);