forked from nicoduj/homebridge-harmony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharmonyPlatform.js
125 lines (101 loc) · 3.39 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
var AccessoryType;
const HarmonySubPlatform = require('./harmonySubPlatform').HarmonySubPlatform;
const HarmonyTools = require('./harmonyTools.js');
module.exports = {
HarmonyPlatform: HarmonyPlatform,
};
function HarmonyPlatform(log, config, api) {
this.log = log;
this.log('HarmonyPlatform Init');
this.api = api;
AccessoryType = this.api.hap.Accessory.Categories;
if (!config) {
log('No configuration found for homebridge-harmonyHub');
return;
}
this.plaformsConfigs = [];
this.plaformsConfigs.push(config);
if (config['otherPlatforms']) {
this.plaformsConfigs.push.apply(
this.plaformsConfigs,
config['otherPlatforms']
);
}
this.cleanCache = HarmonyTools.checkParameter(config['cleanCache'], false);
this.publishAllTVAsExternalAccessory = HarmonyTools.checkParameter(
config['publishAllTVAsExternalAccessory'],
false
);
this.platforms = [];
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-harmonyHub',
'HarmonyHubWebSocket',
platform._foundAccessories
);
platform._foundAccessories = [];
}
}
for (let i = 0, len = this.platforms.length; i < len; i++) {
let platform = this.platforms[i];
platform.harmonyBase.configureAccessories(platform);
}
}.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.api.unregisterPlatformAccessories(
'homebridge-harmonyHub',
'HarmonyHubWebSocket',
[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'
);
}
}
},
};