@openrfid/rest 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 +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +246 -0
- package/dist/index.mjs +206 -0
- package/package.json +49 -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,52 @@
|
|
|
1
|
+
import { FastifyInstance } from 'fastify';
|
|
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 RestPlugin implements IPlugin {
|
|
13
|
+
server: FastifyInstance | null;
|
|
14
|
+
private context;
|
|
15
|
+
getMetadata(): PluginMetadata;
|
|
16
|
+
initialize(context: PluginContext): Promise<void>;
|
|
17
|
+
start(): Promise<void>;
|
|
18
|
+
stop(): Promise<void>;
|
|
19
|
+
dispose(): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
|
|
24
|
+
* Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
25
|
+
*
|
|
26
|
+
* Licensed under the MIT License.
|
|
27
|
+
* For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
declare function readerRoutes(fastify: FastifyInstance, context: PluginContext): Promise<void>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
|
|
34
|
+
* Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
35
|
+
*
|
|
36
|
+
* Licensed under the MIT License.
|
|
37
|
+
* For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
declare function tagRoutes(fastify: FastifyInstance, context: PluginContext): Promise<void>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
|
|
44
|
+
* Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
45
|
+
*
|
|
46
|
+
* Licensed under the MIT License.
|
|
47
|
+
* For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
declare function eventRoutes(fastify: FastifyInstance, context: PluginContext): Promise<void>;
|
|
51
|
+
|
|
52
|
+
export { RestPlugin, eventRoutes, readerRoutes, tagRoutes };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { FastifyInstance } from 'fastify';
|
|
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 RestPlugin implements IPlugin {
|
|
13
|
+
server: FastifyInstance | null;
|
|
14
|
+
private context;
|
|
15
|
+
getMetadata(): PluginMetadata;
|
|
16
|
+
initialize(context: PluginContext): Promise<void>;
|
|
17
|
+
start(): Promise<void>;
|
|
18
|
+
stop(): Promise<void>;
|
|
19
|
+
dispose(): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
|
|
24
|
+
* Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
25
|
+
*
|
|
26
|
+
* Licensed under the MIT License.
|
|
27
|
+
* For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
declare function readerRoutes(fastify: FastifyInstance, context: PluginContext): Promise<void>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
|
|
34
|
+
* Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
35
|
+
*
|
|
36
|
+
* Licensed under the MIT License.
|
|
37
|
+
* For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
declare function tagRoutes(fastify: FastifyInstance, context: PluginContext): Promise<void>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
|
|
44
|
+
* Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
|
|
45
|
+
*
|
|
46
|
+
* Licensed under the MIT License.
|
|
47
|
+
* For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
declare function eventRoutes(fastify: FastifyInstance, context: PluginContext): Promise<void>;
|
|
51
|
+
|
|
52
|
+
export { RestPlugin, eventRoutes, readerRoutes, tagRoutes };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
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
|
+
RestPlugin: () => RestPlugin,
|
|
34
|
+
eventRoutes: () => eventRoutes,
|
|
35
|
+
readerRoutes: () => readerRoutes,
|
|
36
|
+
tagRoutes: () => tagRoutes
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/rest-plugin.ts
|
|
41
|
+
var import_fastify = __toESM(require("fastify"));
|
|
42
|
+
|
|
43
|
+
// src/routes/readers.ts
|
|
44
|
+
var import_readers = require("@openrfid/readers");
|
|
45
|
+
async function readerRoutes(fastify, context) {
|
|
46
|
+
fastify.get("/readers", async () => {
|
|
47
|
+
return context.simulator.getAllReaders().map((r) => r.toJSON());
|
|
48
|
+
});
|
|
49
|
+
fastify.get("/readers/:id", async (request, reply) => {
|
|
50
|
+
const reader = context.simulator.getReader(request.params.id);
|
|
51
|
+
if (!reader) {
|
|
52
|
+
return reply.status(404).send({ error: "Reader not found" });
|
|
53
|
+
}
|
|
54
|
+
return reader.toJSON();
|
|
55
|
+
});
|
|
56
|
+
fastify.post(
|
|
57
|
+
"/readers",
|
|
58
|
+
async (request, reply) => {
|
|
59
|
+
const { name, vendor, model, ip, port } = request.body || {};
|
|
60
|
+
if (!name) {
|
|
61
|
+
return reply.status(400).send({ error: "Reader name is required" });
|
|
62
|
+
}
|
|
63
|
+
const reader = new import_readers.VirtualReader({ name, vendor, model, ip, port });
|
|
64
|
+
context.simulator.addReader(reader);
|
|
65
|
+
return reply.status(201).send(reader.toJSON());
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
fastify.delete("/readers/:id", async (request, reply) => {
|
|
69
|
+
context.simulator.removeReader(request.params.id);
|
|
70
|
+
return { success: true };
|
|
71
|
+
});
|
|
72
|
+
fastify.post("/readers/:id/start", async (request, reply) => {
|
|
73
|
+
try {
|
|
74
|
+
context.simulator.startReader(request.params.id);
|
|
75
|
+
return { success: true, message: "Reader started" };
|
|
76
|
+
} catch (err) {
|
|
77
|
+
return reply.status(400).send({ error: err.message });
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
fastify.post("/readers/:id/stop", async (request, reply) => {
|
|
81
|
+
context.simulator.stopReader(request.params.id);
|
|
82
|
+
return { success: true, message: "Reader stopped" };
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/routes/tags.ts
|
|
87
|
+
var import_tags = require("@openrfid/tags");
|
|
88
|
+
async function tagRoutes(fastify, context) {
|
|
89
|
+
fastify.get("/tags", async () => {
|
|
90
|
+
return context.simulator.getAllTags().map((t) => t.toJSON());
|
|
91
|
+
});
|
|
92
|
+
fastify.post("/tags", async (request, reply) => {
|
|
93
|
+
const { epc, tid, userMemory } = request.body || {};
|
|
94
|
+
if (!epc) {
|
|
95
|
+
return reply.status(400).send({ error: "EPC is required" });
|
|
96
|
+
}
|
|
97
|
+
const tag = new import_tags.Tag({ epc, tid, userMemory });
|
|
98
|
+
context.simulator.addTag(tag);
|
|
99
|
+
return reply.status(201).send(tag.toJSON());
|
|
100
|
+
});
|
|
101
|
+
fastify.post(
|
|
102
|
+
"/tags/generate",
|
|
103
|
+
async (request, reply) => {
|
|
104
|
+
const { count = 10, type = "sequential", header } = request.body || {};
|
|
105
|
+
const batch = import_tags.TagGenerator.generateBatch({ count, type, header });
|
|
106
|
+
context.simulator.addTagBatch(batch);
|
|
107
|
+
return reply.status(201).send(batch.map((t) => t.toJSON()));
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
fastify.delete("/tags/:epc", async (request, reply) => {
|
|
111
|
+
context.simulator.removeTag(request.params.epc);
|
|
112
|
+
return { success: true };
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/routes/events.ts
|
|
117
|
+
async function eventRoutes(fastify, context) {
|
|
118
|
+
fastify.get("/events", async (request) => {
|
|
119
|
+
if (!context.storage) return [];
|
|
120
|
+
const { limit, offset, date, from, to, readerId, epc, protocol } = request.query;
|
|
121
|
+
return context.storage.getEvents({
|
|
122
|
+
limit: limit ? parseInt(limit, 10) : 1e3,
|
|
123
|
+
offset: offset ? parseInt(offset, 10) : 0,
|
|
124
|
+
date,
|
|
125
|
+
from,
|
|
126
|
+
to,
|
|
127
|
+
readerId,
|
|
128
|
+
epc,
|
|
129
|
+
protocol
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
fastify.get("/events/stats", async (_request, reply) => {
|
|
133
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
134
|
+
return context.storage.getEventStats();
|
|
135
|
+
});
|
|
136
|
+
fastify.get("/events/dates", async (_request, reply) => {
|
|
137
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
138
|
+
return context.storage.getEventDates();
|
|
139
|
+
});
|
|
140
|
+
fastify.get("/events/dates/:date", async (request, reply) => {
|
|
141
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
142
|
+
const { date } = request.params;
|
|
143
|
+
const limit = parseInt(request.query.limit || "1000", 10);
|
|
144
|
+
const offset = parseInt(request.query.offset || "0", 10);
|
|
145
|
+
return context.storage.getEventsByDate(date, limit, offset);
|
|
146
|
+
});
|
|
147
|
+
fastify.get("/events/export", async (request, reply) => {
|
|
148
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
149
|
+
const events = await context.storage.getEvents({
|
|
150
|
+
...request.query,
|
|
151
|
+
limit: 1e5
|
|
152
|
+
// large export cap
|
|
153
|
+
});
|
|
154
|
+
const csvHeader = "id,date,timestamp,readerId,antennaId,epc,rssi,protocol\n";
|
|
155
|
+
const csvBody = events.map((e) => `${e.id},${e.date},${e.timestamp},${e.readerId},${e.antennaId},${e.epc},${e.rssi},${e.protocol}`).join("\n");
|
|
156
|
+
const filename = `openrfid-events-${request.query.date || "export"}.csv`;
|
|
157
|
+
reply.header("Content-Type", "text/csv").header("Content-Disposition", `attachment; filename="${filename}"`).send(csvHeader + csvBody);
|
|
158
|
+
});
|
|
159
|
+
fastify.delete("/events/dates/:date", async (request, reply) => {
|
|
160
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
161
|
+
const result = await context.storage.deleteEventsByDate(request.params.date);
|
|
162
|
+
return { success: true, ...result };
|
|
163
|
+
});
|
|
164
|
+
fastify.delete("/events", async (request, reply) => {
|
|
165
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
166
|
+
if (request.query.confirm !== "true") {
|
|
167
|
+
return reply.status(400).send({ error: "Pass ?confirm=true to clear all events" });
|
|
168
|
+
}
|
|
169
|
+
const result = await context.storage.clearAllEvents();
|
|
170
|
+
return { success: true, ...result };
|
|
171
|
+
});
|
|
172
|
+
fastify.delete("/storage", async (request, reply) => {
|
|
173
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
174
|
+
if (request.query.confirm !== "WIPE") {
|
|
175
|
+
return reply.status(400).send({ error: "Pass ?confirm=WIPE to clear all data" });
|
|
176
|
+
}
|
|
177
|
+
await context.storage.clearAllData();
|
|
178
|
+
return { success: true, message: "All data cleared" };
|
|
179
|
+
});
|
|
180
|
+
fastify.get("/health", async () => {
|
|
181
|
+
return { status: "OK", timestamp: (/* @__PURE__ */ new Date()).toISOString() };
|
|
182
|
+
});
|
|
183
|
+
fastify.get("/status", async () => {
|
|
184
|
+
const readers = context.simulator.getAllReaders();
|
|
185
|
+
const tags = context.simulator.getAllTags();
|
|
186
|
+
const stats = context.storage ? await context.storage.getEventStats() : null;
|
|
187
|
+
return {
|
|
188
|
+
uptime: process.uptime ? Math.floor(process.uptime()) : null,
|
|
189
|
+
readers: {
|
|
190
|
+
total: readers.length,
|
|
191
|
+
online: readers.filter((r) => r.status === "ONLINE").length,
|
|
192
|
+
offline: readers.filter((r) => r.status === "OFFLINE").length
|
|
193
|
+
},
|
|
194
|
+
tags: { total: tags.length },
|
|
195
|
+
events: stats ? { totalLogged: stats.totalEvents, totalDays: stats.totalDays } : { totalLogged: 0, totalDays: 0 }
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/rest-plugin.ts
|
|
201
|
+
var RestPlugin = class {
|
|
202
|
+
server = null;
|
|
203
|
+
context = null;
|
|
204
|
+
getMetadata() {
|
|
205
|
+
return {
|
|
206
|
+
name: "rest-api",
|
|
207
|
+
version: "0.1.0",
|
|
208
|
+
description: "Auto-generated REST API server plugin for OpenRFID Simulator"
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
async initialize(context) {
|
|
212
|
+
this.context = context;
|
|
213
|
+
this.server = (0, import_fastify.default)({ logger: false });
|
|
214
|
+
await readerRoutes(this.server, this.context);
|
|
215
|
+
await tagRoutes(this.server, this.context);
|
|
216
|
+
await eventRoutes(this.server, this.context);
|
|
217
|
+
}
|
|
218
|
+
async start() {
|
|
219
|
+
if (!this.server || !this.context) return;
|
|
220
|
+
const port = this.context.config.get("server.port") || 3e3;
|
|
221
|
+
const host = this.context.config.get("server.host") || "0.0.0.0";
|
|
222
|
+
try {
|
|
223
|
+
await this.server.listen({ port, host });
|
|
224
|
+
this.context.logger.info(`REST API plugin listening on http://${host}:${port}`);
|
|
225
|
+
} catch (err) {
|
|
226
|
+
this.context.logger.error("Failed to start REST API plugin", err);
|
|
227
|
+
throw err;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async stop() {
|
|
231
|
+
if (this.server) {
|
|
232
|
+
await this.server.close();
|
|
233
|
+
this.server = null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
async dispose() {
|
|
237
|
+
await this.stop();
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
241
|
+
0 && (module.exports = {
|
|
242
|
+
RestPlugin,
|
|
243
|
+
eventRoutes,
|
|
244
|
+
readerRoutes,
|
|
245
|
+
tagRoutes
|
|
246
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// src/rest-plugin.ts
|
|
2
|
+
import Fastify from "fastify";
|
|
3
|
+
|
|
4
|
+
// src/routes/readers.ts
|
|
5
|
+
import { VirtualReader } from "@openrfid/readers";
|
|
6
|
+
async function readerRoutes(fastify, context) {
|
|
7
|
+
fastify.get("/readers", async () => {
|
|
8
|
+
return context.simulator.getAllReaders().map((r) => r.toJSON());
|
|
9
|
+
});
|
|
10
|
+
fastify.get("/readers/:id", async (request, reply) => {
|
|
11
|
+
const reader = context.simulator.getReader(request.params.id);
|
|
12
|
+
if (!reader) {
|
|
13
|
+
return reply.status(404).send({ error: "Reader not found" });
|
|
14
|
+
}
|
|
15
|
+
return reader.toJSON();
|
|
16
|
+
});
|
|
17
|
+
fastify.post(
|
|
18
|
+
"/readers",
|
|
19
|
+
async (request, reply) => {
|
|
20
|
+
const { name, vendor, model, ip, port } = request.body || {};
|
|
21
|
+
if (!name) {
|
|
22
|
+
return reply.status(400).send({ error: "Reader name is required" });
|
|
23
|
+
}
|
|
24
|
+
const reader = new VirtualReader({ name, vendor, model, ip, port });
|
|
25
|
+
context.simulator.addReader(reader);
|
|
26
|
+
return reply.status(201).send(reader.toJSON());
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
fastify.delete("/readers/:id", async (request, reply) => {
|
|
30
|
+
context.simulator.removeReader(request.params.id);
|
|
31
|
+
return { success: true };
|
|
32
|
+
});
|
|
33
|
+
fastify.post("/readers/:id/start", async (request, reply) => {
|
|
34
|
+
try {
|
|
35
|
+
context.simulator.startReader(request.params.id);
|
|
36
|
+
return { success: true, message: "Reader started" };
|
|
37
|
+
} catch (err) {
|
|
38
|
+
return reply.status(400).send({ error: err.message });
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
fastify.post("/readers/:id/stop", async (request, reply) => {
|
|
42
|
+
context.simulator.stopReader(request.params.id);
|
|
43
|
+
return { success: true, message: "Reader stopped" };
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/routes/tags.ts
|
|
48
|
+
import { Tag, TagGenerator } from "@openrfid/tags";
|
|
49
|
+
async function tagRoutes(fastify, context) {
|
|
50
|
+
fastify.get("/tags", async () => {
|
|
51
|
+
return context.simulator.getAllTags().map((t) => t.toJSON());
|
|
52
|
+
});
|
|
53
|
+
fastify.post("/tags", async (request, reply) => {
|
|
54
|
+
const { epc, tid, userMemory } = request.body || {};
|
|
55
|
+
if (!epc) {
|
|
56
|
+
return reply.status(400).send({ error: "EPC is required" });
|
|
57
|
+
}
|
|
58
|
+
const tag = new Tag({ epc, tid, userMemory });
|
|
59
|
+
context.simulator.addTag(tag);
|
|
60
|
+
return reply.status(201).send(tag.toJSON());
|
|
61
|
+
});
|
|
62
|
+
fastify.post(
|
|
63
|
+
"/tags/generate",
|
|
64
|
+
async (request, reply) => {
|
|
65
|
+
const { count = 10, type = "sequential", header } = request.body || {};
|
|
66
|
+
const batch = TagGenerator.generateBatch({ count, type, header });
|
|
67
|
+
context.simulator.addTagBatch(batch);
|
|
68
|
+
return reply.status(201).send(batch.map((t) => t.toJSON()));
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
fastify.delete("/tags/:epc", async (request, reply) => {
|
|
72
|
+
context.simulator.removeTag(request.params.epc);
|
|
73
|
+
return { success: true };
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/routes/events.ts
|
|
78
|
+
async function eventRoutes(fastify, context) {
|
|
79
|
+
fastify.get("/events", async (request) => {
|
|
80
|
+
if (!context.storage) return [];
|
|
81
|
+
const { limit, offset, date, from, to, readerId, epc, protocol } = request.query;
|
|
82
|
+
return context.storage.getEvents({
|
|
83
|
+
limit: limit ? parseInt(limit, 10) : 1e3,
|
|
84
|
+
offset: offset ? parseInt(offset, 10) : 0,
|
|
85
|
+
date,
|
|
86
|
+
from,
|
|
87
|
+
to,
|
|
88
|
+
readerId,
|
|
89
|
+
epc,
|
|
90
|
+
protocol
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
fastify.get("/events/stats", async (_request, reply) => {
|
|
94
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
95
|
+
return context.storage.getEventStats();
|
|
96
|
+
});
|
|
97
|
+
fastify.get("/events/dates", async (_request, reply) => {
|
|
98
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
99
|
+
return context.storage.getEventDates();
|
|
100
|
+
});
|
|
101
|
+
fastify.get("/events/dates/:date", async (request, reply) => {
|
|
102
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
103
|
+
const { date } = request.params;
|
|
104
|
+
const limit = parseInt(request.query.limit || "1000", 10);
|
|
105
|
+
const offset = parseInt(request.query.offset || "0", 10);
|
|
106
|
+
return context.storage.getEventsByDate(date, limit, offset);
|
|
107
|
+
});
|
|
108
|
+
fastify.get("/events/export", async (request, reply) => {
|
|
109
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
110
|
+
const events = await context.storage.getEvents({
|
|
111
|
+
...request.query,
|
|
112
|
+
limit: 1e5
|
|
113
|
+
// large export cap
|
|
114
|
+
});
|
|
115
|
+
const csvHeader = "id,date,timestamp,readerId,antennaId,epc,rssi,protocol\n";
|
|
116
|
+
const csvBody = events.map((e) => `${e.id},${e.date},${e.timestamp},${e.readerId},${e.antennaId},${e.epc},${e.rssi},${e.protocol}`).join("\n");
|
|
117
|
+
const filename = `openrfid-events-${request.query.date || "export"}.csv`;
|
|
118
|
+
reply.header("Content-Type", "text/csv").header("Content-Disposition", `attachment; filename="${filename}"`).send(csvHeader + csvBody);
|
|
119
|
+
});
|
|
120
|
+
fastify.delete("/events/dates/:date", async (request, reply) => {
|
|
121
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
122
|
+
const result = await context.storage.deleteEventsByDate(request.params.date);
|
|
123
|
+
return { success: true, ...result };
|
|
124
|
+
});
|
|
125
|
+
fastify.delete("/events", async (request, reply) => {
|
|
126
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
127
|
+
if (request.query.confirm !== "true") {
|
|
128
|
+
return reply.status(400).send({ error: "Pass ?confirm=true to clear all events" });
|
|
129
|
+
}
|
|
130
|
+
const result = await context.storage.clearAllEvents();
|
|
131
|
+
return { success: true, ...result };
|
|
132
|
+
});
|
|
133
|
+
fastify.delete("/storage", async (request, reply) => {
|
|
134
|
+
if (!context.storage) return reply.status(503).send({ error: "Storage not available" });
|
|
135
|
+
if (request.query.confirm !== "WIPE") {
|
|
136
|
+
return reply.status(400).send({ error: "Pass ?confirm=WIPE to clear all data" });
|
|
137
|
+
}
|
|
138
|
+
await context.storage.clearAllData();
|
|
139
|
+
return { success: true, message: "All data cleared" };
|
|
140
|
+
});
|
|
141
|
+
fastify.get("/health", async () => {
|
|
142
|
+
return { status: "OK", timestamp: (/* @__PURE__ */ new Date()).toISOString() };
|
|
143
|
+
});
|
|
144
|
+
fastify.get("/status", async () => {
|
|
145
|
+
const readers = context.simulator.getAllReaders();
|
|
146
|
+
const tags = context.simulator.getAllTags();
|
|
147
|
+
const stats = context.storage ? await context.storage.getEventStats() : null;
|
|
148
|
+
return {
|
|
149
|
+
uptime: process.uptime ? Math.floor(process.uptime()) : null,
|
|
150
|
+
readers: {
|
|
151
|
+
total: readers.length,
|
|
152
|
+
online: readers.filter((r) => r.status === "ONLINE").length,
|
|
153
|
+
offline: readers.filter((r) => r.status === "OFFLINE").length
|
|
154
|
+
},
|
|
155
|
+
tags: { total: tags.length },
|
|
156
|
+
events: stats ? { totalLogged: stats.totalEvents, totalDays: stats.totalDays } : { totalLogged: 0, totalDays: 0 }
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/rest-plugin.ts
|
|
162
|
+
var RestPlugin = class {
|
|
163
|
+
server = null;
|
|
164
|
+
context = null;
|
|
165
|
+
getMetadata() {
|
|
166
|
+
return {
|
|
167
|
+
name: "rest-api",
|
|
168
|
+
version: "0.1.0",
|
|
169
|
+
description: "Auto-generated REST API server plugin for OpenRFID Simulator"
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
async initialize(context) {
|
|
173
|
+
this.context = context;
|
|
174
|
+
this.server = Fastify({ logger: false });
|
|
175
|
+
await readerRoutes(this.server, this.context);
|
|
176
|
+
await tagRoutes(this.server, this.context);
|
|
177
|
+
await eventRoutes(this.server, this.context);
|
|
178
|
+
}
|
|
179
|
+
async start() {
|
|
180
|
+
if (!this.server || !this.context) return;
|
|
181
|
+
const port = this.context.config.get("server.port") || 3e3;
|
|
182
|
+
const host = this.context.config.get("server.host") || "0.0.0.0";
|
|
183
|
+
try {
|
|
184
|
+
await this.server.listen({ port, host });
|
|
185
|
+
this.context.logger.info(`REST API plugin listening on http://${host}:${port}`);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
this.context.logger.error("Failed to start REST API plugin", err);
|
|
188
|
+
throw err;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async stop() {
|
|
192
|
+
if (this.server) {
|
|
193
|
+
await this.server.close();
|
|
194
|
+
this.server = null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async dispose() {
|
|
198
|
+
await this.stop();
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
export {
|
|
202
|
+
RestPlugin,
|
|
203
|
+
eventRoutes,
|
|
204
|
+
readerRoutes,
|
|
205
|
+
tagRoutes
|
|
206
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openrfid/rest",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Fastify HTTP REST API 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
|
+
"fastify": "^4.26.2",
|
|
20
|
+
"@openrfid/core": "0.1.0",
|
|
21
|
+
"@openrfid/plugin-api": "0.1.0",
|
|
22
|
+
"@openrfid/simulator": "0.1.0",
|
|
23
|
+
"@openrfid/readers": "0.1.0",
|
|
24
|
+
"@openrfid/tags": "0.1.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"tsup": "^8.0.2",
|
|
28
|
+
"typescript": "^5.4.5",
|
|
29
|
+
"vitest": "^1.5.0"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/rfidsoftwares/openrfid-simulator.git"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/rfidsoftwares/openrfid-simulator/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://rfidsoftwares.com",
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
45
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
46
|
+
"test": "vitest run",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
}
|
|
49
|
+
}
|