forked from jensweigele/ioBroker.yahka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yahka.ioBroker-adapter.ts
176 lines (150 loc) · 8.04 KB
/
yahka.ioBroker-adapter.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
import { THomeKitIPCamera } from './yahka.homekit-ipcamera';
import { Configuration } from './yahka.configuration';
import {IInOutFunction} from './yahka.homekit-bridge';
/// <reference path="./typings/index.d.ts" />
import * as hkBridge from './yahka.homekit-bridge';
// import * as mac from './node_modules/macaddress';
import {functionFactory, IInternalInOutFunction} from './yahka.function-factory';
interface ICustomCharacteristicConfig extends Configuration.ICharacteristicConfig {
conversionFunction?:string;
conversionParameters?:any;
inOutFunction?:string;
inOutParameters?:any;
}
function isCustomCharacteristicConfig(config:Configuration.ICharacteristicConfig):config is ICustomCharacteristicConfig {
if (!config)
return false;
let myConfig = <ICustomCharacteristicConfig>config;
return (myConfig.inOutFunction !== undefined) || (myConfig.conversionFunction !== undefined) || (myConfig.inOutParameters !== undefined);
}
export class TIOBrokerAdapter implements hkBridge.IHomeKitBridgeBindingFactory {
stateToEventMap:Map<string, hkBridge.IInOutChangeNotify[]> = new Map<string, hkBridge.IInOutChangeNotify[]>();
objectToEventMap:Map<string, hkBridge.IInOutChangeNotify[]> = new Map<string, hkBridge.IInOutChangeNotify[]>();
devices: Array<Object> = [];
verboseHAPLogging:boolean = false;
constructor(private adapter:ioBroker.IAdapter, private controllerPath) {
adapter.on('ready', this.adapterReady.bind(this));
adapter.on('objectChange', this.handleObjectChange.bind(this));
adapter.on('stateChange', this.handleState.bind(this));
adapter.on('message', this.handleMessage.bind(this));
adapter.on('unload', this.handleUnload.bind(this));
}
private adapterReady() {
hkBridge.initHAP(this.controllerPath + '/' + this.adapter.systemConfig.dataDir + this.adapter.name + '.' + this.adapter.instance + '.hapdata', this.handleHAPLogEvent.bind(this));
this.adapter.log.info('adapter ready, checking config');
let config = this.adapter.config;
this.createHomeKitBridges(config);
this.createCameraDevices(config);
}
private createHomeKitBridges(config: any) {
let bridgeConfig:Configuration.IBridgeConfig = config.bridge;
if (!config.firstTimeInitialized) {
this.adapter.log.info('first time initialization');
this.adapter.log.debug('system config:' + JSON.stringify(this.adapter.systemConfig));
let hostName = 'unknownHostname';
if (this.adapter.systemConfig != undefined)
if (this.adapter.systemConfig.system != undefined)
if (this.adapter.systemConfig.system.hostname != undefined)
hostName = this.adapter.systemConfig.system.hostname;
bridgeConfig.ident = hostName + ':' + this.adapter.name + '.' + this.adapter.instance;
bridgeConfig.name = bridgeConfig.ident;
bridgeConfig.serial = bridgeConfig.ident;
let usr = [];
for (let i = 0; i < 6; i++)
usr[i] = ('00' + (Math.floor((Math.random() * 256)).toString(16))).substr(-2);
bridgeConfig.username = usr.join(':');
bridgeConfig.pincode = '123-45-678';
bridgeConfig.port = 0;
bridgeConfig.verboseLogging = false;
config.firstTimeInitialized = true;
this.adapter.extendForeignObject('system.adapter.' + this.adapter.name + '.' + this.adapter.instance, {native: config}, undefined);
}
this.verboseHAPLogging = bridgeConfig.verboseLogging == true;
this.adapter.log.debug('creating bridge');
this.devices.push(new hkBridge.THomeKitBridge(config.bridge, this, this.adapter.log));
}
private createCameraDevices(config: any) {
let cameraArray:Array<Configuration.ICameraConfig> = config.cameras;
if (cameraArray === undefined)
return;
for (let cameraConfig of cameraArray) {
this.adapter.log.debug('creating camera');
this.devices.push(new THomeKitIPCamera(cameraConfig, this.adapter.log));
}
}
private handleHAPLogEvent(message) {
if (this.verboseHAPLogging)
this.adapter.log.debug(message);
}
private handleObjectChange(id:string, obj:ioBroker.IObject) {
// Warning, obj can be null if it was deleted
this.adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
}
private handleState(id:string, state:ioBroker.IState) {
// Warning, state can be null if it was deleted
let notifyArray = this.stateToEventMap.get(id);
if (!notifyArray) {
//this.adapter.log.debug('nobody subscribed for this state');
return;
}
this.adapter.log.debug('got a stateChange for [' + id + ']');
for (let method of notifyArray)
method(state);
}
private handleMessage(obj:any) {
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// Send response in callback if required
if (obj.callback)
this.adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
}
private handleUnload(callback) {
try {
this.adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
}
private handleInOutSubscriptionRequest(inOutFunction:IInternalInOutFunction, changeNotify:hkBridge.IInOutChangeNotify) {
if (inOutFunction.subscriptionRequests.length == 0)
return;
for (let subscriptionRequest of inOutFunction.subscriptionRequests) {
let changeInterceptor = (ioValue:any) => subscriptionRequest.subscriptionEvent(ioValue, changeNotify);
if (subscriptionRequest.subscriptionType === 'state') {
let existingArray = this.stateToEventMap.get(subscriptionRequest.subscriptionIdentifier);
if (!existingArray) {
existingArray = [changeInterceptor];
this.stateToEventMap.set(subscriptionRequest.subscriptionIdentifier, existingArray);
} else
existingArray.push(changeInterceptor);
this.adapter.subscribeForeignStates(subscriptionRequest.subscriptionIdentifier);
this.adapter.log.debug('added subscription for: [' + subscriptionRequest.subscriptionType + ']' + subscriptionRequest.subscriptionIdentifier);
} else {
this.adapter.log.warn('unknown subscription type: ' + subscriptionRequest.subscriptionType);
}
}
}
public CreateBinding(characteristicConfig:Configuration.ICharacteristicConfig, changeNotify:hkBridge.IInOutChangeNotify):hkBridge.IHomeKitBridgeBinding {
if (isCustomCharacteristicConfig(characteristicConfig)) {
let inoutFunc = functionFactory.createInOutFunction(this.adapter, characteristicConfig.inOutFunction, characteristicConfig.inOutParameters);
if (inoutFunc === undefined) {
this.adapter.log.error('[' + characteristicConfig.name + '] could not create inout-function: ' + characteristicConfig.inOutFunction + ' with params: ' + JSON.stringify(characteristicConfig.inOutParameters));
return undefined;
}
let convFunc = functionFactory.createConversionFunction(this.adapter, characteristicConfig.conversionFunction, characteristicConfig.conversionParameters);
if (convFunc === undefined) {
this.adapter.log.error('[' + characteristicConfig.name + '] could not create conversion-function: ' + characteristicConfig.conversionFunction + ' with params: ' + JSON.stringify(characteristicConfig.conversionParameters));
return undefined;
}
this.handleInOutSubscriptionRequest(inoutFunc, changeNotify);
return {
conversion: convFunc,
inOut: inoutFunc
}
}
return null;
}
}