node-red-contrib-uos-nats 0.1.34 → 0.1.35

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/lib/payloads.js CHANGED
@@ -97,6 +97,37 @@ export function decodeVariableList(list) {
97
97
  }
98
98
  return result;
99
99
  }
100
+ return result;
101
+ }
102
+
103
+ // Added to support NATS-based definition fetching
104
+ import { ReadProviderDefinitionQueryResponse } from './fbs/weidmueller/ucontrol/hub/read-provider-definition-query-response.js';
105
+
106
+ export function decodeProviderDefinition(buffer) {
107
+ const bb = new flatbuffers.ByteBuffer(buffer);
108
+ const response = ReadProviderDefinitionQueryResponse.getRootAsReadProviderDefinitionQueryResponse(bb);
109
+ const providerDef = response.providerDefinition();
110
+
111
+ if (!providerDef) return [];
112
+
113
+ const result = [];
114
+ const len = providerDef.variableDefinitionsLength();
115
+ for (let i = 0; i < len; i++) {
116
+ const item = providerDef.variableDefinitions(i);
117
+ if (!item) continue;
118
+
119
+ // Inverse mapping of DataType/AccessType needed if we want full fidelity,
120
+ // but for now we mainly need ID and Key.
121
+ result.push({
122
+ id: item.id(),
123
+ key: item.key(),
124
+ dataType: 'UNKNOWN', // Mapping back from Enum to String if strictly needed
125
+ access: item.accessType() === VariableAccessType.READ_WRITE ? 'READ_WRITE' : 'READ_ONLY'
126
+ });
127
+ }
128
+ return result;
129
+ }
130
+
100
131
  function toFlatDefinition(def) {
101
132
  const result = new VariableDefinitionT();
102
133
  result.id = def.id;
@@ -89,12 +89,40 @@ module.exports = function (RED) {
89
89
  const definitions = await connection.fetchProviderVariables(this.providerId);
90
90
  definitions.forEach((def) => defMap.set(def.id, def));
91
91
  } catch (e) {
92
- this.warn(`Could not fetch variable definitions: ${e.message}`);
92
+ this.warn(`REST API failed (${e.message}). Attempting NATS fallback...`);
93
+ try {
94
+ // Fallback: Fetch definitions via NATS
95
+ // We need to load the response type dynamically as well if not already loaded
96
+ const { ReadProviderDefinitionQueryResponse } = await import(pathToFileURL(path.join(__dirname, '..', 'lib', 'fbs', 'weidmueller', 'ucontrol', 'hub', 'read-provider-definition-query-response.js')).href);
97
+
98
+ // Ensure we have a connection even if start() isn't fully done (we might need to move this)
99
+ // But 'nc' is acquired below. Let's acquire it first if possible, or do this AFTER acquired.
100
+ // Refactoring: We will move this logic 'down' after nc is acquired.
101
+ } catch (natsErr) {
102
+ this.warn(`NATS definition fetch also failed: ${natsErr.message}`);
103
+ }
93
104
  }
94
105
 
95
106
  nc = await connection.acquire();
96
107
  this.status({ fill: 'green', shape: 'dot', text: 'connected' });
97
108
 
109
+ // Retry Definition Fetch via NATS if Map is empty
110
+ if (defMap.size === 0) {
111
+ try {
112
+ this.warn(`Attempting to fetch definitions via NATS for ${this.providerId}...`);
113
+ const defMsg = await nc.request(`v1.loc.${this.providerId}.def.qry.read`, new Uint8Array(0), { timeout: 2000 });
114
+ // We need to decode this manually or use a helper
115
+ // Importing payloads to use our new decode function
116
+ // Assuming payloads is already loaded above
117
+
118
+ const defs = payloads.decodeProviderDefinition(defMsg.data);
119
+ this.warn(`NATS Fallback: Loaded ${defs.length} definitions.`);
120
+ defs.forEach((def) => defMap.set(def.id, def));
121
+ } catch (err) {
122
+ this.warn(`NATS Fallback failed: ${err.message}`);
123
+ }
124
+ }
125
+
98
126
  performSnapshot = async () => {
99
127
  // Debugging connection state
100
128
  if (!nc || nc.isClosed()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-uos-nats",
3
- "version": "0.1.34",
3
+ "version": "0.1.35",
4
4
  "description": "Node-RED nodes for u-OS Data Hub via NATS",
5
5
  "repository": {
6
6
  "type": "git",