node-red-contrib-mobius-flow-base 2.5.91 → 3.0.0-beta.13

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 (44) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +16 -0
  2. package/.github/ISSUE_TEMPLATE/feature_request.md +16 -0
  3. package/.github/ISSUE_TEMPLATE/general.md +7 -0
  4. package/.github/workflows/build_publish.yml +80 -0
  5. package/README.md +4 -10
  6. package/dist/engine-api-calls.d.ts +19 -0
  7. package/dist/engine-api-calls.js +235 -0
  8. package/dist/engine-api-client.d.ts +8 -0
  9. package/dist/engine-api-client.js +106 -0
  10. package/dist/engine-websocket.d.ts +60 -0
  11. package/dist/engine-websocket.js +264 -0
  12. package/dist/node-red-ui-funcs.js +180 -0
  13. package/dist/nodes/common/mobsearch.js +602 -0
  14. package/dist/nodes/mobius-change-admin-password.d.ts +26 -0
  15. package/dist/nodes/mobius-change-admin-password.html +98 -0
  16. package/dist/nodes/mobius-change-admin-password.js +87 -0
  17. package/dist/nodes/mobius-clear-resource.html +37 -12
  18. package/dist/nodes/mobius-clear-resource.js +17 -13
  19. package/dist/nodes/mobius-connection-interface.d.ts +45 -0
  20. package/dist/nodes/mobius-connection-interface.js +2 -0
  21. package/dist/nodes/mobius-connection.html +30 -8
  22. package/dist/nodes/mobius-connection.js +216 -85
  23. package/dist/nodes/mobius-discover-object-list.html +185 -126
  24. package/dist/nodes/mobius-discover-object-list.js +24 -5
  25. package/dist/nodes/mobius-discover.js +7 -7
  26. package/dist/nodes/mobius-filter-by-rid-value.html +62 -42
  27. package/dist/nodes/mobius-filter-by-rid-value.js +38 -31
  28. package/dist/nodes/mobius-flow-inject.html +32 -2
  29. package/dist/nodes/mobius-get-multi-object.html +39 -5
  30. package/dist/nodes/mobius-get-multi-resource.html +37 -4
  31. package/dist/nodes/mobius-get-multi-resource.js +13 -12
  32. package/dist/nodes/mobius-get-object.html +39 -11
  33. package/dist/nodes/mobius-get-object.js +12 -9
  34. package/dist/nodes/mobius-get-resource.html +41 -12
  35. package/dist/nodes/mobius-get-resource.js +17 -12
  36. package/dist/nodes/mobius-object-cov.html +40 -11
  37. package/dist/nodes/mobius-object-cov.js +9 -6
  38. package/dist/nodes/mobius-resource-cov.html +37 -11
  39. package/dist/nodes/mobius-resource-cov.js +5 -5
  40. package/dist/nodes/mobius-set-multi-resource.html +39 -12
  41. package/dist/nodes/mobius-set-multi-resource.js +9 -11
  42. package/dist/nodes/mobius-set-resource.html +38 -11
  43. package/package.json +15 -8
  44. package/dist/nodes/node-red-ui-discover-devices.js +0 -103
