node-red-contrib-mobius-flow-base 2.5.86-beta.9 → 2.5.86-beta.90

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.
@@ -193,6 +193,74 @@ module.exports = function (RED) {
193
193
  });
194
194
  };
195
195
 
196
+ /**
197
+ * Select status text and colour of node status based on last active time
198
+ * @param {number} lastActiveTime - The node's last active time
199
+ * @returns Object containing status text and status colour
200
+ */
201
+ node.getLastActiveTimeStatus = function (lastActiveTime) {
202
+ let response = {
203
+ text: 'Unknown',
204
+ colour: 'green',
205
+ };
206
+ const timeSinceLastActive = Date.now() - lastActiveTime;
207
+ if (timeSinceLastActive < 15 * 60000) {
208
+ response.text = 'Active less than 15 minutes ago';
209
+ }
210
+ if (timeSinceLastActive >= 15 * 60000 && timeSinceLastActive < 3 * 60 * 60000) {
211
+ response.text = 'Active less than 3 hours ago';
212
+ }
213
+ if (timeSinceLastActive >= 3 * 60 * 60000 && timeSinceLastActive < 12 * 60 * 60000) {
214
+ response.text = 'Active less than 12 hours ago';
215
+ }
216
+ if (timeSinceLastActive >= 12 * 60 * 60000 && timeSinceLastActive < 24 * 60 * 60000) {
217
+ response.text = 'Active less than 24 hours ago';
218
+ }
219
+ if (timeSinceLastActive > 24 * 60 * 60000 && lastActiveTime > 0) {
220
+ response.colour = 'grey';
221
+ response.text = 'Active more than 24 hours ago';
222
+ }
223
+ if (lastActiveTime === 0) {
224
+ response.colour = 'grey';
225
+ response.text = 'Never Active';
226
+ }
227
+ return response;
228
+ };
229
+
230
+ /**
231
+ * Update a given node's node status based on last active time
232
+ * @param {Object} mobiusNode - The node whos status is to be updated
233
+ */
234
+ node.updateNodeStatus = function (mobiusNode) {
235
+ console.log('Update');
236
+ console.log(node.users[mobiusNode.id].statusType);
237
+ node.users[mobiusNode.id] = mobiusNode;
238
+ let statusText = 'Unknown';
239
+ let statusColour = 'green';
240
+ if (node.users[mobiusNode.id].statusType === 'timeSinceLastActive') {
241
+ let result = node.getLastActiveTimeStatus(node.users[mobiusNode.id].lastActiveTime);
242
+ statusText = result.text;
243
+ statusColour = result.colour;
244
+ } else {
245
+ statusText = node.users[mobiusNode.id].latestUri;
246
+ }
247
+ node.users[mobiusNode.id].status({
248
+ fill: statusColour,
249
+ shape: 'dot',
250
+ text: statusText,
251
+ });
252
+ };
253
+
254
+ /**
255
+ * Update a given node's node last active time
256
+ * @param {Object} mobiusNode - The node whos last active time is to be updated
257
+ */
258
+ node.updateLastActiveTime = function (mobiusNode) {
259
+ node.users[mobiusNode.id] = mobiusNode;
260
+ node.users[mobiusNode.id].lastActiveTime = Date.now();
261
+ node.updateNodeStatus(mobiusNode);
262
+ };
263
+
196
264
  /**
197
265
  * Register a new Mobius node with this config node
198
266
  * @param {Object} mobiusNode - The new node to register
@@ -206,6 +274,12 @@ module.exports = function (RED) {
206
274
  text: 'Connected',
207
275
  });
208
276
  }
277
+ node.users[mobiusNode.id].lastActiveTime = 0;
278
+ node.users[mobiusNode.id].statusCheckLoop = setInterval(() => {
279
+ if (node.connected) {
280
+ node.updateNodeStatus(mobiusNode);
281
+ }
282
+ }, 60000);
209
283
  };
210
284
 
211
285
  /**
@@ -214,6 +288,7 @@ module.exports = function (RED) {
214
288
  * @param {Function} done - A callback to be run by the node after it has been deregistered
215
289
  */
