-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (84 loc) · 3.08 KB
/
index.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
const AppKettleClient = require('./lib/AppKettleClient.js');
module.exports = (api) => {
api.registerAccessory('AppKettle', AppKettle);
}
var MyKettle = {"cmd": "unknown",
"status": "unknown",
"keep_warm_sec": 0,
"keep_warm": false,
"current_temp": 0,
"target_temp": 0,
"set_temp": 0,
"volume": 0,
"power": "OFF",
"seq": 0
}
class AppKettle {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.kettleIP = config.ip;
this.kettlePort = config.port;
MyKettle.target_temp = config.temp
MyKettle.keep_warm = config.keepwarm
MyKettle.keep_warm_sec = config.keepwarmsec
if ((this.kettlePort == null) || (this.kettlePort == null)){
this.log.error("AppKettle will not be initialized due to a configuration error. Check IP and Port in the config");
return
}
this.log.debug('AppKettle Accessory Plugin Loaded: ', MyKettle);
// your accessory must have an AccessoryInformation service
this.informationService = new this.api.hap.Service.AccessoryInformation()
.setCharacteristic(this.api.hap.Characteristic.Manufacturer, "Custom Manufacturer")
.setCharacteristic(this.api.hap.Characteristic.Model, "Custom Model");
// create a new "Switch" service
this.switchService = new this.api.hap.Service.Switch(this.name);
// link methods used when getting or setting the state of the service
this.switchService.getCharacteristic(this.api.hap.Characteristic.On)
.onGet(this.getOnHandler.bind(this)) // bind to getOnHandler method below
.onSet(this.setOnHandler.bind(this)); // bind to setOnHandler method below
const apk = this.apk = new AppKettleClient(this.log,this.kettleIP,this.kettlePort);
apk.on('connected', this._apkConnected.bind(this))
apk.on('kettleState', this._updateKettleState.bind(this))
this.apk.connect();
}
_apkConnected () {
this.log('AppKettle Connected!')
}//_apkConnected
_updateKettleState (newkettlestate){
this.log.debug("Before:",MyKettle);
MyKettle.status = newkettlestate.status;
MyKettle.power = newkettlestate.power;
MyKettle.seq = newkettlestate.seq;
this.log.debug("New:",MyKettle);
this.log('AppKettle state changed outside HomeKit: ', MyKettle.power)
const powerState = (MyKettle.power == "ON") ? true : false;
this.switchService.getCharacteristic(this.api.hap.Characteristic.On)
.updateValue(powerState);
this.log.debug('AppKettle state changed outside HomeKit: ', powerState)
}
getServices() {
//Return an array of the services to expose.
return [
this.informationService,
this.switchService,
];
}
async getOnHandler() {
this.log.info('Getting AppKettle state: ',MyKettle.power);
const powerState = (MyKettle.power == "ON") ? true : false;
return powerState;
}
async setOnHandler(value) {
this.log.info('Setting switch state to:', value);
if (value) {
//Turn on the Kettle
this.log.debug("Turn Kettle On: ",MyKettle);
this.apk.turn_on(MyKettle.target_temp,MyKettle.keep_warm_sec, MyKettle.keep_warm);
}else {
//Turn off the kettle
this.apk.turn_off();
}
}
}