@@ -0,0 +1,264 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.unsubscribeToBCMD = exports.subscribeToBCMD = exports.unsubscribeToResourceCOV = exports.subscribeToResourceCOV = exports.unsubscribeToObjectCOV = exports.subscribeToObjectCOV = exports.disconnectFromWebSocket = exports.connectToWebSocket = exports.killWebSocket = exports.clearAllCallbacks = exports.registerCallback = exports.on = exports.setWebSocketConnectParams = void 0;
7
+ const ws_1 = __importDefault(require("ws"));
8
+ const engine_api_client_1 = require("./engine-api-client");
9
+ let webSocket = null;
10
+ let pendingWebSocketMessages = [];
11
+ let pendingWebSocketMessageInterval = null;
12
+ let pingInterval = null;
13
+ let registeredCallbacks = [];
14
+ let eventListeners = [];
15
+ let reconnectInterval = null;
16
+ let attemptReconnect = false;
17
+ let connectParams = null;
18
+ const setWebSocketConnectParams = (hostname, port, protocol) => {
19
+ connectParams = { hostname, port, protocol };
20
+ };
21
+ exports.setWebSocketConnectParams = setWebSocketConnectParams;
22
+ const on = (eventName, callback) => {
23
+ eventListeners.push({ eventName, callback });
24
+ };
25
+ exports.on = on;
26
+ const triggerEvent = (eventName) => {
27
+ const callbacks = eventListeners
28
+ .filter((cb) => cb.eventName === eventName)
29
+ .map((cb) => cb.callback);
30
+ callbacks.forEach((cb) => cb());
31
+ };
32
+ function registerCallback(topic, callback) {
33
+ registeredCallbacks.push({ topic, callback });
34
+ }
35
+ exports.registerCallback = registerCallback;
36
+ function clearAllCallbacks() {
37
+ registeredCallbacks = [];
38
+ }
39
+ exports.clearAllCallbacks = clearAllCallbacks;
40
+ function sendPingMessage() {
41
+ sendWebSocketMessage({
42
+ topic: 'ping',
43
+ payload: {},
44
+ });
45
+ }
46
+ function sendWebSocketMessage(message) {
47
+ if (webSocket?.readyState === ws_1.default.OPEN) {
48
+ webSocket.send(JSON.stringify(message));
49
+ }
50
+ else {
51
+ pendingWebSocketMessages.push(message);
52
+ }
53
+ }
54
+ function handleWebSocketMessage(message) {
55
+ if (message.topic === 'error') {
56
+ console.log(`Websocket Error: ${message.payload}`);
57
+ }
58
+ if (message.topic === 'authorized') {
59
+ console.log('Engine API Subscription WebSocket Auth Success');
60
+ triggerEvent('connected');
61
+ }
62
+ const callbacks = registeredCallbacks
63
+ .filter((cb) => cb.topic === message.topic)
64
+ .map((cb) => cb.callback);
65
+ callbacks.forEach((cb) => cb(message.payload));
66
+ }
67
+ const reconnect = () => {
68
+ console.log('Engine API Subscription WebSocket Client reconnecting...');
69
+ (0, exports.connectToWebSocket)();
70
+ };
71
+ // To be used only when connection node is deleted, hence removal of all callbacks etc.
72
+ const killWebSocket = () => {
73
+ attemptReconnect = false;
74
+ if (reconnectInterval) {
75
+ clearInterval(reconnectInterval);
76
+ }
77
+ registeredCallbacks = [];
78
+ pendingWebSocketMessages = [];
79
+ eventListeners = [];
80
+ if (webSocket) {
81
+ webSocket.close();
82
+ }
83
+ console.log('Killing Engine API Subscription WebSocket Client');
84
+ };
85
+ exports.killWebSocket = killWebSocket;
86
+ /**
87
+ * Connect to the Mobius WebSocket API
88
+ * This method will connect to the Mobius WebSocket API and authenticate with the API token
89
+ */
90
+ const connectToWebSocket = () => {
91
+ if (!connectParams) {
92
+ console.log('Unabled to connect to Engine API Subscription WebSocket as no connect params are set');
93
+ return;
94
+ }
95
+ if (!engine_api_client_1.tokens) {
96
+ console.log('Unabled to connect to Engine API Subscription WebSocket as no tokens are available');
97
+ return;
98
+ }
99
+ if (webSocket) {
100
+ webSocket.close();
101
+ webSocket = null;
102
+ }
103
+ let webSocketProtocol = 'wss';
104
+ if (connectParams.protocol === 'http') {
105
+ webSocketProtocol = 'ws';
106
+ }
107
+ webSocket = new ws_1.default(`${webSocketProtocol}://${connectParams.hostname}:${connectParams.port}/api/v1/mobius/subscribe/connect`);
108
+ webSocket.onopen = () => {
109
+ attemptReconnect = true;
110
+ console.log('Engine API Subscription WebSocket Client Connected');
111
+ pingInterval = setInterval(sendPingMessage, 30000);
112
+ clearInterval(reconnectInterval);
113
+ webSocket?.send(JSON.stringify({
114
+ topic: 'authorize',
115
+ payload: `Bearer ${engine_api_client_1.tokens?.token}`,
116
+ }));
117
+ pendingWebSocketMessageInterval = setInterval(() => {
118
+ for (let i = 0; i < pendingWebSocketMessages.length; i++) {
119
+ const pendingMessage = pendingWebSocketMessages.shift();
120
+ sendWebSocketMessage(pendingMessage);
121
+ }
122
+ }, 1000);
123
+ };
124
+ webSocket.onclose = () => {
125
+ console.log('Engine API Subscription WebSocket Client Closed');
126
+ if (pingInterval) {
127
+ clearInterval(pingInterval);
128
+ }
129
+ webSocket = null;
130
+ clearInterval(reconnectInterval);
131
+ if (pendingWebSocketMessageInterval) {
132
+ clearInterval(pendingWebSocketMessageInterval);
133
+ }
134
+ if (attemptReconnect) {
135
+ reconnectInterval = setInterval(() => {
136
+ if (engine_api_client_1.loggedIn) {
137
+ reconnect();
138
+ }
139
+ }, 30000);
140
+ }
141
+ triggerEvent('disconnected');
142
+ };
143
+ webSocket.onmessage = (message) => {
144
+ try {
145
+ if (message.data) {
146
+ const parsedMessage = JSON.parse(message.data);
147
+ handleWebSocketMessage(parsedMessage);
148
+ }
149
+ }
150
+ catch (e) {
151
+ console.log('Error parsing message ', e);
152
+ }
153
+ };
154
+ webSocket.on('error', (e) => {
155
+ if (e.code === 'ECONNREFUSED') {
156
+ (0, engine_api_client_1.setLoggedIn)(false);
157
+ }
158
+ else {
159
+ console.error('Engine API Subscription WebSocket Client Error: ', e);
160
+ }
161
+ });
162
+ };
163
+ exports.connectToWebSocket = connectToWebSocket;
164
+ /**
165
+ * Disconnect from the Mobius WebSocket API - Using this will not stop socket from attempting to reconnect
166
+ * This method will close the connection to the Mobius WebSocket API
167
+ */
168
+ const disconnectFromWebSocket = () => {
169
+ if (webSocket) {
170
+ webSocket.close();
171
+ }
172
+ };
173
+ exports.disconnectFromWebSocket = disconnectFromWebSocket;
174
+ /**
175
+ * Subscribe to an object's COV
176
+ * This method will subscribe to an object's COV
177
+ * @param uri The URI of the object to subscribe to
178
+ * @param listener The listener to dispatch the event to
179
+ */
180
+ const subscribeToObjectCOV = (uri) => {
181
+ console.log('Subscribing to object COV', uri);
182
+ sendWebSocketMessage({
183
+ topic: 'subscribe',
184
+ payload: { type: 'object-cov', uri },
185
+ });
186
+ // subList.push({ topic: 'object-cov', uri });
187
+ };
188
+ exports.subscribeToObjectCOV = subscribeToObjectCOV;
189
+ /**
190
+ * Unsubscribe from an object's COV
191
+ * This method will unsubscribe from an object's COV
192
+ * @param uri The URI of the object to unsubscribe from
193
+ * @param listener The listener to remove
194
+ */
195
+ const unsubscribeToObjectCOV = (uri) => {
196
+ sendWebSocketMessage({
197
+ topic: 'unsubscribe',
198
+ payload: { type: 'object-cov', uri },
199
+ });
200
+ // subList = subList.filter(
201
+ // (sub) => JSON.stringify(sub) !== JSON.stringify({ topic: 'object-cov', uri })
202
+ // );
203
+ };
204
+ exports.unsubscribeToObjectCOV = unsubscribeToObjectCOV;
205
+ /**
206
+ * Subscribe to an object's COV
207
+ * This method will subscribe to an object's COV
208
+ * @param uri The URI of the object to subscribe to
209
+ * @param listener The listener to dispatch the event to
210
+ */
211
+ const subscribeToResourceCOV = (uri) => {
212
+ sendWebSocketMessage({
213
+ topic: 'subscribe',
214
+ payload: { type: 'resource-cov', uri },
215
+ });
216
+ // subList.push({ topic: 'resource-cov', uri });
217
+ };
218
+ exports.subscribeToResourceCOV = subscribeToResourceCOV;
219
+ /**
220
+ * Unsubscribe from an object's COV
221
+ * This method will unsubscribe from an object's COV
222
+ * @param uri The URI of the object to unsubscribe from
223
+ * @param listener The listener to remove
224
+ */
225
+ const unsubscribeToResourceCOV = (uri) => {
226
+ sendWebSocketMessage({
227
+ topic: 'unsubscribe',
228
+ payload: { type: 'resource-cov', uri },
229
+ });
230
+ // subList = subList.filter(
231
+ // (sub) => JSON.stringify(sub) !== JSON.stringify({ topic: 'resource-cov', uri })
232
+ // );
233
+ };
234
+ exports.unsubscribeToResourceCOV = unsubscribeToResourceCOV;
235
+ /**
236
+ * Subscribe to a BCMD event
237
+ * This method will subscribe to a BCMD event
238
+ * @param uri The URI of the object to subscribe to
239
+ * @param listener The listener to dispatch the event to
240
+ */
241
+ const subscribeToBCMD = (uri) => {
242
+ sendWebSocketMessage({
243
+ topic: 'subscribe',
244
+ payload: { type: 'broadcast-command', uri },
245
+ });
246
+ // subList.push({ topic: 'broadcast-command', uri });
247
+ };
248
+ exports.subscribeToBCMD = subscribeToBCMD;
249
+ /**
250
+ * Unsubscribe from a BCMD event
251
+ * This method will unsubscribe from a BCMD event
252
+ * @param uri The URI of the object to unsubscribe from
253
+ * @param listener The listener to remove
254
+ */
255
+ const unsubscribeToBCMD = (uri) => {
256
+ sendWebSocketMessage({
257
+ topic: 'unsubscribe',
258
+ payload: { type: 'broadcast-command', uri },
259
+ });
260
+ // subList = subList.filter(
261
+ // (sub) => JSON.stringify(sub) !== JSON.stringify({ topic: 'broadcast-command', uri })
262
+ // );
263
+ };
264
+ exports.unsubscribeToBCMD = unsubscribeToBCMD;
@@ -0,0 +1,180 @@
1
+ /**
2
+ Copyright (C) 2016, IAConnects Technology Limited
3
+ All Rights Reserved
4
+
5
+ ALL INFORMATION CONTAINED HEREIN IS, AND REMAINS
6
+ THE PROPERTY OF IACONNECTS TECHNOLOGY LIMITED AND ITS SUPPLIERS,
7
+ IF ANY. THE INTELLECTUAL AND TECHNICAL CONCEPTS CONTAINED
8
+ HEREIN ARE PROPRIETARY TO IACONNECTS TECHNOLOGY LIMITED
9
+ AND ITS SUPPLIERS AND ARE PROTECTED BY TRADE SECRET OR COPYRIGHT LAW.
10
+ DISSEMINATION OF THIS INFORMATION OR REPRODUCTION OF THIS MATERIAL
11
+ IS STRICTLY FORBIDDEN UNLESS PRIOR WRITTEN PERMISSION IS OBTAINED
12
+ FROM IACONNECTS TECHNOLOGY LIMITED.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
18
+ BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ POSSIBILITY OF SUCH DAMAGE.
25
+ **/
26
+
27
+ 'use strict';
28
+
29
+ const _ = require('lodash');
30
+ const excludedSpids = ['0004', '0012'];
31
+
32
+ module.exports = {
33
+
34
+ // Promisified version of discover
35
+ discover (connection, uri) {
36
+ return new Promise((resolve, reject) => {
37
+ connection.discover(uri, (err, response) => {
38
+ if (err) {
39
+ reject(err);
40
+ } else {
41
+ resolve(response);
42
+ }
43
+ });
44
+ });
45
+ },
46
+
47
+ // Promisified version of getServiceConfig
48
+ getNodeConfig (connection, hid) {
49
+ return new Promise((resolve, reject) => {
50
+ connection.getNodeConfig(hid, (err, response) => {
51
+ if (!err) {
52
+ resolve(response);
53
+ } else {
54
+ reject(err);
55
+ }
56
+ });
57
+ });
58
+ },
59
+
60
+ // Promisified version of getServiceConfig
61
+ getServiceConfig (connection, hid, sid) {
62
+ return new Promise((resolve, reject) => {
63
+ connection.getServiceConfig(hid, sid, (err, response) => {
64
+ if (!err) {
65
+ resolve(response);
66
+ } else {
67
+ reject(err);
68
+ }
69
+ });
70
+ });
71
+ },
72
+
73
+ // Discover object by By PID
74
+ async discoverDevicesByPID (RED, connection, hid, spids, pid) {
75
+ let devices = [];
76
+ if (!hid || hid === '') {
77
+ hid = connection.service.hubId;
78
+ }
79
+ let services = null;
80
+ try {
81
+ services = await this.discover(connection, hid);
82
+ } catch (err) {
83
+ RED.log.error(`Discover devices failed (${hid}) - ${err.message}`);
84
+ throw new Error(`Discover devices failed (${hid}) - ${err.message}`);
85
+ }
86
+ for (let srvcLoop = 0; srvcLoop < services.data.length; srvcLoop++) {
87
+ if (services.data[srvcLoop].status === 'online' && (spids.length === 0 || spids.includes(services.data[srvcLoop].spid))) {
88
+ let insURI = `${hid}/${services.data[srvcLoop].id.split('/')[1]}/${pid}`;
89
+ try {
90
+ let instances = await this.discover(connection, insURI);
91
+ for (let insLoop = 0; insLoop < instances.data.length; insLoop++) {
92
+ let deviceURI = `${hid}/${services.data[srvcLoop].id.split('/')[1]}/${pid}/${instances.data[insLoop]}`;
93
+ let device = await this.discover(connection, deviceURI);
94
+ let resourcesByName = _.keyBy(device.data[0].resources, 'name');
95
+ let deviceName = resourcesByName['name'].pv;
96
+ if (deviceName !== '') {
97
+ devices.push(device.uri + ' ' + deviceName);
98
+ } else {
99
+ devices.push(device.uri);
100
+ }
101
+ }
102
+ } catch (err) {
103
+ RED.log.warn(`Discover devices failed (${insURI}) - ${err.message}`);
104
+ }
105
+ }
106
+ }
107
+ return devices;
108
+ },
109
+
110
+ // Discover services by SPID
111
+ async discoverServicesBySPID (RED, connection, hid, spid) {
112
+ if (!hid || hid === '') {
113
+ hid = connection.service.hubId;
114
+ }
115
+ let services = null;
116
+ try {
117
+ services = await this.discover(connection, hid);
118
+ } catch (err) {
119
+ RED.log.error(`Discover services failed (${hid}) - ${err.message}`);
120
+ throw new Error(`Discover services failed (${hid}) - ${err.message}`);
121
+ }
122
+ let srvcs = [];
123
+ for (let srvcLoop = 0; srvcLoop < services.data.length; srvcLoop++) {
124
+ if (services.data[srvcLoop].status === 'online' && services.data[srvcLoop].spid === spid) {
125
+ srvcs.push(`${hid}/${services.data[srvcLoop].id.split('/')[1]}`);
126
+ }
127
+ }
128
+ return srvcs;
129
+ },
130
+
131
+ // Get list of services
132
+ async getServiceList(connection, hid) {
133
+ try {
134
+ // Fetch information from node config
135
+ const nodeConfig = await this.getNodeConfig(connection, hid);
136
+ const servicesFromConfig = nodeConfig.data;
137
+ let services = [];
138
+ for (let i = 0; i < servicesFromConfig.length; i++) {
139
+ const spid = servicesFromConfig[i].id.split('/')[0];
140
+ if (!excludedSpids.includes(spid)) {
141
+ services.push({
142
+ id: servicesFromConfig[i].id,
143
+ spid,
144
+ sid: servicesFromConfig[i].id.split('/')[1],
145
+ uri: `${nodeConfig.uri}/${servicesFromConfig[i].id.split('/')[1]}`,
146
+ name: servicesFromConfig[i].name,
147
+ enabled: servicesFromConfig[i].enabled,
148
+ runAtStart: servicesFromConfig[i].runAtStart,
149
+ });
150
+ }
151
+ }
152
+ return services;
153
+ } catch (err) {
154
+ throw JSON.stringify(err);
155
+ }
156
+ },
157
+
158
+ // Get list og objects on given service
159
+ async getObjectList(connection, hid, sid) {
160
+ try {
161
+ let objectList = [];
162
+ const serviceResponse = await this.discover(connection, `${hid}/${sid}`);
163
+ const pids = [...new Set(serviceResponse.data)];
164
+ for (let i = 0; i < pids.length; i++) {
165
+ const inssResponse = await this.discover(connection, `${hid}/${sid}/${pids[i]}`);
166
+ const inss = inssResponse.data;
167
+ for (let j = 0; j < inss.length; j++) {
168
+ const objResponse = await this.discover(
169
+ connection,
170
+ `${hid}/${sid}/${pids[i]}/${inss[j]}`
171
+ );
172
+ objectList.push(objResponse.data[0]);
173
+ }
174
+ }
175
+ return objectList;
176
+ } catch (err) {
177
+ throw JSON.stringify(err);
178
+ }
179
+ },
180
+ };