node-red-contrib-huemagic 3.0.0 → 4.0.3

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.
Files changed (138) hide show
  1. package/README.md +764 -603
  2. package/examples/Hue Bridge.json +189 -35
  3. package/examples/Hue Brightness.json +124 -14
  4. package/examples/Hue Buttons.json +88 -0
  5. package/examples/Hue Group.json +575 -63
  6. package/examples/Hue Light.json +805 -58
  7. package/examples/Hue Magic.json +69 -24
  8. package/examples/Hue Motion.json +110 -42
  9. package/examples/Hue Rules.json +105 -37
  10. package/examples/Hue Scene.json +145 -28
  11. package/examples/Hue Temperature.json +124 -14
  12. package/huemagic/animations/019-christmas.json +30 -0
  13. package/huemagic/animations/020-superhero.json +56 -0
  14. package/huemagic/animations/021-neoncity.json +52 -0
  15. package/huemagic/animations/previews/006-fireplace.gif +0 -0
  16. package/huemagic/animations/previews/013-strobo.gif +0 -0
  17. package/huemagic/animations/previews/014-randomrainbow.gif +0 -0
  18. package/huemagic/animations/previews/016-milkyway.gif +0 -0
  19. package/huemagic/animations/previews/018-forest.gif +0 -0
  20. package/huemagic/animations/previews/019-christmas.gif +0 -0
  21. package/huemagic/animations/previews/020-superhero.gif +0 -0
  22. package/huemagic/animations/previews/021-neoncity.gif +0 -0
  23. package/huemagic/hue-bridge-config.html +105 -61
  24. package/huemagic/hue-bridge-config.js +631 -319
  25. package/huemagic/hue-bridge.html +41 -21
  26. package/huemagic/hue-bridge.js +190 -396
  27. package/huemagic/hue-brightness.html +127 -59
  28. package/huemagic/hue-brightness.js +111 -55
  29. package/huemagic/hue-buttons.html +197 -0
  30. package/huemagic/hue-buttons.js +158 -0
  31. package/huemagic/hue-group.html +137 -66
  32. package/huemagic/hue-group.js +387 -403
  33. package/huemagic/hue-light.html +132 -64
  34. package/huemagic/hue-light.js +544 -378
  35. package/huemagic/hue-magic.html +36 -20
  36. package/huemagic/hue-magic.js +49 -27
  37. package/huemagic/hue-motion.html +127 -59
  38. package/huemagic/hue-motion.js +105 -101
  39. package/huemagic/hue-rules.html +121 -50
  40. package/huemagic/hue-rules.js +89 -63
  41. package/huemagic/hue-scene.html +95 -114
  42. package/huemagic/hue-scene.js +64 -95
  43. package/huemagic/hue-temperature.html +127 -59
  44. package/huemagic/hue-temperature.js +120 -56
  45. package/huemagic/icons/hue-bridge.png +0 -0
  46. package/huemagic/icons/hue-bridge.svg +3 -0
  47. package/huemagic/icons/hue-brightness.png +0 -0
  48. package/huemagic/icons/hue-brightness.svg +11 -0
  49. package/huemagic/icons/hue-buttons.png +0 -0
  50. package/huemagic/icons/hue-buttons.svg +4 -0
  51. package/huemagic/icons/hue-group.png +0 -0
  52. package/huemagic/icons/hue-group.svg +12 -0
  53. package/huemagic/icons/hue-light.png +0 -0
  54. package/huemagic/icons/hue-light.svg +3 -0
  55. package/huemagic/icons/hue-magic.png +0 -0
  56. package/huemagic/icons/hue-magic.svg +3 -0
  57. package/huemagic/icons/hue-motion.png +0 -0
  58. package/huemagic/icons/hue-motion.svg +9 -0
  59. package/huemagic/icons/hue-rules.png +0 -0
  60. package/huemagic/icons/hue-rules.svg +4 -0
  61. package/huemagic/icons/hue-scene.png +0 -0
  62. package/huemagic/icons/hue-scene.svg +5 -0
  63. package/huemagic/icons/hue-temperature.png +0 -0
  64. package/huemagic/icons/hue-temperature.svg +7 -0
  65. package/huemagic/locales/de/hue-bridge-config.html +17 -12
  66. package/huemagic/locales/de/hue-bridge-config.json +7 -5
  67. package/huemagic/locales/de/hue-bridge.html +64 -145
  68. package/huemagic/locales/de/hue-bridge.json +6 -17
  69. package/huemagic/locales/de/hue-brightness.html +50 -50
  70. package/huemagic/locales/de/hue-brightness.json +6 -5
  71. package/huemagic/locales/de/hue-buttons.html +58 -0
  72. package/huemagic/locales/de/{hue-button.json → hue-buttons.json} +7 -11
  73. package/huemagic/locales/de/hue-group.html +46 -122
  74. package/huemagic/locales/de/hue-group.json +5 -5
  75. package/huemagic/locales/de/hue-light.html +68 -116
  76. package/huemagic/locales/de/hue-light.json +5 -5
  77. package/huemagic/locales/de/hue-magic.html +65 -57
  78. package/huemagic/locales/de/hue-magic.json +2 -1
  79. package/huemagic/locales/de/hue-motion.html +47 -50
  80. package/huemagic/locales/de/hue-motion.json +5 -5
  81. package/huemagic/locales/de/hue-rules.html +68 -73
  82. package/huemagic/locales/de/hue-rules.json +5 -2
  83. package/huemagic/locales/de/hue-scene.html +23 -32
  84. package/huemagic/locales/de/hue-scene.json +5 -2
  85. package/huemagic/locales/de/hue-temperature.html +51 -48
  86. package/huemagic/locales/de/hue-temperature.json +6 -5
  87. package/huemagic/locales/en-US/hue-bridge-config.html +17 -12
  88. package/huemagic/locales/en-US/hue-bridge-config.json +5 -3
  89. package/huemagic/locales/en-US/hue-bridge.html +64 -145
  90. package/huemagic/locales/en-US/hue-bridge.json +5 -16
  91. package/huemagic/locales/en-US/hue-brightness.html +50 -50
  92. package/huemagic/locales/en-US/hue-brightness.json +5 -2
  93. package/huemagic/locales/en-US/hue-buttons.html +58 -0
  94. package/huemagic/locales/en-US/{hue-button.json → hue-buttons.json} +6 -4
  95. package/huemagic/locales/en-US/hue-group.html +46 -122
  96. package/huemagic/locales/en-US/hue-group.json +4 -2
  97. package/huemagic/locales/en-US/hue-light.html +68 -116
  98. package/huemagic/locales/en-US/hue-light.json +4 -2
  99. package/huemagic/locales/en-US/hue-magic.html +65 -57
  100. package/huemagic/locales/en-US/hue-magic.json +3 -1
  101. package/huemagic/locales/en-US/hue-motion.html +47 -50
  102. package/huemagic/locales/en-US/hue-motion.json +4 -2
  103. package/huemagic/locales/en-US/hue-rules.html +68 -73
  104. package/huemagic/locales/en-US/hue-rules.json +5 -2
  105. package/huemagic/locales/en-US/hue-scene.html +23 -32
  106. package/huemagic/locales/en-US/hue-scene.json +5 -2
  107. package/huemagic/locales/en-US/hue-temperature.html +51 -48
  108. package/huemagic/locales/en-US/hue-temperature.json +5 -2
  109. package/huemagic/utils/api.js +290 -0
  110. package/huemagic/utils/color.js +382 -0
  111. package/{utils → huemagic/utils}/merge.js +4 -6
  112. package/huemagic/utils/messages.js +447 -0
  113. package/package.json +9 -15
  114. package/examples/Hue Button.json +0 -40
  115. package/examples/Hue Switch.json +0 -40
  116. package/examples/Hue Tap.json +0 -40
  117. package/huemagic/hue-button.html +0 -132
  118. package/huemagic/hue-button.js +0 -143
  119. package/huemagic/hue-switch.html +0 -132
  120. package/huemagic/hue-switch.js +0 -163
  121. package/huemagic/hue-tap.html +0 -132
  122. package/huemagic/hue-tap.js +0 -123
  123. package/huemagic/icons/hue-button.png +0 -0
  124. package/huemagic/icons/hue-switch.png +0 -0
  125. package/huemagic/icons/hue-tap.png +0 -0
  126. package/huemagic/locales/de/hue-button.html +0 -64
  127. package/huemagic/locales/de/hue-switch.html +0 -64
  128. package/huemagic/locales/de/hue-switch.json +0 -35
  129. package/huemagic/locales/de/hue-tap.html +0 -55
  130. package/huemagic/locales/de/hue-tap.json +0 -27
  131. package/huemagic/locales/en-US/hue-button.html +0 -64
  132. package/huemagic/locales/en-US/hue-switch.html +0 -64
  133. package/huemagic/locales/en-US/hue-switch.json +0 -35
  134. package/huemagic/locales/en-US/hue-tap.html +0 -55
  135. package/huemagic/locales/en-US/hue-tap.json +0 -27
  136. package/utils/color.js +0 -9
  137. package/utils/messages.js +0 -658
  138. package/utils/rgb.js +0 -188
