feat(cctv): add Quebec, Ottawa, and World feeds

This commit is contained in:
InoukT committed 2026-11-02 22:57:22 -05:00
1 parent 72ceeb2fce
commit fa4ce4b2a5
59 files changed
+40815 -88

No files matched your search

+1
View File
@@ -1139,6 +1139,7 @@ export class StyleManager extends ShellFacade {
_cctvSummary: this._cctvSummary,
},
cctv: cctvLayer,
viewer: this.viewer,
actions: {
isEnabled: () => this._dataManager?.isEnabled('cctv'),
setParams: (params, options) =>
+5
View File
@@ -23,6 +23,7 @@ import { useCctvStore } from './stores/cctv.js';
import { useStatusStore } from './stores/status.js';
import { applicationPinia } from './vueRuntime.js';
import { onLocaleChange } from '../i18n/index.js';
import { createQuebecCctvOverlay } from './quebecCctvOverlay.js';
/**
* Own camera-panel interaction and presentation; receive the camera port and
@@ -34,6 +35,7 @@ export class CctvControls {
constructor({
elements,
cctv,
viewer = null,
actions,
store = useCctvStore(applicationPinia()),
statusStore = useStatusStore(applicationPinia()),
@@ -42,11 +44,13 @@ export class CctvControls {
this.store = store;
this.statusStore = statusStore;
this.cctv = cctv;
this._quebecCctvOverlay = createQuebecCctvOverlay(viewer);
this.actions = actions;
this.destroyed = false;
this.listeners = new AbortController();
this._cctvUnsubscribe = null;
this._cctvState = null;
this._lastQuebecSelectionRevision = null;
this._cctvSummaryTypingTimer = null;
this._lastCctvSummaryText = '';
this._lastSeenCctvActiveId = null;
@@ -146,6 +150,7 @@ export class CctvControls {
if (this.destroyed) return;
this.destroyed = true;
this._offLocale();
this._quebecCctvOverlay?.dispose();
this._cctvVideoSurface?.stop();
this._videoVisibilityObserver?.disconnect();
this._actionGeneration++;
+19 -3
View File
@@ -154,6 +154,20 @@ export function _renderCctvState(state) {
const enabled = !!state?.enabled && !!this.actions.isEnabled();
const activeId = state?.activeCameraId || '';
const activeCamera = state?.activeCamera || null;
const selectionRevision = Number(state?.activeCameraReselection);
const reselected =
this._lastQuebecSelectionRevision !== null &&
Number.isFinite(selectionRevision) &&
selectionRevision !== this._lastQuebecSelectionRevision;
this._lastQuebecSelectionRevision = Number.isFinite(selectionRevision)
? selectionRevision
: null;
const isQuebecCamera =
enabled && activeCamera?.sourceKind?.startsWith('quebec-');
this._quebecCctvOverlay?.setCamera(isQuebecCamera ? activeCamera : null, {
reselected,
});
if (this._cctvFrameWrap) this._cctvFrameWrap.hidden = Boolean(isQuebecCamera);
// Auto-expand the panel when the active camera CHANGES to a new non-null
// id while the layer is enabled. Covers click-on-globe, panel controls,
@@ -241,7 +255,8 @@ export function _renderCctvState(state) {
this.store.publish(presentCctvPanel(state, enabled));
const liveIntent = enabled && !!activeCamera?.isVideo;
const liveIntent =
enabled && !!activeCamera?.isVideo && !activeCamera?.browserMediaUrl;
this.store.setLive(liveIntent);
if (this._cctvVideo) {
const visible =
@@ -261,8 +276,9 @@ export function _renderCctvState(state) {
}
if (this._cctvFrame && !liveIntent) {
const nextSrc = enabled ? activeCamera?.frameUrl : null;
const nextCameraId = enabled ? activeCamera?.id || '' : '';
const nextSrc = enabled && !isQuebecCamera ? activeCamera?.frameUrl : null;
const nextCameraId =
enabled && !isQuebecCamera ? activeCamera?.id || '' : '';
const cameraChanged = this._frame.cameraId !== nextCameraId;
const frameLoading = this._frame.loading;
// A same-camera refresh waits for the current image to settle. Replacing
+389
View File
@@ -0,0 +1,389 @@
import * as Cesium from 'cesium';
import { horizonOccluder } from '../data/iconOrientation.js';
import { t } from '../i18n/index.js';
function isQuebecCamera(camera) {
return String(camera?.sourceKind || '').startsWith('quebec-');
}
function safeSourceUrl(value) {
try {
const url = new URL(String(value || ''));
return url.protocol === 'https:' && !url.username && !url.password
? url.href
: '';
} catch {
return '';
}
}
function safeMediaUrl(value) {
try {
const url = new URL(String(value || ''));
if (
url.protocol !== 'https:' ||
url.hostname !== 'www.quebec511.info' ||
url.username ||
url.password ||
url.pathname !== '/Carte/Fenetres/camera.ashx' ||
url.searchParams.get('format') !== 'mp4' ||
!/^\d+$/.test(url.searchParams.get('id') || '')
)
return '';
return url.href;
} catch {
return '';
}
}
function setMedia(video, url) {
const current = video.currentSrc || video.src;
if (current === url) return;
video.pause();
video.removeAttribute('src');
if (!url) {
video.load();
return;
}
video.src = url;
video.load();
}
function mediaRefreshUrl(value) {
const url = new URL(value);
url.searchParams.set('_', String(Date.now()));
return url.href;
}
/** Attach the active Québec source to a floating browser player and map preview. */
export function createQuebecCctvOverlay(viewer) {
if (!viewer?.scene || typeof document === 'undefined') return null;
const controller = new AbortController();
const { signal } = controller;
const panel = document.createElement('section');
panel.className = 'qci-panel';
panel.hidden = true;
panel.setAttribute('role', 'dialog');
panel.setAttribute('aria-label', t('CCTV camera'));
const header = document.createElement('header');
header.className = 'qci-header';
const title = document.createElement('strong');
const close = document.createElement('button');
close.type = 'button';
close.textContent = '×';
close.setAttribute('aria-label', t('Close camera preview'));
header.append(title, close);
const coordinates = document.createElement('p');
coordinates.className = 'qci-coordinates';
const credit = document.createElement('p');
credit.className = 'qci-credit';
const live = document.createElement('video');
live.className = 'qci-live';
live.controls = true;
live.muted = true;
live.autoplay = true;
live.loop = true;
live.playsInline = true;
live.setAttribute('aria-label', t('CCTV camera'));
const actions = document.createElement('div');
actions.className = 'qci-actions';
const refresh = document.createElement('button');
refresh.type = 'button';
refresh.textContent = t('Refresh map still');
actions.append(refresh);
const note = document.createElement('p');
note.className = 'qci-note';
const sourceLink = document.createElement('a');
sourceLink.target = '_blank';
sourceLink.rel = 'noopener noreferrer';
sourceLink.textContent = t("Open this image on the provider's site");
panel.append(header, coordinates, credit, live, actions, note, sourceLink);
const marker = document.createElement('div');
marker.className = 'qci-marker';
marker.hidden = true;
const still = document.createElement('video');
still.className = 'qci-marker-still';
still.muted = true;
still.playsInline = true;
still.preload = 'auto';
still.setAttribute('aria-hidden', 'true');
const markerLabel = document.createElement('span');
markerLabel.className = 'qci-marker-label';
marker.append(still, markerLabel);
document.body.append(panel, marker);
let selected = null;
let selectedPosition = null;
let closedWindowCameraId = null;
let selectedMediaUrl = '';
let mediaRefreshTimer = 0;
let liveUnavailable = false;
let stillUnavailable = false;
function markerLabelText(camera, hasMedia) {
return hasMedia
? `${t('PREVIEW')} · ${camera.positionSource || camera.name}`
: Number.isFinite(camera.accuracyM) && camera.accuracyM > 0
? `${camera.name} · ±${Math.round(camera.accuracyM)} m`
: camera.name;
}
function markLiveUnavailable() {
liveUnavailable = true;
note.textContent = t('Source unavailable');
note.hidden = false;
}
function markStillUnavailable() {
stillUnavailable = true;
markerLabel.textContent = t('Source unavailable');
}
function stopMediaRefresh() {
clearInterval(mediaRefreshTimer);
mediaRefreshTimer = 0;
}
function startMediaRefresh() {
stopMediaRefresh();
if (!selectedMediaUrl) return;
mediaRefreshTimer = window.setInterval(
() => {
if (document.hidden || closedWindowCameraId === selected?.id) return;
setMedia(live, mediaRefreshUrl(selectedMediaUrl));
void live.play().catch(markLiveUnavailable);
refreshStill();
},
2 * 60 * 1000,
);
}
function refreshStill() {
if (!selectedMediaUrl) return;
setMedia(still, '');
setMedia(still, mediaRefreshUrl(selectedMediaUrl));
void still.play().catch(markStillUnavailable);
}
function setCamera(camera, { reselected = false } = {}) {
if (!isQuebecCamera(camera)) {
selected = null;
selectedPosition = null;
closedWindowCameraId = null;
selectedMediaUrl = '';
liveUnavailable = false;
stillUnavailable = false;
stopMediaRefresh();
panel.hidden = true;
marker.hidden = true;
setMedia(live, '');
setMedia(still, '');
return;
}
const changed = selected?.id !== camera.id;
if (changed || reselected) closedWindowCameraId = null;
selected = camera;
selectedPosition = Cesium.Cartesian3.fromDegrees(
Number(camera.lon),
Number(camera.lat),
Number(camera.elevationM) || 10,
);
if (closedWindowCameraId === camera.id && !reselected) {
panel.hidden = true;
updateMarker();
return;
}
const mediaUrl = safeMediaUrl(camera.browserMediaUrl);
const sourceUrl = safeSourceUrl(camera.sourceUrl);
if (changed || mediaUrl !== selectedMediaUrl) {
liveUnavailable = false;
stillUnavailable = false;
}
title.textContent = camera.name || t('CCTV camera');
coordinates.textContent = [
`${Number(camera.lat).toFixed(5)}, ${Number(camera.lon).toFixed(5)}`,
Number.isFinite(camera.accuracyM) && camera.accuracyM > 0
? `±${Math.round(camera.accuracyM)} m`
: '',
camera.positionSource || '',
]
.filter(Boolean)
.join(' · ');
credit.textContent = camera.credit || camera.provider || '';
credit.hidden = !credit.textContent;
live.hidden = !mediaUrl;
refresh.hidden = !mediaUrl;
actions.hidden = !mediaUrl;
note.hidden = !mediaUrl;
note.textContent = mediaUrl
? liveUnavailable
? t('Source unavailable')
: t(
'The map still uses a separate request and may show a different moment.',
)
: sourceUrl
? ''
: t('Source unavailable');
note.hidden = !note.textContent;
sourceLink.href = sourceUrl || '#';
sourceLink.hidden = !sourceUrl;
markerLabel.textContent = stillUnavailable
? t('Source unavailable')
: markerLabelText(camera, mediaUrl);
marker.classList.toggle('qci-marker-link-only', !mediaUrl);
still.hidden = !mediaUrl;
panel.hidden = false;
if (changed || mediaUrl !== selectedMediaUrl) {
selectedMediaUrl = mediaUrl;
setMedia(live, mediaUrl);
setMedia(still, mediaUrl);
if (mediaUrl) {
void live.play().catch(markLiveUnavailable);
void still.play().catch(markStillUnavailable);
startMediaRefresh();
} else {
stopMediaRefresh();
}
} else if (reselected && mediaUrl) {
if (live.error || live.networkState === 3 || live.readyState < 2) {
setMedia(live, mediaRefreshUrl(mediaUrl));
}
if (live.paused) void live.play().catch(markLiveUnavailable);
startMediaRefresh();
if (still.error || still.networkState === 3 || still.readyState < 2)
refreshStill();
}
updateMarker();
}
function updateMarker() {
if (!selected || !selectedPosition) {
marker.hidden = true;
return;
}
const canvas = viewer.scene.canvas;
const point = viewer.scene.cartesianToCanvasCoordinates(selectedPosition);
const visible =
point &&
horizonOccluder(viewer.camera).isPointVisible(selectedPosition) &&
point.x >= 0 &&
point.y >= 0 &&
point.x <= canvas.clientWidth &&
point.y <= canvas.clientHeight;
marker.hidden = !visible;
if (!visible) return;
const rect = canvas.getBoundingClientRect();
marker.style.left = `${rect.left + point.x}px`;
marker.style.top = `${rect.top + point.y}px`;
const distance = Cesium.Cartesian3.distance(
viewer.camera.positionWC,
selectedPosition,
);
marker.style.setProperty(
'--qci-scale',
String(Math.min(1, 600 / Math.max(1, distance))),
);
}
still.addEventListener(
'loadeddata',
() => {
stillUnavailable = false;
if (selected)
markerLabel.textContent = markerLabelText(
selected,
Boolean(selectedMediaUrl),
);
still.pause();
},
{ signal },
);
still.addEventListener('error', markStillUnavailable, { signal });
live.addEventListener('error', markLiveUnavailable, { signal });
live.addEventListener(
'loadeddata',
() => {
liveUnavailable = false;
note.textContent = t(
'The map still uses a separate request and may show a different moment.',
);
note.hidden = false;
},
{ signal },
);
refresh.addEventListener('click', refreshStill, { signal });
close.addEventListener(
'click',
() => {
closedWindowCameraId = selected?.id || null;
panel.hidden = true;
live.pause();
stopMediaRefresh();
// The floating player closes independently; keep the paused map still.
updateMarker();
},
{ signal },
);
header.addEventListener(
'pointerdown',
(event) => {
if (event.target.closest('button')) return;
const rect = panel.getBoundingClientRect();
const offsetX = event.clientX - rect.left;
const offsetY = event.clientY - rect.top;
header.setPointerCapture(event.pointerId);
const move = (moveEvent) => {
panel.style.left = `${Math.max(0, Math.min(window.innerWidth - panel.offsetWidth, moveEvent.clientX - offsetX))}px`;
panel.style.top = `${Math.max(0, Math.min(window.innerHeight - panel.offsetHeight, moveEvent.clientY - offsetY))}px`;
panel.style.right = 'auto';
};
const stop = () => {
header.removeEventListener('pointermove', move);
header.removeEventListener('pointerup', stop);
header.removeEventListener('pointercancel', stop);
};
header.addEventListener('pointermove', move, { signal });
header.addEventListener('pointerup', stop, { once: true, signal });
header.addEventListener('pointercancel', stop, { once: true, signal });
},
{ signal },
);
document.addEventListener(
'visibilitychange',
() => {
if (!selectedMediaUrl) return;
if (document.hidden) {
live.pause();
still.pause();
} else if (closedWindowCameraId !== selected?.id) {
void live.play().catch(markLiveUnavailable);
refreshStill();
}
},
{ signal },
);
const removeRender = viewer.scene.postRender.addEventListener(updateMarker);
return {
setCamera,
dispose() {
controller.abort();
stopMediaRefresh();
removeRender();
setMedia(live, '');
setMedia(still, '');
panel.remove();
marker.remove();
selected = null;
selectedPosition = null;
},
};
}
+71
View File
@@ -0,0 +1,71 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { Window } from 'happy-dom';
import * as Cesium from 'cesium';
import { createQuebecCctvOverlay } from './quebecCctvOverlay.js';
test('routine same-camera updates keep Québec feed errors visible until recovery', async () => {
const window = new Window({ url: 'http://localhost:4173/' });
const previousWindow = globalThis.window;
const previousDocument = globalThis.document;
globalThis.window = window;
globalThis.document = window.document;
window.HTMLMediaElement.prototype.load = function () {};
window.HTMLMediaElement.prototype.play = function () {
return Promise.resolve();
};
window.HTMLMediaElement.prototype.pause = function () {};
window.setInterval = () => 0;
const viewer = {
camera: {
positionWC: Cesium.Cartesian3.fromDegrees(-73.5502, 45.51651, 1000),
},
scene: {
canvas: { clientWidth: 800, clientHeight: 600 },
cartesianToCanvasCoordinates: () => null,
postRender: { addEventListener: () => () => {} },
},
};
let overlay;
try {
overlay = createQuebecCctvOverlay(viewer);
const camera = {
id: 'qc-511-3735',
sourceKind: 'quebec-511-browser',
name: 'Route 136 at rue Panet',
lat: 45.51651,
lon: -73.5502,
positionSource: 'MTQ georeferenced point',
browserMediaUrl:
'https://www.quebec511.info/Carte/Fenetres/camera.ashx?id=3735&format=mp4',
sourceUrl:
'https://www.quebec511.info/Carte/Fenetres/FenetreVideo.html?id=3735',
};
overlay.setCamera(camera);
const [live, still] = window.document.querySelectorAll('video');
live.dispatchEvent(new window.Event('error'));
still.dispatchEvent(new window.Event('error'));
overlay.setCamera({ ...camera });
assert.equal(window.document.querySelector('.qci-note').textContent, 'Source unavailable');
assert.equal(window.document.querySelector('.qci-marker-label').textContent, 'Source unavailable');
live.dispatchEvent(new window.Event('loadeddata'));
still.dispatchEvent(new window.Event('loadeddata'));
assert.match(
window.document.querySelector('.qci-note').textContent,
/separate request/,
);
assert.match(
window.document.querySelector('.qci-marker-label').textContent,
/^PREVIEW ·/,
);
} finally {
overlay?.dispose();
globalThis.window = previousWindow;
globalThis.document = previousDocument;
window.close();
}
});
+98
View File
@@ -0,0 +1,98 @@
.qci-panel {
position: fixed;
z-index: 2000;
top: 80px;
right: 24px;
width: min(360px, calc(100vw - 32px));
overflow: hidden;
border: 1px solid #3eabbc;
border-radius: 8px;
background: #071117;
color: #e6f8fb;
box-shadow: 0 12px 36px #000a;
font: 12px/1.4 sans-serif;
}
.qci-panel[hidden],
.qci-live[hidden],
.qci-note[hidden],
.qci-panel a[hidden],
.qci-marker[hidden],
.qci-marker-still[hidden] {
display: none;
}
.qci-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 10px;
cursor: move;
touch-action: none;
}
.qci-panel button {
border: 1px solid #43818d;
border-radius: 4px;
padding: 4px 8px;
background: #102830;
color: inherit;
cursor: pointer;
}
.qci-panel button:focus-visible {
outline: 2px solid #82e9f7;
outline-offset: 2px;
}
.qci-coordinates,
.qci-credit,
.qci-note {
margin: 0 10px 8px;
color: #a9c5ca;
font: 10px/1.45 'Cascadia Code', Consolas, monospace;
}
.qci-credit {
color: #7fc7d2;
}
.qci-live {
display: block;
width: 100%;
aspect-ratio: 16 / 9;
background: #000;
}
.qci-actions {
display: flex;
gap: 8px;
padding: 8px 10px 0;
}
.qci-actions[hidden] {
display: none;
}
.qci-panel a {
display: inline-block;
margin: 0 10px 10px;
color: #82e9f7;
}
.qci-marker {
position: fixed;
z-index: 500;
width: 160px;
transform: translate(-50%, -100%) scale(var(--qci-scale, 1));
transform-origin: 50% 100%;
pointer-events: none;
padding: 3px;
border: 1px solid #65d7e7;
background: #071117;
color: #d8f9ff;
font: 10px/1.3 sans-serif;
}
.qci-marker-still {
display: block;
width: 100%;
aspect-ratio: 16 / 9;
background: #000;
}
.qci-marker-label {
display: block;
padding: 3px;
}
.qci-marker-link-only {
width: max-content;
max-width: 220px;
}