-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
98 lines (84 loc) · 4.1 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
'use strict';
var Homebridge, Accessory;
var request = require("request");
var ItemFactory = require('./libs/ItemFactory.js');
var Utility = require('./libs/Utility.js');
var WSListener = require('./libs/WSListener.js');
module.exports = function(homebridge) {
console.log("homebridge API version: " + homebridge.version);
// Accessory must be created from PlatformAccessory Constructor
Accessory = homebridge.platformAccessory;
// Keep refference to the passes API object
Homebridge = homebridge;
//Add inheritance of the AbstractItem to the Accessory object
Utility.addSupportTo(ItemFactory.AbstractItem, Accessory);
//All other items are child of the abstractItem
Utility.addSupportTo(ItemFactory.LightControllerV2MoodSwitch, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.TemperatureSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.HumiditySensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.MotionSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.ContactSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.LightSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Dimmer, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Colorpicker, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Gate, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.DoorBell, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Jalousie, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.TimedSwitch, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Switch, ItemFactory.AbstractItem);
//Add childs of switch
Utility.addSupportTo(ItemFactory.Lightbulb, ItemFactory.Switch);
Utility.addSupportTo(ItemFactory.Pushbutton, ItemFactory.Switch);
homebridge.registerPlatform("homebridge-loxoneWs", "LoxoneWs", LoxPlatform);
};
// Platform constructor
// config may be null
function LoxPlatform(log, config) {
//log("LoxPlatform Init");
var platform = this;
this.log = log;
this.config = config;
this.protocol = "http";
if (!this.config['host']) throw new Error("Configuration missing: loxone host (please provide the IP address here)");
if (!this.config['port']) throw new Error("Configuration missing: loxone port (if default port, specify 7777)");
if (!this.config['username']) throw new Error("Configuration missing: loxone username");
if (!this.config['password']) throw new Error("Configuration missing: loxone password");
if (!this.config['rooms']) this.log("Info: rooms array not configured. Adding every room.");
this.host = config["host"];
this.port = config["port"];
this.username = config["username"];
this.password = config["password"];
if (this.config['rooms']) {
this.rooms = config["rooms"];
} else {
this.rooms =[];
}
this.log(typeof this.rooms);
if (this.config['moodSwitches']) {
this.moodSwitches = config["moodSwitches"];
} else {
this.moodSwitches = 'none';
}
//Also make a WS connection
this.ws = new WSListener(platform);
}
LoxPlatform.prototype.accessories = function(callback) {
var that = this;
//this.log("Getting Loxone configuration.");
var itemFactory = new ItemFactory.Factory(this,Homebridge);
var url = itemFactory.sitemapUrl();
this.log("Platform - Waiting 8 seconds until initial state is retrieved via WebSocket.");
setTimeout(function(){
that.log("Platform - Retrieving initial config from " + url);
request.get({
url: url,
json: true
}, function(err, response, json) {
if (!err && response.statusCode === 200) {
callback(itemFactory.parseSitemap(json));
} else {
that.log("Platform - There was a problem connecting to Loxone.");
}
})
},8000);
};