homebridge-http-webhooks-plus 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,153 @@
1
+ const Constants = require('../../Constants');
2
+ const Util = require('../../Util');
3
+
4
+ function HttpWebHookLightBulbAccessory(ServiceParam, CharacteristicParam, platform, lightConfig) {
5
+ Service = ServiceParam;
6
+ Characteristic = CharacteristicParam;
7
+
8
+ this.platform = platform;
9
+ this.log = platform.log;
10
+ this.storage = platform.storage;
11
+
12
+ this.id = lightConfig["id"];
13
+ this.type = "lightbulb";
14
+ this.name = lightConfig["name"];
15
+ this.rejectUnauthorized = lightConfig["rejectUnauthorized"] === undefined ? true: lightConfig["rejectUnauthorized"] === true;
16
+ this.onURL = lightConfig["on_url"] || "";
17
+ this.onMethod = lightConfig["on_method"] || "GET";
18
+ this.onBody = lightConfig["on_body"] || "";
19
+ this.onForm = lightConfig["on_form"] || "";
20
+ this.onHeaders = lightConfig["on_headers"] || "{}";
21
+ this.offURL = lightConfig["off_url"] || "";
22
+ this.offMethod = lightConfig["off_method"] || "GET";
23
+ this.offBody = lightConfig["off_body"] || "";
24
+ this.offForm = lightConfig["off_form"] || "";
25
+ this.offHeaders = lightConfig["off_headers"] || "{}";
26
+ this.brightnessURL = lightConfig["brightness_url"] || "";
27
+ this.brightnessMethod = lightConfig["brightness_method"] || "GET";
28
+ this.brightnessBody = lightConfig["brightness_body"] || "";
29
+ this.brightnessForm = lightConfig["brightness_form"] || "";
30
+ this.brightnessHeaders = lightConfig["brightness_headers"] || "{}";
31
+ this.brightnessFactor = lightConfig["brightness_factor"] || 1;
32
+
33
+ this.informationService = new Service.AccessoryInformation();
34
+ this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
35
+ this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookLightAccessory-" + this.name);
36
+ this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookLightAccessory-" + this.id);
37
+
38
+ this.service = new Service.Lightbulb(this.name);
39
+ this.service.getCharacteristic(Characteristic.On).on('get', this.getState.bind(this)).on('set', this.setState.bind(this));
40
+ this.service.getCharacteristic(Characteristic.Brightness).on('get', this.getBrightness.bind(this)).on('set', this.setBrightness.bind(this));
41
+ }
42
+
43
+ HttpWebHookLightBulbAccessory.prototype.changeFromServer = function(urlParams) {
44
+ var cachedState = this.storage.getItemSync("http-webhook-" + this.id);
45
+ if (cachedState === undefined) {
46
+ cachedState = false;
47
+ }
48
+ var cachedBrightness = this.storage.getItemSync("http-webhook-brightness-" + this.id);
49
+ if (cachedBrightness === undefined) {
50
+ cachedBrightness = -1;
51
+ }
52
+ if (!urlParams.state && !urlParams.value) {
53
+ return {
54
+ "success" : true,
55
+ "brightness" : cachedBrightness,
56
+ "state" : cachedState
57
+ };
58
+ }
59
+ else {
60
+ var brightness = urlParams.value || cachedBrightness;
61
+ var state = urlParams.state || cachedState;
62
+ var stateBool = state === "true" || state === true;
63
+ this.storage.setItemSync("http-webhook-" + this.id, stateBool);
64
+ var brightnessInt = parseInt(brightness);
65
+ this.storage.setItemSync("http-webhook-brightness-" + this.id, brightnessInt);
66
+ if (cachedState !== stateBool || cachedBrightness != brightnessInt) {
67
+ var brightnessToSet = Math.ceil(brightnessInt / this.brightnessFactor);
68
+ this.log(this.name + ": Change HomeKit state for light to '%s'.", stateBool);
69
+ this.log(this.name + ": Change HomeKit brightness for light to '%s'.", brightnessToSet);
70
+ this.service.getCharacteristic(Characteristic.On).updateValue(stateBool, undefined, Constants.CONTEXT_FROM_WEBHOOK);
71
+ this.service.getCharacteristic(Characteristic.Brightness).updateValue(brightnessToSet, undefined, Constants.CONTEXT_FROM_WEBHOOK);
72
+ }
73
+ return {
74
+ "success" : true
75
+ };
76
+ }
77
+ };
78
+
79
+ HttpWebHookLightBulbAccessory.prototype.getState = function(callback) {
80
+ this.log.debug(this.name + ": Getting current state for '%s'...", this.id);
81
+ var state = this.storage.getItemSync("http-webhook-" + this.id);
82
+ if (state === undefined) {
83
+ state = false;
84
+ }
85
+ callback(null, state);
86
+ };
87
+
88
+ HttpWebHookLightBulbAccessory.prototype.setState = function(powerOn, callback, context) {
89
+ this.log(this.name + ": Light state for '%s'...", this.id);
90
+ this.storage.setItemSync("http-webhook-" + this.id, powerOn);
91
+ var urlToCall = this.onURL;
92
+ var urlMethod = this.onMethod;
93
+ var urlBody = this.onBody;
94
+ var urlForm = this.onForm;
95
+ var urlHeaders = this.onHeaders;
96
+ if (!powerOn) {
97
+ urlToCall = this.offURL;
98
+ urlMethod = this.offMethod;
99
+ urlBody = this.offBody;
100
+ urlForm = this.offForm;
101
+ urlHeaders = this.offHeaders;
102
+ }
103
+ Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context);
104
+ };
105
+
106
+ HttpWebHookLightBulbAccessory.prototype.getBrightness = function(callback) {
107
+ this.log.debug(this.name + ": Getting current brightness for '%s'...", this.id);
108
+ var state = this.storage.getItemSync("http-webhook-" + this.id);
109
+ if (state === undefined) {
110
+ state = false;
111
+ }
112
+ var brightness = 0;
113
+ if (state) {
114
+ brightness = this.storage.getItemSync("http-webhook-brightness-" + this.id);
115
+ if (brightness === undefined) {
116
+ brightness = 100;
117
+ }
118
+ }
119
+ callback(null, parseInt(brightness));
120
+ };
121
+
122
+ HttpWebHookLightBulbAccessory.prototype.setBrightness = function(brightness, callback, context) {
123
+ this.log(this.name + ": Light brightness for '%s'...", this.id);
124
+ var newState = brightness > 0;
125
+ this.storage.setItemSync("http-webhook-" + this.id, newState);
126
+ this.storage.setItemSync("http-webhook-brightness-" + this.id, brightness);
127
+ var brightnessFactor = this.brightnessFactor;
128
+ var brightnessToSet = Math.ceil(brightness * brightnessFactor);
129
+ var urlToCall = this.replaceVariables(this.brightnessURL, newState, brightnessToSet);
130
+ var urlMethod = this.brightnessMethod;
131
+ var urlBody = this.brightnessBody;
132
+ var urlForm = this.brightnessForm;
133
+ var urlHeaders = this.brightnessHeaders;
134
+
135
+ if (urlForm) {
136
+ urlForm = this.replaceVariables(urlForm, newState, brightnessToSet);
137
+ }
138
+ else if (urlBody) {
139
+ urlBody = this.replaceVariables(urlBody, newState, brightnessToSet);
140
+ }
141
+
142
+ Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context);
143
+ };
144
+
145
+ HttpWebHookLightBulbAccessory.prototype.replaceVariables = function(text, state, brightness) {
146
+ return text.replace("%statusPlaceholder", state).replace("%brightnessPlaceholder", brightness);
147
+ };
148
+
149
+ HttpWebHookLightBulbAccessory.prototype.getServices = function() {
150
+ return [ this.service, this.informationService ];
151
+ };
152
+
153
+ module.exports = HttpWebHookLightBulbAccessory;
@@ -0,0 +1,125 @@
1
+ const Constants = require('../../Constants');
2
+ const Util = require('../../Util');
3
+
4
+ function HttpWebHookLockMechanismAccessory(ServiceParam, CharacteristicParam, platform, lockMechanismOpenerConfig) {
5
+ Service = ServiceParam;
6
+ Characteristic = CharacteristicParam;
7
+
8
+ this.platform = platform;
9
+ this.log = platform.log;
10
+ this.storage = platform.storage;
11
+
12
+ this.id = lockMechanismOpenerConfig["id"];
13
+ this.name = lockMechanismOpenerConfig["name"];
14
+ this.type = "lockmechanism";
15
+ this.rejectUnauthorized = lockMechanismOpenerConfig["rejectUnauthorized"] === undefined ? true: lockMechanismOpenerConfig["rejectUnauthorized"] === true;
16
+ this.setLockTargetStateOpenURL = lockMechanismOpenerConfig["open_url"] || "";
17
+ this.setLockTargetStateOpenMethod = lockMechanismOpenerConfig["open_method"] || "GET";
18
+ this.setLockTargetStateOpenBody = lockMechanismOpenerConfig["open_body"] || "";
19
+ this.setLockTargetStateOpenForm = lockMechanismOpenerConfig["open_form"] || "";
20
+ this.setLockTargetStateOpenHeaders = lockMechanismOpenerConfig["open_headers"] || "{}";
21
+ this.setLockTargetStateCloseURL = lockMechanismOpenerConfig["close_url"] || "";
22
+ this.setLockTargetStateCloseMethod = lockMechanismOpenerConfig["close_method"] || "GET";
23
+ this.setLockTargetStateCloseBody = lockMechanismOpenerConfig["close_body"] || "";
24
+ this.setLockTargetStateCloseForm = lockMechanismOpenerConfig["close_form"] || "";
25
+ this.setLockTargetStateCloseHeaders = lockMechanismOpenerConfig["close_headers"] || "{}";
26
+
27
+ this.informationService = new Service.AccessoryInformation();
28
+ this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
29
+ this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookLockMechanismAccessory-" + this.name);
30
+ this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookLockMechanismAccessory-" + this.id);
31
+
32
+ this.service = new Service.LockMechanism(this.name);
33
+ this.service.getCharacteristic(Characteristic.LockTargetState).on('get', this.getLockTargetState.bind(this)).on('set', this.setLockTargetState.bind(this));
34
+ this.service.getCharacteristic(Characteristic.LockCurrentState).on('get', this.getLockCurrentState.bind(this));
35
+ }
36
+
37
+ HttpWebHookLockMechanismAccessory.prototype.changeFromServer = function(urlParams) {
38
+ if (urlParams.lockcurrentstate != null) {
39
+ var cachedLockCurrentState = this.storage.getItemSync("http-webhook-lock-current-state-" + this.id);
40
+ if (cachedLockCurrentState === undefined) {
41
+ cachedLockCurrentState = Characteristic.LockCurrentState.SECURED;
42
+ }
43
+ this.storage.setItemSync("http-webhook-lock-current-state-" + this.id, urlParams.lockcurrentstate);
44
+ if (cachedLockCurrentState !== urlParams.lockcurrentstate) {
45
+ if (urlParams.lockcurrentstate) {
46
+ this.log(this.name + ": Change Current Lock State for locking to '%s'.", urlParams.lockcurrentstate);
47
+ this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(urlParams.lockcurrentstate, undefined, Constants.CONTEXT_FROM_WEBHOOK);
48
+ }
49
+ }
50
+ }
51
+ if (urlParams.locktargetstate != null) {
52
+ var cachedLockTargetState = this.storage.getItemSync("http-webhook-lock-target-state-" + this.id);
53
+ if (cachedLockTargetState === undefined) {
54
+ cachedLockTargetState = Characteristic.LockTargetState.SECURED;
55
+ }
56
+ this.storage.setItemSync("http-webhook-lock-target-state-" + this.id, urlParams.locktargetstate);
57
+ if (cachedLockTargetState !== urlParams.locktargetstate) {
58
+ if (urlParams.locktargetstate) {
59
+ this.log(this.name + ": Change Target Lock State for locking to '%s'.", urlParams.locktargetstate);
60
+ this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(urlParams.locktargetstate, undefined, Constants.CONTEXT_FROM_WEBHOOK);
61
+ }
62
+ }
63
+ }
64
+ return {
65
+ "success" : true,
66
+ "currentState" : cachedLockCurrentState,
67
+ "targetState" : cachedLockTargetState
68
+ };
69
+ }
70
+
71
+ HttpWebHookLockMechanismAccessory.prototype.getLockTargetState = function(callback) {
72
+ this.log.debug(this.name + ": Getting current Target Lock State for '%s'...", this.id);
73
+ var state = this.storage.getItemSync("http-webhook-lock-target-state-" + this.id);
74
+ if (state === undefined) {
75
+ state = Characteristic.LockTargetState.SECURED;
76
+ }
77
+ callback(null, state);
78
+ };
79
+
80
+ HttpWebHookLockMechanismAccessory.prototype.setLockTargetState = function(homeKitState, callback, context) {
81
+ var doLock = homeKitState === Characteristic.LockTargetState.SECURED;
82
+ var newHomeKitState = doLock ? Characteristic.LockCurrentState.SECURED : Characteristic.LockCurrentState.UNSECURED;
83
+ var newHomeKitStateTarget = doLock ? Characteristic.LockTargetState.SECURED : Characteristic.LockTargetState.UNSECURED;
84
+
85
+ this.log(this.name + ": Target Lock State for '%s' to '%s'...", this.id, doLock);
86
+ this.storage.setItemSync("http-webhook-lock-target-state-" + this.id, homeKitState);
87
+ var urlToCall = this.setLockTargetStateCloseURL;
88
+ var urlMethod = this.setLockTargetStateCloseMethod;
89
+ var urlBody = this.setLockTargetStateCloseBody;
90
+ var urlForm = this.setLockTargetStateCloseForm;
91
+ var urlHeaders = this.setLockTargetStateCloseHeaders;
92
+
93
+ if (!doLock) {
94
+ urlToCall = this.setLockTargetStateOpenURL;
95
+ urlMethod = this.setLockTargetStateOpenMethod;
96
+ urlBody = this.setLockTargetStateOpenBody;
97
+ urlForm = this.setLockTargetStateOpenForm;
98
+ urlHeaders = this.setLockTargetStateOpenHeaders;
99
+ }
100
+
101
+ Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context, (function() {
102
+ this.storage.setItemSync("http-webhook-lock-current-state-" + this.id, newHomeKitState);
103
+ this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(newHomeKitStateTarget, undefined, null);
104
+ this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(newHomeKitState, undefined, null);
105
+ }).bind(this), (function() {
106
+ this.storage.setItemSync("http-webhook-lock-current-state-" + this.id, Characteristic.LockCurrentState.UNKNOWN);
107
+ this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(newHomeKitStateTarget, undefined, null);
108
+ this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(Characteristic.LockCurrentState.UNKNOWN, undefined, null);
109
+ }).bind(this));
110
+ };
111
+
112
+ HttpWebHookLockMechanismAccessory.prototype.getLockCurrentState = function(callback) {
113
+ this.log.debug(this.name + ": Getting Current Lock State for '%s'...", this.id);
114
+ var state = this.storage.getItemSync("http-webhook-lock-current-state-" + this.id);
115
+ if (state === undefined) {
116
+ state = Characteristic.LockCurrentState.SECURED;
117
+ }
118
+ callback(null, state);
119
+ };
120
+
121
+ HttpWebHookLockMechanismAccessory.prototype.getServices = function() {
122
+ return [ this.service, this.informationService ];
123
+ };
124
+
125
+ module.exports = HttpWebHookLockMechanismAccessory;
@@ -0,0 +1,125 @@
1
+ const Constants = require('../../Constants');
2
+ const Util = require('../../Util');
3
+
4
+ function HttpWebHookOutletAccessory(ServiceParam, CharacteristicParam, platform, outletConfig) {
5
+ Service = ServiceParam;
6
+ Characteristic = CharacteristicParam;
7
+
8
+ this.platform = platform;
9
+ this.log = platform.log;
10
+ this.storage = platform.storage;
11
+
12
+ this.id = outletConfig["id"];
13
+ this.name = outletConfig["name"];
14
+ this.type = "outlet";
15
+ this.rejectUnauthorized = outletConfig["rejectUnauthorized"] === undefined ? true: outletConfig["rejectUnauthorized"] === true;
16
+ this.onURL = outletConfig["on_url"] || "";
17
+ this.onMethod = outletConfig["on_method"] || "GET";
18
+ this.onBody = outletConfig["on_body"] || "";
19
+ this.onForm = outletConfig["on_form"] || "";
20
+ this.onHeaders = outletConfig["on_headers"] || "{}";
21
+ this.offURL = outletConfig["off_url"] || "";
22
+ this.offMethod = outletConfig["off_method"] || "GET";
23
+ this.offBody = outletConfig["off_body"] || "";
24
+ this.offForm = outletConfig["off_form"] || "";
25
+ this.offHeaders = outletConfig["off_headers"] || "{}";
26
+
27
+ this.informationService = new Service.AccessoryInformation();
28
+ this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
29
+ this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookOutletAccessory-" + this.name);
30
+ this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookOutletAccessory-" + this.id);
31
+
32
+ this.service = new Service.Outlet(this.name);
33
+ this.service.getCharacteristic(Characteristic.On).on('get', this.getState.bind(this)).on('set', this.setState.bind(this));
34
+ this.service.getCharacteristic(Characteristic.OutletInUse).on('get', this.getStateInUse.bind(this));
35
+ }
36
+
37
+ HttpWebHookOutletAccessory.prototype.changeFromServer = function(urlParams) {
38
+ var cachedState = this.storage.getItemSync("http-webhook-" + this.id);
39
+ if (cachedState === undefined) {
40
+ cachedState = false;
41
+ }
42
+ var cachedStateInUse = this.storage.getItemSync("http-webhook-" + this.id + "-inUse");
43
+ if (cachedStateInUse === undefined) {
44
+ cachedStateInUse = false;
45
+ }
46
+ if (!urlParams.state && !urlParams.stateOutletInUse) {
47
+ return {
48
+ "success" : true,
49
+ "state" : cachedState,
50
+ "stateOutletInUse" : cachedStateInUse
51
+ };
52
+ }
53
+ else {
54
+ if (urlParams.state) {
55
+ var state = urlParams.state;
56
+ var stateBool = state === "true";
57
+ this.storage.setItemSync("http-webhook-" + this.id, stateBool);
58
+ // this.log("[INFO Http WebHook Server] State change of
59
+ // '%s'
60
+ // to '%s'.",accessory.id,stateBool);
61
+ if (cachedState !== stateBool) {
62
+ this.log(this.name + ": Change HomeKit state for outlet to '%s'.", stateBool);
63
+ this.service.getCharacteristic(Characteristic.On).updateValue(stateBool, undefined, Constants.CONTEXT_FROM_WEBHOOK);
64
+ }
65
+ }
66
+ if (urlParams.stateOutletInUse) {
67
+ var stateOutletInUse = urlParams.stateOutletInUse;
68
+ var stateOutletInUseBool = stateOutletInUse === "true";
69
+ this.storage.setItemSync("http-webhook-" + this.id + "-inUse", stateOutletInUseBool);
70
+ // this.log("[INFO Http WebHook Server] State change of
71
+ // '%s'
72
+ // to '%s'.",accessory.id,stateBool);
73
+ if (cachedStateInUse !== stateOutletInUseBool) {
74
+ this.log(this.name + ": Change HomeKit stateInUse for outlet to '%s'.", stateOutletInUseBool);
75
+ this.service.getCharacteristic(Characteristic.OutletInUse).updateValue(stateOutletInUseBool, undefined, Constants.CONTEXT_FROM_WEBHOOK);
76
+ }
77
+ }
78
+ return {
79
+ "success" : true
80
+ };
81
+ }
82
+ }
83
+
84
+ HttpWebHookOutletAccessory.prototype.getState = function(callback) {
85
+ this.log.debug(this.name + ": Getting current state for '%s'...", this.id);
86
+ var state = this.storage.getItemSync("http-webhook-" + this.id);
87
+ if (state === undefined) {
88
+ state = false;
89
+ }
90
+ callback(null, state);
91
+ };
92
+
93
+ HttpWebHookOutletAccessory.prototype.getStateInUse = function(callback) {
94
+ this.log.debug(this.name + ": Getting current state for '%s'...", this.id);
95
+ var stateInUse = this.storage.getItemSync("http-webhook-" + this.id + "-inUse");
96
+ if (stateInUse === undefined) {
97
+ stateInUse = false;
98
+ }
99
+ callback(null, stateInUse);
100
+ };
101
+
102
+ HttpWebHookOutletAccessory.prototype.setState = function(powerOn, callback, context) {
103
+ this.log(this.name + ": Switch outlet state for '%s'...", this.id);
104
+ this.storage.setItemSync("http-webhook-" + this.id, powerOn);
105
+ var urlToCall = this.onURL;
106
+ var urlMethod = this.onMethod;
107
+ var urlBody = this.onBody;
108
+ var urlForm = this.onForm;
109
+ var urlHeaders = this.onHeaders;
110
+ if (!powerOn) {
111
+ urlToCall = this.offURL;
112
+ urlMethod = this.offMethod;
113
+ urlBody = this.offBody;
114
+ urlForm = this.offForm;
115
+ urlHeaders = this.offHeaders;
116
+ }
117
+
118
+ Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context);
119
+ };
120
+
121
+ HttpWebHookOutletAccessory.prototype.getServices = function() {
122
+ return [ this.service, this.informationService ];
123
+ };
124
+
125
+ module.exports = HttpWebHookOutletAccessory;
@@ -0,0 +1,93 @@
1
+ const Constants = require('../../Constants');
2
+ const Util = require('../../Util');
3
+
4
+ function HttpWebHookPushButtonAccessory(ServiceParam, CharacteristicParam, platform, pushButtonConfig) {
5
+ Service = ServiceParam;
6
+ Characteristic = CharacteristicParam;
7
+
8
+ this.platform = platform;
9
+ this.log = platform.log;
10
+ this.storage = platform.storage;
11
+
12
+ this.id = pushButtonConfig["id"];
13
+ this.type = "pushbutton";
14
+ this.name = pushButtonConfig["name"];
15
+ this.rejectUnauthorized = pushButtonConfig["rejectUnauthorized"] === undefined ? true: pushButtonConfig["rejectUnauthorized"] === true;
16
+ this.pushURL = pushButtonConfig["push_url"] || "";
17
+ this.pushMethod = pushButtonConfig["push_method"] || "GET";
18
+ this.pushBody = pushButtonConfig["push_body"] || "";
19
+ this.pushForm = pushButtonConfig["push_form"] || "";
20
+ this.pushHeaders = pushButtonConfig["push_headers"] || "{}";
21
+
22
+ this.informationService = new Service.AccessoryInformation();
23
+ this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
24
+ this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookPushButtonAccessory-" + this.name);
25
+ this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookPushButtonAccessory-" + this.id);
26
+
27
+ this.service = new Service.Switch(this.name);
28
+ this.service.getCharacteristic(Characteristic.On).on('get', this.getState.bind(this)).on('set', this.setState.bind(this));
29
+ }
30
+
31
+ HttpWebHookPushButtonAccessory.prototype.changeFromServer = function(urlParams) {
32
+ if (!urlParams.state) {
33
+ return {
34
+ "success" : true
35
+ };
36
+ }
37
+ else {
38
+ var state = urlParams.state;
39
+ var stateBool = state === "true";
40
+ // this.log("[INFO Http WebHook Server] State change of '%s'
41
+ // to '%s'.",accessory.id,stateBool);
42
+ if (stateBool) {
43
+ this.log(this.name + ": Change HomeKit state for push button to '%s'.", stateBool);
44
+ this.service.getCharacteristic(Characteristic.On).updateValue(stateBool, undefined, Constants.CONTEXT_FROM_WEBHOOK);
45
+ setTimeout(function() {
46
+ this.service.getCharacteristic(Characteristic.On).updateValue(false, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL);
47
+ }.bind(this), 1000);
48
+ }
49
+ return {
50
+ "success" : true
51
+ };
52
+ }
53
+ }
54
+
55
+ HttpWebHookPushButtonAccessory.prototype.getState = function(callback) {
56
+ this.log.debug(this.name + ": Getting current state for '%s'...", this.id);
57
+ var state = false;
58
+ callback(null, state);
59
+ };
60
+
61
+ HttpWebHookPushButtonAccessory.prototype.setState = function(powerOn, callback, context) {
62
+ this.log(this.name + ": Push buttons state change for '%s'...", this.id);
63
+ if (!powerOn) {
64
+ callback(null);
65
+ }
66
+ else if (this.pushURL === "" || context === Constants.CONTEXT_FROM_WEBHOOK) {
67
+ callback(null);
68
+ setTimeout(function() {
69
+ this.service.getCharacteristic(Characteristic.On).updateValue(false, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL);
70
+ }.bind(this), 1000);
71
+ }
72
+ else {
73
+ var urlToCall = this.pushURL;
74
+ var urlMethod = this.pushMethod;
75
+ var urlBody = this.pushBody;
76
+ var urlForm = this.pushForm;
77
+ var urlHeaders = this.pushHeaders;
78
+
79
+ var onSuccessAndFailureCallback = (function() {
80
+ setTimeout(function() {
81
+ this.service.getCharacteristic(Characteristic.On).updateValue(false, undefined, Constants.CONTEXT_FROM_TIMEOUTCALL);
82
+ }.bind(this), 1000);
83
+ }).bind(this);
84
+
85
+ Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context, onSuccessAndFailureCallback, onSuccessAndFailureCallback);
86
+ }
87
+ };
88
+
89
+ HttpWebHookPushButtonAccessory.prototype.getServices = function() {
90
+ return [ this.service, this.informationService ];
91
+ };
92
+
93
+ module.exports = HttpWebHookPushButtonAccessory;
@@ -0,0 +1,104 @@
1
+ const Constants = require('../../Constants');
2
+ const Util = require('../../Util');
3
+
4
+ function HttpWebHookSecurityAccessory(ServiceParam, CharacteristicParam, platform, securityConfig) {
5
+ Service = ServiceParam;
6
+ Characteristic = CharacteristicParam;
7
+
8
+ this.platform = platform;
9
+ this.log = platform.log;
10
+ this.storage = platform.storage;
11
+
12
+ this.id = securityConfig["id"];
13
+ this.name = securityConfig["name"];
14
+ this.type = "security";
15
+ this.rejectUnauthorized = securityConfig["rejectUnauthorized"] === undefined ? true: securityConfig["rejectUnauthorized"] === true;
16
+ this.setStateURL = securityConfig["set_state_url"] || "";
17
+ this.setStateMethod = securityConfig["set_state_method"] || "GET";
18
+ this.setStateBody = securityConfig["set_state_body"] || "";
19
+ this.setStateForm = securityConfig["set_state_form"] || "";
20
+ this.setStateHeaders = securityConfig["set_state_headers"] || "{}";
21
+
22
+ this.informationService = new Service.AccessoryInformation();
23
+ this.informationService.setCharacteristic(Characteristic.Manufacturer, "HttpWebHooksPlatform");
24
+ this.informationService.setCharacteristic(Characteristic.Model, "HttpWebHookSecurityAccessory-" + this.name);
25
+ this.informationService.setCharacteristic(Characteristic.SerialNumber, "HttpWebHookSecurityAccessory-" + this.id);
26
+
27
+ this.service = new Service.SecuritySystem(this.name);
28
+ this.service.getCharacteristic(Characteristic.SecuritySystemTargetState).on('get', this.getTargetSecurityState.bind(this)).on('set', this.setTargetSecurityState.bind(this));
29
+ this.service.getCharacteristic(Characteristic.SecuritySystemCurrentState).on('get', this.getCurrentSecurityState.bind(this));
30
+ }
31
+
32
+ HttpWebHookSecurityAccessory.prototype.changeFromServer = function(urlParams) {
33
+ var cachedCurrentState = this.storage.getItemSync("http-webhook-current-security-state-" + this.id);
34
+ if (cachedCurrentState === undefined) {
35
+ cachedCurrentState = Characteristic.SecuritySystemCurrentState.DISARMED;
36
+ }
37
+ if (!urlParams.currentstate && !urlParams.targetstate) {
38
+ return {
39
+ "success" : true,
40
+ "currentState" : cachedCurrentState
41
+ };
42
+ }
43
+
44
+ if (urlParams.currentstate != null) {
45
+ this.storage.setItemSync("http-webhook-current-security-state-" + this.id, urlParams.currentstate);
46
+ if (cachedCurrentState !== urlParams.currentstate) {
47
+ this.log(this.name + ": Change current state for security to '%d'.", urlParams.currentstate);
48
+ this.service.getCharacteristic(Characteristic.SecuritySystemCurrentState).updateValue(urlParams.currentstate, undefined, Constants.CONTEXT_FROM_WEBHOOK);
49
+ }
50
+ }
51
+
52
+ if (urlParams.targetstate != null) {
53
+ var cachedState = this.storage.getItemSync("http-webhook-target-security-state-" + this.id);
54
+ if (cachedState === undefined) {
55
+ cachedState = Characteristic.SecuritySystemTargetState.DISARM;
56
+ }
57
+ this.storage.setItemSync("http-webhook-target-security-state-" + this.id, urlParams.targetstate);
58
+ if (cachedState !== urlParams.targetstate) {
59
+ this.log(this.name + ": Change target state for security to '%d'.", urlParams.targetstate);
60
+ this.service.getCharacteristic(Characteristic.SecuritySystemTargetState).updateValue(urlParams.targetstate, undefined, Constants.CONTEXT_FROM_WEBHOOK);
61
+ }
62
+ }
63
+ return {
64
+ "success" : true
65
+ };
66
+ }
67
+
68
+ HttpWebHookSecurityAccessory.prototype.getTargetSecurityState = function(callback) {
69
+ this.log.debug(this.name + ": Getting Target Security state for '%s'...", this.id);
70
+ var state = this.storage.getItemSync("http-webhook-target-security-state-" + this.id);
71
+ if (state === undefined) {
72
+ state = Characteristic.SecuritySystemTargetState.DISARM;
73
+ }
74
+ callback(null, state);
75
+ };
76
+
77
+ HttpWebHookSecurityAccessory.prototype.setTargetSecurityState = function(newState, callback, context) {
78
+ this.log(this.name + ": Target Security state for '%s'...", this.id);
79
+ this.storage.setItemSync("http-webhook-target-security-state-" + this.id, newState);
80
+ this.storage.setItemSync("http-webhook-current-security-state-" + this.id, newState);
81
+ var urlToCall = this.setStateURL.replace("%d", newState);
82
+ var urlMethod = this.setStateMethod;
83
+ var urlBody = this.setStateBody;
84
+ var urlForm = this.setStateForm;
85
+ var urlHeaders = this.setStateHeaders;
86
+ Util.callHttpApi(this.log, urlToCall, urlMethod, urlBody, urlForm, urlHeaders, this.rejectUnauthorized, callback, context, (function() {
87
+ this.service.getCharacteristic(Characteristic.SecuritySystemCurrentState).updateValue(newState, undefined, null);
88
+ }).bind(this));
89
+ };
90
+
91
+ HttpWebHookSecurityAccessory.prototype.getCurrentSecurityState = function(callback) {
92
+ this.log.debug(this.name + ": Getting Current Security state for '%s'...", this.id);
93
+ var state = this.storage.getItemSync("http-webhook-current-security-state-" + this.id);
94
+ if (state === undefined) {
95
+ state = Characteristic.SecuritySystemCurrentState.DISARMED;
96
+ }
97
+ callback(null, state);
98
+ };
99
+
100
+ HttpWebHookSecurityAccessory.prototype.getServices = function() {
101
+ return [ this.service, this.informationService ];
102
+ };
103
+
104
+ module.exports = HttpWebHookSecurityAccessory;