-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmqtt.ts
More file actions
763 lines (716 loc) · 24.2 KB
/
Copy pathmqtt.ts
File metadata and controls
763 lines (716 loc) · 24.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
import * as mqtt from "mqtt";
import type {
ISparkplugEdgeOptions,
ISparkplugHostOptions,
SparkplugHost,
SparkplugNode,
SparkplugTopic,
} from "./types.ts";
import * as sparkplug from "sparkplug-payload";
import { pipe } from "@joyautomation/dark-matter";
import { logs } from "./log.ts";
const { main: log } = logs;
import type {
UMetric,
UPayload,
} from "sparkplug-payload/lib/sparkplugbpayload.js";
import {
compressPayloadCurry as compressPayloadCurry,
decompressPayload,
} from "./compression/index.ts";
import type { PayloadOptions } from "./compression/types.ts";
import { cond, isBuffer } from "./utils.ts";
import type { EventEmitter } from "node:events";
import { Buffer } from "node:buffer";
import { getMqttConfigFromSparkplug } from "./stateMachines/utils.ts";
/**
* Wrapper function to connect to an MQTT broker
* @param {string} url - The URL of the MQTT broker
* @param {mqtt.IClientOptions} options - MQTT client options
* @returns {mqtt.MqttClient} MQTT client instance
*/
export function mqttConnect(
url: string,
options: mqtt.IClientOptions
): mqtt.MqttClient {
return mqtt.connect(url, options);
}
/**
* Internal utilities exposed for testing purposes
* @internal
*/
export const _internals = {
mqttConnect,
};
/** Sparkplug B protocol version */
const version: string = "spBv1.0";
/** Sparkplug B payload encoder/decoder instance */
const spb: {
encodePayload: (object: UPayload) => Uint8Array;
decodePayload: (proto: Uint8Array) => UPayload;
} = sparkplug.get(version)!;
/**
* Encodes a Sparkplug B payload object into a Uint8Array
* @param {UPayload} payload - The Sparkplug B payload object to encode
* @returns {Uint8Array} The encoded payload as a Uint8Array
*/
export const encodePayload: (payload: UPayload) => Uint8Array =
spb.encodePayload;
/**
* Decodes a Sparkplug B payload from a Uint8Array into a payload object
* @param {Uint8Array} buffer - The Uint8Array containing the encoded Sparkplug B payload
* @returns {UPayload} The decoded Sparkplug B payload object
*/
export const decodePayload: (buffer: Uint8Array) => UPayload =
spb.decodePayload;
/**
* Converts a Uint8Array to a Buffer
* @param {Uint8Array} payload - The payload to convert
* @returns {Buffer} The converted Buffer
*/
const toBuffer = (payload: Uint8Array): Buffer => Buffer.from(payload);
/**
* Creates a Sparkplug B topic string
* @param {string} commandType - The Sparkplug B command type
* @param {ISparkplugEdgeOptions} options - Sparkplug edge configuration options
* @param {string} [deviceId] - Optional device ID
* @returns {string} Formatted Sparkplug B topic string
*/
export const createSpbTopic = (
commandType:
| "NBIRTH"
| "NDEATH"
| "NDATA"
| "NCMD"
| "DCMD"
| "DBIRTH"
| "DDEATH"
| "DDATA",
{ version, groupId, edgeNode }: ISparkplugEdgeOptions,
deviceId?: string
): string =>
`${version}/${groupId}/${commandType}/${edgeNode}${
deviceId ? "/" + deviceId : ""
}`;
/**
* Creates a payload buffer from a given payload object
* @param {any} payload - The payload object to encode
* @returns {Buffer} Encoded payload as a Buffer
*/
export const createPayload = (payload: UPayload): Buffer =>
pipe(payload, encodePayload, toBuffer);
/**
* Adds a bdSeq metric to the given payload
* @param {number} bdSeq - Birth/death sequence number
* @param {UPayload} payload - The payload to modify
* @returns {UPayload} Modified payload with bdSeq metric added
*/
export const addBdSeqMetric = (bdSeq: number, payload: UPayload): UPayload => ({
...payload,
metrics: [
...(payload.metrics || []),
{
name: "bdSeq",
value: bdSeq,
type: "UInt64",
},
],
});
/**
* Creates a curried function to add bdSeq metric to a payload
* @param {number} bdSeq - Birth/death sequence number
* @returns {Function} Curried function that takes a payload and returns a modified payload
*/
export const addBdSeqMetricCurry =
(bdSeq: number) =>
(payload: UPayload): UPayload =>
addBdSeqMetric(bdSeq, payload);
/**
* Adds a sequence number to the given payload
* @param {SparkplugNode | SparkplugHost} sparkplug - Sparkplug node or host instance
* @param {UPayload} payload - The payload to modify
* @returns {UPayload} Modified payload with sequence number added
*/
export const addSeqNumber = (
sparkplug: SparkplugNode | SparkplugHost,
payload: UPayload
): UPayload => {
if (sparkplug.seq == 256) sparkplug.seq = 0;
return {
...payload,
seq: sparkplug.seq++ || 0,
};
};
/**
* Creates a curried function to add sequence number to a payload
* @param {SparkplugNode | SparkplugHost} sparkplug - Sparkplug node or host instance
* @returns {Function} Curried function that takes a payload and returns a modified payload
*/
export const addSeqNumberCurry =
(sparkplug: SparkplugNode | SparkplugHost) =>
(payload: UPayload): UPayload =>
addSeqNumber(sparkplug, payload);
/**
* Publishes a node death message
* @param {number} bdSeq - Birth/death sequence number
* @param {ISparkplugEdgeOptions} mqttConfig - MQTT configuration options
* @param {mqtt.MqttClient} client - MQTT client instance
*/
export const publishNodeDeath = (
bdSeq: number,
mqttConfig: ISparkplugEdgeOptions,
client: mqtt.MqttClient
): void => {
const payload = getDeathPayload(bdSeq);
const topic = createSpbTopic("NDEATH", mqttConfig);
publish(topic, pipe(payload, encodePayload, toBuffer), client);
};
/**
* Publishes a node birth message
* @param {number} bdSeq - Birth/death sequence number
* @param {number} seq - Sequence number
* @param {PayloadOptions | undefined} options - Payload compression options
* @param {UPayload} payload - The payload to publish
* @param {ISparkplugEdgeOptions} mqttConfig - MQTT configuration options
* @param {mqtt.MqttClient} client - MQTT client instance
*/
export const publishNodeBirth = (
stateMachine: SparkplugNode,
options: PayloadOptions | undefined,
payload: UPayload
): void => {
const mqttConfig = getMqttConfigFromSparkplug(stateMachine);
const topic = createSpbTopic("NBIRTH", mqttConfig);
publish(
topic,
pipe(
payload,
addSeqNumberCurry(stateMachine),
addBdSeqMetricCurry(stateMachine.bdseq),
compressPayloadCurry(options),
encodePayload,
toBuffer
) as Buffer,
stateMachine.mqtt!
);
log.info(`published node ${mqttConfig.edgeNode} birth`);
};
/**
* Function type for publishing Sparkplug B payloads (DBIRTH, DDEATH, DDATA, or NDATA)
* @internal
*/
export type PublishPayload = ReturnType<typeof publishPayload>;
/**
* Publishes a payload for a specific Sparkplug command
* @param {string} command - The Sparkplug command type
* @returns {Function} A function that publishes the payload for the specified command
*/
const publishPayload =
(command: "DBIRTH" | "DDEATH" | "DDATA" | "NDATA" | "NCMD" | "DCMD") =>
(
sparkplug: SparkplugNode,
payload: UPayload,
mqttConfig: ISparkplugEdgeOptions,
client: mqtt.MqttClient,
deviceId?: string
): void => {
const topic = createSpbTopic(command, mqttConfig, deviceId);
if (command === "NDATA") {
log.debug(`Publishing NDATA on node ${mqttConfig.edgeNode}`);
} else {
log.debug(
`Publishing Device ${deviceId} ${command} on node ${mqttConfig.edgeNode}`
);
}
publish(
topic,
pipe(
payload,
addSeqNumberCurry(sparkplug) as Modify<UPayload>,
compressPayloadCurry(sparkplug.payloadOptions) as Modify<UPayload>,
encodePayload,
toBuffer
) as Buffer,
client
);
sparkplug.events.emit(`publish-${command.toLowerCase()}`, topic, payload);
};
/**
* Publishes a device birth message
* @param {SparkplugNode} sparkplug - The Sparkplug node instance
* @param {UPayload} payload - The payload to publish
* @param {ISparkplugEdgeOptions} mqttConfig - MQTT configuration options
* @param {mqtt.MqttClient} client - MQTT client instance
* @param {string} [deviceId] - Optional device identifier
*/
export const publishDeviceBirth: PublishPayload = publishPayload("DBIRTH");
/**
* Publishes a device death (DDEATH) message to a Sparkplug B MQTT topic
* @param {SparkplugNode} sparkplug - The Sparkplug node instance
* @param {UPayload} payload - The payload to publish
* @param {ISparkplugEdgeOptions} mqttConfig - MQTT configuration options
* @param {mqtt.MqttClient} client - MQTT client instance
* @param {string} [deviceId] - Optional device identifier
*/
export const publishDeviceDeath: PublishPayload = publishPayload("DDEATH");
/**
* Publishes device data (DDATA) message to a Sparkplug B MQTT topic
* @param {SparkplugNode} sparkplug - The Sparkplug node instance
* @param {UPayload} payload - The payload containing device metrics
* @param {ISparkplugEdgeOptions} mqttConfig - MQTT configuration options
* @param {mqtt.MqttClient} client - MQTT client instance
* @param {string} [deviceId] - Optional device identifier
*/
export const publishDeviceData: PublishPayload = publishPayload("DDATA");
/**
* Publishes node data (NDATA) message to a Sparkplug B MQTT topic
* @param {SparkplugNode} sparkplug - The Sparkplug node instance
* @param {UPayload} payload - The payload containing node metrics
* @param {ISparkplugEdgeOptions} mqttConfig - MQTT configuration options
* @param {mqtt.MqttClient} client - MQTT client instance
*/
export const publishNodeData: PublishPayload = publishPayload("NDATA");
const createCommandPayload = (
command: "NCMD" | "DCMD",
commandName: string,
type: UMetric["type"],
value: UMetric["value"]
): UPayload => ({
metrics: [
{
name: `${
command == "NCMD" ? "Node Control" : "Device Control"
}/${commandName}`,
value,
type,
},
],
});
/**
* Type representing a function that publishes Sparkplug B commands (NCMD or DCMD)
* @internal
*/
export type PublishCommand = ReturnType<typeof publishCommand>;
/**
* Creates a function to publish Sparkplug B commands (NCMD or DCMD)
* @param {("NCMD" | "DCMD")} command - The type of command to publish (Node or Device)
* @returns {Function} A function that publishes the specified command type with the following parameters:
* - sparkplug: SparkplugHost - The Sparkplug host instance
* - commandName: string - Name of the command to publish
* - type: UMetric["type"] - Type of the metric value
* - value: UMetric["value"] - Value of the metric
* - groupId: string - Sparkplug group identifier
* - edgeNode: string - Edge node identifier
* - mqttConfig: ISparkplugHostOptions - MQTT configuration options
* - client: mqtt.MqttClient - MQTT client instance
* - deviceId?: string - Optional device identifier (only for DCMD)
*/
const publishCommand =
(command: "NCMD" | "DCMD") =>
(
sparkplug: SparkplugHost,
commandName: string,
type: UMetric["type"],
value: UMetric["value"],
groupId: string,
edgeNode: string,
mqttConfig: ISparkplugHostOptions,
client: mqtt.MqttClient,
deviceId?: string
): void => {
const topic = createSpbTopic(
command,
{ ...mqttConfig, groupId, edgeNode },
deviceId
);
const payload = createCommandPayload(command, commandName, type, value);
publish(
topic,
pipe(
payload,
addSeqNumberCurry(sparkplug) as Modify<UPayload>,
compressPayloadCurry(sparkplug.payloadOptions) as Modify<UPayload>,
encodePayload,
toBuffer
) as Buffer,
client
);
sparkplug.events.emit(`publish-${command.toLowerCase()}`, topic, payload);
};
/**
* Publishes a node command (NCMD) message to a Sparkplug B MQTT topic
* @param {SparkplugHost} sparkplug - The Sparkplug host instance
* @param {string} commandName - Name of the command to publish
* @param {UMetric["type"]} type - Type of the metric value
* @param {UMetric["value"]} value - Value of the metric
* @param {string} groupId - Sparkplug group identifier
* @param {string} edgeNode - Edge node identifier
* @param {ISparkplugHostOptions} mqttConfig - MQTT configuration options
* @param {mqtt.MqttClient} client - MQTT client instance
*/
export const publishNodeCommand: PublishCommand = publishCommand("NCMD");
/**
* Publishes a device command (DCMD) message to a Sparkplug B MQTT topic
* @param {SparkplugHost} sparkplug - The Sparkplug host instance
* @param {string} commandName - Name of the command to publish
* @param {UMetric["type"]} type - Type of the metric value
* @param {UMetric["value"]} value - Value of the metric
* @param {string} groupId - Sparkplug group identifier
* @param {string} edgeNode - Edge node identifier
* @param {ISparkplugHostOptions} mqttConfig - MQTT configuration options
* @param {mqtt.MqttClient} client - MQTT client instance
* @param {string} [deviceId] - Optional device identifier
*/
export const publishDeviceCommand: PublishCommand = publishCommand("DCMD");
/**
* Publishes a message to an MQTT topic
* @param {string} topic - The MQTT topic to publish to
* @param {string | Buffer} message - The message to publish
* @param {mqtt.MqttClient} client - The MQTT client instance
*/
export const publish = (
topic: string,
message: Buffer,
client: mqtt.MqttClient
): void => {
try {
client.publish(topic, message);
} catch (error: unknown) {
if (error instanceof Error) {
log.error(`Error publishing message to topic ${topic}: ${error.stack}`);
} else {
log.error(`Error publishing message to topic ${topic}: ${String(error)}`);
}
}
};
/**
* Subscribes to an MQTT topic
* @param {string | string[]} topic - The topic(s) to subscribe to
* @param {mqtt.IClientSubscribeOptions | mqtt.IClientSubscribeProperties | undefined} options - Subscription options
* @param {mqtt.MqttClient} mqttClient - The MQTT client instance
* @returns {mqtt.MqttClient} The MQTT client instance
*/
export const subscribe = (
topic: string,
options: mqtt.IClientSubscribeOptions,
mqttClient: mqtt.MqttClient,
sharedGroup?: string
): mqtt.MqttClient => {
const fullTopic = `${sharedGroup ? `$share/${sharedGroup}/` : ""}${topic}`;
log.info("subscribed to " + fullTopic);
mqttClient.subscribe(fullTopic, options);
return mqttClient;
};
/**
* Creates a curried function to subscribe to an MQTT topic
* @param {string} topic - The topic to subscribe to
* @param {mqtt.IClientSubscribeOptions} options - Subscription options
* @returns {Function} A function that takes an MQTT client and subscribes to the topic
*/
export const subscribeCurry =
(
topic: string,
options: mqtt.IClientSubscribeOptions,
sharedGroup?: string
) =>
(mqttClient: mqtt.MqttClient): mqtt.MqttClient =>
subscribe(topic, options, mqttClient, sharedGroup);
/**
* Unsubscribes from an MQTT topic
* @param {string} topic - The topic to unsubscribe from
* @param {mqtt.IClientSubscribeOptions} options - Unsubscribe options
* @param {mqtt.MqttClient} mqttClient - The MQTT client instance
* @returns {mqtt.MqttClient} The MQTT client instance
*/
export const unsubscribe = (
topic: string,
client: mqtt.MqttClient,
sharedGroup?: string
): void => {
log.info("unsubscribed from " + topic);
client.unsubscribe(`${sharedGroup ? `$share/${sharedGroup}/` : ""}${topic}`);
};
/** Type for functions that modify a given type */
export type Modify<U> = (u: U) => U;
/**
* Gets the death payload with the given birth/death sequence number
* @param {number} bdSeq - Birth/death sequence number
* @returns {UPayload} Death payload
*/
const getDeathPayload = (bdSeq: number): UPayload => ({
timestamp: new Date().getTime(),
metrics: [
{
name: "bdSeq",
value: bdSeq,
type: "UInt64",
},
],
});
/**
* Publishes the host online message
* @param {SparkplugHost} host - The Sparkplug host instance
*/
export const publishHostOnline = (host: SparkplugHost): void => {
const topic = `STATE/${host.primaryHostId}`;
const payload = "ONLINE";
log.info("Publishing Primary Host Online.");
host.mqtt?.publish(topic, payload, { retain: true });
};
/**
* Creates an MQTT client for a Sparkplug host
* @param {ISparkplugHostOptions} config - Host configuration options
* @returns {mqtt.MqttClient} MQTT client instance
*/
export const createHostMqttClient = (
config: ISparkplugHostOptions
): mqtt.MqttClient => {
const { serverUrl, clientId, keepalive, username, password, primaryHostId } =
config;
const mqttOptions: mqtt.IClientOptions = {
...(config.mqttOptions || {}), // allow additional options
clientId,
clean: true,
keepalive,
reschedulePings: false,
connectTimeout: 30000,
username,
password,
will: {
topic: `STATE/${primaryHostId}`,
payload: Buffer.from(`OFFLINE`, "utf8"),
qos: 0,
retain: true,
},
};
return _internals.mqttConnect(serverUrl, mqttOptions);
};
/**
* Creates an MQTT client for a Sparkplug edge node
* @param {ISparkplugEdgeOptions} config - Edge node configuration options
* @param {number} [bdSeq=0] - Initial birth/death sequence number
* @returns {mqtt.MqttClient} MQTT client instance
*/
export const createMqttClient = (
config: ISparkplugEdgeOptions,
bdSeq = 0
): mqtt.MqttClient => {
const {
serverUrl,
clientId,
keepalive,
username,
password,
version,
groupId,
edgeNode,
} = config;
const mqttOptions: mqtt.IClientOptions = {
...(config.mqttOptions || {}), // allow additional options
clientId,
clean: true,
keepalive,
reschedulePings: false,
connectTimeout: 30000,
username,
password,
will: {
topic: `${version}/${groupId}/NDEATH/${edgeNode}`,
payload: pipe(getDeathPayload(bdSeq), encodePayload, toBuffer),
qos: 0,
retain: false,
},
};
return _internals.mqttConnect(serverUrl, mqttOptions);
};
/**
* Destroys the MQTT client
* @param {mqtt.MqttClient | null} client - The MQTT client to destroy
*/
export const destroyMqttClient = (client: mqtt.MqttClient | null): void => {
client?.end();
};
/** Input type for Sparkplug B message conditions and actions */
type SpbMessageConditionInput = {
topic: SparkplugTopic;
message: UPayload | Uint8Array;
};
/** Type for Sparkplug B message condition functions */
type SpbMessageCondition = (input: SpbMessageConditionInput) => boolean;
/** Type for Sparkplug B message action functions */
type SpbMessageAction = (input: SpbMessageConditionInput) => void;
/** Type representing a conditional Sparkplug B message handler */
type SpbMessageConditional = {
condition: SpbMessageCondition;
action: SpbMessageAction;
};
/** Array of all Sparkplug B command types */
const commands = [
"DDATA",
"NBIRTH",
"DBIRTH",
"NDEATH",
"DDEATH",
"NDATA",
"NCMD",
"DCMD",
];
/** Array of Sparkplug B command types specific to edge nodes */
const edgeCommands = commands.filter((command) =>
["NCMD", "DCMD"].includes(command)
);
/**
* Parses a Sparkplug B topic into a human-readable message
* @param {SparkplugTopic} topic - The parsed Sparkplug B topic
* @returns {string} A human-readable message describing the topic
*/
const parseTopicMessage = (topic: SparkplugTopic) => {
const parts = [
topic.groupId && `group: ${topic.groupId}`,
topic.edgeNode && `node: ${topic.edgeNode}`,
topic.deviceId && `device: ${topic.deviceId}`,
].filter(Boolean);
return `${parts.join(", ")}`;
};
/**
* Creates a command action for handling Sparkplug messages
* @param {string} key - The command key
* @param {EventEmitter} emitter - The event emitter instance
* @returns {Function} A function that handles the command action
*/
const createCommandAction =
(key: string, emitter: EventEmitter) =>
({ topic, message }: SpbMessageConditionInput) => {
try {
const decompressed = decompressPayload(message);
if (isBuffer(decompressed)) {
const decoded = decodePayload(decompressed);
emitter.emit(key.toLowerCase(), topic, decoded);
}
log.debug(`${key} message received for ${parseTopicMessage(topic)}`);
} catch (error) {
log.error(
`Error processing ${key} message for ${parseTopicMessage(topic)}: ${error instanceof Error ? error.message : error}`
);
}
};
/**
* Creates handlers for edge commands
* @param {EventEmitter} emitter - The event emitter instance
* @param {ISparkplugEdgeOptions} mqttConfig - The MQTT configuration for the edge
* @returns {SpbMessageConditional[]} An array of command handlers
*/
const createHandleEdgeCommands = (
emitter: EventEmitter,
mqttConfig: ISparkplugEdgeOptions
) => {
return edgeCommands.map(
(key): SpbMessageConditional => ({
condition: ({ topic }) =>
mqttConfig.version === topic.version &&
mqttConfig.groupId === topic.groupId &&
key === topic.commandType &&
mqttConfig.edgeNode === topic.edgeNode,
action: createCommandAction(key, emitter),
})
);
};
/**
* Creates handlers for host commands
* @param {EventEmitter} emitter - The event emitter instance
* @param {ISparkplugHostOptions} mqttConfig - The MQTT configuration for the host
* @returns {SpbMessageConditional[]} An array of command handlers
*/
const createHandleHostCommands = (
emitter: EventEmitter,
mqttConfig: ISparkplugHostOptions
) => {
return commands.map(
(key): SpbMessageConditional => ({
condition: ({ topic }) =>
mqttConfig.version === topic.version && key === topic.commandType,
action: createCommandAction(key, emitter),
})
);
};
/**
* Parses a Sparkplug B topic string into its components
* @param {string} topic - The Sparkplug B topic string to parse
* @returns {SparkplugTopic} An object containing the parsed topic components
*/
export const parseSpbTopic = (topic: string): SparkplugTopic => {
const [version, groupId, commandType, edgeNode, deviceId] = topic.split("/");
return { version, groupId, commandType, edgeNode, deviceId };
};
/**
* Handles incoming MQTT messages
* @param {string} topic - The topic of the received message
* @param {Buffer} message - The message payload
* @param {EventEmitter} emitter - Event emitter to trigger events
* @param {ISparkplugEdgeOptions | ISparkplugHostOptions} mqttConfig - MQTT configuration
*/
export const handleMessage = (
topic: string,
message: Buffer,
emitter: EventEmitter,
mqttConfig: ISparkplugEdgeOptions | ISparkplugHostOptions
): void => {
const spbTopic = parseSpbTopic(topic);
const isEdgeConfig = (
config: ISparkplugEdgeOptions | ISparkplugHostOptions
): config is ISparkplugEdgeOptions => {
return "groupId" in config && "edgeNode" in config;
};
const handleCommands = isEdgeConfig(mqttConfig)
? createHandleEdgeCommands(emitter, mqttConfig)
: createHandleHostCommands(emitter, mqttConfig);
// Convert Buffer to Uint8Array
const messageArray = new Uint8Array(
message.buffer,
message.byteOffset,
message.byteLength
);
return cond({ topic: spbTopic, message: messageArray }, [
{
condition: ({ topic }) => topic.version === "STATE",
action: ({ topic, message }) => {
const state = Buffer.from(message).toString();
log.info(`STATE message received for ${topic.groupId}, it is ${state}`);
const primaryHostId = topic.groupId;
emitter.emit("state", state, primaryHostId);
},
},
{
// Sparkplug B 3.0: spBv1.0/STATE/{hostId} with JSON payload {"online":true,"timestamp":...}
condition: ({ topic }) => topic.groupId === "STATE" && !!topic.commandType,
action: ({ topic, message }) => {
const state = Buffer.from(message).toString();
const primaryHostId = topic.commandType;
log.info(`spBv1.0 STATE message received for ${primaryHostId}, payload: ${state}`);
emitter.emit("state", state, primaryHostId);
},
},
...handleCommands,
{
condition: () => true,
action: ({ topic, message }) => {
try {
const decompressed = decompressPayload(message);
if (isBuffer(decompressed)) {
const decoded = decodePayload(decompressed);
log.debug(`Uncaught message received on topic ${topic}.`);
emitter.emit("message", topic, decoded);
}
} catch (_error) {
const payload = Buffer.from(message).toString();
log.debug(
`Uncaught non-decompressable, undecodable message received for ${topic} with payload ${String(
payload
)}`
);
emitter.emit("message", topic, payload);
}
},
},
]);
};