216
290
  node.deregister = function (mobiusNode, done) {
291
+ clearInterval(node.users[mobiusNode.id].statusCheckLoop);
217
292
  delete node.users[mobiusNode.id];
218
293
  if (node.service) {
219
294
  try {
@@ -520,30 +595,6 @@ module.exports = function (RED) {
520
595
  node.updateObjectInServiceConfig = function (hid, sid, pid, ins, config, callback) {
521
596
  node.service.srvcAPI.updateObjectInServiceConfig(hid, sid, pid, ins, config, callback);
522
597
  };
523
-
524
- node.getNodeConfig = function (hid) {
525
- return new Promise((resolve, reject) => {
526
- node.service.srvcAPI.getNodeConfig(hid, (err, response) => {
527
- if (!err) {
528
- resolve(response);
529
- } else {
530
- reject(err);
531
- }
532
- });
533
- });
534
- };
535
-
536
- node.getServiceConfigProm = function (hid, sid) {
537
- return new Promise((resolve, reject) => {
538
- node.service.srvcAPI.getServiceConfig(hid, sid, (err, response) => {
539
- if (!err) {
540
- resolve(response);
541
- } else {
542
- reject(err);
543
- }
544
- });
545
- });
546
- };
547
598
  }
548
599
 
549
600
  RED.nodes.registerType('mobius connection', MobiusConnectionNode);
@@ -604,42 +655,4 @@ module.exports = function (RED) {
604
655
  }
605
656
  }
606
657
  );
607
-
608
- // Register the endpoint for node-red mobius discovery in node-red node config pages
609
- RED.httpAdmin.get('/mdiscover', RED.auth.needsPermission('serial.read'), function (req, res) {
610
- let conn = RED.nodes.getNode(req.query.mobius);
611
- let uri = req.query.uri;
612
- if (conn === null || !conn.service.connected) {
613
- RED.log.error('Discover services failed - could not to connect to MOBiUS hub.');
614
- throw new Error('Discover services failed - could not to connect to MOBiUS hub.');
615
- } else {
616
- uiDeviceDiscovery
617
- .discover(conn, uri)
618
- .then((response) => {
619
- res.json(response);
620
- })
621
- .catch((err) => {
622
- throw err;
623
- });
624
- }
625
- });
626
-
627
- // Register the endpoint for node-red mobius discovery in node-red node config pages
628
- RED.httpAdmin.get('/mgetservices', RED.auth.needsPermission('serial.read'), function (req, res) {
629
- let conn = RED.nodes.getNode(req.query.mobius);
630
- let hid = req.query.hid;
631
- if (conn === null || !conn.service.connected) {
632
- RED.log.error('Get services failed - could not to connect to MOBiUS hub.');
633
- throw new Error('Get services failed - could not to connect to MOBiUS hub.');
634
- } else {
635
- uiDeviceDiscovery
636
- .getServiceList(conn, hid)
637
- .then((response) => {
638
- res.json(response);
639
- })
640
- .catch((err) => {
641
- RED.log.warn(`Get services failed - ${err}`);
642
- });
643
- }
644
- });
645
658
  };
@@ -53,6 +53,13 @@ POSSIBILITY OF SUCH DAMAGE.
53
53
  <label for="node-input-detailedOutput" style="width: 70%;"> Use detailed outputs</label>
54
54
  </div>
55
55
  <hr>
56
+ <div class="form-row">
57
+ <label for="node-input-statusType"><i class="fa fa-bars"></i> Status Display</label>
58
+ <select type="text" id="node-input-statusType" style="display: inline-block; width: auto; vertical-align: center;">
59
+ <option value="timeSinceLastActive">Time since last active</option>
60
+ <option value="lastUri">Last URI</option>
61
+ </select>
62
+ </div>
56
63
  <div class="form-row">
57
64
  <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
58
65
  <input type="text" id="node-input-name" placeholder="Name">
@@ -64,6 +71,7 @@ POSSIBILITY OF SUCH DAMAGE.
64
71
  category: 'mobius',
