-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
115 lines (101 loc) · 3.23 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
110
111
112
113
114
115
var Service, Characteristic, Accessory, uuid;
var Veraconfig = {};
var debug = require("debug")('VeraLink');
var request = require("request-promise");
var hashing = require("create-hash");
module.exports = function (homebridge)
{
if(typeof homebridge !== "undefined")
{
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Accessory = homebridge.hap.Accessory;
uuid = homebridge.hap.uuid;
console.log("VeraLink initializing");
homebridge.registerPlatform("homebridge-veralink", "Vera", VeraLinkPlatform);
}
};
VeraLinkPlatform.prototype = {
accessories: function(callback)
{
this
.functions
.getVeraInfo()
.then( (verainfo)=>{
if(typeof verainfo === 'object'){
var devices = this.functions.processall(verainfo);
var accessories = devices.map( (device)=>{
return this.createAccessory(device,this);
});
callback(accessories);
}
}
);
},
createAccessory: function(device,platform) {
var properties = new Object({
platform: platform,
name: device.displayName,
getServices : function(){
return this.services;
}
});
Object.assign(device, properties);
return device;
}
};
function VeraLinkPlatform(log, config)
{
var Veraconfig = loadconfig();
this.log = log;
this.rooms = {};
this.HAPNode = {'request':request, 'uuid':uuid, 'Accessory':Accessory, 'Service':Service, 'Characteristic':Characteristic, 'debug':debug, 'hashing':hashing, 'return': true};
process.on('uncaughtException', function (err) {
debug(err);
});
defaults = {
bridged: true,
includesensor: false,
dimmertest: false,
ignorerooms: [],
ignoredevices: [],
securitypoll: 2000
};
Veraconfig = merge_options(defaults, Veraconfig);
Veraconfig = merge_options(Veraconfig,config);
if(typeof config.veraIP === "undefined")
{
console.log("\033[31m No configuration found, please write your configuration on .homebridge/config.json \033[0m");
console.log("\033[31m or add your configuration file to "+home+"/.veralink/config.js \033[0m");
process.exit();
}
this.functions = require('./lib/functions.js')(this.HAPNode,Veraconfig);
}
function loadconfig()
{
var fs = require('fs');
home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
try {
fs.accessSync(home+'/.veralink', fs.F_OK);
try {
fs.accessSync(home+'/.veralink/config.js', fs.F_OK);
return require('./config.js');
} catch(e) {
return {};
}
} catch (e) {
try {
fs.mkdirSync(home+'/.veralink');
return {};
} catch(e) {
if ( e.code != 'EEXIST' ) throw e;
}
}
}
function merge_options(obj1,obj2)
{
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}