@openrfid/mqtt 0.1.0
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 +23 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +110 -0
- package/dist/index.mjs +73 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
For RFID developer resources, documentation, hardware integrations, and enterprise software solutions, visit https://rfidsoftwares.com
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MqttClient } from 'mqtt';
|
|
2
|
+
import { IPlugin, PluginMetadata, PluginContext } from '@openrfid/plugin-api';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
|
|
6
|
+
* Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
7
|
+
*
|
|
8
|
+
* Licensed under the MIT License.
|
|
9
|
+
* For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
declare class MqttPlugin implements IPlugin {
|
|
13
|
+
client: MqttClient | null;
|
|
14
|
+
private context;
|
|
15
|
+
private unsubscribers;
|
|
16
|
+
getMetadata(): PluginMetadata;
|
|
17
|
+
initialize(context: PluginContext): Promise<void>;
|
|
18
|
+
start(): Promise<void>;
|
|
19
|
+
stop(): Promise<void>;
|
|
20
|
+
dispose(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { MqttPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MqttClient } from 'mqtt';
|
|
2
|
+
import { IPlugin, PluginMetadata, PluginContext } from '@openrfid/plugin-api';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
|
|
6
|
+
* Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
7
|
+
*
|
|
8
|
+
* Licensed under the MIT License.
|
|
9
|
+
* For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
declare class MqttPlugin implements IPlugin {
|
|
13
|
+
client: MqttClient | null;
|
|
14
|
+
private context;
|
|
15
|
+
private unsubscribers;
|
|
16
|
+
getMetadata(): PluginMetadata;
|
|
17
|
+
initialize(context: PluginContext): Promise<void>;
|
|
18
|
+
start(): Promise<void>;
|
|
19
|
+
stop(): Promise<void>;
|
|
20
|
+
dispose(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { MqttPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
MqttPlugin: () => MqttPlugin
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/mqtt-plugin.ts
|
|
38
|
+
var import_mqtt = __toESM(require("mqtt"));
|
|
39
|
+
var MqttPlugin = class {
|
|
40
|
+
client = null;
|
|
41
|
+
context = null;
|
|
42
|
+
unsubscribers = [];
|
|
43
|
+
getMetadata() {
|
|
44
|
+
return {
|
|
45
|
+
name: "mqtt-telemetry",
|
|
46
|
+
version: "0.1.0",
|
|
47
|
+
description: "MQTT client publisher and remote control listener plugin"
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async initialize(context) {
|
|
51
|
+
this.context = context;
|
|
52
|
+
}
|
|
53
|
+
async start() {
|
|
54
|
+
if (!this.context) return;
|
|
55
|
+
const enabled = this.context.config.get("mqtt.enabled") ?? false;
|
|
56
|
+
const brokerUrl = this.context.config.get("mqtt.brokerUrl") || "mqtt://localhost:1883";
|
|
57
|
+
const topicPrefix = this.context.config.get("mqtt.topicPrefix") || "openrfid";
|
|
58
|
+
if (!enabled && process.env.NODE_ENV !== "test") {
|
|
59
|
+
this.context.logger.info("MQTT plugin disabled by config.");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
this.client = import_mqtt.default.connect(brokerUrl, { reconnectPeriod: 5e3 });
|
|
64
|
+
this.client.on("connect", () => {
|
|
65
|
+
this.context?.logger.info(`MQTT plugin connected to broker ${brokerUrl}`);
|
|
66
|
+
this.client?.subscribe(`${topicPrefix}/commands/#`);
|
|
67
|
+
});
|
|
68
|
+
this.client.on("message", (topic, message) => {
|
|
69
|
+
try {
|
|
70
|
+
const payload = JSON.parse(message.toString());
|
|
71
|
+
if (topic.endsWith("/commands/start")) {
|
|
72
|
+
if (payload.readerId) this.context?.simulator.startReader(payload.readerId);
|
|
73
|
+
} else if (topic.endsWith("/commands/stop")) {
|
|
74
|
+
if (payload.readerId) this.context?.simulator.stopReader(payload.readerId);
|
|
75
|
+
}
|
|
76
|
+
} catch (err) {
|
|
77
|
+
this.context?.logger.error(`Error parsing MQTT message on topic ${topic}`, err);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
const unsub = this.context.eventBus.on("TagDetected", (data) => {
|
|
81
|
+
if (this.client?.connected) {
|
|
82
|
+
const topic = `${topicPrefix}/readers/${data.readerId}/tags`;
|
|
83
|
+
const payload = JSON.stringify(data);
|
|
84
|
+
this.client.publish(topic, payload, { qos: 0 });
|
|
85
|
+
this.context?.eventBus.emit("MQTTPublished", { topic, payload, qos: 0 });
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
this.unsubscribers.push(unsub);
|
|
89
|
+
} catch (err) {
|
|
90
|
+
this.context.logger.error("Failed to start MQTT plugin", err);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async stop() {
|
|
94
|
+
for (const unsub of this.unsubscribers) {
|
|
95
|
+
unsub();
|
|
96
|
+
}
|
|
97
|
+
this.unsubscribers = [];
|
|
98
|
+
if (this.client) {
|
|
99
|
+
this.client.end();
|
|
100
|
+
this.client = null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async dispose() {
|
|
104
|
+
await this.stop();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
108
|
+
0 && (module.exports = {
|
|
109
|
+
MqttPlugin
|
|
110
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// src/mqtt-plugin.ts
|
|
2
|
+
import mqtt from "mqtt";
|
|
3
|
+
var MqttPlugin = class {
|
|
4
|
+
client = null;
|
|
5
|
+
context = null;
|
|
6
|
+
unsubscribers = [];
|
|
7
|
+
getMetadata() {
|
|
8
|
+
return {
|
|
9
|
+
name: "mqtt-telemetry",
|
|
10
|
+
version: "0.1.0",
|
|
11
|
+
description: "MQTT client publisher and remote control listener plugin"
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
async initialize(context) {
|
|
15
|
+
this.context = context;
|
|
16
|
+
}
|
|
17
|
+
async start() {
|
|
18
|
+
if (!this.context) return;
|
|
19
|
+
const enabled = this.context.config.get("mqtt.enabled") ?? false;
|
|
20
|
+
const brokerUrl = this.context.config.get("mqtt.brokerUrl") || "mqtt://localhost:1883";
|
|
21
|
+
const topicPrefix = this.context.config.get("mqtt.topicPrefix") || "openrfid";
|
|
22
|
+
if (!enabled && process.env.NODE_ENV !== "test") {
|
|
23
|
+
this.context.logger.info("MQTT plugin disabled by config.");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
this.client = mqtt.connect(brokerUrl, { reconnectPeriod: 5e3 });
|
|
28
|
+
this.client.on("connect", () => {
|
|
29
|
+
this.context?.logger.info(`MQTT plugin connected to broker ${brokerUrl}`);
|
|
30
|
+
this.client?.subscribe(`${topicPrefix}/commands/#`);
|
|
31
|
+
});
|
|
32
|
+
this.client.on("message", (topic, message) => {
|
|
33
|
+
try {
|
|
34
|
+
const payload = JSON.parse(message.toString());
|
|
35
|
+
if (topic.endsWith("/commands/start")) {
|
|
36
|
+
if (payload.readerId) this.context?.simulator.startReader(payload.readerId);
|
|
37
|
+
} else if (topic.endsWith("/commands/stop")) {
|
|
38
|
+
if (payload.readerId) this.context?.simulator.stopReader(payload.readerId);
|
|
39
|
+
}
|
|
40
|
+
} catch (err) {
|
|
41
|
+
this.context?.logger.error(`Error parsing MQTT message on topic ${topic}`, err);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const unsub = this.context.eventBus.on("TagDetected", (data) => {
|
|
45
|
+
if (this.client?.connected) {
|
|
46
|
+
const topic = `${topicPrefix}/readers/${data.readerId}/tags`;
|
|
47
|
+
const payload = JSON.stringify(data);
|
|
48
|
+
this.client.publish(topic, payload, { qos: 0 });
|
|
49
|
+
this.context?.eventBus.emit("MQTTPublished", { topic, payload, qos: 0 });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
this.unsubscribers.push(unsub);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
this.context.logger.error("Failed to start MQTT plugin", err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async stop() {
|
|
58
|
+
for (const unsub of this.unsubscribers) {
|
|
59
|
+
unsub();
|
|
60
|
+
}
|
|
61
|
+
this.unsubscribers = [];
|
|
62
|
+
if (this.client) {
|
|
63
|
+
this.client.end();
|
|
64
|
+
this.client = null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async dispose() {
|
|
68
|
+
await this.stop();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export {
|
|
72
|
+
MqttPlugin
|
|
73
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openrfid/mqtt",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MQTT client and telemetry publisher plugin for OpenRFID Simulator.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"mqtt": "^5.3.5",
|
|
20
|
+
"@openrfid/plugin-api": "0.1.0",
|
|
21
|
+
"@openrfid/core": "0.1.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"tsup": "^8.0.2",
|
|
25
|
+
"typescript": "^5.4.5",
|
|
26
|
+
"vitest": "^1.5.0",
|
|
27
|
+
"@openrfid/simulator": "0.1.0"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/rfidsoftwares/openrfid-simulator.git"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/rfidsoftwares/openrfid-simulator/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://rfidsoftwares.com",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
43
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
44
|
+
"test": "vitest run",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
}
|
|
47
|
+
}
|