@uns-kit/core 2.0.38 → 2.0.40
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/LICENSE +21 -21
- package/README.md +147 -148
- package/dist/app-config.d.ts.map +1 -0
- package/dist/app-config.js.map +1 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.js +7 -7
- package/dist/tools/datahub/datahub-client.js +1 -1
- package/dist/tools/datahub/datahub-client.js.map +1 -1
- package/dist/tools/file-utils.d.ts +2 -2
- package/dist/tools/file-utils.js +24 -24
- package/dist/tools/generate-uns-dictionary.d.ts +23 -23
- package/dist/tools/generate-uns-dictionary.js +154 -154
- package/dist/tools/generate-uns-measurements.d.ts +21 -21
- package/dist/tools/generate-uns-measurements.js +126 -126
- package/dist/tools/generate-uns-reference.d.ts +2 -2
- package/dist/tools/generate-uns-reference.js +85 -85
- package/dist/tools/refresh-uns.js +50 -50
- package/dist/tools/sync-uns-schema.d.ts +2 -2
- package/dist/tools/sync-uns-schema.js +489 -489
- package/dist/uns/uns-asset.d.ts +1 -1
- package/dist/uns/uns-asset.js +1 -1
- package/dist/uns/uns-attributes.d.ts +6 -6
- package/dist/uns/uns-attributes.js +9 -9
- package/dist/uns/uns-dictionary-registry.d.ts +4 -4
- package/dist/uns/uns-dictionary-registry.js +20 -20
- package/dist/uns/uns-dictionary.generated.d.ts +663 -663
- package/dist/uns/uns-dictionary.generated.js +745 -745
- package/dist/uns/uns-interfaces.d.ts +365 -365
- package/dist/uns/uns-measurements.generated.d.ts +362 -362
- package/dist/uns/uns-measurements.generated.js +359 -359
- package/dist/uns/uns-object.d.ts +20 -20
- package/dist/uns/uns-object.js +9 -9
- package/dist/uns/uns-packet.js +263 -263
- package/dist/uns/uns-path.d.ts +2 -2
- package/dist/uns/uns-path.js +10 -10
- package/dist/uns/uns-proxy-process.js +203 -203
- package/dist/uns/uns-proxy.d.ts +51 -51
- package/dist/uns/uns-proxy.js +190 -190
- package/dist/uns-config/uns-core-schema.d.ts +3276 -3276
- package/dist/uns-grpc/uns-gateway.proto +104 -104
- package/dist/uns-mqtt/uns-mqtt-proxy.js +477 -477
- package/package.json +1 -1
|
@@ -1,204 +1,204 @@
|
|
|
1
|
-
import logger from "../logger.js";
|
|
2
|
-
import { randomBytes } from "crypto";
|
|
3
|
-
import MqttProxy from "../uns-mqtt/mqtt-proxy.js";
|
|
4
|
-
import UnsMqttProxy from "../uns-mqtt/uns-mqtt-proxy.js";
|
|
5
|
-
import { HandoverManager } from "./handover-manager.js";
|
|
6
|
-
// Import configuration and initialization modules.
|
|
7
|
-
import { PACKAGE_INFO, MQTT_UPDATE_INTERVAL } from "./process-config.js";
|
|
8
|
-
import { MqttTopicBuilder } from "../uns-mqtt/mqtt-topic-builder.js";
|
|
9
|
-
import { StatusMonitor } from "./status-monitor.js";
|
|
10
|
-
import { UnsPacket } from "./uns-packet.js";
|
|
11
|
-
class UnsProxyProcess {
|
|
12
|
-
active = false;
|
|
13
|
-
processStatusTopic;
|
|
14
|
-
processName;
|
|
15
|
-
processId;
|
|
16
|
-
unsMqttProxies;
|
|
17
|
-
unsApiProxies;
|
|
18
|
-
unsTemporalProxies;
|
|
19
|
-
processMqttProxy;
|
|
20
|
-
handoverManager;
|
|
21
|
-
statusMonitor;
|
|
22
|
-
// Plugin
|
|
23
|
-
static pluginApiVersion = 1;
|
|
24
|
-
static registered = new Set();
|
|
25
|
-
static registeredMethodNames = new Set();
|
|
26
|
-
static applied = false;
|
|
27
|
-
static use(plugin) {
|
|
28
|
-
if (this.registered.has(plugin))
|
|
29
|
-
return this;
|
|
30
|
-
this.registered.add(plugin);
|
|
31
|
-
if (this.applied)
|
|
32
|
-
this.applyPlugin(plugin);
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
static applyPlugin(plugin) {
|
|
36
|
-
const define = (methods) => {
|
|
37
|
-
for (const [name, fn] of Object.entries(methods)) {
|
|
38
|
-
if (typeof fn !== "function") {
|
|
39
|
-
throw new TypeError(`UnsProxyProcess plugin attempted to register non-function member \"${name}\".`);
|
|
40
|
-
}
|
|
41
|
-
if (this.registeredMethodNames.has(name) || Reflect.has(this.prototype, name)) {
|
|
42
|
-
throw new Error(`UnsProxyProcess plugin attempted to overwrite existing member \"${name}\".`);
|
|
43
|
-
}
|
|
44
|
-
Object.defineProperty(this.prototype, name, {
|
|
45
|
-
value: fn,
|
|
46
|
-
writable: true,
|
|
47
|
-
configurable: true,
|
|
48
|
-
enumerable: false,
|
|
49
|
-
});
|
|
50
|
-
this.registeredMethodNames.add(name);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
plugin({
|
|
54
|
-
define,
|
|
55
|
-
version: this.pluginApiVersion,
|
|
56
|
-
UnsProxyProcess: this,
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
static applyAll() {
|
|
60
|
-
if (this.applied)
|
|
61
|
-
return;
|
|
62
|
-
this.applied = true;
|
|
63
|
-
for (const p of this.registered)
|
|
64
|
-
this.applyPlugin(p);
|
|
65
|
-
}
|
|
66
|
-
// References for cleanup.
|
|
67
|
-
mqttInputHandler;
|
|
68
|
-
constructor(mqttHost, unsProxyProcessParameters) {
|
|
69
|
-
this.constructor.applyAll(); // Activate all the plugins
|
|
70
|
-
this.unsMqttProxies = [];
|
|
71
|
-
this.unsApiProxies = [];
|
|
72
|
-
this.unsTemporalProxies = [];
|
|
73
|
-
if (!unsProxyProcessParameters?.processName) {
|
|
74
|
-
throw new Error("UnsProxyProcess requires a processName in configuration.");
|
|
75
|
-
}
|
|
76
|
-
this.processName = unsProxyProcessParameters.processName;
|
|
77
|
-
this.processId = randomBytes(16).toString("hex");
|
|
78
|
-
const { name: packageName, version } = PACKAGE_INFO;
|
|
79
|
-
// Instantiate the topic builder.
|
|
80
|
-
const topicBuilder = new MqttTopicBuilder(`uns-infra/${MqttTopicBuilder.sanitizeTopicPart(packageName)}/${MqttTopicBuilder.sanitizeTopicPart(version)}/${MqttTopicBuilder.sanitizeTopicPart(this.processName)}/`);
|
|
81
|
-
// Generate topics.
|
|
82
|
-
const processStatusTopic = topicBuilder.getProcessStatusTopic();
|
|
83
|
-
const handoverTopic = topicBuilder.getHandoverTopic();
|
|
84
|
-
const wildcardActiveTopic = topicBuilder.getWildcardActiveTopic();
|
|
85
|
-
this.processStatusTopic = processStatusTopic;
|
|
86
|
-
// Configure MQTT topics for subscription.
|
|
87
|
-
const mqttSubToTopics = unsProxyProcessParameters?.mqttSubToTopics ?? [
|
|
88
|
-
wildcardActiveTopic,
|
|
89
|
-
handoverTopic,
|
|
90
|
-
];
|
|
91
|
-
const mqttParameters = {
|
|
92
|
-
mqttSubToTopics,
|
|
93
|
-
username: unsProxyProcessParameters?.username ?? "",
|
|
94
|
-
password: unsProxyProcessParameters?.password ?? "",
|
|
95
|
-
mqttSSL: unsProxyProcessParameters?.mqttSSL ?? false,
|
|
96
|
-
statusTopic: this.processStatusTopic,
|
|
97
|
-
clientId: unsProxyProcessParameters?.clientId ?? `${this.processName}-${this.processId}`,
|
|
98
|
-
hosts: unsProxyProcessParameters?.hosts,
|
|
99
|
-
servers: unsProxyProcessParameters?.servers,
|
|
100
|
-
port: unsProxyProcessParameters?.port,
|
|
101
|
-
protocol: unsProxyProcessParameters?.protocol,
|
|
102
|
-
keepalive: unsProxyProcessParameters?.keepalive,
|
|
103
|
-
clean: unsProxyProcessParameters?.clean,
|
|
104
|
-
connectTimeout: unsProxyProcessParameters?.connectTimeout,
|
|
105
|
-
reconnectPeriod: unsProxyProcessParameters?.reconnectPeriod,
|
|
106
|
-
reconnectOnConnackError: unsProxyProcessParameters?.reconnectOnConnackError,
|
|
107
|
-
resubscribe: unsProxyProcessParameters?.resubscribe,
|
|
108
|
-
queueQoSZero: unsProxyProcessParameters?.queueQoSZero,
|
|
109
|
-
rejectUnauthorized: unsProxyProcessParameters?.rejectUnauthorized ?? false,
|
|
110
|
-
properties: unsProxyProcessParameters?.properties,
|
|
111
|
-
ca: unsProxyProcessParameters?.ca,
|
|
112
|
-
cert: unsProxyProcessParameters?.cert,
|
|
113
|
-
key: unsProxyProcessParameters?.key,
|
|
114
|
-
servername: unsProxyProcessParameters?.servername,
|
|
115
|
-
};
|
|
116
|
-
// Initialize MQTT proxy and start connection.
|
|
117
|
-
this.processMqttProxy = new MqttProxy(mqttHost, this.processName, mqttParameters);
|
|
118
|
-
this.processMqttProxy.start();
|
|
119
|
-
// Instantiate and start the StatusMonitor for memory and status updates.
|
|
120
|
-
this.statusMonitor = new StatusMonitor(this.processMqttProxy, this.processStatusTopic, () => this.active, MQTT_UPDATE_INTERVAL, MQTT_UPDATE_INTERVAL, { processName: this.processName, processId: this.processId });
|
|
121
|
-
this.statusMonitor.start();
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Initializes the HandoverManager instance and sets up event listeners for handover and MQTT input events.
|
|
125
|
-
* Determines handover and force start modes based on process arguments.
|
|
126
|
-
*/
|
|
127
|
-
initHandoverManager(instanceMode, handover) {
|
|
128
|
-
const handoverRequestEnabled = instanceMode == "handover" ? true : false;
|
|
129
|
-
const forceStartEnabled = instanceMode == "force" ? true : false;
|
|
130
|
-
this.handoverManager = new HandoverManager(this.processName, this.processId, this.processMqttProxy, this.unsMqttProxies, handoverRequestEnabled, handover ?? true, forceStartEnabled);
|
|
131
|
-
// Listen for handover events.
|
|
132
|
-
this.handoverManager.event.on("handoverManager", (event) => {
|
|
133
|
-
this.active = event.active;
|
|
134
|
-
});
|
|
135
|
-
// Delegate MQTT events to the HandoverManager.
|
|
136
|
-
this.mqttInputHandler = async (event) => {
|
|
137
|
-
await this.handoverManager.handleMqttMessage(event);
|
|
138
|
-
};
|
|
139
|
-
this.processMqttProxy.event.on("input", this.mqttInputHandler);
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Creates a new UNS proxy instance and stores it for future management.
|
|
143
|
-
*/
|
|
144
|
-
async createUnsMqttProxy(mqttHost, instanceName, instanceMode, handover, unsParameters) {
|
|
145
|
-
// Wait until the MQTT connection is established before proceeding.
|
|
146
|
-
await this.waitForProcessConnection();
|
|
147
|
-
// Currently handover manager can handle only mqtt events
|
|
148
|
-
if (!this.handoverManager) {
|
|
149
|
-
this.initHandoverManager(instanceMode, handover);
|
|
150
|
-
}
|
|
151
|
-
const resolvedUnsParameters = {
|
|
152
|
-
...(unsParameters ?? {}),
|
|
153
|
-
clientId: unsParameters?.clientId ?? `${this.processName}-${instanceName}-${this.processId}`,
|
|
154
|
-
};
|
|
155
|
-
const unsMqttProxy = new UnsMqttProxy(mqttHost, this.processName, instanceName, resolvedUnsParameters);
|
|
156
|
-
// Listen for UNS proxy producing topics and publish them via MQTT.
|
|
157
|
-
unsMqttProxy.event.on("unsProxyProducedTopics", (event) => {
|
|
158
|
-
this.processMqttProxy.publish(event.statusTopic, JSON.stringify(event.producedTopics));
|
|
159
|
-
});
|
|
160
|
-
// Listen for UNS proxy status events and publish them via MQTT.
|
|
161
|
-
unsMqttProxy.event.on("mqttProxyStatus", (event) => {
|
|
162
|
-
const time = UnsPacket.formatToISO8601(new Date());
|
|
163
|
-
const unsMessage = { data: { time, value: event.value, uom: event.uom } };
|
|
164
|
-
UnsPacket.unsPacketFromUnsMessage(unsMessage).then((packet) => {
|
|
165
|
-
this.processMqttProxy.publish(event.statusTopic, JSON.stringify(packet));
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
this.unsMqttProxies.push(unsMqttProxy);
|
|
169
|
-
return unsMqttProxy;
|
|
170
|
-
}
|
|
171
|
-
async waitForProcessConnection() {
|
|
172
|
-
while (!this.processMqttProxy?.isConnected) {
|
|
173
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Shuts down the process by clearing intervals, timeouts, and removing
|
|
178
|
-
* MQTT event listeners, and stopping the StatusMonitor.
|
|
179
|
-
*/
|
|
180
|
-
shutdown() {
|
|
181
|
-
logger.info(`${this.processName} - Shutting down UnsProxyProcess...`);
|
|
182
|
-
try {
|
|
183
|
-
this.statusMonitor.stop();
|
|
184
|
-
}
|
|
185
|
-
catch (e) {
|
|
186
|
-
logger.error(`${this.processName} - Error stopping StatusMonitor: ${e.message}`);
|
|
187
|
-
}
|
|
188
|
-
if (this.mqttInputHandler) {
|
|
189
|
-
this.processMqttProxy.event.off("input", this.mqttInputHandler);
|
|
190
|
-
}
|
|
191
|
-
try {
|
|
192
|
-
if (typeof this.processMqttProxy.stop === "function") {
|
|
193
|
-
this.processMqttProxy.stop();
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
catch (e) {
|
|
197
|
-
logger.error(`${this.processName} - Error stopping MQTT proxy: ${e.message}`);
|
|
198
|
-
}
|
|
199
|
-
logger.info(`${this.processName} - Shutdown complete.`);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
export default UnsProxyProcess;
|
|
203
|
-
export { UnsProxyProcess };
|
|
1
|
+
import logger from "../logger.js";
|
|
2
|
+
import { randomBytes } from "crypto";
|
|
3
|
+
import MqttProxy from "../uns-mqtt/mqtt-proxy.js";
|
|
4
|
+
import UnsMqttProxy from "../uns-mqtt/uns-mqtt-proxy.js";
|
|
5
|
+
import { HandoverManager } from "./handover-manager.js";
|
|
6
|
+
// Import configuration and initialization modules.
|
|
7
|
+
import { PACKAGE_INFO, MQTT_UPDATE_INTERVAL } from "./process-config.js";
|
|
8
|
+
import { MqttTopicBuilder } from "../uns-mqtt/mqtt-topic-builder.js";
|
|
9
|
+
import { StatusMonitor } from "./status-monitor.js";
|
|
10
|
+
import { UnsPacket } from "./uns-packet.js";
|
|
11
|
+
class UnsProxyProcess {
|
|
12
|
+
active = false;
|
|
13
|
+
processStatusTopic;
|
|
14
|
+
processName;
|
|
15
|
+
processId;
|
|
16
|
+
unsMqttProxies;
|
|
17
|
+
unsApiProxies;
|
|
18
|
+
unsTemporalProxies;
|
|
19
|
+
processMqttProxy;
|
|
20
|
+
handoverManager;
|
|
21
|
+
statusMonitor;
|
|
22
|
+
// Plugin
|
|
23
|
+
static pluginApiVersion = 1;
|
|
24
|
+
static registered = new Set();
|
|
25
|
+
static registeredMethodNames = new Set();
|
|
26
|
+
static applied = false;
|
|
27
|
+
static use(plugin) {
|
|
28
|
+
if (this.registered.has(plugin))
|
|
29
|
+
return this;
|
|
30
|
+
this.registered.add(plugin);
|
|
31
|
+
if (this.applied)
|
|
32
|
+
this.applyPlugin(plugin);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
static applyPlugin(plugin) {
|
|
36
|
+
const define = (methods) => {
|
|
37
|
+
for (const [name, fn] of Object.entries(methods)) {
|
|
38
|
+
if (typeof fn !== "function") {
|
|
39
|
+
throw new TypeError(`UnsProxyProcess plugin attempted to register non-function member \"${name}\".`);
|
|
40
|
+
}
|
|
41
|
+
if (this.registeredMethodNames.has(name) || Reflect.has(this.prototype, name)) {
|
|
42
|
+
throw new Error(`UnsProxyProcess plugin attempted to overwrite existing member \"${name}\".`);
|
|
43
|
+
}
|
|
44
|
+
Object.defineProperty(this.prototype, name, {
|
|
45
|
+
value: fn,
|
|
46
|
+
writable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
enumerable: false,
|
|
49
|
+
});
|
|
50
|
+
this.registeredMethodNames.add(name);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
plugin({
|
|
54
|
+
define,
|
|
55
|
+
version: this.pluginApiVersion,
|
|
56
|
+
UnsProxyProcess: this,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
static applyAll() {
|
|
60
|
+
if (this.applied)
|
|
61
|
+
return;
|
|
62
|
+
this.applied = true;
|
|
63
|
+
for (const p of this.registered)
|
|
64
|
+
this.applyPlugin(p);
|
|
65
|
+
}
|
|
66
|
+
// References for cleanup.
|
|
67
|
+
mqttInputHandler;
|
|
68
|
+
constructor(mqttHost, unsProxyProcessParameters) {
|
|
69
|
+
this.constructor.applyAll(); // Activate all the plugins
|
|
70
|
+
this.unsMqttProxies = [];
|
|
71
|
+
this.unsApiProxies = [];
|
|
72
|
+
this.unsTemporalProxies = [];
|
|
73
|
+
if (!unsProxyProcessParameters?.processName) {
|
|
74
|
+
throw new Error("UnsProxyProcess requires a processName in configuration.");
|
|
75
|
+
}
|
|
76
|
+
this.processName = unsProxyProcessParameters.processName;
|
|
77
|
+
this.processId = randomBytes(16).toString("hex");
|
|
78
|
+
const { name: packageName, version } = PACKAGE_INFO;
|
|
79
|
+
// Instantiate the topic builder.
|
|
80
|
+
const topicBuilder = new MqttTopicBuilder(`uns-infra/${MqttTopicBuilder.sanitizeTopicPart(packageName)}/${MqttTopicBuilder.sanitizeTopicPart(version)}/${MqttTopicBuilder.sanitizeTopicPart(this.processName)}/`);
|
|
81
|
+
// Generate topics.
|
|
82
|
+
const processStatusTopic = topicBuilder.getProcessStatusTopic();
|
|
83
|
+
const handoverTopic = topicBuilder.getHandoverTopic();
|
|
84
|
+
const wildcardActiveTopic = topicBuilder.getWildcardActiveTopic();
|
|
85
|
+
this.processStatusTopic = processStatusTopic;
|
|
86
|
+
// Configure MQTT topics for subscription.
|
|
87
|
+
const mqttSubToTopics = unsProxyProcessParameters?.mqttSubToTopics ?? [
|
|
88
|
+
wildcardActiveTopic,
|
|
89
|
+
handoverTopic,
|
|
90
|
+
];
|
|
91
|
+
const mqttParameters = {
|
|
92
|
+
mqttSubToTopics,
|
|
93
|
+
username: unsProxyProcessParameters?.username ?? "",
|
|
94
|
+
password: unsProxyProcessParameters?.password ?? "",
|
|
95
|
+
mqttSSL: unsProxyProcessParameters?.mqttSSL ?? false,
|
|
96
|
+
statusTopic: this.processStatusTopic,
|
|
97
|
+
clientId: unsProxyProcessParameters?.clientId ?? `${this.processName}-${this.processId}`,
|
|
98
|
+
hosts: unsProxyProcessParameters?.hosts,
|
|
99
|
+
servers: unsProxyProcessParameters?.servers,
|
|
100
|
+
port: unsProxyProcessParameters?.port,
|
|
101
|
+
protocol: unsProxyProcessParameters?.protocol,
|
|
102
|
+
keepalive: unsProxyProcessParameters?.keepalive,
|
|
103
|
+
clean: unsProxyProcessParameters?.clean,
|
|
104
|
+
connectTimeout: unsProxyProcessParameters?.connectTimeout,
|
|
105
|
+
reconnectPeriod: unsProxyProcessParameters?.reconnectPeriod,
|
|
106
|
+
reconnectOnConnackError: unsProxyProcessParameters?.reconnectOnConnackError,
|
|
107
|
+
resubscribe: unsProxyProcessParameters?.resubscribe,
|
|
108
|
+
queueQoSZero: unsProxyProcessParameters?.queueQoSZero,
|
|
109
|
+
rejectUnauthorized: unsProxyProcessParameters?.rejectUnauthorized ?? false,
|
|
110
|
+
properties: unsProxyProcessParameters?.properties,
|
|
111
|
+
ca: unsProxyProcessParameters?.ca,
|
|
112
|
+
cert: unsProxyProcessParameters?.cert,
|
|
113
|
+
key: unsProxyProcessParameters?.key,
|
|
114
|
+
servername: unsProxyProcessParameters?.servername,
|
|
115
|
+
};
|
|
116
|
+
// Initialize MQTT proxy and start connection.
|
|
117
|
+
this.processMqttProxy = new MqttProxy(mqttHost, this.processName, mqttParameters);
|
|
118
|
+
this.processMqttProxy.start();
|
|
119
|
+
// Instantiate and start the StatusMonitor for memory and status updates.
|
|
120
|
+
this.statusMonitor = new StatusMonitor(this.processMqttProxy, this.processStatusTopic, () => this.active, MQTT_UPDATE_INTERVAL, MQTT_UPDATE_INTERVAL, { processName: this.processName, processId: this.processId });
|
|
121
|
+
this.statusMonitor.start();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Initializes the HandoverManager instance and sets up event listeners for handover and MQTT input events.
|
|
125
|
+
* Determines handover and force start modes based on process arguments.
|
|
126
|
+
*/
|
|
127
|
+
initHandoverManager(instanceMode, handover) {
|
|
128
|
+
const handoverRequestEnabled = instanceMode == "handover" ? true : false;
|
|
129
|
+
const forceStartEnabled = instanceMode == "force" ? true : false;
|
|
130
|
+
this.handoverManager = new HandoverManager(this.processName, this.processId, this.processMqttProxy, this.unsMqttProxies, handoverRequestEnabled, handover ?? true, forceStartEnabled);
|
|
131
|
+
// Listen for handover events.
|
|
132
|
+
this.handoverManager.event.on("handoverManager", (event) => {
|
|
133
|
+
this.active = event.active;
|
|
134
|
+
});
|
|
135
|
+
// Delegate MQTT events to the HandoverManager.
|
|
136
|
+
this.mqttInputHandler = async (event) => {
|
|
137
|
+
await this.handoverManager.handleMqttMessage(event);
|
|
138
|
+
};
|
|
139
|
+
this.processMqttProxy.event.on("input", this.mqttInputHandler);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Creates a new UNS proxy instance and stores it for future management.
|
|
143
|
+
*/
|
|
144
|
+
async createUnsMqttProxy(mqttHost, instanceName, instanceMode, handover, unsParameters) {
|
|
145
|
+
// Wait until the MQTT connection is established before proceeding.
|
|
146
|
+
await this.waitForProcessConnection();
|
|
147
|
+
// Currently handover manager can handle only mqtt events
|
|
148
|
+
if (!this.handoverManager) {
|
|
149
|
+
this.initHandoverManager(instanceMode, handover);
|
|
150
|
+
}
|
|
151
|
+
const resolvedUnsParameters = {
|
|
152
|
+
...(unsParameters ?? {}),
|
|
153
|
+
clientId: unsParameters?.clientId ?? `${this.processName}-${instanceName}-${this.processId}`,
|
|
154
|
+
};
|
|
155
|
+
const unsMqttProxy = new UnsMqttProxy(mqttHost, this.processName, instanceName, resolvedUnsParameters);
|
|
156
|
+
// Listen for UNS proxy producing topics and publish them via MQTT.
|
|
157
|
+
unsMqttProxy.event.on("unsProxyProducedTopics", (event) => {
|
|
158
|
+
this.processMqttProxy.publish(event.statusTopic, JSON.stringify(event.producedTopics));
|
|
159
|
+
});
|
|
160
|
+
// Listen for UNS proxy status events and publish them via MQTT.
|
|
161
|
+
unsMqttProxy.event.on("mqttProxyStatus", (event) => {
|
|
162
|
+
const time = UnsPacket.formatToISO8601(new Date());
|
|
163
|
+
const unsMessage = { data: { time, value: event.value, uom: event.uom } };
|
|
164
|
+
UnsPacket.unsPacketFromUnsMessage(unsMessage).then((packet) => {
|
|
165
|
+
this.processMqttProxy.publish(event.statusTopic, JSON.stringify(packet));
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
this.unsMqttProxies.push(unsMqttProxy);
|
|
169
|
+
return unsMqttProxy;
|
|
170
|
+
}
|
|
171
|
+
async waitForProcessConnection() {
|
|
172
|
+
while (!this.processMqttProxy?.isConnected) {
|
|
173
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Shuts down the process by clearing intervals, timeouts, and removing
|
|
178
|
+
* MQTT event listeners, and stopping the StatusMonitor.
|
|
179
|
+
*/
|
|
180
|
+
shutdown() {
|
|
181
|
+
logger.info(`${this.processName} - Shutting down UnsProxyProcess...`);
|
|
182
|
+
try {
|
|
183
|
+
this.statusMonitor.stop();
|
|
184
|
+
}
|
|
185
|
+
catch (e) {
|
|
186
|
+
logger.error(`${this.processName} - Error stopping StatusMonitor: ${e.message}`);
|
|
187
|
+
}
|
|
188
|
+
if (this.mqttInputHandler) {
|
|
189
|
+
this.processMqttProxy.event.off("input", this.mqttInputHandler);
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
if (typeof this.processMqttProxy.stop === "function") {
|
|
193
|
+
this.processMqttProxy.stop();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch (e) {
|
|
197
|
+
logger.error(`${this.processName} - Error stopping MQTT proxy: ${e.message}`);
|
|
198
|
+
}
|
|
199
|
+
logger.info(`${this.processName} - Shutdown complete.`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
export default UnsProxyProcess;
|
|
203
|
+
export { UnsProxyProcess };
|
|
204
204
|
//# sourceMappingURL=uns-proxy-process.js.map
|
package/dist/uns/uns-proxy.d.ts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import { IApiObject, ITopicObject, UnsEvents } from "./uns-interfaces.js";
|
|
2
|
-
import { UnsEventEmitter } from "./uns-event-emitter.js";
|
|
3
|
-
import { UnsAsset } from "./uns-asset.js";
|
|
4
|
-
import { UnsObjectId, UnsObjectType } from "./uns-object.js";
|
|
5
|
-
import { IApiCatchallMapping } from "./uns-interfaces.js";
|
|
6
|
-
export default class UnsProxy {
|
|
7
|
-
private publishInterval;
|
|
8
|
-
event: UnsEventEmitter<UnsEvents>;
|
|
9
|
-
protected instanceStatusTopic: string;
|
|
10
|
-
protected instanceNameWithSuffix: string;
|
|
11
|
-
private producedTopics;
|
|
12
|
-
private producedApiEndpoints;
|
|
13
|
-
private producedApiCatchall;
|
|
14
|
-
private readonly controllerNameEnv;
|
|
15
|
-
private readonly controllerHostEnv;
|
|
16
|
-
private readonly controllerPortEnv;
|
|
17
|
-
private readonly controllerPublicBaseEnv;
|
|
18
|
-
constructor();
|
|
19
|
-
/**
|
|
20
|
-
* Publishes the list of produced topics to the MQTT broker.
|
|
21
|
-
*/
|
|
22
|
-
private emitProducedTopics;
|
|
23
|
-
/**
|
|
24
|
-
* Publishes the list of produced API endpoints to the MQTT broker.
|
|
25
|
-
*/
|
|
26
|
-
private emitProducedApiEndpoints;
|
|
27
|
-
/**
|
|
28
|
-
* Publishes the list of catch-all API mappings to the MQTT broker.
|
|
29
|
-
*/
|
|
30
|
-
private emitProducedApiCatchall;
|
|
31
|
-
/**
|
|
32
|
-
* Registers a unique topic and keeps its timestamp current.
|
|
33
|
-
*
|
|
34
|
-
* On first call the full entry is created and immediately emitted so the
|
|
35
|
-
* controller can persist the new schema node. On subsequent calls only the
|
|
36
|
-
* timestamp is updated in-place; the 60-second heartbeat interval will carry
|
|
37
|
-
* the updated value to the controller, making the timestamp reflect the time
|
|
38
|
-
* of the most recent data publication rather than the one-off registration
|
|
39
|
-
* moment.
|
|
40
|
-
*
|
|
41
|
-
* @param topicObject - The object containing topic details.
|
|
42
|
-
*/
|
|
43
|
-
protected registerUniqueTopic(topicObject: ITopicObject): void;
|
|
44
|
-
/**
|
|
45
|
-
* Registers an API endpoint to handle requests for a specific topic and attribute.
|
|
46
|
-
*/
|
|
47
|
-
protected registerApiEndpoint(apiObject: IApiObject): void;
|
|
48
|
-
protected registerApiCatchAll(mapping: IApiCatchallMapping): void;
|
|
49
|
-
protected unregisterApiEndpoint(topic: string, asset: UnsAsset, objectType: UnsObjectType, objectId: UnsObjectId, attribute: string): void;
|
|
50
|
-
stop(): Promise<void>;
|
|
51
|
-
}
|
|
1
|
+
import { IApiObject, ITopicObject, UnsEvents } from "./uns-interfaces.js";
|
|
2
|
+
import { UnsEventEmitter } from "./uns-event-emitter.js";
|
|
3
|
+
import { UnsAsset } from "./uns-asset.js";
|
|
4
|
+
import { UnsObjectId, UnsObjectType } from "./uns-object.js";
|
|
5
|
+
import { IApiCatchallMapping } from "./uns-interfaces.js";
|
|
6
|
+
export default class UnsProxy {
|
|
7
|
+
private publishInterval;
|
|
8
|
+
event: UnsEventEmitter<UnsEvents>;
|
|
9
|
+
protected instanceStatusTopic: string;
|
|
10
|
+
protected instanceNameWithSuffix: string;
|
|
11
|
+
private producedTopics;
|
|
12
|
+
private producedApiEndpoints;
|
|
13
|
+
private producedApiCatchall;
|
|
14
|
+
private readonly controllerNameEnv;
|
|
15
|
+
private readonly controllerHostEnv;
|
|
16
|
+
private readonly controllerPortEnv;
|
|
17
|
+
private readonly controllerPublicBaseEnv;
|
|
18
|
+
constructor();
|
|
19
|
+
/**
|
|
20
|
+
* Publishes the list of produced topics to the MQTT broker.
|
|
21
|
+
*/
|
|
22
|
+
private emitProducedTopics;
|
|
23
|
+
/**
|
|
24
|
+
* Publishes the list of produced API endpoints to the MQTT broker.
|
|
25
|
+
*/
|
|
26
|
+
private emitProducedApiEndpoints;
|
|
27
|
+
/**
|
|
28
|
+
* Publishes the list of catch-all API mappings to the MQTT broker.
|
|
29
|
+
*/
|
|
30
|
+
private emitProducedApiCatchall;
|
|
31
|
+
/**
|
|
32
|
+
* Registers a unique topic and keeps its timestamp current.
|
|
33
|
+
*
|
|
34
|
+
* On first call the full entry is created and immediately emitted so the
|
|
35
|
+
* controller can persist the new schema node. On subsequent calls only the
|
|
36
|
+
* timestamp is updated in-place; the 60-second heartbeat interval will carry
|
|
37
|
+
* the updated value to the controller, making the timestamp reflect the time
|
|
38
|
+
* of the most recent data publication rather than the one-off registration
|
|
39
|
+
* moment.
|
|
40
|
+
*
|
|
41
|
+
* @param topicObject - The object containing topic details.
|
|
42
|
+
*/
|
|
43
|
+
protected registerUniqueTopic(topicObject: ITopicObject): void;
|
|
44
|
+
/**
|
|
45
|
+
* Registers an API endpoint to handle requests for a specific topic and attribute.
|
|
46
|
+
*/
|
|
47
|
+
protected registerApiEndpoint(apiObject: IApiObject): void;
|
|
48
|
+
protected registerApiCatchAll(mapping: IApiCatchallMapping): void;
|
|
49
|
+
protected unregisterApiEndpoint(topic: string, asset: UnsAsset, objectType: UnsObjectType, objectId: UnsObjectId, attribute: string): void;
|
|
50
|
+
stop(): Promise<void>;
|
|
51
|
+
}
|
|
52
52
|
//# sourceMappingURL=uns-proxy.d.ts.map
|