-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (78 loc) · 3.25 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
require('./Devices/MiWaterPurifier');
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", "MiWaterPurifierPlatform")) {
return;
}
PlatformAccessory = homebridge.platformAccessory;
Accessory = homebridge.hap.Accessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform('homebridge-mi-waterpuri', 'MiWaterPurifierPlatform', MiWaterPurifierPlatform, 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 MiWaterPurifierPlatform(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("[MiWaterPurifier][INFO]*********************************************************************");
this.log.info("[Miwaterpurifier][INFO] Miwaterpurifier v%s *", packageFile.version);
this.log.info("[Miwaterpurifier][INFO] GitHub: https://github.com/biner88/homebridge-mi-waterpuri *");
this.log.info("[Miwaterpurifier][INFO] *");
this.log.info("[Miwaterpurifier][INFO]*********************************************************************");
this.log.info("[Miwaterpurifier][INFO]start success...");
}
MiWaterPurifierPlatform.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'] == "MiWaterPurifier") {
new MiWaterPurifier(this, deviceCfg).forEach(function (accessory, index, arr) {
myAccessories.push(accessory);
});
} else {
}
}
this.log.info("[Miwaterpurifier][INFO]device size: " + deviceCfgs.length + ", accessories size: " + myAccessories.length);
}
callback(myAccessories);
}
}