-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLight-M16-P13PWM_accessory.ts
More file actions
186 lines (163 loc) · 6.05 KB
/
Light-M16-P13PWM_accessory.ts
File metadata and controls
186 lines (163 loc) · 6.05 KB
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
import {
Accessory,
AccessoryEventTypes,
Categories,
Characteristic,
CharacteristicEventTypes,
CharacteristicSetCallback,
CharacteristicGetCallback,
CharacteristicValue,
Service,
uuid,
VoidCallback
} from '..';
import http from "http";
type Options = {
host: string,
path: string
};
const opts : Options = {
host: '192.168.0.16',
path: ''
};
class LightControllerClass {
name: CharacteristicValue = "Dimmer"; //name of accessory
pincode: CharacteristicValue = "031-45-154";
username: CharacteristicValue = "FA:3C:ED:5A:1A:1A"; // MAC like address used by HomeKit to differentiate accessories.
manufacturer: CharacteristicValue = "[KONTUR.HOME]"; //manufacturer (optional)
model: CharacteristicValue = "Dimmer"; //model (optional)
serialNumber: CharacteristicValue = "170221"; //serial number (optional)
power: CharacteristicValue = false; //current power status
brightness: CharacteristicValue = 255; //current brightness
setPower(status: CharacteristicValue) {
if ( status == "true" || status == 1 ) opts.path = "/sec/?cmd=13:" + LightController.brightness; // включение
else opts.path = "/sec/?cmd=13:0"; // выключение
const request = http.request(opts, (res) => {
let data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
this.power = status;
}
getPower() {
opts.path = '/sec/?pt=13&cmd=get'; // считывание состояния 13 порта
const request = http.request(opts, function (res) {
let data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
if ( Number(data) == 0 ) {
LightController.power = false;
}
else {
LightController.power = true;
}
lightbulb.getCharacteristic(Characteristic.On)!.updateValue(LightController.power);
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
return this.power;
}
setBrightness(brightness: CharacteristicValue) {
opts.path = "/sec/?cmd=13:" + brightness; // установка ярости
const request = http.request(opts, function (res) {
let data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
this.brightness = brightness;
}
getBrightness() {
opts.path = '/sec/?pt=13&cmd=get' // Считывание состояния 13 порта
const request = http.request(opts, function (res) {
let data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
if (Number(data) > 0) {
LightController.brightness = data;
lightbulb.getCharacteristic(Characteristic.Brightness)!.updateValue(data);}
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
return this.brightness;
}
identify() { //identify the accessory
}
}
const LightController = new LightControllerClass();
// Generate a consistent UUID for our light Accessory that will remain the same even when
// restarting our server. We use the `uuid.generate` helper function to create a deterministic
// UUID based on an arbitrary "namespace" and the word "light".
var lightUUID = uuid.generate('hap-nodejs:accessories:light' + LightController.name);
// This is the Accessory that we'll return to HAP-NodeJS that represents our light.
var lightAccessory = exports.accessory = new Accessory(LightController.name as string, lightUUID);
// Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
// @ts-ignore
lightAccessory.username = LightController.username;
// @ts-ignore
lightAccessory.pincode = LightController.pincode;
// @ts-ignore
lightAccessory.category = Categories.LIGHTBULB;
// set some basic properties (these values are arbitrary and setting them is optional)
lightAccessory
.getService(Service.AccessoryInformation)!
.setCharacteristic(Characteristic.Manufacturer, LightController.manufacturer)
.setCharacteristic(Characteristic.Model, LightController.model)
.setCharacteristic(Characteristic.SerialNumber, LightController.serialNumber);
// listen for the "identify" event for this Accessory
lightAccessory.on(AccessoryEventTypes.IDENTIFY, (paired: boolean, callback: VoidCallback) => {
LightController.identify();
callback();
});
// Add the actual Lightbulb Service and listen for change events from iOS.
const lightbulb = lightAccessory.addService(Service.Lightbulb, LightController.name);
lightbulb.getCharacteristic(Characteristic.On)!
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
LightController.setPower(value);
callback();
})
.updateValue(LightController.getPower())
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, LightController.getPower());
});
lightbulb.addCharacteristic(Characteristic.Brightness)!
.setProps({
minValue: 0,
maxValue: 255,
})
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
LightController.setBrightness(value);
callback();
})
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(null, LightController.getBrightness());
});
// обновление значений характеристики происходит при открытии Home App,
// для обновления значений характеристики с интервалом 10 сек раскомментить строки
// setInterval(() => {
// LightController.getPower();
// LightController.getBrightness();
// }, 10000);