forked from jensweigele/ioBroker.yahka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yahka.homekit-bridge.ts
232 lines (188 loc) · 8.66 KB
/
yahka.homekit-bridge.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
/// <reference path="./typings/index.d.ts" />
import debug = require('debug');
debug.enable(<any>'*');
import util = require('util');
import HAP = require('hap-nodejs');
import { Configuration } from './yahka.configuration';
// export let HAPAccessory:any = HAP.Accessory;
export let HAPService = HAP.Service;
export let HAPCharacteristic = HAP.Characteristic;
type IHAPService = any;
// type IHAPCharacteristic = any;
export interface IConversionFunction {
toHomeKit(value:any):any;
toIOBroker(value:any):any;
}
export interface IInOutFunction {
toIOBroker(ioValue:any, callback:() => void);
fromIOBroker(callback:(error:any, plainIOValue:any) => void);
}
export type IInOutChangeNotify = (plainIOValue:any) => void;
export interface IHomeKitBridgeBinding {
conversion:IConversionFunction;
inOut:IInOutFunction;
}
export interface ILogger {
/** log message with debug level */
debug(message:string);
/** log message with info level */
info(message:string);
/** log message with info warn */
warn(message:string);
/** log message with info error */
error(message:string);
}
export interface IHomeKitBridgeBindingFactory {
CreateBinding(characteristicConfig:Configuration.ICharacteristicConfig, changeNotify:IInOutChangeNotify):IHomeKitBridgeBinding;
}
export class THomeKitBridge {
private bridgeObject;
constructor(private config:Configuration.IBridgeConfig, private FBridgeFactory:IHomeKitBridgeBindingFactory, private FLogger:ILogger) {
this.init();
}
init() {
this.bridgeObject = this.setupBridge();
if (this.config.devices)
for (let device of this.config.devices) {
if (device.enabled === false) {
continue;
}
let hapDevice = this.createDevice(device);
try {
this.bridgeObject.addBridgedAccessory(hapDevice);
} catch (e) {
this.FLogger.warn(e);
this.FLogger.warn('Error by adding: ' + JSON.stringify(device));
}
}
this.bridgeObject.publish({
username: this.config.username,
port: this.config.port,
pincode: this.config.pincode,
category: 2
});
}
private setupBridge() {
let hapBridge: HAPNodeJS.Accessory = new (<any>HAP).Bridge(this.config.name, HAP.uuid.generate(this.config.ident));
hapBridge.getService(HAPService.AccessoryInformation)
.setCharacteristic(HAPCharacteristic.Manufacturer, this.config.manufacturer || "not configured")
.setCharacteristic(HAPCharacteristic.Model, this.config.model || "not configured")
.setCharacteristic(HAPCharacteristic.SerialNumber, this.config.serial || "not configured");
// Listen for bridge identification event
hapBridge.on('identify', (paired, callback) => {
this.FLogger.debug('Node Bridge identify:' + paired);
callback(); // success
});
return hapBridge;
}
private createDevice(device:Configuration.IDeviceConfig) {
let devName = device.name;
let deviceID = HAP.uuid.generate(this.config.ident + ':' + devName);
let i = 0;
while (this.bridgeObject.bridgedAccessories.some((a) => a.UUID == deviceID)) {
devName = device.name + '_' + ++i;
deviceID = HAP.uuid.generate(this.config.ident + ':' + devName);
}
this.FLogger.info('adding ' + devName + ' with UUID: ' + deviceID);
let hapDevice = new HAP.Accessory(devName, deviceID);
hapDevice.getService(HAPService.AccessoryInformation)
.setCharacteristic(HAPCharacteristic.Manufacturer, device.manufacturer || 'not configured')
.setCharacteristic(HAPCharacteristic.Model, device.model || 'not configured')
.setCharacteristic(HAPCharacteristic.SerialNumber, device.serial || 'not configured');
hapDevice.on('identify', (paired, callback) => {
this.FLogger.debug('device identify');
callback(); // success
});
for (let serviceConfig of device.services)
this.initService(hapDevice, serviceConfig);
return hapDevice;
}
private initService(hapDevice:any, serviceConfig:Configuration.IServiceConfig) {
if (!(serviceConfig.type in HAP.Service)) {
throw Error('unknown service type: ' + serviceConfig.type);
}
let isNew = false;
let hapService = hapDevice.getService(HAP.Service[serviceConfig.type]);
if (hapService !== undefined && hapService.subtype !== serviceConfig.subType) {
hapService = undefined;
}
if (hapService === undefined) {
hapService = new HAP.Service[serviceConfig.type](serviceConfig.name, serviceConfig.subType);
isNew = true;
}
for (let charactConfig of serviceConfig.characteristics) {
this.initCharacteristic(hapService, charactConfig);
}
// fix for wrong min Temperature Value in HAPNode
let curTempCharacetristic = hapService.getCharacteristic('Current Temperature');
if (curTempCharacetristic !== undefined) {
curTempCharacetristic.props.minValue = -99
}
if (isNew) {
hapDevice.addService(hapService);
}
}
private initCharacteristic(hapService:IHAPService, characteristicConfig:Configuration.ICharacteristicConfig) {
let hapCharacteristic = hapService.getCharacteristic(HAPCharacteristic[characteristicConfig.name]);
if (!hapCharacteristic) {
this.FLogger.warn("unknown characteristic: " + characteristicConfig.name);
return;
}
if (!characteristicConfig.enabled)
return;
hapCharacteristic.binding = this.FBridgeFactory.CreateBinding(characteristicConfig, (plainIOValue:any) => {
this.FLogger.debug('[' + characteristicConfig.name + '] got a change notify event, ioValue: ' + JSON.stringify(plainIOValue));
let binding:IHomeKitBridgeBinding = hapCharacteristic.binding;
if (!binding) {
this.FLogger.error('[' + characteristicConfig.name + '] no binding!');
return;
}
let hkValue = binding.conversion.toHomeKit(plainIOValue);
this.FLogger.debug('[' + characteristicConfig.name + '] forwarding value from ioBroker (' + JSON.stringify(plainIOValue) + ') to homekit as (' + JSON.stringify(hkValue) + ')');
hapCharacteristic.setValue(hkValue, undefined, binding);
});
hapCharacteristic.on('set', (hkValue:any, callback:() => void, context:any) => {
this.FLogger.debug('[' + characteristicConfig.name + '] got a set event, hkValue: ' + JSON.stringify(hkValue));
let binding:IHomeKitBridgeBinding = hapCharacteristic.binding;
if (!binding) {
this.FLogger.error('[' + characteristicConfig.name + '] no binding!');
callback();
return;
}
if (context === binding) {
this.FLogger.debug('[' + characteristicConfig.name + '] set was initiated from ioBroker - exiting here');
callback();
return;
}
let ioValue = binding.conversion.toIOBroker(hkValue);
binding.inOut.toIOBroker(ioValue, () => {
this.FLogger.debug('[' + characteristicConfig.name + '] set was accepted by ioBroker (value: ' + JSON.stringify(ioValue) + ')');
callback();
});
});
hapCharacteristic.on('get', (hkCallback) => {
this.FLogger.debug('[' + characteristicConfig.name + '] got a get event');
let binding:IHomeKitBridgeBinding = hapCharacteristic.binding;
if (!binding) {
this.FLogger.error('[' + characteristicConfig.name + '] no binding!');
hkCallback('no binding', null);
return;
}
binding.inOut.fromIOBroker((ioBrokerError, ioValue) => {
let hkValue = binding.conversion.toHomeKit(ioValue);
this.FLogger.debug('[' + characteristicConfig.name + '] forwarding value from ioBroker (' + JSON.stringify(ioValue) + ') to homekit as (' + JSON.stringify(hkValue) + ')');
hkCallback(ioBrokerError, hkValue);
});
});
}
}
let hapInited:boolean = false;
export function initHAP(storagePath:string, HAPdebugLogMethod:Function) {
if (hapInited) {
return;
}
HAP.init(storagePath);
debug.log = function () {
HAPdebugLogMethod(util.format.apply(this, arguments));
};
}