-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessory.js
67 lines (57 loc) · 1.69 KB
/
accessory.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
const { Service, Characteristic } = require('homebridge');
class MyAccessory {
constructor(log, mower) {
this.log = log;
this.mower = mower;
this.service = new Service.Switch(this.mower.name);
this.service.getCharacteristic(Characteristic.On)
.on('set', this.setOn.bind(this))
.on('get', this.getOn.bind(this));
this.batteryService = new Service.BatteryService(this.mower.name);
this.batteryService.getCharacteristic(Characteristic.BatteryLevel)
.on('get', this.getBatteryLevel.bind(this));
this.batteryService.getCharacteristic(Characteristic.ChargingState)
.on('get', this.getChargingState.bind(this));
}
async setOn(value, callback) {
try {
if (value) {
await this.mower.start();
} else {
await this.mower.stop();
}
callback();
} catch (error) {
callback(error);
}
}
async getOn(callback) {
try {
const status = await this.mower.getStatus();
callback(null, status.mainState === 6); // Assuming 6 indicates mowing
} catch (error) {
callback(error);
}
}
async getBatteryLevel(callback) {
try {
const status = await this.mower.getStatus();
callback(null, status.chargeLevel);
} catch (error) {
callback(error);
}
}
async getChargingState(callback) {
try {
const status = await this.mower.getStatus();
const isCharging = status.mainState === 1; // Assuming 1 indicates charging
callback(null, isCharging ? 1 : 0); // 1 means charging, 0 means not charging
} catch (error) {
callback(error);
}
}
getServices() {
return [this.service, this.batteryService];
}
}
module.exports = MyAccessory;