@7365admin1/layer-common 4.97.1-staging.620 → 4.97.1-staging.622

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.
@@ -0,0 +1,158 @@
1
+ <!-- Known plates: the CCTV watch list. Not linked to resident, visitor or vehicle records. -->
2
+ <template>
3
+ <div>
4
+ <v-alert type="info" variant="tonal" density="compact" class="mb-3" icon="mdi-information-outline">
5
+ This is a <strong>CCTV watch list</strong>. It is sent to the engine so it can recognise these plates, and it can raise an alert when one is seen.
6
+ It is <strong>separate</strong> from resident, visitor and vehicle records - adding a plate here does not register a vehicle, and registering a vehicle does not add it here.
7
+ </v-alert>
8
+
9
+ <div class="d-flex flex-wrap ga-2 align-center">
10
+ <v-text-field v-model="search" label="Search plate, name or note" prepend-inner-icon="mdi-magnify" density="comfortable" hide-details clearable style="max-width: 360px" />
11
+ <v-spacer />
12
+ <v-btn v-if="canEdit" color="primary" prepend-icon="mdi-plus" @click="openForm()">Add plate</v-btn>
13
+ </div>
14
+
15
+ <v-progress-linear v-if="loading" indeterminate class="mt-3" />
16
+ <CctvEngineNotice v-else-if="error" class="mt-3" :error="error" retry @retry="load" />
17
+ <CctvEngineNotice
18
+ v-else-if="!plates.length"
19
+ class="mt-3"
20
+ title="No plates on the watch list yet"
21
+ body="Add a plate you want the cameras to recognise, for example a vehicle of interest. Turn on 'Alert when seen' to be told when it appears."
22
+ />
23
+ <CctvEngineNotice v-else-if="!shown.length" class="mt-3" title="No plate matches that search" body="Clear the search to see the whole list." />
24
+ <v-list v-else lines="two" class="mt-2">
25
+ <v-list-item v-for="p in shown" :key="p._id" :title="p.plate" :subtitle="[p.label, p.note].filter(Boolean).join(' - ')">
26
+ <template #append>
27
+ <v-chip v-if="p.alert" size="small" color="error" label class="mr-2" prepend-icon="mdi-bell-ring-outline">Alerts</v-chip>
28
+ <template v-if="canEdit">
29
+ <v-btn icon="mdi-pencil" variant="text" size="small" aria-label="Edit plate" @click="openForm(p)" />
30
+ <v-btn icon="mdi-delete-outline" variant="text" size="small" aria-label="Delete plate" @click="askDelete(p)" />
31
+ </template>
32
+ </template>
33
+ </v-list-item>
34
+ </v-list>
35
+
36
+ <v-dialog v-model="formOpen" max-width="480">
37
+ <v-card>
38
+ <v-card-title>{{ editing ? "Edit plate" : "Add plate to watch list" }}</v-card-title>
39
+ <v-card-text>
40
+ <v-text-field v-model="form.plate" label="Plate number" density="comfortable" :disabled="editing" hint="Spaces and dashes are ignored. Saved as capitals, e.g. SGX1234A." persistent-hint />
41
+ <v-text-field v-model="form.label" label="Name / reason (e.g. Banned vehicle)" density="comfortable" class="mt-3" />
42
+ <v-textarea v-model="form.note" label="Note (optional)" rows="2" density="comfortable" />
43
+ <v-switch v-model="form.alert" color="primary" label="Alert when seen" hint="Notify the people chosen under Alerts when a camera sees this plate." persistent-hint />
44
+ <div v-if="problem" class="text-error mt-2" role="alert">{{ problem }}</div>
45
+ <CctvEngineNotice v-if="formError" class="mt-2" :error="formError" />
46
+ </v-card-text>
47
+ <v-card-actions>
48
+ <v-spacer />
49
+ <v-btn variant="text" @click="formOpen = false">Cancel</v-btn>
50
+ <v-btn color="primary" :loading="saving" @click="save">Save</v-btn>
51
+ </v-card-actions>
52
+ </v-card>
53
+ </v-dialog>
54
+
55
+ <v-dialog v-model="deleteOpen" max-width="420">
56
+ <v-card>
57
+ <v-card-title>Remove {{ target?.plate }}?</v-card-title>
58
+ <v-card-text>The engine stops recognising this plate as known. Earlier events stay.</v-card-text>
59
+ <v-card-actions>
60
+ <v-spacer />
61
+ <v-btn variant="text" @click="deleteOpen = false">Keep</v-btn>
62
+ <v-btn color="error" :loading="saving" @click="doDelete">Remove</v-btn>
63
+ </v-card-actions>
64
+ </v-card>
65
+ </v-dialog>
66
+ </div>
67
+ </template>
68
+
69
+ <script setup lang="ts">
70
+ import { computed, reactive, ref } from "vue";
71
+ import { CCTV_PERMISSIONS, normalisePlate } from "../../utils/cctv-engine";
72
+
73
+ const props = defineProps<{ site: string; access?: { can: (p: any) => boolean } }>();
74
+ const { listPlates, createPlate, updatePlate, deletePlate } = useCctvEngine();
75
+ const canEdit = computed(() => props.access?.can(CCTV_PERMISSIONS.managePlates) ?? false);
76
+
77
+ const plates = ref<any[]>([]);
78
+ const loading = ref(false);
79
+ const error = ref<unknown>(null);
80
+ const search = ref("");
81
+
82
+ const shown = computed(() => {
83
+ const q = (search.value ?? "").trim().toLowerCase();
84
+ const qp = normalisePlate(q);
85
+ if (!q) return plates.value;
86
+ return plates.value.filter((p) => (qp && String(p.plate).includes(qp)) || `${p.label ?? ""} ${p.note ?? ""}`.toLowerCase().includes(q));
87
+ });
88
+
89
+ async function load() {
90
+ loading.value = true;
91
+ error.value = null;
92
+ try {
93
+ const res: any = await listPlates(props.site);
94
+ plates.value = res?.items ?? [];
95
+ } catch (e) {
96
+ error.value = e;
97
+ } finally {
98
+ loading.value = false;
99
+ }
100
+ }
101
+
102
+ const form = reactive({ _id: "", plate: "", label: "", note: "", alert: false });
103
+ const editing = ref(false);
104
+ const formOpen = ref(false);
105
+ const saving = ref(false);
106
+ const problem = ref("");
107
+ const formError = ref<unknown>(null);
108
+
109
+ function openForm(p?: any) {
110
+ Object.assign(form, { _id: "", plate: "", label: "", note: "", alert: false }, p ? { _id: p._id, plate: p.plate, label: p.label ?? "", note: p.note ?? "", alert: !!p.alert } : {});
111
+ editing.value = !!p;
112
+ problem.value = "";
113
+ formError.value = null;
114
+ formOpen.value = true;
115
+ }
116
+
117
+ async function save() {
118
+ const plate = normalisePlate(form.plate);
119
+ if (!plate) return void (problem.value = "Enter the plate number.");
120
+ if (!editing.value && plates.value.some((p) => p.plate === plate)) return void (problem.value = "This plate is already on the list. Edit it instead.");
121
+ problem.value = "";
122
+ saving.value = true;
123
+ formError.value = null;
124
+ const body = { site: props.site, plate, label: form.label.trim(), note: form.note.trim(), alert: form.alert };
125
+ try {
126
+ if (editing.value) await updatePlate(props.site, form._id, body);
127
+ else await createPlate(body);
128
+ formOpen.value = false;
129
+ await load();
130
+ } catch (e) {
131
+ formError.value = e;
132
+ } finally {
133
+ saving.value = false;
134
+ }
135
+ }
136
+
137
+ const deleteOpen = ref(false);
138
+ const target = ref<any>(null);
139
+ const askDelete = (p: any) => {
140
+ target.value = p;
141
+ deleteOpen.value = true;
142
+ };
143
+ async function doDelete() {
144
+ saving.value = true;
145
+ try {
146
+ await deletePlate(props.site, target.value._id);
147
+ deleteOpen.value = false;
148
+ await load();
149
+ } catch (e) {
150
+ deleteOpen.value = false;
151
+ error.value = e;
152
+ } finally {
153
+ saving.value = false;
154
+ }
155
+ }
156
+
157
+ load();
158
+ </script>
@@ -0,0 +1,239 @@
1
+ <!--
2
+ Playback & export. Pick cameras (one, or up to four side by side with ONE shared time slider) and a time range
3
+ of at most 30 minutes to play. For longer or for keeping a copy, create an export and download it when ready.
4
+ -->
5
+ <template>
6
+ <div>
7
+ <CctvEngineCameraPicker v-model:engine="engine" no-camera :site="site" />
8
+
9
+ <template v-if="engine">
10
+ <v-row dense class="mt-2">
11
+ <v-col cols="12" md="6">
12
+ <v-select
13
+ v-model="picked"
14
+ :items="cameras"
15
+ item-title="name"
16
+ item-value="name"
17
+ label="Cameras (pick up to 4 to compare side by side)"
18
+ multiple
19
+ chips
20
+ closable-chips
21
+ density="comfortable"
22
+ :loading="camLoading"
23
+ :rules="[() => picked.length <= 4 || 'Up to 4 cameras at once']"
24
+ :no-data-text="camError ? 'Cameras could not be read' : 'This engine has no cameras yet'"
25
+ />
26
+ </v-col>
27
+ <v-col cols="6" md="3"><v-text-field v-model="from" type="datetime-local" label="From" density="comfortable" /></v-col>
28
+ <v-col cols="6" md="3"><v-text-field v-model="to" type="datetime-local" label="To" density="comfortable" /></v-col>
29
+ </v-row>
30
+ <CctvEngineNotice v-if="camError" class="mb-3" :error="camError" />
31
+
32
+ <div class="d-flex flex-wrap ga-2 align-center">
33
+ <v-btn color="primary" prepend-icon="mdi-play" :loading="playing" :disabled="!picked.length || picked.length > 4" @click="play">Play clip</v-btn>
34
+ <span class="hero-muted">Up to 30 minutes. For longer, create an export below.</span>
35
+ </div>
36
+ <div v-if="problem" class="text-error mt-2" role="alert">{{ problem }}</div>
37
+ <CctvEngineNotice v-if="playError" class="mt-3" :error="playError" retry @retry="play" />
38
+
39
+ <div v-if="clips.length" class="mt-4">
40
+ <div class="cctv-pb__grid" :style="{ gridTemplateColumns: `repeat(${clips.length > 1 && !smAndDown ? 2 : 1}, minmax(0, 1fr))` }">
41
+ <figure v-for="c in clips" :key="c.camera" class="cctv-pb__fig">
42
+ <video :ref="(el) => setVideo(c.camera, el as HTMLVideoElement | null)" :src="c.url" muted playsinline class="cctv-pb__video" @timeupdate="onTime(c.camera)" />
43
+ <figcaption>{{ c.camera }}</figcaption>
44
+ </figure>
45
+ </div>
46
+ <div class="d-flex align-center ga-3 mt-2">
47
+ <v-btn :icon="paused ? 'mdi-play' : 'mdi-pause'" variant="tonal" :aria-label="paused ? 'Play all' : 'Pause all'" @click="toggle" />
48
+ <v-slider v-model="position" :min="0" :max="1" :step="0.001" hide-details aria-label="Time in clip" @update:model-value="scrub" @start="scrubbing = true" @end="scrubbing = false" />
49
+ <span class="hero-muted cctv-pb__time">{{ clock }}</span>
50
+ </div>
51
+ </div>
52
+
53
+ <v-divider class="my-6" />
54
+ <h3 class="hero-title">Export</h3>
55
+ <p class="hero-muted">An export is a saved video file of the time range above (one per selected camera). It is prepared at the site, so it can take a few minutes. Download it here when it says Ready.</p>
56
+ <div v-if="access && !can(CCTV_PERMISSIONS.export)" class="hero-notice">Your role cannot create exports. Ask a site admin to add cctv-engine:export.</div>
57
+ <template v-else>
58
+ <div class="d-flex flex-wrap ga-2 align-center">
59
+ <v-text-field v-model="exportName" label="Export name" density="comfortable" hide-details style="max-width: 320px" />
60
+ <v-btn :loading="exporting" :disabled="!picked.length" prepend-icon="mdi-export" @click="makeExport">Create export</v-btn>
61
+ <v-btn variant="text" prepend-icon="mdi-refresh" :loading="exportsLoading" @click="loadExports">Refresh list</v-btn>
62
+ </div>
63
+ <div v-if="exportMsg" class="mt-2" role="status">{{ exportMsg }}</div>
64
+ <CctvEngineNotice v-if="exportError" class="mt-3" :error="exportError" retry @retry="loadExports" />
65
+ <CctvEngineNotice v-else-if="!exportsLoading && !exportList.length" class="mt-3" title="No exports yet" body="Pick cameras and a time range, give it a name and press Create export." />
66
+ <v-list v-else lines="two" class="mt-2">
67
+ <v-list-item v-for="x in exportList" :key="x.id" :title="x.name || 'Export'" :subtitle="`${x.camera ?? ''} - ${x.date ? new Date(x.date).toLocaleString() : ''}`">
68
+ <template #append>
69
+ <v-chip size="small" label :color="x.inProgress ? 'warning' : 'success'" class="mr-2">{{ x.inProgress ? "Preparing" : "Ready" }}</v-chip>
70
+ <v-btn size="small" variant="tonal" prepend-icon="mdi-download" :disabled="x.inProgress" :loading="downloading === x.id" @click="download(x)">Download</v-btn>
71
+ </template>
72
+ </v-list-item>
73
+ </v-list>
74
+ </template>
75
+ </template>
76
+ </div>
77
+ </template>
78
+
79
+ <script setup lang="ts">
80
+ import { computed, onBeforeUnmount, ref, watch } from "vue";
81
+ import { useDisplay } from "vuetify";
82
+ import { CCTV_PERMISSIONS, clipRangeProblem, seekSeconds, toIso } from "../../utils/cctv-engine";
83
+
84
+ const props = defineProps<{ site: string; access?: { can: (p: any) => boolean } }>();
85
+ const can = (p: string) => props.access?.can(p as any) ?? false;
86
+ const { listCameras, cameraClip, createExport, listExports, downloadExport } = useCctvEngine();
87
+ const { smAndDown } = useDisplay();
88
+
89
+ const engine = ref("");
90
+ const cameras = ref<{ name: string }[]>([]);
91
+ const picked = ref<string[]>([]);
92
+ const camLoading = ref(false);
93
+ const camError = ref<unknown>(null);
94
+
95
+ const pad = (n: number) => String(n).padStart(2, "0");
96
+ const local = (d: Date) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
97
+ const now = new Date();
98
+ const from = ref(local(new Date(now.getTime() - 10 * 60000)));
99
+ const to = ref(local(now));
100
+
101
+ watch(engine, async (id) => {
102
+ cameras.value = [];
103
+ picked.value = [];
104
+ camError.value = null;
105
+ freeClips();
106
+ if (!id) return;
107
+ camLoading.value = true;
108
+ try {
109
+ const res: any = await listCameras(id);
110
+ cameras.value = res?.items ?? [];
111
+ if (cameras.value[0]) picked.value = [cameras.value[0].name];
112
+ } catch (e) {
113
+ camError.value = e;
114
+ } finally {
115
+ camLoading.value = false;
116
+ }
117
+ loadExports();
118
+ });
119
+
120
+ // ---- clips with one shared slider
121
+ const clips = ref<{ camera: string; url: string }[]>([]);
122
+ const videos = new Map<string, HTMLVideoElement>();
123
+ const problem = ref("");
124
+ const playing = ref(false);
125
+ const playError = ref<unknown>(null);
126
+ const position = ref(0);
127
+ const scrubbing = ref(false);
128
+ const paused = ref(true);
129
+
130
+ const spanSeconds = computed(() => (new Date(to.value).getTime() - new Date(from.value).getTime()) / 1000);
131
+ const clock = computed(() => {
132
+ const s = Math.round(position.value * Math.max(0, spanSeconds.value));
133
+ return `${Math.floor(s / 60)}:${pad(s % 60)}`;
134
+ });
135
+
136
+ const setVideo = (camera: string, el: HTMLVideoElement | null) => (el ? videos.set(camera, el) : videos.delete(camera));
137
+
138
+ function freeClips() {
139
+ for (const c of clips.value) URL.revokeObjectURL(c.url);
140
+ clips.value = [];
141
+ videos.clear();
142
+ position.value = 0;
143
+ paused.value = true;
144
+ }
145
+
146
+ async function play() {
147
+ problem.value = clipRangeProblem(from.value, to.value);
148
+ if (problem.value) return;
149
+ playing.value = true;
150
+ playError.value = null;
151
+ freeClips();
152
+ const iso = { from: toIso(from.value)!, to: toIso(to.value)! };
153
+ const results = await Promise.allSettled(picked.value.map(async (camera) => ({ camera, url: await cameraClip(engine.value, camera, iso.from, iso.to) })));
154
+ const ok = results.filter((r): r is PromiseFulfilledResult<{ camera: string; url: string }> => r.status === "fulfilled").map((r) => r.value);
155
+ const bad = results.find((r): r is PromiseRejectedResult => r.status === "rejected");
156
+ clips.value = ok;
157
+ if (bad) playError.value = bad.reason;
158
+ playing.value = false;
159
+ }
160
+
161
+ function scrub(v: number) {
162
+ for (const el of videos.values()) el.currentTime = seekSeconds(v, spanSeconds.value, el.duration);
163
+ }
164
+ function onTime(camera: string) {
165
+ const first = videos.get(clips.value[0]?.camera ?? "");
166
+ if (scrubbing.value || !first || camera !== clips.value[0]?.camera || !(first.duration > 0)) return;
167
+ position.value = Math.min(1, first.currentTime / first.duration);
168
+ }
169
+ function toggle() {
170
+ paused.value = !paused.value;
171
+ for (const el of videos.values()) (paused.value ? el.pause() : el.play().catch(() => undefined));
172
+ }
173
+
174
+ // ---- exports
175
+ const exportName = ref("");
176
+ const exporting = ref(false);
177
+ const exportMsg = ref("");
178
+ const exportError = ref<unknown>(null);
179
+ const exportList = ref<any[]>([]);
180
+ const exportsLoading = ref(false);
181
+ const downloading = ref("");
182
+
183
+ async function loadExports() {
184
+ if (!engine.value || !can(CCTV_PERMISSIONS.export)) return;
185
+ exportsLoading.value = true;
186
+ exportError.value = null;
187
+ try {
188
+ const res: any = await listExports(engine.value);
189
+ exportList.value = res?.items ?? [];
190
+ } catch (e) {
191
+ exportError.value = e;
192
+ } finally {
193
+ exportsLoading.value = false;
194
+ }
195
+ }
196
+ async function makeExport() {
197
+ exportMsg.value = "";
198
+ const bad = !from.value || !to.value || new Date(to.value) <= new Date(from.value);
199
+ if (bad) return void (exportMsg.value = "Pick a start time and an end time after it.");
200
+ exporting.value = true;
201
+ exportError.value = null;
202
+ try {
203
+ for (const camera of picked.value) {
204
+ await createExport(engine.value, { camera, from: toIso(from.value)!, to: toIso(to.value)!, name: exportName.value.trim() || `${camera} ${from.value}` });
205
+ }
206
+ exportMsg.value = "Export started. It is prepared at the site; press Refresh list until it says Ready.";
207
+ await loadExports();
208
+ } catch (e) {
209
+ exportError.value = e;
210
+ } finally {
211
+ exporting.value = false;
212
+ }
213
+ }
214
+ async function download(x: any) {
215
+ const id = x.id;
216
+ downloading.value = id;
217
+ try {
218
+ const url = await downloadExport(engine.value, id);
219
+ const a = document.createElement("a");
220
+ a.href = url;
221
+ a.download = `${x.name || "export"}.mp4`;
222
+ a.click();
223
+ setTimeout(() => URL.revokeObjectURL(url), 60000);
224
+ } catch (e) {
225
+ exportError.value = e;
226
+ } finally {
227
+ downloading.value = "";
228
+ }
229
+ }
230
+
231
+ onBeforeUnmount(freeClips);
232
+ </script>
233
+
234
+ <style scoped>
235
+ .cctv-pb__grid { display: grid; gap: 8px; }
236
+ .cctv-pb__fig { margin: 0; }
237
+ .cctv-pb__video { width: 100%; max-height: 50vh; background: #000; border-radius: 8px; }
238
+ .cctv-pb__time { min-width: 48px; text-align: right; }
239
+ </style>
@@ -0,0 +1,86 @@
1
+ <!--
2
+ CCTV Engine - the frame around every CCTV Engine screen.
3
+ Draws the section tabs the person is allowed to use, and says plainly why nothing is shown when the site
4
+ switch is off, the role lacks the permission, or the face library's own switch is off.
5
+ Route: /[org]/[site]/cctv-engine/[section] in the host app.
6
+ -->
7
+ <template>
8
+ <div class="cctv-shell">
9
+ <h2 class="hero-title">CCTV Engine</h2>
10
+ <p class="hero-muted mb-3">
11
+ Cameras analysed by the iSecure365 engine at the site: live view, events, playback, zones, plates and alerts.
12
+ This sits next to the CCTV wall and does not change it.
13
+ </p>
14
+
15
+ <v-progress-linear v-if="!resolved" indeterminate class="mb-3" />
16
+
17
+ <CctvEngineNotice
18
+ v-else-if="!access.enabled"
19
+ title="CCTV Engine is switched off for this site"
20
+ body="Nothing here is shown until it is switched on. Ask your Seven365 contact to switch CCTV Engine on for this site, then reload this page."
21
+ />
22
+ <CctvEngineNotice
23
+ v-else-if="!access.canAny"
24
+ title="You do not have permission"
25
+ body="Your role has no CCTV Engine permission. Ask a site admin to add one (for example cctv-engine:view-live) to your role."
26
+ />
27
+ <template v-else>
28
+ <v-tabs
29
+ :model-value="current"
30
+ show-arrows
31
+ density="comfortable"
32
+ class="mb-4"
33
+ @update:model-value="go"
34
+ >
35
+ <v-tab v-for="s in access.sections" :key="s.key" :value="s.key" :prepend-icon="s.icon">{{ s.title }}</v-tab>
36
+ </v-tabs>
37
+
38
+ <CctvEngineNotice
39
+ v-if="!sectionAllowed"
40
+ title="You do not have permission for this page"
41
+ body="Pick one of the tabs above, or ask a site admin to add the permission to your role."
42
+ />
43
+ <CctvEngineNotice
44
+ v-else-if="current === 'faces' && !access.faceEnabled"
45
+ title="Face recognition is switched off for this site"
46
+ body="The face library is hidden until face recognition is switched on for the site. Ask your Seven365 contact to switch it on. No faces are lost by switching it off."
47
+ />
48
+ <component :is="view" v-else :key="current" :site="site" :access="access" />
49
+ </template>
50
+ </div>
51
+ </template>
52
+
53
+ <script setup lang="ts">
54
+ import { computed } from "vue";
55
+ import { useRoute, useRouter } from "vue-router";
56
+ import type { TCctvSection } from "../../utils/cctv-engine";
57
+
58
+ const props = defineProps<{ site: string; section?: string }>();
59
+ const route = useRoute();
60
+ const router = useRouter();
61
+
62
+ const { access, resolved } = useCctvEngineAccess(() => props.site);
63
+
64
+ const current = computed<TCctvSection>(() => {
65
+ const asked = props.section as TCctvSection | undefined;
66
+ if (asked && access.value.sections.some((s) => s.key === asked)) return asked;
67
+ return (access.value.sections[0]?.key ?? "live") as TCctvSection;
68
+ });
69
+ const sectionAllowed = computed(() => access.value.sections.some((s) => s.key === current.value));
70
+
71
+ const VIEWS: Record<TCctvSection, any> = {
72
+ live: resolveComponent("CctvEngineLive"),
73
+ events: resolveComponent("CctvEngineEvents"),
74
+ playback: resolveComponent("CctvEnginePlayback"),
75
+ zones: resolveComponent("CctvEngineZones"),
76
+ plates: resolveComponent("CctvEnginePlates"),
77
+ faces: resolveComponent("CctvEngineFaces"),
78
+ alerts: resolveComponent("CctvEngineAlerts"),
79
+ engines: resolveComponent("CctvEngineEngines"),
80
+ };
81
+ const view = computed(() => VIEWS[current.value]);
82
+
83
+ function go(section: unknown) {
84
+ router.replace({ ...route, params: { ...route.params, section: String(section) } });
85
+ }
86
+ </script>
@@ -0,0 +1,177 @@
1
+ <!-- Wrong-way rules: "Cars must go zone A then zone B; B then A raises a wrong-way alert." -->
2
+ <template>
3
+ <div>
4
+ <div class="d-flex align-center flex-wrap ga-2">
5
+ <h3 class="hero-title flex-grow-1">Wrong-way rules</h3>
6
+ <v-btn v-if="canEdit" color="primary" prepend-icon="mdi-plus" @click="openForm()">Add rule</v-btn>
7
+ </div>
8
+ <p class="hero-muted">
9
+ A rule watches two zones on one camera. Things must go <strong>zone A, then zone B</strong> (the normal direction).
10
+ Going <strong>B, then A</strong> within the time limit raises a wrong-way alert. Draw the two zones above first.
11
+ </p>
12
+
13
+ <v-progress-linear v-if="loading" indeterminate />
14
+ <CctvEngineNotice v-else-if="error" :error="error" retry @retry="load" />
15
+ <CctvEngineNotice
16
+ v-else-if="!rules.length"
17
+ title="No wrong-way rules yet"
18
+ body="Example: a one-way ramp. Draw a zone called entry and one called exit above, then add a rule: cars must go entry then exit."
19
+ />
20
+ <v-list v-else lines="two">
21
+ <v-list-item v-for="r in rules" :key="r._id" :title="r.name || `${r.camera}: ${r.zoneA} to ${r.zoneB}`" :subtitle="`${r.camera} - ${wrongWayExplain(r.zoneA, r.zoneB)}`">
22
+ <template #append>
23
+ <v-chip size="small" label :color="r.enabled === false ? undefined : 'success'" class="mr-2">{{ r.enabled === false ? "Off" : "On" }}</v-chip>
24
+ <template v-if="canEdit">
25
+ <v-btn icon="mdi-pencil" variant="text" size="small" aria-label="Edit rule" @click="openForm(r)" />
26
+ <v-btn icon="mdi-delete-outline" variant="text" size="small" aria-label="Delete rule" @click="askDelete(r)" />
27
+ </template>
28
+ </template>
29
+ </v-list-item>
30
+ </v-list>
31
+
32
+ <v-dialog v-model="formOpen" max-width="560" scrollable>
33
+ <v-card>
34
+ <v-card-title>{{ editing ? "Edit rule" : "Add wrong-way rule" }}</v-card-title>
35
+ <v-card-text>
36
+ <CctvEngineCameraPicker v-model:engine="form.engine" v-model:camera="form.camera" :site="site" />
37
+ <v-text-field v-model="form.name" label="Rule name (e.g. Ramp one-way)" density="comfortable" class="mt-3" />
38
+ <v-row dense>
39
+ <v-col cols="6"><v-select v-model="form.zoneA" :items="zoneNames" label="Zone A (first)" density="comfortable" :hint="zoneHint" persistent-hint /></v-col>
40
+ <v-col cols="6"><v-select v-model="form.zoneB" :items="zoneNames" label="Zone B (then)" density="comfortable" /></v-col>
41
+ </v-row>
42
+ <v-alert type="info" variant="tonal" density="compact" class="my-3">{{ wrongWayExplain(form.zoneA, form.zoneB) }}</v-alert>
43
+ <v-combobox v-model="form.labels" label="What counts (car, motorcycle, person...)" multiple chips closable-chips density="comfortable" />
44
+ <v-text-field v-model.number="form.maxGapSeconds" type="number" min="1" label="Max seconds between A and B" density="comfortable" hint="If more time passes between the two zones, it is not treated as one journey." persistent-hint />
45
+ <v-switch v-model="form.enabled" label="Rule is on" color="primary" hide-details />
46
+ <div v-if="formProblem" class="text-error mt-2" role="alert">{{ formProblem }}</div>
47
+ <CctvEngineNotice v-if="formError" class="mt-2" :error="formError" />
48
+ </v-card-text>
49
+ <v-card-actions>
50
+ <v-spacer />
51
+ <v-btn variant="text" @click="formOpen = false">Cancel</v-btn>
52
+ <v-btn color="primary" :loading="saving" @click="saveRule">Save rule</v-btn>
53
+ </v-card-actions>
54
+ </v-card>
55
+ </v-dialog>
56
+
57
+ <v-dialog v-model="deleteOpen" max-width="420">
58
+ <v-card>
59
+ <v-card-title>Delete this rule?</v-card-title>
60
+ <v-card-text>The engine stops raising wrong-way alerts for it. Past events stay.</v-card-text>
61
+ <v-card-actions>
62
+ <v-spacer />
63
+ <v-btn variant="text" @click="deleteOpen = false">Keep</v-btn>
64
+ <v-btn color="error" :loading="saving" @click="doDelete">Delete</v-btn>
65
+ </v-card-actions>
66
+ </v-card>
67
+ </v-dialog>
68
+ </div>
69
+ </template>
70
+
71
+ <script setup lang="ts">
72
+ import { computed, reactive, ref, watch } from "vue";
73
+ import { CCTV_PERMISSIONS, wrongWayExplain } from "../../utils/cctv-engine";
74
+
75
+ const props = defineProps<{ site: string; access?: { can: (p: any) => boolean }; engine?: string; camera?: string }>();
76
+ const { listWrongWayRules, createWrongWayRule, updateWrongWayRule, deleteWrongWayRule, getZones } = useCctvEngine();
77
+ const canEdit = computed(() => props.access?.can(CCTV_PERMISSIONS.manageZones) ?? false);
78
+
79
+ const rules = ref<any[]>([]);
80
+ const loading = ref(false);
81
+ const error = ref<unknown>(null);
82
+
83
+ async function load() {
84
+ loading.value = true;
85
+ error.value = null;
86
+ try {
87
+ const res: any = await listWrongWayRules(props.site);
88
+ rules.value = res?.items ?? [];
89
+ } catch (e) {
90
+ error.value = e;
91
+ } finally {
92
+ loading.value = false;
93
+ }
94
+ }
95
+
96
+ const DEFAULT_LABELS = ["car", "motorcycle", "person"];
97
+ const blank = () => ({ _id: "", engine: props.engine ?? "", camera: props.camera ?? "", name: "", zoneA: "", zoneB: "", labels: [...DEFAULT_LABELS], maxGapSeconds: 30, enabled: true });
98
+ const form = reactive(blank());
99
+ const editing = ref(false);
100
+ const formOpen = ref(false);
101
+ const saving = ref(false);
102
+ const formProblem = ref("");
103
+ const formError = ref<unknown>(null);
104
+ const zoneNames = ref<string[]>([]);
105
+ const zoneHint = computed(() => (form.camera && !zoneNames.value.length ? "This camera has no zones yet - draw them above first." : ""));
106
+
107
+ function openForm(rule?: any) {
108
+ Object.assign(form, blank(), rule ? { ...rule, labels: [...(rule.labels ?? DEFAULT_LABELS)] } : {});
109
+ editing.value = !!rule;
110
+ formProblem.value = "";
111
+ formError.value = null;
112
+ formOpen.value = true;
113
+ }
114
+
115
+ watch(
116
+ () => [form.engine, form.camera],
117
+ async ([engine, camera]) => {
118
+ zoneNames.value = [];
119
+ if (!engine || !camera) return;
120
+ try {
121
+ const res: any = await getZones(engine, camera);
122
+ zoneNames.value = (res?.zones ?? []).map((x: any) => x.name).filter(Boolean);
123
+ } catch {
124
+ zoneNames.value = [];
125
+ }
126
+ },
127
+ );
128
+
129
+ async function saveRule() {
130
+ formProblem.value = "";
131
+ if (!form.engine || !form.camera) formProblem.value = "Pick the engine and camera.";
132
+ else if (!form.zoneA || !form.zoneB) formProblem.value = "Pick both zone A and zone B.";
133
+ else if (form.zoneA === form.zoneB) formProblem.value = "Zone A and zone B must be different zones.";
134
+ else if (!(form.maxGapSeconds >= 1)) formProblem.value = "Max seconds must be 1 or more.";
135
+ else if (!form.labels.length) formProblem.value = "Add at least one thing to watch for, e.g. car.";
136
+ if (formProblem.value) return;
137
+ saving.value = true;
138
+ formError.value = null;
139
+ const body = {
140
+ site: props.site, engine: form.engine, camera: form.camera, name: form.name.trim(),
141
+ zoneA: form.zoneA, zoneB: form.zoneB, labels: form.labels.map((l: string) => l.trim().toLowerCase()).filter(Boolean),
142
+ maxGapSeconds: Math.round(form.maxGapSeconds), enabled: form.enabled,
143
+ };
144
+ try {
145
+ if (editing.value) await updateWrongWayRule(props.site, form._id, body);
146
+ else await createWrongWayRule(body);
147
+ formOpen.value = false;
148
+ await load();
149
+ } catch (e) {
150
+ formError.value = e;
151
+ } finally {
152
+ saving.value = false;
153
+ }
154
+ }
155
+
156
+ const deleteOpen = ref(false);
157
+ let target: any = null;
158
+ function askDelete(r: any) {
159
+ target = r;
160
+ deleteOpen.value = true;
161
+ }
162
+ async function doDelete() {
163
+ saving.value = true;
164
+ try {
165
+ await deleteWrongWayRule(props.site, target._id);
166
+ deleteOpen.value = false;
167
+ await load();
168
+ } catch (e) {
169
+ error.value = e;
170
+ deleteOpen.value = false;
171
+ } finally {
172
+ saving.value = false;
173
+ }
174
+ }
175
+
176
+ load();
177
+ </script>