-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebthingsio-get-property.js
86 lines (86 loc) · 2.98 KB
/
webthingsio-get-property.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
module.exports = function(RED) {
function WebthingsioGetPropertyNode(config) {
RED.nodes.createNode(this, config);
if (!config.gateway) {
this.error(RED._('webthingsio-get-property.gatewayMissing'));
return;
}
this.gateway = RED.nodes.getNode(config.gateway);
if (!this.gateway) {
this.error(RED._('webthingsio-get-property.gatewayNotFound'));
return;
}
if (!this.gateway.webthingsEmitter) {
// eslint-disable-next-line max-len
this.error(RED._('webthingsio-get-property.webthingsClientNotFound'));
return;
}
this.gateway.webthingsEmitter.on('connected', () => {
this.status({
fill: 'green',
shape: 'dot',
text: 'node-red:common.status.connected',
});
});
this.gateway.webthingsEmitter.on('disconnected', () => {
this.status({
fill: 'red',
shape: 'ring',
text: 'node-red:common.status.disconnected',
});
});
this.on('input', async (msg, send, done) => {
if (typeof config.thing !== 'string') {
if (done) {
done(RED._('webthingsio-get-property.thingNameInvalid'));
} else {
this.error(
RED._('webthingsio-get-property.thingNameInvalid'),
msg,
);
}
return;
}
const decodedThingId = decodeURIComponent(config.thing);
if (typeof config.property !== 'string') {
if (done) {
done(RED._('webthingsio-get-property.propertyNameInvalid'));
} else {
this.error(
RED._('webthingsio-get-property.propertyNameInvalid'),
msg,
);
}
return;
}
let value;
try {
value = await this.gateway.webthingsEmitter.getProperty(
decodedThingId, config.property,
);
} catch (ex) {
const e = typeof ex === 'string' ? ex : JSON.stringify(ex);
if (done) {
done(
RED._('webthingsio-get-property.getPropertyFailed')
.replace('%error', e),
);
} else {
this.error(
RED._('webthingsio-get-property.getPropertyFailed')
.replace('%error', e),
msg,
);
}
return;
}
msg.payload = value;
send(msg);
done();
});
}
RED.nodes.registerType(
'webthingsio-get-property',
WebthingsioGetPropertyNode,
);
};