@openrfid/simulator 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 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
@@ -0,0 +1,77 @@
1
+ import { EventBus, IStorageDriver } from '@openrfid/core';
2
+ import { VirtualReader } from '@openrfid/readers';
3
+ import { Tag } from '@openrfid/tags';
4
+
5
+ /**
6
+ * OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
7
+ * Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
8
+ *
9
+ * Licensed under the MIT License.
10
+ * For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
11
+ */
12
+
13
+ interface InventoryOptions {
14
+ readIntervalMs?: number;
15
+ readProbability?: number;
16
+ baseRssi?: number;
17
+ maxTagsPerSecond?: number;
18
+ epcFilterStart?: string;
19
+ epcFilterEnd?: string;
20
+ epcFilterPrefix?: string;
21
+ }
22
+ declare class InventoryEngine {
23
+ private reader;
24
+ /** A1 fix — accepts a live getter instead of a frozen snapshot */
25
+ private getTags;
26
+ private eventBus;
27
+ private options;
28
+ private timer;
29
+ private isRunning;
30
+ private lastSecondStart;
31
+ private tagsReadThisSecond;
32
+ constructor(reader: VirtualReader,
33
+ /** A1 fix — accepts a live getter instead of a frozen snapshot */
34
+ getTags: () => Tag[], eventBus: EventBus, options?: InventoryOptions);
35
+ private getPeriodicIntervalMs;
36
+ private getEffectiveOptions;
37
+ start(): void;
38
+ stop(): void;
39
+ executeReadCycle(): number;
40
+ getStatus(): boolean;
41
+ }
42
+
43
+ /**
44
+ * OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
45
+ * Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
46
+ *
47
+ * Licensed under the MIT License.
48
+ * For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
49
+ */
50
+
51
+ declare class SimulatorManager {
52
+ eventBus: EventBus;
53
+ storage?: IStorageDriver | undefined;
54
+ private readers;
55
+ private tags;
56
+ private engines;
57
+ constructor(eventBus: EventBus, storage?: IStorageDriver | undefined);
58
+ loadFromStorage(): Promise<void>;
59
+ getStorageSummary(): {
60
+ readerCount: number;
61
+ tagCount: number;
62
+ };
63
+ addReader(reader: VirtualReader): void;
64
+ removeReader(readerId: string): void;
65
+ getReader(readerId: string): VirtualReader | undefined;
66
+ getAllReaders(): VirtualReader[];
67
+ addTag(tag: Tag): void;
68
+ addTagBatch(tags: Tag[]): void;
69
+ removeTag(epc: string): void;
70
+ getAllTags(): Tag[];
71
+ startReader(readerId: string, options?: InventoryOptions): void;
72
+ stopReader(readerId: string): void;
73
+ startAll(options?: InventoryOptions): void;
74
+ stopAll(): void;
75
+ }
76
+
77
+ export { InventoryEngine, type InventoryOptions, SimulatorManager };
@@ -0,0 +1,77 @@
1
+ import { EventBus, IStorageDriver } from '@openrfid/core';
2
+ import { VirtualReader } from '@openrfid/readers';
3
+ import { Tag } from '@openrfid/tags';
4
+
5
+ /**
6
+ * OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
7
+ * Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
8
+ *
9
+ * Licensed under the MIT License.
10
+ * For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
11
+ */
12
+
13
+ interface InventoryOptions {
14
+ readIntervalMs?: number;
15
+ readProbability?: number;
16
+ baseRssi?: number;
17
+ maxTagsPerSecond?: number;
18
+ epcFilterStart?: string;
19
+ epcFilterEnd?: string;
20
+ epcFilterPrefix?: string;
21
+ }
22
+ declare class InventoryEngine {
23
+ private reader;
24
+ /** A1 fix — accepts a live getter instead of a frozen snapshot */
25
+ private getTags;
26
+ private eventBus;
27
+ private options;
28
+ private timer;
29
+ private isRunning;
30
+ private lastSecondStart;
31
+ private tagsReadThisSecond;
32
+ constructor(reader: VirtualReader,
33
+ /** A1 fix — accepts a live getter instead of a frozen snapshot */
34
+ getTags: () => Tag[], eventBus: EventBus, options?: InventoryOptions);
35
+ private getPeriodicIntervalMs;
36
+ private getEffectiveOptions;
37
+ start(): void;
38
+ stop(): void;
39
+ executeReadCycle(): number;
40
+ getStatus(): boolean;
41
+ }
42
+
43
+ /**
44
+ * OpenRFID Simulator - The Open Source RFID Reader Simulator for Developers.
45
+ * Copyright (c) 2026 RFID Software India Private Limited - https://rfidsoftwares.com
46
+ *
47
+ * Licensed under the MIT License.
48
+ * For RFID software, enterprise tools, and hardware drivers, visit https://rfidsoftwares.com
49
+ */
50
+
51
+ declare class SimulatorManager {
52
+ eventBus: EventBus;
53
+ storage?: IStorageDriver | undefined;
54
+ private readers;
55
+ private tags;
56
+ private engines;
57
+ constructor(eventBus: EventBus, storage?: IStorageDriver | undefined);
58
+ loadFromStorage(): Promise<void>;
59
+ getStorageSummary(): {
60
+ readerCount: number;
61
+ tagCount: number;
62
+ };
63
+ addReader(reader: VirtualReader): void;
64
+ removeReader(readerId: string): void;
65
+ getReader(readerId: string): VirtualReader | undefined;
66
+ getAllReaders(): VirtualReader[];
67
+ addTag(tag: Tag): void;
68
+ addTagBatch(tags: Tag[]): void;
69
+ removeTag(epc: string): void;
70
+ getAllTags(): Tag[];
71
+ startReader(readerId: string, options?: InventoryOptions): void;
72
+ stopReader(readerId: string): void;
73
+ startAll(options?: InventoryOptions): void;
74
+ stopAll(): void;
75
+ }
76
+
77
+ export { InventoryEngine, type InventoryOptions, SimulatorManager };
package/dist/index.js ADDED
@@ -0,0 +1,305 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ InventoryEngine: () => InventoryEngine,
24
+ SimulatorManager: () => SimulatorManager
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/inventory-engine.ts
29
+ var import_utils = require("@openrfid/utils");
30
+ var InventoryEngine = class {
31
+ constructor(reader, getTags, eventBus, options = {}) {
32
+ this.reader = reader;
33
+ this.getTags = getTags;
34
+ this.eventBus = eventBus;
35
+ this.options = options;
36
+ }
37
+ reader;
38
+ getTags;
39
+ eventBus;
40
+ options;
41
+ timer = null;
42
+ isRunning = false;
43
+ lastSecondStart = 0;
44
+ tagsReadThisSecond = 0;
45
+ getPeriodicIntervalMs() {
46
+ const value = this.reader.readIntervalValue || 1;
47
+ const unit = this.reader.readIntervalUnit || "seconds";
48
+ if (unit === "hours") return value * 60 * 60 * 1e3;
49
+ if (unit === "minutes") return value * 60 * 1e3;
50
+ return value * 1e3;
51
+ }
52
+ getEffectiveOptions() {
53
+ return {
54
+ readIntervalMs: this.reader.readMode === "periodic" ? this.getPeriodicIntervalMs() : this.options.readIntervalMs || 500,
55
+ readProbability: this.options.readProbability ?? 0.95,
56
+ baseRssi: this.options.baseRssi ?? -55,
57
+ maxTagsPerSecond: this.options.maxTagsPerSecond ?? this.reader.readRate,
58
+ epcFilterStart: this.options.epcFilterStart ?? this.reader.epcFilterStart,
59
+ epcFilterEnd: this.options.epcFilterEnd ?? this.reader.epcFilterEnd,
60
+ epcFilterPrefix: this.options.epcFilterPrefix ?? this.reader.epcFilterPrefix
61
+ };
62
+ }
63
+ start() {
64
+ if (this.isRunning) return;
65
+ this.isRunning = true;
66
+ this.reader.setStatus("ONLINE");
67
+ this.eventBus.emit("ReaderConnected", {
68
+ readerId: this.reader.id,
69
+ name: this.reader.name,
70
+ ip: this.reader.ip,
71
+ port: this.reader.port
72
+ }).catch((e) => console.error("[EventBus] ReaderConnected error:", e));
73
+ const effectiveOpts = this.getEffectiveOptions();
74
+ const interval = effectiveOpts.readIntervalMs;
75
+ this.timer = setInterval(() => {
76
+ this.executeReadCycle();
77
+ }, interval);
78
+ }
79
+ stop() {
80
+ if (!this.isRunning) return;
81
+ if (this.timer) {
82
+ clearInterval(this.timer);
83
+ this.timer = null;
84
+ }
85
+ this.isRunning = false;
86
+ this.reader.setStatus("OFFLINE");
87
+ this.eventBus.emit("ReaderDisconnected", {
88
+ readerId: this.reader.id,
89
+ reason: "Simulator stopped"
90
+ }).catch((e) => console.error("[EventBus] ReaderDisconnected error:", e));
91
+ }
92
+ executeReadCycle() {
93
+ if (this.reader.status !== "ONLINE") return 0;
94
+ let readCount = 0;
95
+ const activeAntennas = this.reader.getActiveAntennas();
96
+ if (activeAntennas.length === 0) return 0;
97
+ const effectiveOpts = this.getEffectiveOptions();
98
+ const baseProb = effectiveOpts.readProbability;
99
+ const baseRssi = effectiveOpts.baseRssi;
100
+ const now = Date.now();
101
+ if (now - this.lastSecondStart >= 1e3) {
102
+ this.lastSecondStart = now;
103
+ this.tagsReadThisSecond = 0;
104
+ }
105
+ let tagsToRead = this.getTags();
106
+ if (effectiveOpts.epcFilterPrefix) {
107
+ const prefix = effectiveOpts.epcFilterPrefix.toUpperCase();
108
+ tagsToRead = tagsToRead.filter((tag) => tag.epc.startsWith(prefix));
109
+ }
110
+ if (effectiveOpts.epcFilterStart || effectiveOpts.epcFilterEnd) {
111
+ tagsToRead = tagsToRead.filter((tag) => {
112
+ const epc = tag.epc;
113
+ if (effectiveOpts.epcFilterStart && epc < effectiveOpts.epcFilterStart.toUpperCase()) return false;
114
+ if (effectiveOpts.epcFilterEnd && epc > effectiveOpts.epcFilterEnd.toUpperCase()) return false;
115
+ return true;
116
+ });
117
+ }
118
+ for (const tag of tagsToRead) {
119
+ if (effectiveOpts.maxTagsPerSecond > 0 && this.tagsReadThisSecond >= effectiveOpts.maxTagsPerSecond) {
120
+ break;
121
+ }
122
+ for (const antenna of activeAntennas) {
123
+ if (effectiveOpts.maxTagsPerSecond > 0 && this.tagsReadThisSecond >= effectiveOpts.maxTagsPerSecond) {
124
+ break;
125
+ }
126
+ const antennaFactor = (antenna.power + antenna.gain) / 36;
127
+ const totalProb = Math.min(1, baseProb * antennaFactor);
128
+ if (import_utils.RandomGenerator.boolean(totalProb)) {
129
+ const rssiNoise = import_utils.RandomGenerator.gaussian(0, 2.5);
130
+ const calculatedRssi = parseFloat((baseRssi + antenna.rssiOffset + rssiNoise).toFixed(1));
131
+ tag.recordRead(calculatedRssi);
132
+ readCount++;
133
+ this.tagsReadThisSecond++;
134
+ this.eventBus.emit("TagDetected", {
135
+ readerId: this.reader.id,
136
+ antennaId: antenna.index,
137
+ epc: tag.epc,
138
+ tid: tag.tid,
139
+ rssi: calculatedRssi,
140
+ timestamp: Date.now(),
141
+ protocol: tag.protocol
142
+ }).catch((e) => console.error("[EventBus] TagDetected error:", e));
143
+ }
144
+ }
145
+ }
146
+ return readCount;
147
+ }
148
+ getStatus() {
149
+ return this.isRunning;
150
+ }
151
+ };
152
+
153
+ // src/simulator-manager.ts
154
+ var import_readers = require("@openrfid/readers");
155
+ var import_tags = require("@openrfid/tags");
156
+ var SimulatorManager = class {
157
+ constructor(eventBus, storage) {
158
+ this.eventBus = eventBus;
159
+ this.storage = storage;
160
+ this.eventBus.on("TagDetected", (payload) => {
161
+ const now = /* @__PURE__ */ new Date();
162
+ const date = now.toISOString().split("T")[0];
163
+ this.storage?.logEvent({
164
+ id: `ev_${now.getTime()}_${Math.random().toString(36).slice(2, 7)}`,
165
+ date,
166
+ timestamp: now.toISOString(),
167
+ readerId: payload.readerId,
168
+ antennaId: payload.antennaId,
169
+ epc: payload.epc,
170
+ rssi: payload.rssi,
171
+ protocol: payload.protocol ?? "GEN2"
172
+ });
173
+ });
174
+ }
175
+ eventBus;
176
+ storage;
177
+ readers = /* @__PURE__ */ new Map();
178
+ tags = /* @__PURE__ */ new Map();
179
+ engines = /* @__PURE__ */ new Map();
180
+ // A3.1 — Load readers and tags from storage on startup
181
+ async loadFromStorage() {
182
+ if (!this.storage) return;
183
+ const readers = await this.storage.getReaders();
184
+ for (const r of readers) {
185
+ const reader = new import_readers.VirtualReader(r);
186
+ const antennas = await this.storage.getAntennasByReaderId(reader.id);
187
+ if (antennas && antennas.length > 0) {
188
+ reader.antennas.clear();
189
+ for (const ant of antennas) {
190
+ reader.addAntenna(ant);
191
+ }
192
+ }
193
+ this.readers.set(reader.id, reader);
194
+ }
195
+ const tags = await this.storage.getTags();
196
+ for (const t of tags) {
197
+ const tag = new import_tags.Tag(t);
198
+ this.tags.set(tag.epc, tag);
199
+ }
200
+ }
201
+ // Returns summary for the startup banner
202
+ getStorageSummary() {
203
+ return {
204
+ readerCount: this.readers.size,
205
+ tagCount: this.tags.size
206
+ };
207
+ }
208
+ addReader(reader) {
209
+ this.readers.set(reader.id, reader);
210
+ if (this.storage) {
211
+ this.storage.saveReader(reader.toJSON());
212
+ for (const antenna of reader.antennas.values()) {
213
+ this.storage.saveAntenna({
214
+ id: antenna.id,
215
+ readerId: antenna.readerId,
216
+ index: antenna.index,
217
+ gain: antenna.gain,
218
+ power: antenna.power,
219
+ frequency: antenna.frequency,
220
+ rssiOffset: antenna.rssiOffset,
221
+ readZone: antenna.readZone,
222
+ enabled: antenna.enabled
223
+ });
224
+ }
225
+ }
226
+ }
227
+ removeReader(readerId) {
228
+ this.stopReader(readerId);
229
+ this.readers.delete(readerId);
230
+ if (this.storage) {
231
+ this.storage.deleteReader(readerId);
232
+ }
233
+ }
234
+ getReader(readerId) {
235
+ return this.readers.get(readerId);
236
+ }
237
+ getAllReaders() {
238
+ return Array.from(this.readers.values());
239
+ }
240
+ addTag(tag) {
241
+ this.tags.set(tag.epc, tag);
242
+ if (this.storage) {
243
+ this.storage.saveTag(tag.toJSON());
244
+ }
245
+ }
246
+ addTagBatch(tags) {
247
+ for (const tag of tags) {
248
+ this.addTag(tag);
249
+ }
250
+ }
251
+ removeTag(epc) {
252
+ const tag = this.tags.get(epc);
253
+ if (tag) {
254
+ this.tags.delete(epc);
255
+ if (this.storage) {
256
+ this.storage.deleteTag(tag.id);
257
+ }
258
+ }
259
+ }
260
+ getAllTags() {
261
+ return Array.from(this.tags.values());
262
+ }
263
+ startReader(readerId, options) {
264
+ const reader = this.readers.get(readerId);
265
+ if (!reader) {
266
+ throw new Error(`Reader '${readerId}' not found.`);
267
+ }
268
+ const engine = new InventoryEngine(
269
+ reader,
270
+ () => Array.from(this.tags.values()),
271
+ // A1 — live tag getter
272
+ this.eventBus,
273
+ options
274
+ );
275
+ this.engines.set(readerId, engine);
276
+ engine.start();
277
+ }
278
+ stopReader(readerId) {
279
+ const engine = this.engines.get(readerId);
280
+ if (engine) {
281
+ engine.stop();
282
+ this.engines.delete(readerId);
283
+ }
284
+ }
285
+ // B2 fix — wrap each start in try/catch so one failure doesn't stop others
286
+ startAll(options) {
287
+ for (const reader of this.readers.values()) {
288
+ try {
289
+ this.startReader(reader.id, options);
290
+ } catch (e) {
291
+ console.error(`[Simulator] Failed to start reader '${reader.id}': ${e.message}`);
292
+ }
293
+ }
294
+ }
295
+ stopAll() {
296
+ for (const readerId of Array.from(this.engines.keys())) {
297
+ this.stopReader(readerId);
298
+ }
299
+ }
300
+ };
301
+ // Annotate the CommonJS export names for ESM import in node:
302
+ 0 && (module.exports = {
303
+ InventoryEngine,
304
+ SimulatorManager
305
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,277 @@
1
+ // src/inventory-engine.ts
2
+ import { RandomGenerator } from "@openrfid/utils";
3
+ var InventoryEngine = class {
4
+ constructor(reader, getTags, eventBus, options = {}) {
5
+ this.reader = reader;
6
+ this.getTags = getTags;
7
+ this.eventBus = eventBus;
8
+ this.options = options;
9
+ }
10
+ reader;
11
+ getTags;
12
+ eventBus;
13
+ options;
14
+ timer = null;
15
+ isRunning = false;
16
+ lastSecondStart = 0;
17
+ tagsReadThisSecond = 0;
18
+ getPeriodicIntervalMs() {
19
+ const value = this.reader.readIntervalValue || 1;
20
+ const unit = this.reader.readIntervalUnit || "seconds";
21
+ if (unit === "hours") return value * 60 * 60 * 1e3;
22
+ if (unit === "minutes") return value * 60 * 1e3;
23
+ return value * 1e3;
24
+ }
25
+ getEffectiveOptions() {
26
+ return {
27
+ readIntervalMs: this.reader.readMode === "periodic" ? this.getPeriodicIntervalMs() : this.options.readIntervalMs || 500,
28
+ readProbability: this.options.readProbability ?? 0.95,
29
+ baseRssi: this.options.baseRssi ?? -55,
30
+ maxTagsPerSecond: this.options.maxTagsPerSecond ?? this.reader.readRate,
31
+ epcFilterStart: this.options.epcFilterStart ?? this.reader.epcFilterStart,
32
+ epcFilterEnd: this.options.epcFilterEnd ?? this.reader.epcFilterEnd,
33
+ epcFilterPrefix: this.options.epcFilterPrefix ?? this.reader.epcFilterPrefix
34
+ };
35
+ }
36
+ start() {
37
+ if (this.isRunning) return;
38
+ this.isRunning = true;
39
+ this.reader.setStatus("ONLINE");
40
+ this.eventBus.emit("ReaderConnected", {
41
+ readerId: this.reader.id,
42
+ name: this.reader.name,
43
+ ip: this.reader.ip,
44
+ port: this.reader.port
45
+ }).catch((e) => console.error("[EventBus] ReaderConnected error:", e));
46
+ const effectiveOpts = this.getEffectiveOptions();
47
+ const interval = effectiveOpts.readIntervalMs;
48
+ this.timer = setInterval(() => {
49
+ this.executeReadCycle();
50
+ }, interval);
51
+ }
52
+ stop() {
53
+ if (!this.isRunning) return;
54
+ if (this.timer) {
55
+ clearInterval(this.timer);
56
+ this.timer = null;
57
+ }
58
+ this.isRunning = false;
59
+ this.reader.setStatus("OFFLINE");
60
+ this.eventBus.emit("ReaderDisconnected", {
61
+ readerId: this.reader.id,
62
+ reason: "Simulator stopped"
63
+ }).catch((e) => console.error("[EventBus] ReaderDisconnected error:", e));
64
+ }
65
+ executeReadCycle() {
66
+ if (this.reader.status !== "ONLINE") return 0;
67
+ let readCount = 0;
68
+ const activeAntennas = this.reader.getActiveAntennas();
69
+ if (activeAntennas.length === 0) return 0;
70
+ const effectiveOpts = this.getEffectiveOptions();
71
+ const baseProb = effectiveOpts.readProbability;
72
+ const baseRssi = effectiveOpts.baseRssi;
73
+ const now = Date.now();
74
+ if (now - this.lastSecondStart >= 1e3) {
75
+ this.lastSecondStart = now;
76
+ this.tagsReadThisSecond = 0;
77
+ }
78
+ let tagsToRead = this.getTags();
79
+ if (effectiveOpts.epcFilterPrefix) {
80
+ const prefix = effectiveOpts.epcFilterPrefix.toUpperCase();
81
+ tagsToRead = tagsToRead.filter((tag) => tag.epc.startsWith(prefix));
82
+ }
83
+ if (effectiveOpts.epcFilterStart || effectiveOpts.epcFilterEnd) {
84
+ tagsToRead = tagsToRead.filter((tag) => {
85
+ const epc = tag.epc;
86
+ if (effectiveOpts.epcFilterStart && epc < effectiveOpts.epcFilterStart.toUpperCase()) return false;
87
+ if (effectiveOpts.epcFilterEnd && epc > effectiveOpts.epcFilterEnd.toUpperCase()) return false;
88
+ return true;
89
+ });
90
+ }
91
+ for (const tag of tagsToRead) {
92
+ if (effectiveOpts.maxTagsPerSecond > 0 && this.tagsReadThisSecond >= effectiveOpts.maxTagsPerSecond) {
93
+ break;
94
+ }
95
+ for (const antenna of activeAntennas) {
96
+ if (effectiveOpts.maxTagsPerSecond > 0 && this.tagsReadThisSecond >= effectiveOpts.maxTagsPerSecond) {
97
+ break;
98
+ }
99
+ const antennaFactor = (antenna.power + antenna.gain) / 36;
100
+ const totalProb = Math.min(1, baseProb * antennaFactor);
101
+ if (RandomGenerator.boolean(totalProb)) {
102
+ const rssiNoise = RandomGenerator.gaussian(0, 2.5);
103
+ const calculatedRssi = parseFloat((baseRssi + antenna.rssiOffset + rssiNoise).toFixed(1));
104
+ tag.recordRead(calculatedRssi);
105
+ readCount++;
106
+ this.tagsReadThisSecond++;
107
+ this.eventBus.emit("TagDetected", {
108
+ readerId: this.reader.id,
109
+ antennaId: antenna.index,
110
+ epc: tag.epc,
111
+ tid: tag.tid,
112
+ rssi: calculatedRssi,
113
+ timestamp: Date.now(),
114
+ protocol: tag.protocol
115
+ }).catch((e) => console.error("[EventBus] TagDetected error:", e));
116
+ }
117
+ }
118
+ }
119
+ return readCount;
120
+ }
121
+ getStatus() {
122
+ return this.isRunning;
123
+ }
124
+ };
125
+
126
+ // src/simulator-manager.ts
127
+ import { VirtualReader } from "@openrfid/readers";
128
+ import { Tag } from "@openrfid/tags";
129
+ var SimulatorManager = class {
130
+ constructor(eventBus, storage) {
131
+ this.eventBus = eventBus;
132
+ this.storage = storage;
133
+ this.eventBus.on("TagDetected", (payload) => {
134
+ const now = /* @__PURE__ */ new Date();
135
+ const date = now.toISOString().split("T")[0];
136
+ this.storage?.logEvent({
137
+ id: `ev_${now.getTime()}_${Math.random().toString(36).slice(2, 7)}`,
138
+ date,
139
+ timestamp: now.toISOString(),
140
+ readerId: payload.readerId,
141
+ antennaId: payload.antennaId,
142
+ epc: payload.epc,
143
+ rssi: payload.rssi,
144
+ protocol: payload.protocol ?? "GEN2"
145
+ });
146
+ });
147
+ }
148
+ eventBus;
149
+ storage;
150
+ readers = /* @__PURE__ */ new Map();
151
+ tags = /* @__PURE__ */ new Map();
152
+ engines = /* @__PURE__ */ new Map();
153
+ // A3.1 — Load readers and tags from storage on startup
154
+ async loadFromStorage() {
155
+ if (!this.storage) return;
156
+ const readers = await this.storage.getReaders();
157
+ for (const r of readers) {
158
+ const reader = new VirtualReader(r);
159
+ const antennas = await this.storage.getAntennasByReaderId(reader.id);
160
+ if (antennas && antennas.length > 0) {
161
+ reader.antennas.clear();
162
+ for (const ant of antennas) {
163
+ reader.addAntenna(ant);
164
+ }
165
+ }
166
+ this.readers.set(reader.id, reader);
167
+ }
168
+ const tags = await this.storage.getTags();
169
+ for (const t of tags) {
170
+ const tag = new Tag(t);
171
+ this.tags.set(tag.epc, tag);
172
+ }
173
+ }
174
+ // Returns summary for the startup banner
175
+ getStorageSummary() {
176
+ return {
177
+ readerCount: this.readers.size,
178
+ tagCount: this.tags.size
179
+ };
180
+ }
181
+ addReader(reader) {
182
+ this.readers.set(reader.id, reader);
183
+ if (this.storage) {
184
+ this.storage.saveReader(reader.toJSON());
185
+ for (const antenna of reader.antennas.values()) {
186
+ this.storage.saveAntenna({
187
+ id: antenna.id,
188
+ readerId: antenna.readerId,
189
+ index: antenna.index,
190
+ gain: antenna.gain,
191
+ power: antenna.power,
192
+ frequency: antenna.frequency,
193
+ rssiOffset: antenna.rssiOffset,
194
+ readZone: antenna.readZone,
195
+ enabled: antenna.enabled
196
+ });
197
+ }
198
+ }
199
+ }
200
+ removeReader(readerId) {
201
+ this.stopReader(readerId);
202
+ this.readers.delete(readerId);
203
+ if (this.storage) {
204
+ this.storage.deleteReader(readerId);
205
+ }
206
+ }
207
+ getReader(readerId) {
208
+ return this.readers.get(readerId);
209
+ }
210
+ getAllReaders() {
211
+ return Array.from(this.readers.values());
212
+ }
213
+ addTag(tag) {
214
+ this.tags.set(tag.epc, tag);
215
+ if (this.storage) {
216
+ this.storage.saveTag(tag.toJSON());
217
+ }
218
+ }
219
+ addTagBatch(tags) {
220
+ for (const tag of tags) {
221
+ this.addTag(tag);
222
+ }
223
+ }
224
+ removeTag(epc) {
225
+ const tag = this.tags.get(epc);
226
+ if (tag) {
227
+ this.tags.delete(epc);
228
+ if (this.storage) {
229
+ this.storage.deleteTag(tag.id);
230
+ }
231
+ }
232
+ }
233
+ getAllTags() {
234
+ return Array.from(this.tags.values());
235
+ }
236
+ startReader(readerId, options) {
237
+ const reader = this.readers.get(readerId);
238
+ if (!reader) {
239
+ throw new Error(`Reader '${readerId}' not found.`);
240
+ }
241
+ const engine = new InventoryEngine(
242
+ reader,
243
+ () => Array.from(this.tags.values()),
244
+ // A1 — live tag getter
245
+ this.eventBus,
246
+ options
247
+ );
248
+ this.engines.set(readerId, engine);
249
+ engine.start();
250
+ }
251
+ stopReader(readerId) {
252
+ const engine = this.engines.get(readerId);
253
+ if (engine) {
254
+ engine.stop();
255
+ this.engines.delete(readerId);
256
+ }
257
+ }
258
+ // B2 fix — wrap each start in try/catch so one failure doesn't stop others
259
+ startAll(options) {
260
+ for (const reader of this.readers.values()) {
261
+ try {
262
+ this.startReader(reader.id, options);
263
+ } catch (e) {
264
+ console.error(`[Simulator] Failed to start reader '${reader.id}': ${e.message}`);
265
+ }
266
+ }
267
+ }
268
+ stopAll() {
269
+ for (const readerId of Array.from(this.engines.keys())) {
270
+ this.stopReader(readerId);
271
+ }
272
+ }
273
+ };
274
+ export {
275
+ InventoryEngine,
276
+ SimulatorManager
277
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@openrfid/simulator",
3
+ "version": "0.1.0",
4
+ "description": "Core RFID inventory engine, read cycle loops, read probability model, and event pipeline.",
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
+ "@openrfid/core": "0.1.0",
20
+ "@openrfid/tags": "0.1.0",
21
+ "@openrfid/utils": "0.1.0",
22
+ "@openrfid/readers": "0.1.0"
23
+ },
24
+ "devDependencies": {
25
+ "tsup": "^8.0.2",
26
+ "typescript": "^5.4.5",
27
+ "vitest": "^1.5.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
+ }