Repository navigation
Expand file tree
/
Copy pathEventSummary.ts
More file actions
130 lines (104 loc) · 4.15 KB
/
Copy pathEventSummary.ts
File metadata and controls
130 lines (104 loc) · 4.15 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
export type SubEventDirection = "INWARD" | "OUTWARD";
export type SubEventAction = "PEEK" | "TRANSIT" | "DENY" | "BREACH";
export type RfidCodeSubstitutionKey = `$${number}`;
export class SubEvent {
startFrameIndex: number;
endFrameIndex: number;
rfidCode: string | null;
direction: SubEventDirection;
action: SubEventAction;
[key: string]: any; // Allow any additional properties
constructor(initObj: Partial<SubEvent> & Record<string, any>) {
this.startFrameIndex = Number(initObj.startFrameIndex);
this.endFrameIndex = Number(initObj.endFrameIndex);
this.rfidCode = initObj.rfidCode ?? null;
this.direction = initObj.direction!;
this.action = initObj.action!;
for (const key in initObj) {
if (initObj.hasOwnProperty(key) && !this.hasOwnProperty(key)) {
this[key] = initObj[key];
}
}
}
}
export interface EventSummaryLocalisedDescription {
summaryKey: string;
locale: string;
localisedDescription: string | null;
}
export class EventSummary {
static readonly missingRfidCodeKey = "none";
private static readonly rfidCodeKeyPattern = /^\$(\d+)$/;
deviceId: string;
eventId: number;
processedFrameCount: number;
subevents: SubEvent[];
invalidatedAt?: Date | null;
[key: string]: any; // Allow any additional properties
constructor(initObj: Partial<EventSummary> & Record<string, any>) {
this.deviceId = initObj.deviceId!;
this.eventId = initObj.eventId!;
this.processedFrameCount = initObj.processedFrameCount ?? 0;
this.subevents = (initObj.subevents ?? []).map((subevent) => new SubEvent(subevent));
// Assign other properties from initObj to this instance
for (const key in initObj) {
if (initObj.hasOwnProperty(key) && !this.hasOwnProperty(key)) {
this[key] = initObj[key];
}
}
}
static formatRfidCodeSubstitutionKey(index: number): RfidCodeSubstitutionKey {
if (!Number.isInteger(index) || index < 1) {
throw new Error(`RFID substitution key index must be a positive integer, got ${index}.`);
}
return `$${index}`;
}
static parseRfidCodeSubstitutionKey(key: string): number | null {
const match = EventSummary.rfidCodeKeyPattern.exec(key.trim());
if (!match) {
return null;
}
return Number(match[1]);
}
get rfidCodes(): string[] {
const uniqueRfidCodes = new Set<string>();
for (const subevent of this.subevents) {
if (subevent.rfidCode) {
uniqueRfidCodes.add(subevent.rfidCode);
}
}
return Array.from(uniqueRfidCodes);
}
get rfidCodeSubstitutionEntries(): Array<[RfidCodeSubstitutionKey, string]> {
return this.rfidCodes.map((rfidCode, index) => [
EventSummary.formatRfidCodeSubstitutionKey(index + 1),
rfidCode
]);
}
get rfidCodeSubstitutionMap(): Record<string, RfidCodeSubstitutionKey> {
return Object.fromEntries(this.rfidCodeSubstitutionEntries.map(([key, rfidCode]) => [rfidCode, key]));
}
getRfidCodeSubstitutionKey(rfidCode: string | null | undefined): RfidCodeSubstitutionKey | null {
if (!rfidCode) {
return null;
}
return this.rfidCodeSubstitutionMap[rfidCode] ?? null;
}
getRfidCodeFromSubstitutionKey(key: string | null | undefined): string | null {
if (!key) {
return null;
}
const index = EventSummary.parseRfidCodeSubstitutionKey(key);
if (index === null) {
return null;
}
return this.rfidCodes[index - 1] ?? null;
}
getSubeventKey(subevent: Pick<SubEvent, "rfidCode" | "action" | "direction">): string {
const rfidCodeKey = this.getRfidCodeSubstitutionKey(subevent.rfidCode) ?? EventSummary.missingRfidCodeKey;
return [rfidCodeKey, subevent.action.toLowerCase(), subevent.direction.toLowerCase()].join("-");
}
get summaryKey(): string {
return this.subevents.map((subevent) => this.getSubeventKey(subevent)).join(",");
}
}