forked from LASER-Yi/homebridge-mi-acpartner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
109 lines (92 loc) · 3.77 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
107
108
109
const events = require('events');
const connectUtil = require('./lib/connectUtil');
const syncLockUtil = require('./lib/syncLockUtil');
const packageFile = require('./package.json');
const fs_Accessory = require('./accessories');
let PlatformAccessory, Accessory, Service, Characteristic, UUIDGen;
module.exports = function (homebridge) {
PlatformAccessory = homebridge.platformAccessory;
Accessory = homebridge.hap.Accessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform('homebridge-mi-acpartner', 'XiaoMiAcPartner', XiaoMiAcPartner, true);
}
function XiaoMiAcPartner(log, config, api) {
if (config === null) return;
this.log = log;
this.config = config;
this.PlatformAccessory = PlatformAccessory;
this.Accessory = Accessory;
this.Service = Service;
this.Characteristic = Characteristic;
this.UUIDGen = UUIDGen;
//Miio devices
this.devices = [];
//Config
this.refreshInterval = config["refreshInterval"] !== undefined ? config["refreshInterval"] : 10 * 60 * 1000;
if (!config['accessories']) {
this.log.error("[ERROR]'accessories' not defined! Please check your 'config.json' file.");
}
if (!config.devices) {
this.log.error("[ERROR]'devices' not defined! Please check your 'config.json' file.");
return;
}
this.welcomeInfo();
//New syncLock
this.syncLock = new syncLockUtil(this);
//Accessory EventEmitter
this.startEvent = new events.EventEmitter();
//Connect devices
this.conUtil = new connectUtil(config.devices, this);
setInterval((() => {
this.conUtil.refresh();
}), this.refreshInterval);
if (api) {
this.api = api;
this.api.on("didFinishLaunching", () => {
})
} else {
this.log.error("[ERROR]Homebridge's version is too old, please upgrade it!");
}
}
XiaoMiAcPartner.prototype = {
welcomeInfo: function () {
this.log.info("----------------------------------------------------");
this.log.info("XiaoMiAcPartner By LASER-Yi");
this.log.info("Current version: %s", packageFile.version);
this.log.info("GitHub: https://github.com/LASER-Yi/homebridge-mi-acpartner");
this.log.info("QQ Group: 107927710");
this.log.info("----------------------------------------------------");
},
accessories: function (callback) {
//Start register accessories
let accessories = [];
this.config['accessories'].forEach(element => {
if (element['type'] !== undefined && element['name'] !== undefined) {
//Register
this.log("[INIT]%s -> Type: %s", element['name'], element['type']);
switch (element['type']) {
case "switch":
accessories.push(new fs_Accessory.SwitchAccessory(element, this));
break;
case "learnIR":
accessories.push(new fs_Accessory.LearnIRAccessory(element, this));
break;
case "switchRepeat":
accessories.push(new fs_Accessory.SwitchRepeatAccessory(element, this));
break;
case "heaterCooler":
accessories.push(new fs_Accessory.HeaterCoolerAccessory(element, this));
break;
case "climate":
accessories.push(new fs_Accessory.ClimateAccessory(element, this));
break;
default:
this.log.warn("[WARN]Wrong Type -> %s", element['type']);
}
}
});
callback(accessories);
}
}