forked from dufourgilles/node-red-contrib-emberplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemberplus.js
More file actions
executable file
·131 lines (124 loc) · 4.85 KB
/
emberplus.js
File metadata and controls
executable file
·131 lines (124 loc) · 4.85 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { EmberLib } = require('node-emberplus');
const { EmberClient } = require('node-emberplus/lib/client/ember-client');
/**
* @typedef NodeRedFunctionArgs
* @type {object}
* @property {number} type - argument type
* @property {number|string} value
*/
module.exports = function (RED) {
/**
*
* @param {NodeRedFunctionArgs} arg
* @returns {EmberLib.FunctionArgument}
*/
function convertArg(arg) {
return new EmberLib.FunctionArgument(arg.type, arg.value);
}
function EmberPlusNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.configRead = config.read;
node.path = config.path.substring(0, config.path.indexOf(':'));
/** @type {EmberClient} */
node.client = null;
/**
*
* @param {string} _path
* @param {EmberClient} client
*/
async function handleClientReady(_path, client) {
node.client = client;
if (node.path) {
if (node.configRead) {
try {
const element = await client.getElementByPathAsync(node.path);
updateOutput(element);
} catch(e) {
console.log(e);
}
}
try {
await client.getElementByPathAsync(node.path, update => updateOutput(update))
} catch(e) {
node.warn(e.stack);
console.log(e);
}
}
}
/**
*
* @param {Element} element
* @param {function | null} send
*/
function updateOutput(element, send) {
const msg = { 'payload': null };
if (config.outputMode == 'full') {
msg.payload = { 'full': element };
} else if (config.outputMode == 'contents') {
msg.payload = { 'contents': element.contents };
} else if (element.isInvocationResult() || config.outputMode == 'json') {
msg.payload = element.toJSON();
} else {
msg.payload = element.value
}
if (send) {
send(msg);
} else {
node.send(msg);
}
}
// Retrieve the config node
this.server = RED.nodes.getNode(config.server);
if (this.server) {
var server = this.server;
server.addStatusCallback(function (color, message, extraInformation) {
node.status({ fill: color, shape: "dot", text: message });
if (extraInformation != "") {
node.error(extraInformation);
}
});
server.on("clientready", (_paths, client) => {
handleClientReady(_paths, client).catch(e => console.log(e));
});
} else {
// No config node configured
}
node.on('input', async (msg, send, done) => {
const payload = msg.payload;
try {
if (!node.client.isConnected()) {
await node.client.connectAsync();
}
const element = await node.client.getElementByPathAsync(node.path);
const p = (payload.full != undefined && payload.full.path != undefined) ? payload.full.path : node.path;
if (element.isFunction()) {
/**
* @type {NodeRedFunctionArgs}[]
*/
const args = payload.args != null ? payload.args : JSON.parse(payload);
this.log(`Invoking fonction ${element.path} with ${JSON.stringify(args)}`);
const invokeResult = await node.client.invokeFunctionAsync(element, args.map(convertArg));
this.log(`Received fonction ${element.path} result ${JSON.stringify(invokeResult.toJSON())}`);
updateOutput(invokeResult, send);
} else {
const v = (payload.full != undefined && payload.full.value != undefined) ? payload.full.value : payload;
if (element.isParameter() && element.value != v) {
this.log(`Sending new value ${v} to parameter ${element.path}`);
await node.client.setValueAsync(element, v);
} else if(element.isMatrix()) {
await node.client.matrixConnectAsync(element, v.target, v.sources);
}
}
} catch(e) {
if (done) {
done(e);
} else {
node.error(e.stack);
}
this.log(e);
}
});
}
RED.nodes.registerType("ember+", EmberPlusNode);
}