-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathairconditioner.js
146 lines (127 loc) · 4.36 KB
/
airconditioner.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
class AirConditioner{
constructor(log, name,zone, client) {
this.log = log;
this.name = name;
this.zone = zone;
this.currentTemperature = -22;
this.currentDeviceState = 'Off';
this.client = client
}
//this is the loop that updates the Current value of the temp for the homekit service to return (in get Services)
startReading() {
const callback = () => {
setTimeout(() => this.getReading(callback), 50000);
};
this.getReading(callback);
}
getReading(callback) {
//here is wher we go off and read the value.
this.client.getZoneTemp(this.zone).then( temperature =>{
this.currentTemperature = temperature;
this.acService.setCharacteristic(Characteristic.CurrentTemperature, this.currentTemperature);
this.log.info(`Current temparture is ${this.currentTemperature} in zone ${this.zone}`);
return callback();
})
.catch(err =>{
this.log.error(`getreading error :: ${err}`);
});
}
getActive(callback) {
this.client.getState().then( state => {
this.log(`Current state is ${state}`);
const isActive = state === 'on';
return callback(null, isActive);
})
.catch(err=>{
this.log.error(`getActive error : ${err}`);
});
}
setActive(isActive, callback){
this.log("in is active");
this.log(isActive);
if(isActive)
{
this.log(`Turning AC on`);
this.client.setOn().then(result =>
{
this.log(`AC turned on`);
this.acService.getCharacteristic(Characteristic.Active).updateValue(1);
return callback(result);
}
).catch( err =>{
this.log.error(`getActive error : ${err}`);
});
}
else{
this.log(`Turning AC off`);
this.client.setOff().then(result =>{
this.log('AC turned off');
this.acService.getCharacteristic(Characteristic.Active).updateValue(0);
return callback(result);
}).catch( err =>{
this.log.error(`getActive error : ${err}`);
})
}
}
getCurrentState(callback){
this.log(`get heating cooling state`)
callback(null,0);
}
setCurrentState(state,callback)
{
callback(null,state);
}
getCurrentHeatingThreshold(callback){
this.log(`get heating threshold`)
this.client.getZoneTarget(this.zone).then(result =>{
this.log(`getZoneTemperature : ${result}`);
return callback(null,result);
})
}
//sets the current heating target temperature
setCurrentHeatingThreshold(state,callback){
this.log(`set heating threshold ${state}`)
this.client.setZoneTarget(this.zone,state).then(result=>{
this.log(`setting zone ${this.zone} to ${state}`)
return callback(null,state);
})
}
getServices() {
// this.log(`in Get Services for zone ${Service}`);
let informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "My AirCon")
.setCharacteristic(Characteristic.Model, "iZone3")
.setCharacteristic(Characteristic.SerialNumber, "123-456-789");
this.acService = new Service.HeaterCooler(this.name);
this.acService
.getCharacteristic(Characteristic.Active)
.on('get', this.getActive.bind(this))
.on('set', this.setActive.bind(this));
this.acService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', (callback) => {
callback(null, this.currentTemperature);
});
this.acService
.getCharacteristic(Characteristic.TargetHeaterCoolerState)
.on('get', this.getCurrentState.bind(this))
.on('set', this.setCurrentState.bind(this));
this.acService
.getCharacteristic(Characteristic.HeatingThresholdTemperature)
.setProps({
minValue: 16,
maxValue: 30,
minStep: .5
})
.on('get',this.getCurrentHeatingThreshold.bind(this))
.on('set',this.setCurrentHeatingThreshold.bind(this))
this.acService
.getCharacteristic(Characteristic.Name)
.on('get', callback => {
callback(null, this.name);
});
return this.acService;
}
}
module.exports = AirConditioner;