@7365admin1/layer-common 4.9.1-staging.310 → 4.10.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/CHANGELOG.md +20 -4
- package/components/CameraForm.vue +3 -460
- package/components/CameraMain.vue +1 -118
- package/components/EntryPass/QrTemplatePreview.vue +25 -21
- package/components/Facility/BookingSetup.vue +0 -63
- package/components/HidIntercomManagement.vue +43 -193
- package/components/HidReaderUserRoster.vue +2 -32
- package/components/HidUserEnrollment.vue +212 -771
- package/components/InvitationClientForm.vue +1 -19
- package/components/RolePermissionMain.vue +2 -7
- package/components/VehicleForm.vue +3 -11
- package/components/VisitorManagement.vue +5 -267
- package/composables/useFacility.ts +0 -11
- package/composables/useHidAmico.ts +0 -97
- package/composables/useHidNavigation.ts +1 -25
- package/composables/usePeople.ts +0 -19
- package/composables/useSiteSettings.ts +2 -114
- package/composables/useWebUsb.ts +11 -14
- package/package.json +1 -1
- package/plugins/secure-member.client.ts +2 -8
- package/types/facility.d.ts +0 -4
- package/types/people.d.ts +1 -4
- package/utils/materialSymbols.ts +0 -3
- package/utils/org-modules.ts +0 -26
- package/components/CameraPlateSyncDialog.vue +0 -626
- package/components/HidProfileQrCode.vue +0 -323
- package/pages/[org]/[site]/access-mgmt/hid-qr-code/index.vue +0 -23
- package/utils/anpr-direction.ts +0 -70
- package/utils/camera-plate-sync.ts +0 -141
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
<!--
|
|
2
|
-
MY HID QR CODE.
|
|
3
|
-
|
|
4
|
-
The signed-in member's own pass for one reader. Every other HID screen acts on
|
|
5
|
-
somebody else - a resident being enrolled, a visitor being issued a pass. This
|
|
6
|
-
one has no subject picker at all, because the server takes the caller from the
|
|
7
|
-
session and there is no user id to send.
|
|
8
|
-
|
|
9
|
-
Issuing a code does NOT grant access. A member's door permission comes from the
|
|
10
|
-
HID permissions screen, and minting an access rule here would let anyone who
|
|
11
|
-
can press Generate admit themselves. So the screen reports what the permissions
|
|
12
|
-
screen says (`assigned`) and warns when a code would be refused at the door,
|
|
13
|
-
rather than handing out something that silently fails.
|
|
14
|
-
-->
|
|
15
|
-
<template>
|
|
16
|
-
<div class="hid-profile-qr">
|
|
17
|
-
<div class="hid-profile-qr__header">
|
|
18
|
-
<h2 class="hid-profile-qr__title">HID QR Code</h2>
|
|
19
|
-
<AppSelect
|
|
20
|
-
v-if="readerOptions.length"
|
|
21
|
-
v-model="selectedReaderId"
|
|
22
|
-
class="reader-select"
|
|
23
|
-
:items="readerOptions"
|
|
24
|
-
@update:model-value="loadProfile"
|
|
25
|
-
/>
|
|
26
|
-
</div>
|
|
27
|
-
|
|
28
|
-
<v-card class="screen-modal hid-profile-qr__card">
|
|
29
|
-
<v-card-text class="screen-modal__body">
|
|
30
|
-
<div v-if="loading" class="hid-profile-qr__state">Loading...</div>
|
|
31
|
-
|
|
32
|
-
<div v-else-if="!readerOptions.length" class="hid-profile-qr__state">
|
|
33
|
-
No HID reader is set up for this site yet.
|
|
34
|
-
</div>
|
|
35
|
-
|
|
36
|
-
<template v-else>
|
|
37
|
-
<p class="hid-profile-qr__reader">
|
|
38
|
-
{{ profile?.readerName || "HID reader" }}
|
|
39
|
-
<span>{{ profile?.portalName || "Portal not configured" }}</span>
|
|
40
|
-
</p>
|
|
41
|
-
|
|
42
|
-
<!-- A code this reader cannot read is worse than no code, so the
|
|
43
|
-
capability is checked before anything is offered. -->
|
|
44
|
-
<v-alert
|
|
45
|
-
v-if="profile && !profile.qrEnabled"
|
|
46
|
-
type="warning"
|
|
47
|
-
variant="tonal"
|
|
48
|
-
density="compact"
|
|
49
|
-
class="mb-4"
|
|
50
|
-
>
|
|
51
|
-
This reader is not set up to read QR codes.
|
|
52
|
-
</v-alert>
|
|
53
|
-
|
|
54
|
-
<!-- The honest part: a code opens the door only for a member the
|
|
55
|
-
permissions screen already admits. `assigned` is null when the
|
|
56
|
-
lookup failed, which is not the same as "no". -->
|
|
57
|
-
<v-alert
|
|
58
|
-
v-else-if="profile && profile.assigned === false"
|
|
59
|
-
type="warning"
|
|
60
|
-
variant="tonal"
|
|
61
|
-
density="compact"
|
|
62
|
-
class="mb-4"
|
|
63
|
-
>
|
|
64
|
-
You are not assigned to this reader, so this code will be refused at
|
|
65
|
-
the door. Ask a property manager to add you on the HID permissions
|
|
66
|
-
screen.
|
|
67
|
-
</v-alert>
|
|
68
|
-
|
|
69
|
-
<div v-if="qrImage" class="hid-profile-qr__code">
|
|
70
|
-
<img :src="qrImage" alt="Your HID QR code" width="220" height="220" />
|
|
71
|
-
<p class="hid-profile-qr__issued">
|
|
72
|
-
Issued {{ formatIssuedAt(profile?.issuedAt) }}
|
|
73
|
-
</p>
|
|
74
|
-
</div>
|
|
75
|
-
<p v-else class="hid-profile-qr__state">
|
|
76
|
-
You have no QR code on this reader yet.
|
|
77
|
-
</p>
|
|
78
|
-
|
|
79
|
-
<div class="hid-profile-qr__actions">
|
|
80
|
-
<AppButton
|
|
81
|
-
:disabled="working || !profile?.qrEnabled"
|
|
82
|
-
@click="generate"
|
|
83
|
-
>
|
|
84
|
-
{{ profile?.issued ? "Regenerate" : "Generate QR Code" }}
|
|
85
|
-
</AppButton>
|
|
86
|
-
<AppButton
|
|
87
|
-
v-if="qrImage"
|
|
88
|
-
variant="ghost"
|
|
89
|
-
icon="mdi-download"
|
|
90
|
-
@click="downloadQr"
|
|
91
|
-
>
|
|
92
|
-
Download
|
|
93
|
-
</AppButton>
|
|
94
|
-
<AppButton
|
|
95
|
-
v-if="profile?.issued"
|
|
96
|
-
variant="ghost"
|
|
97
|
-
:disabled="working"
|
|
98
|
-
@click="remove"
|
|
99
|
-
>
|
|
100
|
-
Remove
|
|
101
|
-
</AppButton>
|
|
102
|
-
</div>
|
|
103
|
-
|
|
104
|
-
<p class="field-hint">
|
|
105
|
-
Regenerating replaces the previous code. The old one stops working
|
|
106
|
-
immediately.
|
|
107
|
-
</p>
|
|
108
|
-
</template>
|
|
109
|
-
</v-card-text>
|
|
110
|
-
</v-card>
|
|
111
|
-
|
|
112
|
-
<v-snackbar v-model="snackbar.show" :color="snackbar.color" timeout="4000">
|
|
113
|
-
{{ snackbar.message }}
|
|
114
|
-
</v-snackbar>
|
|
115
|
-
</div>
|
|
116
|
-
</template>
|
|
117
|
-
|
|
118
|
-
<script setup lang="ts">
|
|
119
|
-
import QRCode from "qrcode";
|
|
120
|
-
import type { HidProfileQr } from "../composables/useHidAmico";
|
|
121
|
-
|
|
122
|
-
const props = defineProps<{ site: string }>();
|
|
123
|
-
|
|
124
|
-
const { getReaders, getProfileQr, issueProfileQr, deleteProfileQr } = useHidAmico();
|
|
125
|
-
|
|
126
|
-
type HidReaderOption = { _id: string; name?: string; portalName?: string };
|
|
127
|
-
|
|
128
|
-
const readers = ref<HidReaderOption[]>([]);
|
|
129
|
-
const selectedReaderId = ref("");
|
|
130
|
-
const profile = ref<HidProfileQr | null>(null);
|
|
131
|
-
const qrImage = ref("");
|
|
132
|
-
const loading = ref(false);
|
|
133
|
-
const working = ref(false);
|
|
134
|
-
const snackbar = reactive({ show: false, color: "success", message: "" });
|
|
135
|
-
|
|
136
|
-
const readerOptions = computed(() => readers.value.map((reader) => ({
|
|
137
|
-
title: `${reader.name || reader._id} — ${reader.portalName || "Portal not configured"}`,
|
|
138
|
-
value: reader._id,
|
|
139
|
-
})));
|
|
140
|
-
|
|
141
|
-
function showToast(message: string, color: "success" | "error" = "success") {
|
|
142
|
-
snackbar.message = message;
|
|
143
|
-
snackbar.color = color;
|
|
144
|
-
snackbar.show = true;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function toRecord(value: unknown): Record<string, any> {
|
|
148
|
-
return value && typeof value === "object" ? (value as Record<string, any>) : {};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function errorMessage(error: unknown) {
|
|
152
|
-
const record = toRecord(error);
|
|
153
|
-
return String(toRecord(record.data).message || record.message || "Something went wrong.");
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function formatIssuedAt(value?: string) {
|
|
157
|
-
if (!value) return "";
|
|
158
|
-
const date = new Date(value);
|
|
159
|
-
return Number.isNaN(date.getTime()) ? "" : date.toLocaleString();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* The image is drawn here, never fetched. The value is the credential itself,
|
|
164
|
-
* so keeping it on this page and out of any URL is the point.
|
|
165
|
-
*/
|
|
166
|
-
async function renderQr(value: string) {
|
|
167
|
-
if (!value) {
|
|
168
|
-
qrImage.value = "";
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
try {
|
|
172
|
-
qrImage.value = await QRCode.toDataURL(value, { width: 440, margin: 1 });
|
|
173
|
-
} catch (error) {
|
|
174
|
-
console.error("Unable to render the HID QR code:", error);
|
|
175
|
-
qrImage.value = "";
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Saves the rendered code as a PNG.
|
|
181
|
-
*
|
|
182
|
-
* Straight off the data URL already in the page - no request, so the credential
|
|
183
|
-
* never travels again to be downloaded. Same anchor-click approach `VisitorForm`
|
|
184
|
-
* uses for a visitor pass, and the reader name is in the file name because a
|
|
185
|
-
* member can hold one code per reader.
|
|
186
|
-
*/
|
|
187
|
-
function downloadQr() {
|
|
188
|
-
if (!qrImage.value) return;
|
|
189
|
-
const slug = String(profile.value?.readerName || "reader")
|
|
190
|
-
.trim()
|
|
191
|
-
.replace(/[^a-z0-9]+/gi, "-")
|
|
192
|
-
.replace(/^-|-$/g, "")
|
|
193
|
-
.toLowerCase();
|
|
194
|
-
const link = document.createElement("a");
|
|
195
|
-
link.href = qrImage.value;
|
|
196
|
-
link.download = `hid-qr-${slug || "reader"}.png`;
|
|
197
|
-
link.click();
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async function loadReaders() {
|
|
201
|
-
if (!props.site) return;
|
|
202
|
-
const response = toRecord(await getReaders({ site: props.site, page: 1, limit: 100 }));
|
|
203
|
-
const data = toRecord(response.data);
|
|
204
|
-
const items = response.items ?? data.items ?? data.readers ?? [];
|
|
205
|
-
readers.value = (Array.isArray(items) ? items : []).filter((reader) => reader?._id);
|
|
206
|
-
if (!selectedReaderId.value && readers.value.length) {
|
|
207
|
-
selectedReaderId.value = String(readers.value[0]._id);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
async function loadProfile() {
|
|
212
|
-
if (!selectedReaderId.value) return;
|
|
213
|
-
loading.value = true;
|
|
214
|
-
try {
|
|
215
|
-
const response = await getProfileQr(selectedReaderId.value);
|
|
216
|
-
profile.value = response?.data ?? null;
|
|
217
|
-
await renderQr(profile.value?.qrValue || "");
|
|
218
|
-
} catch (error: unknown) {
|
|
219
|
-
profile.value = null;
|
|
220
|
-
qrImage.value = "";
|
|
221
|
-
showToast(errorMessage(error), "error");
|
|
222
|
-
} finally {
|
|
223
|
-
loading.value = false;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
async function generate() {
|
|
228
|
-
if (!selectedReaderId.value) return;
|
|
229
|
-
working.value = true;
|
|
230
|
-
try {
|
|
231
|
-
const response = await issueProfileQr(selectedReaderId.value);
|
|
232
|
-
profile.value = response?.data ?? null;
|
|
233
|
-
await renderQr(profile.value?.qrValue || "");
|
|
234
|
-
showToast("QR code generated.");
|
|
235
|
-
} catch (error: unknown) {
|
|
236
|
-
showToast(errorMessage(error), "error");
|
|
237
|
-
} finally {
|
|
238
|
-
working.value = false;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
async function remove() {
|
|
243
|
-
if (!selectedReaderId.value) return;
|
|
244
|
-
if (!window.confirm("Remove your QR code from this reader?")) return;
|
|
245
|
-
working.value = true;
|
|
246
|
-
try {
|
|
247
|
-
const response = await deleteProfileQr(selectedReaderId.value);
|
|
248
|
-
profile.value = response?.data ?? null;
|
|
249
|
-
await renderQr("");
|
|
250
|
-
showToast("QR code removed.");
|
|
251
|
-
} catch (error: unknown) {
|
|
252
|
-
showToast(errorMessage(error), "error");
|
|
253
|
-
} finally {
|
|
254
|
-
working.value = false;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
watch(
|
|
259
|
-
() => props.site,
|
|
260
|
-
async () => {
|
|
261
|
-
await loadReaders();
|
|
262
|
-
await loadProfile();
|
|
263
|
-
},
|
|
264
|
-
{ immediate: true },
|
|
265
|
-
);
|
|
266
|
-
</script>
|
|
267
|
-
|
|
268
|
-
<style scoped>
|
|
269
|
-
.hid-profile-qr__header {
|
|
270
|
-
display: flex;
|
|
271
|
-
align-items: center;
|
|
272
|
-
gap: 12px;
|
|
273
|
-
flex-wrap: wrap;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
.hid-profile-qr__title {
|
|
277
|
-
margin: 0;
|
|
278
|
-
font-size: 18px;
|
|
279
|
-
font-weight: 600;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
.hid-profile-qr__card {
|
|
283
|
-
margin-top: 16px;
|
|
284
|
-
max-width: 460px;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
.hid-profile-qr__reader {
|
|
288
|
-
margin: 0 0 14px;
|
|
289
|
-
font-size: 14px;
|
|
290
|
-
font-weight: 600;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
.hid-profile-qr__reader span {
|
|
294
|
-
display: block;
|
|
295
|
-
color: var(--muted);
|
|
296
|
-
font-size: 12px;
|
|
297
|
-
font-weight: 500;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
.hid-profile-qr__code {
|
|
301
|
-
display: grid;
|
|
302
|
-
place-items: center;
|
|
303
|
-
margin-bottom: 14px;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
.hid-profile-qr__issued {
|
|
307
|
-
margin: 8px 0 0;
|
|
308
|
-
color: var(--muted);
|
|
309
|
-
font-size: 12px;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
.hid-profile-qr__state {
|
|
313
|
-
color: var(--muted);
|
|
314
|
-
font-size: 13.5px;
|
|
315
|
-
padding: 8px 0 14px;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
.hid-profile-qr__actions {
|
|
319
|
-
display: flex;
|
|
320
|
-
gap: 8px;
|
|
321
|
-
flex-wrap: wrap;
|
|
322
|
-
}
|
|
323
|
-
</style>
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<v-container fluid>
|
|
3
|
-
<HidEnabledGate
|
|
4
|
-
:site="siteId"
|
|
5
|
-
:org="orgId"
|
|
6
|
-
message="Enable HID as a service for this site before generating a QR code."
|
|
7
|
-
>
|
|
8
|
-
<HidProfileQrCode :site="siteId" />
|
|
9
|
-
</HidEnabledGate>
|
|
10
|
-
</v-container>
|
|
11
|
-
</template>
|
|
12
|
-
|
|
13
|
-
<script setup lang="ts">
|
|
14
|
-
definePageMeta({
|
|
15
|
-
layout: "default",
|
|
16
|
-
middleware: ["01-auth", "02-org"],
|
|
17
|
-
memberOnly: true,
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const route = useRoute();
|
|
21
|
-
const siteId = computed(() => String(route.params.site ?? ""));
|
|
22
|
-
const orgId = computed(() => String(route.params.org ?? ""));
|
|
23
|
-
</script>
|
package/utils/anpr-direction.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ANPR camera types ("direction") that carry whitelist and season pass plates.
|
|
3
|
-
*
|
|
4
|
-
* Mirrors `ALLOW_CAMERA_DIRECTIONS` in iservice365-core
|
|
5
|
-
* (`src/utils/anpr-plate.util.ts`): an allowed plate is written to every Entry,
|
|
6
|
-
* Both Entry and Exit, and Residents camera, and never to Visitors or Exit.
|
|
7
|
-
*/
|
|
8
|
-
export const ALLOW_CAMERA_DIRECTIONS = ["entry", "both", "residents"];
|
|
9
|
-
|
|
10
|
-
export const isAllowCameraDirection = (direction: unknown): boolean =>
|
|
11
|
-
ALLOW_CAMERA_DIRECTIONS.includes(
|
|
12
|
-
String(direction ?? "").trim().toLowerCase(),
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
/** ANPR camera types that carry blocked plates: every type except Exit. */
|
|
16
|
-
export const BLOCK_CAMERA_DIRECTIONS = ["entry", "both", "residents", "visitors"];
|
|
17
|
-
|
|
18
|
-
export const isBlockCameraDirection = (direction: unknown): boolean =>
|
|
19
|
-
BLOCK_CAMERA_DIRECTIONS.includes(
|
|
20
|
-
String(direction ?? "").trim().toLowerCase(),
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Which of a camera's plate lists are FILLED with the site's vehicles when it
|
|
25
|
-
* is added (`from` empty) or its type changes - the lists the new type holds
|
|
26
|
-
* and the old one did not. Mirrors `listsFilledByDirectionChange` in
|
|
27
|
-
* iservice365-core. Nothing to confirm (nothing is removed), but saving takes
|
|
28
|
-
* longer, so the form says so.
|
|
29
|
-
*/
|
|
30
|
-
export const listsFilledByDirectionChange = (
|
|
31
|
-
from: unknown,
|
|
32
|
-
to: unknown,
|
|
33
|
-
): { allow: boolean; block: boolean } => {
|
|
34
|
-
const before = String(from ?? "").trim().toLowerCase();
|
|
35
|
-
const after = String(to ?? "").trim().toLowerCase();
|
|
36
|
-
|
|
37
|
-
if (!after || after === before) return { allow: false, block: false };
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
allow: isAllowCameraDirection(after) && !isAllowCameraDirection(before),
|
|
41
|
-
block: isBlockCameraDirection(after) && !isBlockCameraDirection(before),
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Which of a camera's plate lists a type change empties, on the camera itself
|
|
47
|
-
* - every plate on the list, including ones added directly on the camera.
|
|
48
|
-
* Mirrors `listsClearedByDirectionChange` in iservice365-core, which refuses
|
|
49
|
-
* the change unless it is confirmed, so the form asks first.
|
|
50
|
-
*
|
|
51
|
-
* - To Exit: the allowed list and the blocklist.
|
|
52
|
-
* - Entry / Both / Residents to Visitors: the allowed list.
|
|
53
|
-
* - Anything else: nothing.
|
|
54
|
-
*/
|
|
55
|
-
export const listsClearedByDirectionChange = (
|
|
56
|
-
from: unknown,
|
|
57
|
-
to: unknown,
|
|
58
|
-
): { allow: boolean; block: boolean } => {
|
|
59
|
-
const before = String(from ?? "").trim().toLowerCase();
|
|
60
|
-
const after = String(to ?? "").trim().toLowerCase();
|
|
61
|
-
|
|
62
|
-
if (!after || after === before) return { allow: false, block: false };
|
|
63
|
-
|
|
64
|
-
if (after === "exit") return { allow: true, block: true };
|
|
65
|
-
|
|
66
|
-
return {
|
|
67
|
-
allow: isAllowCameraDirection(before) && !isAllowCameraDirection(after),
|
|
68
|
-
block: false,
|
|
69
|
-
};
|
|
70
|
-
};
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Writing a new ANPR camera's plates one at a time from the add-camera screen.
|
|
3
|
-
*
|
|
4
|
-
* The screen shows every plate the camera will receive with its own progress -
|
|
5
|
-
* pending, adding, added, failed - and lets the person retry. The rules for
|
|
6
|
-
* WHEN to stop live here, apart from the component, so they can be tested:
|
|
7
|
-
*
|
|
8
|
-
* - between plates, when the person presses Stop;
|
|
9
|
-
* - at once, when the camera refuses the LOGIN or the permission. Every further
|
|
10
|
-
* attempt would be another failed login, and a Dahua unit locks the account
|
|
11
|
-
* for 30 minutes after 3 failed logins in 30 seconds;
|
|
12
|
-
* - after 3 plates in a row fail for any other reason - the camera has most
|
|
13
|
-
* likely gone away, and each attempt waits for a timeout.
|
|
14
|
-
*
|
|
15
|
-
* Whatever is not attempted stays "pending", and Retry carries on with the
|
|
16
|
-
* failed and pending plates.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
export type PlateSyncStatus = "pending" | "adding" | "added" | "failed";
|
|
20
|
-
|
|
21
|
-
export type PlateSyncRow = {
|
|
22
|
-
id: string;
|
|
23
|
-
vehicle: string;
|
|
24
|
-
list: "allowed" | "blocked";
|
|
25
|
-
plateNumber: string;
|
|
26
|
-
name: string;
|
|
27
|
-
block: string;
|
|
28
|
-
level: string;
|
|
29
|
-
unit: string;
|
|
30
|
-
kind: string;
|
|
31
|
-
status: PlateSyncStatus;
|
|
32
|
-
error?: string;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/** What writing one plate answered. `kind` as the API classifies a refusal. */
|
|
36
|
-
export type PlateWriteAnswer = {
|
|
37
|
-
ok: boolean;
|
|
38
|
-
reason?: string;
|
|
39
|
-
kind?: string;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export type PlateQueueStop = "done" | "requested" | "login" | "failures";
|
|
43
|
-
|
|
44
|
-
export const MAX_FAILURES_IN_A_ROW = 3;
|
|
45
|
-
|
|
46
|
-
export function plateSyncCounts(rows: PlateSyncRow[]) {
|
|
47
|
-
let added = 0;
|
|
48
|
-
let failed = 0;
|
|
49
|
-
let pending = 0;
|
|
50
|
-
for (const row of rows) {
|
|
51
|
-
if (row.status === "added") added++;
|
|
52
|
-
else if (row.status === "failed") failed++;
|
|
53
|
-
else pending++;
|
|
54
|
-
}
|
|
55
|
-
return { total: rows.length, added, failed, pending };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export type PlateSyncCounts = ReturnType<typeof plateSyncCounts>;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* The same counts, kept apart for the allowed list (whitelist, season pass)
|
|
62
|
-
* and the blocklist - a failed blocked plate is a car that can still get in,
|
|
63
|
-
* which is not the same as a resident who cannot.
|
|
64
|
-
*/
|
|
65
|
-
export function plateSyncCountsByList(rows: PlateSyncRow[]): {
|
|
66
|
-
allowed: PlateSyncCounts;
|
|
67
|
-
blocked: PlateSyncCounts;
|
|
68
|
-
} {
|
|
69
|
-
return {
|
|
70
|
-
allowed: plateSyncCounts(rows.filter((row) => row.list !== "blocked")),
|
|
71
|
-
blocked: plateSyncCounts(rows.filter((row) => row.list === "blocked")),
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** The plates Retry carries on with: every failed one and every pending one. */
|
|
76
|
-
export function rowsToRetry(rows: PlateSyncRow[]): PlateSyncRow[] {
|
|
77
|
-
return rows.filter((row) => row.status === "failed" || row.status === "pending");
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Write `targets` one at a time, in order, updating each row's `status` (and
|
|
82
|
-
* `error`) in place. `write` may answer `{ ok: false }` or throw - both mark
|
|
83
|
-
* the row failed. Returns why it stopped, and the sentence to show for it.
|
|
84
|
-
*/
|
|
85
|
-
export async function runPlateQueue(
|
|
86
|
-
targets: PlateSyncRow[],
|
|
87
|
-
write: (row: PlateSyncRow) => Promise<PlateWriteAnswer>,
|
|
88
|
-
options: { shouldStop?: () => boolean } = {},
|
|
89
|
-
): Promise<{ stopped: PlateQueueStop; message: string }> {
|
|
90
|
-
let failuresInARow = 0;
|
|
91
|
-
|
|
92
|
-
for (const row of targets) {
|
|
93
|
-
if (options.shouldStop?.()) {
|
|
94
|
-
return {
|
|
95
|
-
stopped: "requested",
|
|
96
|
-
message: "Stopped. Press Retry to carry on with the pending and failed plates.",
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
row.status = "adding";
|
|
101
|
-
row.error = "";
|
|
102
|
-
|
|
103
|
-
let answer: PlateWriteAnswer;
|
|
104
|
-
try {
|
|
105
|
-
answer = await write(row);
|
|
106
|
-
} catch (error: any) {
|
|
107
|
-
answer = { ok: false, reason: String(error?.message ?? error ?? "") };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (answer?.ok) {
|
|
111
|
-
row.status = "added";
|
|
112
|
-
failuresInARow = 0;
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
row.status = "failed";
|
|
117
|
-
row.error = answer?.reason || "The plate could not be added.";
|
|
118
|
-
failuresInARow++;
|
|
119
|
-
|
|
120
|
-
if (answer?.kind === "challenge" || answer?.kind === "rejected") {
|
|
121
|
-
return {
|
|
122
|
-
stopped: "login",
|
|
123
|
-
message:
|
|
124
|
-
`Stopped: ${row.error.replace(/\.\s*$/, "")}. Check the camera's ` +
|
|
125
|
-
`username and password, then press Retry.`,
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (failuresInARow >= MAX_FAILURES_IN_A_ROW) {
|
|
130
|
-
return {
|
|
131
|
-
stopped: "failures",
|
|
132
|
-
message:
|
|
133
|
-
`Stopped after ${MAX_FAILURES_IN_A_ROW} plates in a row could not ` +
|
|
134
|
-
`be added. Check that the camera is switched on and reachable, then ` +
|
|
135
|
-
`press Retry.`,
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return { stopped: "done", message: "" };
|
|
141
|
-
}
|