2025-09-30 15:32:00 +01:00
|
|
|
import switchSound from "../assets/sounds/ui/switch.mp3";
|
|
|
|
|
import popup from "../assets/sounds/ui/popup_open.mp3";
|
|
|
|
|
import notification from "../assets/sounds/ui/notification.mp3";
|
2025-10-17 10:17:01 +01:00
|
|
|
import beep from "../assets/sounds/ui/Beep.wav";
|
|
|
|
|
import warning from "../assets/sounds/ui/Warning.wav";
|
|
|
|
|
import ding from "../assets/sounds/ui/Ding.wav";
|
|
|
|
|
import shutter from "../assets/sounds/ui/shutter.mp3";
|
2025-10-27 14:00:28 +00:00
|
|
|
import attention from "../assets/sounds/ui/Attention.wav";
|
2025-10-15 12:24:28 +01:00
|
|
|
import type { HotlistMatches, SightingType } from "../types/types";
|
2025-09-30 15:32:00 +01:00
|
|
|
|
2025-10-01 15:21:07 +01:00
|
|
|
export function getSoundFileURL(name: string) {
|
2025-09-30 15:32:00 +01:00
|
|
|
const sounds: Record<string, string> = {
|
|
|
|
|
switch: switchSound,
|
|
|
|
|
popup: popup,
|
|
|
|
|
notification: notification,
|
2025-10-17 10:17:01 +01:00
|
|
|
beep: beep,
|
|
|
|
|
warning: warning,
|
|
|
|
|
ding: ding,
|
|
|
|
|
shutter: shutter,
|
2025-10-27 14:00:28 +00:00
|
|
|
attention: attention,
|
2025-09-30 15:32:00 +01:00
|
|
|
};
|
|
|
|
|
return sounds[name] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 16:17:37 +01:00
|
|
|
export const showSoundURL = (url: URL | string | undefined) => {
|
|
|
|
|
console.log(url);
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-13 14:23:48 +01:00
|
|
|
const randomChars = () => {
|
|
|
|
|
const uppercaseAsciiStart = 65;
|
|
|
|
|
const letterIndex = Math.floor(Math.random() * 26);
|
|
|
|
|
const letter = String.fromCharCode(uppercaseAsciiStart + letterIndex);
|
|
|
|
|
|
|
|
|
|
return letter;
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-07 11:37:37 +01:00
|
|
|
export function cleanArray(str: string) {
|
|
|
|
|
const toArr = str?.split(",");
|
|
|
|
|
|
|
|
|
|
const cleaned = toArr?.map((el: string) => {
|
|
|
|
|
const test = el.replace(/[^0-9a-z]/gi, "");
|
|
|
|
|
|
|
|
|
|
return test;
|
|
|
|
|
});
|
|
|
|
|
return cleaned;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 13:08:21 +01:00
|
|
|
export function parseRTSPUrl(url: string) {
|
|
|
|
|
const regex = /rtsp:\/\/([^:]+):([^@]+)@([^:/]+):?(\d+)?(\/.*)?/;
|
2025-10-06 14:21:56 +01:00
|
|
|
const match = url?.match(regex);
|
2025-10-03 13:08:21 +01:00
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
username: match[1],
|
|
|
|
|
password: match[2],
|
|
|
|
|
ip: match[3],
|
|
|
|
|
port: match[4] || "554", // default RTSP port
|
|
|
|
|
path: match[5] || "/",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 14:23:48 +01:00
|
|
|
const generateNumberPlate = () => {
|
|
|
|
|
const numberPlateLetters = new Array(4);
|
|
|
|
|
const characters: string[] = [];
|
|
|
|
|
for (let index = 0; index <= numberPlateLetters.length; index++) {
|
|
|
|
|
const letter = randomChars();
|
|
|
|
|
characters.push(letter);
|
|
|
|
|
}
|
|
|
|
|
const number = Math.floor(Math.random() * 99);
|
|
|
|
|
characters.splice(2, 0, number + " ");
|
|
|
|
|
const numberPlate = characters.join("");
|
|
|
|
|
|
|
|
|
|
return numberPlate;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const generateNumberPlateList = (numberofSightings = 5) => {
|
|
|
|
|
const numberPlates = [];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i <= numberofSightings; i++) {
|
|
|
|
|
numberPlates.push(generateNumberPlate());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return numberPlates;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const formatNumberPlate = (plate: string) => {
|
|
|
|
|
const splittedPlate = plate?.split("");
|
|
|
|
|
splittedPlate?.splice(4, 0, " ");
|
|
|
|
|
const formattedPlate = splittedPlate?.join("");
|
|
|
|
|
return formattedPlate;
|
|
|
|
|
};
|
2025-08-20 08:27:05 +01:00
|
|
|
|
2025-10-15 12:24:28 +01:00
|
|
|
export const BLANK_IMG = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";
|
2025-08-20 08:27:05 +01:00
|
|
|
|
|
|
|
|
export function capitalize(s?: string) {
|
|
|
|
|
return s ? s.charAt(0).toUpperCase() + s.slice(1) : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatAge(tsMillis: number) {
|
|
|
|
|
const ms = Date.now() - tsMillis;
|
|
|
|
|
const seconds = 5 * Math.floor(ms / 5000); // quantize to 5s like the original
|
|
|
|
|
const d = Math.floor(seconds / (3600 * 24));
|
|
|
|
|
const h = Math.floor((seconds % (3600 * 24)) / 3600);
|
|
|
|
|
const m = Math.floor((seconds % 3600) / 60);
|
|
|
|
|
const s = Math.floor(seconds % 60);
|
|
|
|
|
if (d > 0) return `${d}d ago`;
|
|
|
|
|
if (h > 0) return `${h}h ago`;
|
|
|
|
|
if (m > 0) return `${m}m ago`;
|
|
|
|
|
return `${s}s ago`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function drawRects(
|
|
|
|
|
canvas: HTMLCanvasElement,
|
|
|
|
|
imageEl: HTMLImageElement,
|
|
|
|
|
rects: [number, number, number, number][],
|
|
|
|
|
color: string
|
|
|
|
|
) {
|
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
|
if (!ctx) return;
|
|
|
|
|
// Ensure canvas size matches displayed image size
|
|
|
|
|
const w = imageEl.clientWidth || imageEl.naturalWidth;
|
|
|
|
|
const h = imageEl.clientHeight || imageEl.naturalHeight;
|
|
|
|
|
if (canvas.width !== w) canvas.width = w;
|
|
|
|
|
if (canvas.height !== h) canvas.height = h;
|
|
|
|
|
|
|
|
|
|
ctx.imageSmoothingEnabled = false;
|
|
|
|
|
ctx.lineWidth = 1;
|
|
|
|
|
ctx.strokeStyle = color;
|
|
|
|
|
|
|
|
|
|
rects.forEach((r) => {
|
|
|
|
|
const [x, y, rw, rh] = r;
|
|
|
|
|
ctx.beginPath();
|
2025-10-15 12:24:28 +01:00
|
|
|
ctx.rect(Math.round(x * w), Math.round(y * h), Math.round(rw * w), Math.round(rh * h));
|
2025-08-20 08:27:05 +01:00
|
|
|
ctx.stroke();
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-22 10:38:28 +01:00
|
|
|
|
2025-10-09 14:11:58 +01:00
|
|
|
export const checkIsHotListHit = (sigthing: SightingType | null) => {
|
|
|
|
|
if (!sigthing) return;
|
|
|
|
|
if (sigthing?.metadata?.hotlistMatches) {
|
2025-10-15 12:24:28 +01:00
|
|
|
const isHotListHit = Object.values(sigthing?.metadata?.hotlistMatches).includes(true);
|
2025-10-13 11:48:10 +01:00
|
|
|
|
2025-10-09 14:11:58 +01:00
|
|
|
return isHotListHit;
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-13 16:18:59 +01:00
|
|
|
|
2025-10-15 12:24:28 +01:00
|
|
|
export function getHotlistName(obj: HotlistMatches | undefined) {
|
2025-10-24 10:49:04 +01:00
|
|
|
if (!obj) return;
|
2025-10-15 12:24:28 +01:00
|
|
|
|
2025-10-24 10:49:04 +01:00
|
|
|
const hotlistNames = Object.entries(obj)
|
|
|
|
|
.filter(([, value]) => value === true)
|
|
|
|
|
.map(([key]) => key);
|
|
|
|
|
return hotlistNames;
|
2025-10-15 12:24:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-10-14 08:50:06 +01:00
|
|
|
export const getNPEDCategory = (r?: SightingType | null) =>
|
2025-10-27 14:00:28 +00:00
|
|
|
r?.metadata?.npedJSON?.["NPED CATEGORY"] as "A" | "B" | "C" | "D" | undefined;
|
2025-11-04 13:38:06 +00:00
|
|
|
|
|
|
|
|
export const zoomMapping = (zoomLevel: number) => {
|
|
|
|
|
switch (zoomLevel) {
|
|
|
|
|
case 1:
|
|
|
|
|
return "Far";
|
|
|
|
|
case 2:
|
|
|
|
|
return "Medium";
|
|
|
|
|
case 4:
|
|
|
|
|
return "Close";
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|