@@ -6,23 +6,25 @@ module.exports = function(RED)
6
6
  {
7
7
  RED.nodes.createNode(this, config);
8
8
 
9
- var scope = this;
10
- let bridge = RED.nodes.getNode(config.bridge);
11
- let { HueBridgeMessage, HueBrightnessMessage, HueGroupMessage, HueLightMessage, HueMotionMessage, HueRulesMessage, HueSwitchMessage, HueTapMessage, HueTemperatureMessage } = require('../utils/messages');
9
+ const scope = this;
10
+ const bridge = RED.nodes.getNode(config.bridge);
12
11
 
13
- //
14
- // PREVENT DEVICE MESSAGES IN FIRST 5 SECONDS
15
- this.deviceUpdates = false;
16
- setTimeout(function(){ scope.deviceUpdates = true; }, 5000);
12
+ // IS CONNECTED?
13
+ this.connected = false;
14
+
15
+ // GET BRIDGE INFORMATION INITIALLY
16
+ this.lastBridgeInformation = null;
17
+
18
+ // SAVE LAST COMMAND
19
+ this.lastCommand = null;
20
+
21
+ // NODE UI STATUS TIMEOUT
22
+ this.timeout = null;
17
23
 
18
24
  //
19
25
  // ACTIVE STATE
20
26
  this.nodeActive = true;
21
27
 
22
- //
23
- // BRIDGE INFORMATION
24
- this.bridgeInformation = {};
25
-
26
28
  //
27
29
  // CHECK CONFIG
28
30
  if(bridge == null)
@@ -33,476 +35,268 @@ module.exports = function(RED)
33
35
 
34
36
  //
35
37
  // UPDATE STATE
36
- this.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connecting"});
38
+ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connecting"});
37
39
 
38
- //
39
- // GET INFORMATION
40
- this.getBridgeInformation = function()
40
+ this.setInitialState = function()
41
41
  {
42
- bridge.client.bridge.get()
43
- .then(bridgeInformation => {
44
- var hueBridge = new HueBridgeMessage(bridgeInformation, config);
45
- scope.send(hueBridge.msg);
46
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" });
47
-
48
- // SAVE BRIDGE INFORMATION
49
- scope.bridgeInformation = hueBridge.msg.payload;
50
- })
51
- .catch(error => {
52
- scope.error(error);
53
- if(scope.nodeActive == true)
54
- {
55
- setTimeout(function(){ scope.getBridgeInformation(); }, 3000);
56
- }
57
- });
42
+ scope.status({fill: "green", shape: "dot", text: "hue-bridge.node.connected"});
58
43
  }
59
44
 
60
- //
61
- // INITIAL START
62
- this.getBridgeInformation();
63
45
 
64
46
  //
65
- // AUTO UPDATES?
66
- this.autoUpdateHueBridge = function()
47
+ // GET BRIDGE INFORMATION
48
+ this.getBridgeInformation = async function(forceReload = false)
67
49
  {
68
- if(config.autoupdates == true)
50
+ return new Promise(function(resolve, reject)
69
51
  {
70
- bridge.client.softwareUpdate.check()
71
- .then(() => {
72
- return bridge.client.softwareUpdate.get();
73
- })
74
- .then(softwareUpdate => {
75
- return bridge.client.softwareUpdate.install();
76
- })
77
- .then(() => {
78
- // UPDATING // CHECK STATUS IN 5 MINUTES
79
- if(scope.nodeActive == true)
52
+ if(forceReload === true)
53
+ {
54
+ // REFRESH INFORMATION
55
+ bridge.getBridgeInformation(true)
56
+ .then(function(updated)
80
57
  {
81
- setTimeout(function(){ scope.getBridgeInformation(); }, 60000 * 5);
82
- }
83
- })
84
- .catch(error => {
85
- // NO UPDATES AVAILABLE // TRY AGAIN IN 3H
86
- if(scope.nodeActive == true)
58
+ return scope.getBridgeInformation();
59
+ })
60
+ .then(function(bridgeInformation)
87
61
  {
88
- setTimeout(function(){ scope.autoUpdateHueBridge(); }, 60000 * 180);
89
- }
90
- });
91
- }
92
- }
93
-
94
- this.autoUpdateHueBridge();
95
-
96
- //
97
- // DEVICE UPDATES
98
- if(!config.skipglobalevents)
99
- {
100
- bridge.events.on('globalDeviceUpdates', function(message)
101
- {
102
- if(scope.deviceUpdates == false) { return; }
103
-
104
- let type = message.type;
105
- let payload = message.payload;
106
- var detType = type;
107
-
108
- // CONSTRUCT MESSAGE
109
- var message = {};
110
- message.updated = {};
111
-
112
- // ADD PAYLOAD
113
- if(type == "light")
114
- {
115
- let hueLight = new HueLightMessage(payload, {colornamer: true});
116
- message.updated = hueLight.msg;
117
- }
118
- else if(type == "group")
119
- {
120
- let hueGroup = new HueGroupMessage(payload, {colornamer: true});
121
- message.updated = hueGroup.msg;
62
+ resolve(bridgeInformation);
63
+ });
122
64
  }
123
- else if(type == "rule")
65
+ else if(scope.lastBridgeInformation !== null)
124
66
  {
125
- let hueRules = new HueRulesMessage(payload);
126
- message.updated = hueRules.msg;
67
+ let bridgeInformationCopy = Object.assign({}, scope.lastBridgeInformation);
68
+ resolve(bridgeInformationCopy);
127
69
  }
128
- else if(type == "sensor")
70
+ else
129
71
  {
130
- // DETERMINE TYPE
131
- if(payload.type == "ZLLPresence")
132
- {
133
- let hueMotion = new HueMotionMessage(payload);
134
- message.updated = hueMotion.msg;
72
+ let bridgeInformation = bridge.get("bridge", "bridge", { autoupdate: ((bridge.config.autoupdates && bridge.config.autoupdates == true) || typeof bridge.config.autoupdates == 'undefined') });
73
+ scope.lastBridgeInformation = Object.assign({}, bridgeInformation);
135
74
 
136
- detType = "motion";
137
- }
138
- else if(payload.type == "ZLLLightLevel")
139
- {
140
- let hueBrightness = new HueBrightnessMessage(payload);
141
- message.updated = hueBrightness.msg;
142
-
143
- detType = "brightness";
144
- }
145
- else if(payload.type == "ZLLTemperature")
146
- {
147
- let hueTemperature = new HueTemperatureMessage(payload);
148
- message.updated = hueTemperature.msg;
149
-
150
- detType = "temperature";
151
- }
152
- else if(payload.type == "ZLLSwitch")
153
- {
154
- let hueSwitch = new HueSwitchMessage(payload);
155
- message.updated = hueSwitch.msg;
156
-
157
- detType = "switch";
158
- }
159
- else if(payload.type == "ZGPSwitch")
160
- {
161
- let hueTap = new HueTapMessage(sensor);
162
- message.updated = hueTap.msg;
163
-
164
- detType = "tap";
165
- }
75
+ resolve(bridgeInformation);
166
76
  }
167
-
168
- // ADD TYPE
169
- message.updated.type = detType;
170
-
171
- // ADD BRIDGE INFORMATION
172
- message.info = scope.bridgeInformation;
173
-
174
- // SEND MESSAGE
175
- scope.send(message);
176
77
  });
177
78
  }
178
79
 
179
80
  //
180
- // COMMANDS
181
- this.on('input', function(msg, send, done)
81
+ // SUBSCRIBE TO UPDATES FROM THE BRIDGE
82
+ bridge.subscribe("bridge", "globalResourceUpdates", async function(info)
182
83
  {
183
- // Node-RED < 1.0
184
- send = send || function() { scope.send.apply(scope,arguments); }
185
-
186
- var commandSent = false;
84
+ let currentState = bridge.get(info.updatedType, info.id);
187
85
 
188
- // STARTING TOUCHLINK
189
- if(typeof msg.payload.touchLink != 'undefined')
86
+ // RESOURCE FOUND?
87
+ if(currentState !== false)
190
88
  {
191
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.starting-tl" });
192
- bridge.client.bridge.touchlink()
193
- .then(() => {
194
- scope.status({fill: "blue", shape: "ring", text: "hue-bridge.node.started-tl" });
195
- if(done) { done(); }
196
- setTimeout(function(){ scope.getBridgeInformation(); }, 30000);
197
- })
198
- .catch(error => {
199
- scope.error(error);
200
- if(done) { done(error); }
201
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
202
- });
203
-
204
- // COMMAND SENT
205
- commandSent = true;
206
- }
207
-
208
- // GET DEVICES
209
- if(typeof msg.payload.fetch != 'undefined')
210
- {
211
- if(msg.payload.fetch == "users")
89
+ // UPDATE COUNTER
90
+ if(info.suppressMessage === false)
212
91
  {
213
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-users" });
214
- bridge.client.users.getAll()
215
- .then(users => {
216
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
217
-
218
- var message = {};
219
- message.users = users;
220
- message.info = scope.bridgeInformation;
92
+ scope.status({fill: "blue", shape: "dot", text: "hue-bridge.node.connected"});
221
93
 
222
- send(message);
223
- if(done) { done(); }
224
- })
225
- .catch(error => {
226
- scope.error(error);
227
- if(done) { done(error); }
228
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
229
- });
94
+ if(scope.timeout !== null) { clearTimeout(scope.timeout); }
95
+ scope.timeout = setTimeout(function() {
96
+ scope.setInitialState();
97
+ }, 1000);
230
98
  }
231
- else if(msg.payload.fetch == "lights")
232
- {
233
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-lights" });
234
- bridge.client.lights.getAll()
235
- .then(lights => {
236
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
237
-
238
- var message = {};
239
- message.lights = lights;
240
- message.info = scope.bridgeInformation;
241
99
 
242
- send(message);
243
- if(done) { done(); }
244
- })
245
- .catch(error => {
246
- scope.error(error);
247
- if(done) { done(error); }
248
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
249
- });
250
- }
251
- else if(msg.payload.fetch == "groups")
100
+ // SEND MESSAGE
101
+ if(!config.skipglobalevents && (config.initevents || info.suppressMessage == false))
252
102
  {
253
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-groups" });
254
- bridge.client.groups.getAll()
255
- .then(groups => {
256
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
257
-
258
- var message = {};
259
- message.groups = groups;
260
- message.info = scope.bridgeInformation;
103
+ scope.getBridgeInformation()
104
+ .then(function(info)
105
+ {
106
+ // ADD DEVICE UPDATE TO RESPONSE
107
+ info.updated = currentState;
261
108
 
262
- send(message);
263
- if(done) { done(); }
264
- })
265
- .catch(error => {
266
- scope.error(error);
267
- if(done) { done(error); }
268
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
269
- });
270
- }
271
- else if(msg.payload.fetch == "sensors")
272
- {
273
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-sensors" });
274
- bridge.client.sensors.getAll()
275
- .then(sensors => {
276
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
109
+ // SET LAST COMMAND
110
+ if(scope.lastCommand !== null)
111
+ {
112
+ info.command = scope.lastCommand;
113
+ }
277
114
 
278
- var message = {};
279
- message.sensors = sensors;
280
- message.info = scope.bridgeInformation;
115
+ // SEND MESSAGE
116
+ scope.send(info);
281
117
 
282
- send(message);
283
- if(done) { done(); }
284
- })
285
- .catch(error => {
286
- scope.error(error);
287
- if(done) { done(error); }
288
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
118
+ // RESET LAST COMMAND
119
+ scope.lastCommand = null;
289
120
  });
290
121
  }
291
- else if(msg.payload.fetch == "scenes")
292
- {
293
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-scenes" });
294
- bridge.client.scenes.getAll()
295
- .then(scenes => {
296
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
122
+ }
297
123
 
298
- var message = {};
299
- message.scenes = scenes;
300
- message.info = scope.bridgeInformation;
124
+ // CONNECTED?
125
+ if(scope.connected == false)
126
+ {
127
+ scope.connected = true;
128
+ scope.setInitialState();
129
+ }
130
+ });
301
131
 
302
- send(message);
303
- if(done) { done(); }
304
- })
305
- .catch(error => {
306
- scope.error(error);
307
- if(done) { done(error); }
308
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
309
- });
310
- }
311
- else if(msg.payload.fetch == "rules")
312
- {
313
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-rules" });
314
- bridge.client.rules.getAll()
315
- .then(rules => {
316
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
132
+ //
133
+ // ON COMMAND
134
+ this.on('input', function(msg, send, done)
135
+ {
136
+ // REDEFINE SEND AND DONE IF NOT AVAILABLE
137
+ send = send || function() { scope.send.apply(scope,arguments); }
138
+ done = done || function() { scope.done.apply(scope,arguments); }
317
139
 
318
- var message = {};
319
- message.rules = rules;
320
- message.info = scope.bridgeInformation;
140
+ // SET LAST COMMAND
141
+ scope.lastCommand = RED.util.cloneMessage(msg);
321
142
 
322
- send(message);
323
- if(done) { done(); }
324
- })
325
- .catch(error => {
326
- scope.error(error);
327
- if(done) { done(error); }
328
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
329
- });
330
- }
331
- else if(msg.payload.fetch == "schedules")
143
+ // GET BRIDGE INFORMATION
144
+ scope.getBridgeInformation()
145
+ .then(function(bridgeInformation)
146
+ {
147
+ // START TOUCHLINK
148
+ if(typeof msg.payload != 'undefined' && typeof msg.payload.touchLink != 'undefined')
332
149
  {
333
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-schedules" });
334
- bridge.client.schedules.getAll()
335
- .then(schedules => {
336
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
337
-
338
- var message = {};
339
- message.schedules = schedules;
340
- message.info = scope.bridgeInformation;
150
+ // SET STATUS
151
+ scope.status({fill: "yellow", shape: "dot", text: "hue-bridge.node.starting-tl" });
341
152
 
342
- send(message);
153
+ // ENABLE TOUCHLINK
154
+ bridge.patch("bridge", "/config", { touchlink: true }, 1)
155
+ .then(function(status)
156
+ {
157
+ // SET STATUS
158
+ scope.status({fill: "blue", shape: "ring", text: "hue-bridge.node.started-tl" });
343
159
  if(done) { done(); }
344
- })
345
- .catch(error => {
346
- scope.error(error);
347
- if(done) { done(error); }
348
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
349
- });
350
- }
351
- else if(msg.payload.fetch == "resourceLinks")
352
- {
353
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-rlinks" });
354
- bridge.client.resourceLinks.getAll()
355
- .then(resourceLinks => {
356
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
357
-
358
- var message = {};
359
- message.resourceLinks = resourceLinks;
360
- message.info = scope.bridgeInformation;
361
160
 
362
- send(message);
363
- if(done) { done(); }
161
+ // RESET STATUS AFTER 30 SECONDS
162
+ setTimeout(function()
163
+ {
164
+ scope.setInitialState();
165
+ scope.getBridgeInformation(true);
166
+ }, 30000);
364
167
  })
365
- .catch(error => {
168
+ .catch(function(error)
169
+ {
366
170
  scope.error(error);
367
171
  if(done) { done(error); }
368
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
369
172
  });
370
173
  }
371
- else if(msg.payload.fetch == "timeZones")
174
+ // FETCH RESOURCES
175
+ else if(typeof msg.payload != 'undefined' && typeof msg.payload.fetch != 'undefined')
372
176
  {
373
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-timezones" });
374
- bridge.client.timeZones.getAll()
375
- .then(timeZones => {
376
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
177
+ let fetchTypes = [];
178
+ if(typeof msg.payload.fetch == 'string') { fetchTypes.push(msg.payload.fetch); }
179
+ else if(typeof msg.payload.fetch == 'object') { fetchTypes = msg.payload.fetch; }
180
+ else { return false; }
377
181
 
378
- var message = {};
379
- message.timeZones = timeZones;
380
- message.info = scope.bridgeInformation;
182
+ scope.status({fill: "blue", shape: "dot", text: "hue-bridge.node.f-resources" });
381
183
 
382
- send(message);
383
- if(done) { done(); }
384
- })
385
- .catch(error => {
386
- scope.error(error);
387
- if(done) { done(error); }
388
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
184
+ // FETCH
185
+ bridgeInformation.results = {};
186
+ fetchTypes.forEach(function(fetch)
187
+ {
188
+ bridgeInformation.results[fetch] = bridge.get(fetch);
389
189
  });
390
- }
391
- else if(msg.payload.fetch == "internetServices")
392
- {
393
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-internet" });
394
- bridge.client.internetServices.get()
395
- .then(internetServices => {
396
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
397
190
 
398
- var message = {};
399
- message.internetServices = internetServices;
400
- message.info = scope.bridgeInformation;
191
+ // SEND RESULTS
192
+ if(scope.lastCommand !== null)
193
+ {
194
+ bridgeInformation.command = scope.lastCommand;
195
+ }
401
196
 
402
- send(message);
403
- if(done) { done(); }
404
- })
405
- .catch(error => {
406
- scope.error(error);
407
- if(done) { done(error); }
408
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
409
- });
410
- }
411
- else if(msg.payload.fetch == "portal")
412
- {
413
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.f-portal" });
414
- bridge.client.portal.get()
415
- .then(portal => {
416
- setTimeout(function(){ scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.connected" }); }, 2000);
197
+ send(bridgeInformation);
417
198
 
418
- var message = {};
419
- message.portal = portal;
420
- message.info = scope.bridgeInformation;
199
+ // RESET LAST COMMAND
200
+ scope.lastCommand = null;
201
+ if(done) { done(); }
421
202
 
422
- send(message);
423
- if(done) { done(); }
424
- })
425
- .catch(error => {
426
- scope.error(error);
427
- if(done) { done(error); }
428
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
429
- });
203
+ // RESET STATUS
204
+ setTimeout(function(){ scope.setInitialState(); }, 2000);
430
205
  }
206
+ // UPDATE SETTINGS
207
+ else if(typeof msg.payload != 'undefined' && typeof msg.payload.settings != 'undefined')
208
+ {
209
+ let patchObject = {};
431
210
 
432
- commandSent = true;
433
- }
434
-
435
- // UPDATING SETTINGS
436
- if(typeof msg.payload.settings != 'undefined')
437
- {
438
- scope.status({fill: "grey", shape: "dot", text: "hue-bridge.node.updating-settings" });
439
- bridge.client.bridge.get()
440
- .then(bridgeConfig => {
441
211
  if(typeof msg.payload.settings.name != 'undefined')
442
212
  {
443
- bridgeConfig.name = msg.payload.settings.name;
213
+ patchObject.name = msg.payload.settings.name;
444
214
  }
445
215
  if(typeof msg.payload.settings.zigbeeChannel != 'undefined')
446
216
  {
447
- bridgeConfig.zigbeeChannel = msg.payload.settings.zigbeeChannel;
217
+ patchObject.zigbeechannel = msg.payload.settings.zigbeeChannel;
448
218
  }
449
219
  if(typeof msg.payload.settings.ipAddress != 'undefined')
450
220
  {
451
- bridgeConfig.ipAddress = msg.payload.settings.ipAddress;
221
+ patchObject.ipaddress = msg.payload.settings.ipAddress;
452
222
  }
453
223
  if(typeof msg.payload.settings.dhcpEnabled != 'undefined')
454
224
  {
455
- bridgeConfig.dhcpEnabled = msg.payload.settings.dhcpEnabled;
225
+ patchObject.dhcp = msg.payload.settings.dhcpEnabled;
456
226
  }
457
227
  if(typeof msg.payload.settings.netmask != 'undefined')
458
228
  {
459
- bridgeConfig.netmask = msg.payload.settings.netmask;
229
+ patchObject.netmask = msg.payload.settings.netmask;
460
230
  }
461
231
  if(typeof msg.payload.settings.gateway != 'undefined')
462
232
  {
463
- bridgeConfig.gateway = msg.payload.settings.gateway;
233
+ patchObject.gateway = msg.payload.settings.gateway;
464
234
  }
465
235
  if(typeof msg.payload.settings.proxyPort != 'undefined')
466
236
  {
467
- bridgeConfig.proxyPort = msg.payload.settings.proxyPort;
237
+ patchObject.proxyport = msg.payload.settings.proxyPort;
468
238
  }
469
239
  if(typeof msg.payload.settings.proxyAddress != 'undefined')
470
240
  {
471
- bridgeConfig.proxyAddress = msg.payload.settings.proxyAddress;
241
+ patchObject.proxyaddress = msg.payload.settings.proxyAddress;
472
242
  }
473
243
  if(typeof msg.payload.settings.timeZone != 'undefined')
474
244
  {
475
- bridgeConfig.timeZone = msg.payload.settings.timeZone;
245
+ patchObject.timezone = msg.payload.settings.timeZone;
476
246
  }
477
- return bridge.client.bridge.save(bridgeConfig);
478
- })
479
- .then(bridgeInformation => {
480
- scope.status({fill: "green", shape: "dot", text: "hue-bridge.node.settings-updated" });
481
- if(done) { done(); }
482
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
483
- })
484
- .catch(error => {
485
- scope.error(error);
486
- if(done) { done(error); }
487
- setTimeout(function(){ scope.getBridgeInformation(); }, 2000);
488
- });
489
-
490
- // COMMAND SENT
491
- commandSent = true;
492
- }
493
247
 
494
- // GET INFORMATION // FALLBACK
495
- if(commandSent == false)
496
- {
497
- scope.getBridgeInformation();
498
- }
499
- });
248
+ // PATCH BRIDGE!
249
+ if(Object.values(patchObject).length > 0)
250
+ {
251
+ // SET UPDATING STATUS
252
+ scope.status({fill: "yellow", shape: "dot", text: "hue-bridge.node.updating-settings" });
253
+
254
+ // PATCH BRIDGE
255
+ bridge.patch("bridge", "/config", patchObject, 1)
256
+ .then(function(patched)
257
+ {
258
+ // GET RELOADED BRIDGE INFORMATION
259
+ return scope.getBridgeInformation(true);
260
+ })
261
+ .then(function(bridgeInformation)
262
+ {
263
+ scope.status({fill: "blue", shape: "dot", text: "hue-bridge.node.settings-updated" });
264
+
265
+ // SET LAST COMMAND
266
+ if(scope.lastCommand !== null)
267
+ {
268
+ bridgeInformation.command = scope.lastCommand;
269
+ }
270
+
271
+ scope.send(bridgeInformation);
272
+
273
+ // RESET LAST COMMAND
274
+ scope.lastCommand = null;
275
+
276
+ if(done) { done(); }
277
+ setTimeout(function(){ scope.setInitialState(); }, 3000);
278
+ })
279
+ .catch(function(error) {
280
+ scope.error(error);
281
+ if(done) { done(error); }
282
+ });
283
+ }
284
+ }
285
+ // GIVE BACK CURRENT STATE
286
+ else
287
+ {
288
+ // SEND MESSAGE
289
+ if(scope.lastCommand !== null)
290
+ {
291
+ bridgeInformation.command = scope.lastCommand;
292
+ }
500
293
 
501
- //
502
- // CLOSE NODE
503
- this.on('close', function()
504
- {
505
- scope.nodeActive = false;
294
+ scope.send(bridgeInformation);
295
+
296
+ // RESET LAST COMMAND
297
+ scope.lastCommand = null;
298
+ }
299
+ });
506
300
  });
507
301
  }
508
302