forked from YinHangCode/homebridge-mi-philips-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (88 loc) · 3.9 KB
/
index.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
require('./Devices/MiPhilipsSmartBulb');
require('./Devices/MiPhilipsTableLamp2');
require('./Devices/MiPhilipsCeilingLamp');
var fs = require('fs');
var packageFile = require("./package.json");
var PlatformAccessory, Accessory, Service, Characteristic, UUIDGen;
module.exports = function(homebridge) {
if(!isConfig(homebridge.user.configPath(), "platforms", "MiPhilipsLightPlatform")) {
return;
}
PlatformAccessory = homebridge.platformAccessory;
Accessory = homebridge.hap.Accessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform('homebridge-mi-philips-light', 'MiPhilipsLightPlatform', MiPhilipsLightPlatform, true);
}
function isConfig(configFile, type, name) {
var config = JSON.parse(fs.readFileSync(configFile));
if("accessories" === type) {
var accessories = config.accessories;
for(var i in accessories) {
if(accessories[i]['accessory'] === name) {
return true;
}
}
} else if("platforms" === type) {
var platforms = config.platforms;
for(var i in platforms) {
if(platforms[i]['platform'] === name) {
return true;
}
}
} else {
}
return false;
}
function MiPhilipsLightPlatform(log, config, api) {
if(null == config) {
return;
}
this.Accessory = Accessory;
this.PlatformAccessory = PlatformAccessory;
this.Service = Service;
this.Characteristic = Characteristic;
this.UUIDGen = UUIDGen;
this.log = log;
this.config = config;
if (api) {
this.api = api;
}
this.log.info("[MiPhilipsLightPlatform][INFO]********************************************************************");
this.log.info("[MiPhilipsLightPlatform][INFO] MiPhilipsLightPlatform v%s By YinHang", packageFile.version);
this.log.info("[MiPhilipsLightPlatform][INFO] GitHub: https://github.com/YinHangCode/homebridge-mi-philips-light ");
this.log.info("[MiPhilipsLightPlatform][INFO] QQ Group: 107927710 ");
this.log.info("[MiPhilipsLightPlatform][INFO]********************************************************************");
this.log.info("[MiPhilipsLightPlatform][INFO]start success...");
}
MiPhilipsLightPlatform.prototype = {
accessories: function(callback) {
var myAccessories = [];
var deviceCfgs = this.config['deviceCfgs'];
if(deviceCfgs instanceof Array) {
for (var i = 0; i < deviceCfgs.length; i++) {
var deviceCfg = deviceCfgs[i];
if(null == deviceCfg['type'] || "" == deviceCfg['type'] || null == deviceCfg['token'] || "" == deviceCfg['token'] || null == deviceCfg['ip'] || "" == deviceCfg['ip']) {
continue;
}
if (deviceCfg['type'] == "MiPhilipsSmartBulb") {
new MiPhilipsSmartBulb(this, deviceCfg).forEach(function(accessory, index, arr){
myAccessories.push(accessory);
});
} else if (deviceCfg['type'] == "MiPhilipsTableLamp2") {
new MiPhilipsTableLamp2(this, deviceCfg).forEach(function(accessory, index, arr){
myAccessories.push(accessory);
});
} else if (deviceCfg['type'] == "MiPhilipsCeilingLamp") {
new MiPhilipsCeilingLamp(this, deviceCfg).forEach(function(accessory, index, arr){
myAccessories.push(accessory);
});
} else {
}
}
this.log.info("[MiPhilipsLightPlatform][INFO]device size: " + deviceCfgs.length + ", accessories size: " + myAccessories.length);
}
callback(myAccessories);
}
}