-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.js
88 lines (66 loc) · 2.36 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
'use strict';
var Utils = require('./lib/utils.js').Utils;
var Controller = require('./lib/controller.js').Controller;
var HapAccessory, Service, Characteristic, UUIDGen;
var cachedAccessories = 0;
var platform_name = "mqtt";
var plugin_name = "homebridge-" + platform_name;
var storagePath;
var plugin_version;
var multiple = false;
module.exports = function(homebridge) {
console.log("homebridge API version: " + homebridge.version);
HapAccessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid; // Universally Unique IDentifier
storagePath = homebridge.user.storagePath();
homebridge.registerPlatform(plugin_name, platform_name, PluginPlatform, true);
}
function PluginPlatform(log, config, api) {
this.log = log;
if (typeof(config) === "undefined" || config === null) {
multiple = true;
this.log.warn("config undefined in '%s', irrelevant by multiple homebridge instances.", storagePath);
return;
}
if (typeof config.url === "undefined") {
this.log.error("url undefined. Please got to Settings and add a valid url.");
return;
}
plugin_version = Utils.readPluginVersion();
this.log("%s v%s", plugin_name, plugin_version);
Utils.read_npmVersion(plugin_name, function(npm_version) {
if (npm_version > plugin_version) {
this.log("A new version %s is avaiable", npm_version);
}
}.bind(this));
this.log.debug("storagePath = %s", storagePath);
this.log.debug("config = %s", JSON.stringify(config));
var c_parameters = {
"config": config,
"log": this.log,
"plugin_name": plugin_name,
"plugin_version": plugin_version,
"platform_name": platform_name,
"api": api,
"HapAccessory": HapAccessory,
"Characteristic": Characteristic,
"Service": Service,
"UUIDGen": UUIDGen
}
this.controller = new Controller(c_parameters);
if (api) {
api.on('didFinishLaunching', function() {
this.log("Number of cached Accessories: %s", cachedAccessories);
this.controller.start();
}.bind(this));
}
}
PluginPlatform.prototype.configureAccessory = function(accessory) {
//this.log.debug("configureAccessory %s", JSON.stringify(accessory, null, 2));
cachedAccessories++;
if (!multiple) {
this.controller.configureAccessory(accessory);
}
}