-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
harmonyPlatform.js
186 lines (155 loc) · 5.3 KB
/
harmonyPlatform.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
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
var AccessoryType;
const HarmonySubPlatform = require('./harmonySubPlatform').HarmonySubPlatform;
const HarmonyConst = require('./harmonyConst');
const HarmonyTools = require('./harmonyTools.js');
const Explorer = require('@harmonyhub/discover').Explorer;
const discover = new Explorer(61991);
var EventEmitter = require('events');
var inherits = require('util').inherits;
module.exports = {
HarmonyPlatform: HarmonyPlatform,
};
function HarmonyPlatform(log, config, api) {
EventEmitter.call(this);
this.log = log;
this.log('HarmonyPlatform Init');
this.api = api;
AccessoryType = this.api.hap.Categories;
if (!config) {
log('No configuration found for homebridge-harmony');
return;
}
this.plaformsConfigs = [];
this.plaformsConfigs.push(config);
if (config['otherPlatforms'] && config['otherPlatforms'].length > 0) {
this.plaformsConfigs.push.apply(this.plaformsConfigs, config['otherPlatforms']);
}
this.cleanCache = HarmonyTools.checkParameter(config['cleanCache'], false);
this.publishAllTVAsExternalAccessory = HarmonyTools.checkParameter(
config['publishAllTVAsExternalAccessory'],
true
);
this.platforms = [];
this._AccessoriesToRemove = [];
for (let i = 0, len = this.plaformsConfigs.length; i < len; i++) {
let platformConfig = this.plaformsConfigs[i];
this.platforms.push(new HarmonySubPlatform(log, platformConfig, api, this));
}
this.api
.on(
'shutdown',
function () {
this.log('INFO - shutdown');
for (let i = 0, len = this.platforms.length; i < len; i++) {
let platform = this.platforms[i];
platform.harmonyBase.harmony.removeAllListeners();
platform.harmonyBase.harmony.close();
}
}.bind(this)
)
.on(
'didFinishLaunching',
function () {
this.log('DidFinishLaunching');
if (this.cleanCache) {
this.log('WARNING - Removing Accessories');
for (let i = 0, len = this.platforms.length; i < len; i++) {
let platform = this.platforms[i];
platform.api.unregisterPlatformAccessories(
'homebridge-harmony',
'HarmonyHubWebSocket',
platform._foundAccessories
);
platform._foundAccessories = [];
}
}
if (this._AccessoriesToRemove.length > 0) {
this.api.unregisterPlatformAccessories(
'homebridge-harmony',
'HarmonyHubWebSocket',
this._AccessoriesToRemove
);
}
var launchDiscovery = false;
for (let i = 0, len = this.platforms.length; i < len; i++) {
let platform = this.platforms[i];
platform.harmonyBase.configureAccessories(platform);
//check for discovery
if (platform.hubIP == undefined) {
launchDiscovery = true;
}
}
if (launchDiscovery) {
this.discoverHub();
}
}.bind(this)
);
}
HarmonyPlatform.prototype = {
//Restore from cache
configureAccessory: function (accessory) {
let platformName = accessory.context.subPlatformName;
var platform;
if (this.platforms && this.platforms.length > 0)
platform = this.platforms.find((x) => x.name == platformName);
if (platform == undefined) {
this.log(
'WARNING - configureAccessory - The platform ' +
platformName +
" is not there anymore in your config (name property). It won't be loaded and will be removed from cache."
);
this._AccessoriesToRemove.push(accessory);
} else {
this.log.debug(
accessory.displayName,
'Got cached Accessory ' + accessory.UUID + ' for ' + platform.name
);
platform._foundAccessories.push(accessory);
if (accessory.category == AccessoryType.TELEVISION) {
this._oneTVAdded = true;
this.log(
'WARNING - configureAccessory - TV accessory added in your bridge from cache, if another plugin is exposing a TV accessory this one might not be visible in your remote widget'
);
}
}
},
//HUB discovery
discoverHub: function () {
discover.on('update', (hubs) => {
// Combines the online & update events by returning an array with all known
// hubs for ease of use.
const knownHubs = hubs.reduce(function (prev, hub) {
return (
prev +
(prev.length > 0 ? ',' : '') +
hub.ip +
'|' +
hub.friendlyName +
'|' +
hub.fullHubInfo.remoteId
);
}, '');
this.knownHubsArray = knownHubs.split(',');
});
try {
if (!this.discoverInProgress) {
this.log('INFO - starting hub discovery');
this.discoverInProgress = true;
this.knownHubsArray = undefined;
discover.start();
setTimeout(() => {
discover.stop();
this.discoverInProgress = false;
this.log('INFO - stopping hub discovery, hubs found : ' + this.knownHubsArray);
this.emit('discoveredHubs', this.knownHubsArray);
}, HarmonyConst.DELAY_FOR_AUTO_DISCOVERY);
}
} catch (error) {
this.log('ERROR - cannot discover hub - ' + error);
setTimeout(() => {
this.discoverHub();
}, HarmonyConst.DELAY_BEFORE_RECONNECT);
}
},
};
inherits(HarmonyPlatform, EventEmitter);