-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThermostat_M16_P7_accessory.ts
268 lines (249 loc) · 8.08 KB
/
Thermostat_M16_P7_accessory.ts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import {
Accessory,
AccessoryEventTypes,
Categories,
Characteristic,
CharacteristicEventTypes,
CharacteristicSetCallback,
CharacteristicValue,
CharacteristicGetCallback,
Service,
uuid,
VoidCallback
} from '..';
import http from "http";
type Options = {
host: string,
path: string
};
const opts : Options = {
host: 'http://192.168.0.16',
path: ''
};
class ThermoControllerClass {
CurrentHeatingCoolingState: CharacteristicValue = 0;
TargetHeatingCoolingState: CharacteristicValue = 0;
CurrentTemperature: CharacteristicValue = 50;
TargetTemperature: CharacteristicValue = 18;
name: CharacteristicValue = "Теплый пол";
getTargetTemperature() {
var data: string = "";
var temp: string = "";
var str: number = 0;
var request = http.get(opts.host + "/sec/?pt=33", function (res: any) {
res.on("data", function (chunk: any) {
data += chunk;
});
res.on("end", () => {
try {
str = data.indexOf("<input name=misc size=4 value=");
temp = data.substring(str + 30, 428);
if (Number(temp) > 0)
ThermoController.TargetTemperature = parseFloat(temp);
} catch (error) {
console.error(error);
}
});
});
request.on("error", function (e: any) {
console.log(e.message);
});
request.end();
return this.TargetTemperature;
}
getTargetHeatingCoolingState() {
var data: string = "";
var temp: string = "";
var str: number = 0;
var request = http.get(opts.host + "/sec/?pt=33", function (res: any) {
res.on("data", function (chunk: any) {
data += chunk;
});
res.on("end", () => {
try {
str = data.indexOf("<input name=misc size=4 value=");
temp = data.substring(str + 30, 428);
if (Number(temp) > 0) ThermoController.TargetHeatingCoolingState = 1;
else ThermoController.TargetHeatingCoolingState = 0;
} catch (error) {
console.error(error);
}
});
});
request.on("error", function (e: any) {
console.log(e.message);
});
request.end();
return this.TargetHeatingCoolingState;
}
setTargetTemperature(temperature: CharacteristicValue) {
ThermoController.TargetTemperature = temperature;
console.log("Set Target Temperature ", temperature);
var data: string = "";
var request = http.get(
opts.host + "/sec/?pt=33&misc=" + temperature,
function (res: any) {
res.on("data", function (chunk: any) {
data += chunk;
});
res.on("end", function () {
console.log("Set Target Temperature ", temperature);
});
}
);
request.on("error", function (e: any) {
console.log(e.message);
});
request.end();
}
setTargetHeatingCoolingState(state: CharacteristicValue) {
this.TargetHeatingCoolingState = state;
console.log("Set Target Heating Cooling State ", state);
var data: string = "";
if (state == 0) {
var request = http.request(
opts.host + "/sec/?pt=33&misc=-50",
function (res: any) {
res.on("data", function (chunk: any) {
data += chunk;
});
res.on("end", function () {
console.log("Set Target Heating Cooling State ", state);
});
}
);
request.on("error", function (e: any) {
console.log(e.message);
});
request.end();
} else if (state == 1) {
var request = http.request(
opts.host + "/sec/?pt=33&misc=" + ThermoController.TargetTemperature,
function (res: any) {
res.on("data", function (chunk: any) {
data += chunk;
});
res.on("end", function () {
console.log("Set Target Heating Cooling State ", state);
});
}
);
request.on("error", function (e: any) {
console.log(e.message);
});
request.end();
}
}
getCurrentTemperature() {
var data: string = "";
var temp: string[] = [];
var request = http.get(opts.host + "/sec/?pt=33&cmd=get", function (res: any) {
res.on("data", function (chunk: any) {
data += chunk;
});
res.on("end", function () {
temp = data.split(":");
ThermoController.CurrentTemperature = (Math.round(parseFloat(temp[1]) * 10)) / 10;
thermostat.getCharacteristic(Characteristic.CurrentTemperature)!.updateValue(ThermoController.CurrentTemperature);
});
});
request.on("error", function (e: any) {
console.log(e.message);
});
request.end();
return this.CurrentTemperature;
}
getCurrentHeatingCoolingState() {
var data: string = "";
var request = http.get(opts.host + "/sec/?pt=7&cmd=get", function (res: any) {
res.on("data", (chunk: any) => {
data += chunk;
});
res.on("end", () => {
if (data == "ON") {
ThermoController.CurrentHeatingCoolingState = 1;
thermostat.getCharacteristic(Characteristic.CurrentHeatingCoolingState)!.updateValue(1);
} else if (data == "OFF") {
ThermoController.CurrentHeatingCoolingState = 0;
thermostat.getCharacteristic(Characteristic.CurrentHeatingCoolingState)!.updateValue(0);
}
});
});
request.on("error", function (e: any) {
console.log(e.message);
});
request.end();
return this.CurrentHeatingCoolingState;
}
identify() { //identify the accessory
}
}
const ThermoController = new ThermoControllerClass();
var thermoUUID = uuid.generate('hap-nodejs:accessories:thermo' + ThermoController.name);
var ThermostatService = exports.accessory = new Accessory(ThermoController.name as string, thermoUUID);
// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
ThermostatService.username = "C1:5D:3A:EE:5E:FA";
// @ts-ignore
ThermostatService.pincode = "031-45-154";
// @ts-ignore
ThermostatService.category = Categories.THERMOSTAT;
ThermostatService
.getService(Service.AccessoryInformation)!
.setCharacteristic(Characteristic.Manufacturer, "[KONTUR.HOME]")
.setCharacteristic(Characteristic.Model, "Local-thermo")
.setCharacteristic(Characteristic.SerialNumber, "170221");
ThermostatService.on(AccessoryEventTypes.IDENTIFY, (paired: boolean, callback: VoidCallback) => {
ThermoController.identify();
callback();
});
const thermostat = ThermostatService.addService(Service.Thermostat, ThermoController.name);
thermostat.getCharacteristic(Characteristic.CurrentHeatingCoolingState)!
.setProps({
minValue: 0,
maxValue: 1,
})!
.on(CharacteristicEventTypes.GET,(callback: CharacteristicGetCallback) => {
callback(null, ThermoController.getCurrentHeatingCoolingState());
}
);
thermostat.getCharacteristic(Characteristic.TargetHeatingCoolingState)!
.setProps({
minValue: 0,
maxValue: 1,
})!
.on(CharacteristicEventTypes.GET,(callback: CharacteristicGetCallback) => {
callback(null, ThermoController.getTargetHeatingCoolingState());
}
)
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
ThermoController.setTargetHeatingCoolingState(value);
console.log( "Characteristic TargetHeatingCoolingState changed to %s",value);
callback();
}
);
thermostat.getCharacteristic(Characteristic.CurrentTemperature)!
.setProps({
minValue: -50,
maxValue: 150,
})!
.on(CharacteristicEventTypes.GET,(callback: CharacteristicGetCallback) => {
callback(null, ThermoController.getCurrentTemperature());
}
);
thermostat.getCharacteristic(Characteristic.TargetTemperature)!
.setProps({
minValue: 5,
maxValue: 33,
minStep: 0.5,
})!
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, ThermoController.getTargetTemperature());
}
)
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
ThermoController.setTargetTemperature(value);
console.log("Characteristic TargetTemperature changed to %s", value);
callback();
}
);