node-red-contrib-homebridge-automation 0.1.12-beta.29 → 0.1.12-beta.30

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-homebridge-automation",
3
- "version": "0.1.12-beta.29",
3
+ "version": "0.1.12-beta.30",
4
4
  "description": "NodeRED Automation for HomeBridge",
5
5
  "main": "src/HAP-NodeRed.js",
6
6
  "scripts": {
@@ -8,22 +8,24 @@ class HbControlNode extends hbBaseNode {
8
8
 
9
9
  async handleInput(message, send) {
10
10
  debug('handleInput', message.payload, this.name);
11
+
11
12
  if (!this.hbDevice) {
12
13
  this.handleError('HB not initialized');
13
14
  return;
14
15
  }
15
- if (this.hbDevice.type == 'CameraRTPStreamManagement') {
16
- message.payload = {
17
- "resource-type": "image",
18
- "image-width": 1920,
19
- "image-height": 1080
20
- };
21
- }
22
- if (typeof message.payload !== 'object') {
16
+
17
+ const isCamera = this.hbDevice.type === 'CameraRTPStreamManagement';
18
+ const payloadType = typeof message.payload;
19
+
20
+ // Validate payload
21
+ if (!isCamera && payloadType !== 'object') {
23
22
  const validNames = Object.keys(this.hbDevice.values)
24
23
  .filter(key => key !== 'ConfiguredName')
25
24
  .join(', ');
26
- this.error(`Payload should be a JSON object containing device characteristics and values, e.g. {"On":false, "Brightness":0}. Valid values: ${validNames}`);
25
+
26
+ this.error(
27
+ `Invalid payload. Expected JSON object, e.g., {"On":false, "Brightness":0}. Valid values: ${validNames}`
28
+ );
27
29
  this.status({ text: 'Invalid payload', shape: 'dot', fill: 'red' });
28
30
  return;
29
31
  }
@@ -31,48 +33,46 @@ class HbControlNode extends hbBaseNode {
31
33
  const results = [];
32
34
  let fill = 'green';
33
35
 
34
- if (this.hbDevice.type == 'CameraRTPStreamManagement') {
35
- const result = await this.hbDevice.getResource({
36
- "resource-type": "image",
37
- "image-width": 1920,
38
- "image-height": 1080
39
- });
40
- message = { ...message, ...this.createMessage(this.hbDevice) };
41
- message.payload = result;
42
- send(message);
43
- results.push({ 'Received': result.length })
44
- } else {
45
- for (const key of Object.keys(message.payload)) {
36
+ try {
37
+ if (isCamera) {
38
+ // Handle CameraRTPStreamManagement
39
+ const cameraPayload = {
40
+ "resource-type": "image",
41
+ "image-width": 1920,
42
+ "image-height": 1080
43
+ };
44
+
45
+ const result = await this.hbDevice.getResource(cameraPayload);
46
46
 
47
- try {
48
- const result = await this.hbDevice.setCharacteristicByType(key, message.payload[key]);
49
- results.push({ [result.type]: result.value });
50
- } catch (error) {
51
- this.error(`Failed to set value for ${key}: ${error.message}`);
52
- results.push({ key: key + ' ' + error.message })
53
- fill = 'red';
47
+ message = { ...message, ...this.createMessage(this.hbDevice), payload: result };
48
+ send(message);
49
+ results.push({ Received: result.length });
50
+ } else {
51
+ // Handle other characteristics
52
+ for (const key of Object.keys(message.payload)) {
53
+ try {
54
+ const result = await this.hbDevice.setCharacteristicByType(key, message.payload[key]);
55
+ results.push({ [result.type]: result.value });
56
+ } catch (error) {
57
+ this.error(`Failed to set value for "${key}": ${error.message}`);
58
+ results.push({ [key]: `Error: ${error.message}` });
59
+ fill = 'red';
60
+ }
54
61
  }
55
62
  }
56
- }
57
63
 
58
- this.status({
59
- text: this.statusText(JSON.stringify(Object.assign({}, ...results))),
60
- shape: 'dot',
61
- fill,
62
- });
64
+ // Update status
65
+ const statusText = this.statusText(JSON.stringify(Object.assign({}, ...results)));
66
+ this.status({ text: statusText, shape: 'dot', fill });
67
+ } catch (error) {
68
+ this.error(`Unhandled error: ${error.message}`);
69
+ this.status({ text: 'Unhandled error', shape: 'dot', fill: 'red' });
70
+ }
63
71
  }
64
72
  }
65
73
 
66
74
  function btoa(str) {
67
- var buffer;
68
-
69
- if (str instanceof Buffer) {
70
- buffer = str;
71
- } else {
72
- buffer = Buffer.from(str.toString(), 'binary');
73
- }
74
-
75
- return buffer.toString('base64');
75
+ return Buffer.isBuffer(str) ? str.toString('base64') : Buffer.from(str.toString(), 'binary').toString('base64');
76
76
  }
77
77
 
78
78
  module.exports = HbControlNode;