65
72
  defaults: {
66
73
  name: {value:""},
74
+ statusType: {value:"timeSinceLastActive"},
67
75
  detailedOutput: {value:false},
68
76
  format: {value: "name", required: true},
69
77
  include: {value: "changed", required: true},
@@ -29,29 +29,35 @@
29
29
  var _ = require('lodash');
30
30
 
31
31
  module.exports = function (RED) {
32
- function MobiusObjectCovNode (n) {
32
+ function MobiusObjectCovNode(n) {
33
33
  RED.nodes.createNode(this, n);
34
34
  this.detailedOutput = n.detailedOutput;
35
35
  this.object = n.object;
36
36
  this.format = n.format;
37
37
  this.include = n.include;
38
38
  this.serviceConn = RED.nodes.getNode(n.connection);
39
+ this.latestUri = '';
40
+ this.statusType = n.statusType;
39
41
  var node = this;
40
42
 
43
+ console.log(node.statusType);
44
+
41
45
  if (node.serviceConn) {
42
46
  node.status({
43
47
  fill: 'red',
44
48
  shape: 'ring',
45
- text: 'Disconnected'
49
+ text: 'Disconnected',
46
50
  });
47
51
 
52
+ console.log(node);
48
53
  node.serviceConn.subscribeToObjectCOV(node.object, this.id, (response) => {
49
- node.status({
50
- fill: 'green',
51
- shape: 'dot',
52
- text: JSON.stringify(response.uri)
53
- });
54
- let allResources = _.keyBy(response.value.resources, node.format === 'name' ? 'name' : 'rid');
54
+ console.log('COV Event');
55
+ node.latestUri = JSON.stringify(response.uri);
56
+ node.serviceConn.updateNodeStatus(node);
57
+ let allResources = _.keyBy(
58
+ response.value.resources,
59
+ node.format === 'name' ? 'name' : 'rid'
60
+ );
55
61
  if (node.include === 'changed') {
56
62
  allResources = _.pickBy(allResources, (res) => {
57
63
  return response.changed.includes(res.rid);
@@ -61,10 +67,12 @@ module.exports = function (RED) {
61
67
  node.send([
62
68
  { topic: response.uri, payload: allResources },
63
69
  { topic: response.uri, payload: response.value },
64
- { topic: response.uri, payload: { response: response } }
70
+ { topic: response.uri, payload: { response: response } },
65
71
  ]);
72
+ node.serviceConn.updateLastActiveTime(node);
66
73
  } else {
67
74
  node.send({ topic: response.uri, payload: allResources });
75
+ node.serviceConn.updateLastActiveTime(node);
68
76
  }
69
77
  });
70
78
 
@@ -29,7 +29,7 @@
29
29
  const _ = require('lodash');
30
30
 
31
31
  module.exports = {
32
- async discover(connection, uri) {
32
+ async discover (connection, uri) {
33
33
  return new Promise((resolve, reject) => {
34
34
  connection.discover(uri, (err, response) => {
35
35
  if (err) {
@@ -41,7 +41,7 @@ module.exports = {
41
41
  });
42
42
  },
43
43
 
44
- async discoverDevicesByPID(RED, connection, hid, spids, pid) {
44
+ async discoverDevicesByPID (RED, connection, hid, spids, pid) {
45
45
  let devices = [];
46
46
  if (!hid || hid === '') {
47
47
  hid = connection.service.srvcAPI.status.hubId;
@@ -54,17 +54,12 @@ module.exports = {
54
54
  throw new Error(`Discover devices failed (${hid}) - ${err.message}`);
55
55
  }
56
56
  for (let srvcLoop = 0; srvcLoop < services.value.length; srvcLoop++) {
57
- if (
58
- services.value[srvcLoop].status === 'online' &&
59
- (spids.length === 0 || spids.includes(services.value[srvcLoop].spid))
60
- ) {
57
+ if (services.value[srvcLoop].status === 'online' && (spids.length === 0 || spids.includes(services.value[srvcLoop].spid))) {
61
58
  let insURI = `${hid}/${services.value[srvcLoop].id.split('/')[1]}/${pid}`;
62
59
  try {
63
60
  let instances = await this.discover(connection, insURI);
64
61
  for (let insLoop = 0; insLoop < instances.value.length; insLoop++) {
65
- let deviceURI = `${hid}/${services.value[srvcLoop].id.split('/')[1]}/${pid}/${
66
- instances.value[insLoop]
67
- }`;
62
+ let deviceURI = `${hid}/${services.value[srvcLoop].id.split('/')[1]}/${pid}/${instances.value[insLoop]}`;
68
63
  let device = await this.discover(connection, deviceURI);
69
64
  let resourcesByName = _.keyBy(device.value[0].resources, 'name');
70
65
  let deviceName = resourcesByName['name'].pv;
@@ -84,7 +79,7 @@ module.exports = {
84
79
  });
85
80
  },
86
81
 
87
- async discoverServicesBySPID(RED, connection, hid, spid) {
82
+ async discoverServicesBySPID (RED, connection, hid, spid) {
88
83
  if (!hid || hid === '') {
89
84
  hid = connection.service.srvcAPI.status.hubId;
90
85
  }
@@ -104,103 +99,5 @@ module.exports = {
104
99
  return new Promise((resolve) => {
105
100
  resolve(srvcs);
106
101
  });
107
- },
108
-
109
- getServiceList(connection, hid) {
110
- // getNodeConfig -> id, spid, uri, name, enabled, runAtStart
111
- // For every: getServiceConfig -> mobiusSettings, customSettings
112
- // discover -> registered, last connected, status + any remaining if new service
113
- // For every: discover object -/0001/0001 -> status, statusText
114
- return new Promise(async (resolve, reject) => {
115
- try {
116
- // Fetch information from node config
117
- const nodeConfig = await connection.getNodeConfig(hid);
118
- const servicesFromConfig = nodeConfig.value.services;
119
- let services = [];
120
- for (let i = 0; i < servicesFromConfig.length; i++) {
121
- services.push({
122
- id: servicesFromConfig[i].id,
123
- spid: servicesFromConfig[i].id.split('/')[0],
124
- sid: servicesFromConfig[i].id.split('/')[1],
125
- uri: `${nodeConfig.uri}/${servicesFromConfig[i].id.split('/')[1]}`,
126
- name: servicesFromConfig[i].name,
127
- enabled: servicesFromConfig[i].enabled,
128
- runAtStart: servicesFromConfig[i].runAtStart,
129
- registered: null,
130
- lastConnected: null,
131
- status: null,
132
- statusText: null,
133
- mobiusSettings: null,
134
- customSettings: null,
135
- });
136
- }
137
-
138
- console.log('Following get node config');
139
- console.log(services);
140
-
141
- // Fetch information from each service config
142
- for (i = 0; i < services.length; i++) {
143
- const fullServiceConfig = await connection.getServiceConfigProm(hid, services[i].sid);
144
- services[i].mobiusSettings = fullServiceConfig.value.serviceConfig.settings;
145
- services[i].customSettings = fullServiceConfig.value.serviceConfig.custom;
146
- }
147
-
148
- console.log('Following service node configs');
149
- console.log(services);
150
-
151
- // Fetch information from discovered services
152
- const discoveryResponse = await this.discover(connection, hid);
153
- const servicesFromDiscovery = discoveryResponse.value;
154
- for (i = 0; i < servicesFromDiscovery.length; i++) {
155
- let serviceFound = false;
156
- for (j = 0; j < services.length; j++) {
157
- if (servicesFromDiscovery[i].id === services[j].id) {
158
- serviceFound = true;
159
- services[j].registered = servicesFromDiscovery[i].registered;
160
- services[j].lastConnected = servicesFromDiscovery[i].lastconnected;
161
- j = services.length;
162
- }
163
- }
164
- if (!serviceFound) {
165
- services.push({
166
- id: servicesFromDiscovery[i].id,
167
- spid: servicesFromDiscovery[i].id.split('/')[0],
168
- sid: servicesFromDiscovery[i].id.split('/')[1],
169
- uri: `${nodeConfig.uri}/${servicesFromDiscovery[i].id.split('/')[1]}`,
170
- name: null,
171
- enabled: null,
172
- runAtStart: null,
173
- registered: servicesFromDiscovery[i].registered,
174
- lastConnected: servicesFromDiscovery[i].lastconnected,
175
- status: null,
176
- statusText: null,
177
- mobiusSettings: null,
178
- customSettings: null,
179
- });
180
- }
181
- }
182
-
183
- console.log('Following discover');
184
- console.log(services);
185
-
186
- // Fetch status object from all services
187
- for (i = 0; i < services.length; i++) {
188
- const statusObjectResponse = await this.discover(
189
- connection,
190
- `${hid}/${services[i].sid}/0001/0001`
191
- );
192
- const statusObject = statusObjectResponse.value;
193
- services[i].status = statusObject.resources[1].pv;
194
- services[i].statusText = `${statusObject.resources[2].pv}`;
195
- }
196
-
197
- console.log('Complete');
198
- console.log(services);
199
-
200
- resolve(services);
201
- } catch (err) {
202
- reject(err);
203
- }
204
- });
205
- },
102
+ }
206
103
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-mobius-flow-base",
3
- "version": "2.5.86-beta.9",
3
+ "version": "2.5.86-beta.90",
4
4
  "description": "Node-RED base nodes for Mobius Flow",
5
5
  "main": "index.js",
6
6
  "types": "dist/index.d.ts",