FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
idUtils.ts
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (c) 2026-present FERS Contributors (see AUTHORS.md).
3
4export type SimObjectType =
5 | 'Platform'
6 | 'Transmitter'
7 | 'Receiver'
8 | 'Target'
9 | 'Antenna'
10 | 'Waveform'
11 | 'Timing';
12
13const TYPE_CODES: Record<SimObjectType, bigint> = {
14 Platform: 1n,
15 Transmitter: 2n,
16 Receiver: 3n,
17 Target: 4n,
18 Antenna: 5n,
19 Waveform: 6n,
20 Timing: 7n,
21};
22
23const CODE_TO_TYPE: Record<number, SimObjectType> = {
24 1: 'Platform',
25 2: 'Transmitter',
26 3: 'Receiver',
27 4: 'Target',
28 5: 'Antenna',
29 6: 'Waveform',
30 7: 'Timing',
31};
32
33const MAX_COUNTER = 0x0000ffffffffffffn;
34
35const counters: Record<SimObjectType, bigint> = {
36 Platform: 1n,
37 Transmitter: 1n,
38 Receiver: 1n,
39 Target: 1n,
40 Antenna: 1n,
41 Waveform: 1n,
42 Timing: 1n,
43};
44
45export const normalizeSimId = (value: unknown): string | null => {
46 if (typeof value === 'string') {
47 return /^\d+$/.test(value) ? value : null;
48 }
49 if (typeof value === 'number' && Number.isFinite(value)) {
50 return Math.trunc(value).toString();
51 }
52 return null;
53};
54
55export const reserveSimId = (id: string): void => {
56 const parsed = BigInt(id);
57 const typeCode = Number(parsed >> 48n);
58 const counter = parsed & MAX_COUNTER;
59 const type = CODE_TO_TYPE[typeCode];
60 if (!type) return;
61 const next = counter + 1n;
62 if (next > counters[type]) {
63 counters[type] = next;
64 }
65};
66
67export const seedSimIdCounters = (
68 ids: Array<string | null | undefined>
69): void => {
70 ids.forEach((id) => {
71 if (id) {
72 reserveSimId(id);
73 }
74 });
75};
76
77export const generateSimId = (type: SimObjectType): string => {
78 const counter = counters[type];
79 if (counter > MAX_COUNTER) {
80 throw new Error(`SimId counter overflow for ${type}.`);
81 }
82 const id = (TYPE_CODES[type] << 48n) | counter;
83 counters[type] = counter + 1n;
84 return id.toString();
85};