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 +1 -1
- package/src/hbControlNode.js +43 -43
package/package.json
CHANGED
package/src/hbControlNode.js
CHANGED
|
@@ -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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
shape: 'dot',
|
|
61
|
-
|
|
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
|
-
|
|
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;
|