mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Merge main through #1637 into the renderer seam
Ports the cockpit bridge view, the orbital view riding a satellite with ground-site focus, satellite classes and draw-mark row actions onto the renderer contract. The orbital view's imagery look-ahead pre-load is dropped (no contract member); tiles load as they come on screen.
This commit is contained in:
commit
e48ed3eb9e
169 files changed
+22415
-681
No files matched your search
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* The selected vessel as a Cockpit bridge subject: identity, motion, its
|
||||
* displayed type and a representative bridge eye height from the AIS type.
|
||||
* The Cockpit owns camera placement; this module owns what AIS says.
|
||||
*/
|
||||
import { normalizeVesselType } from '../../data/vesselTypes.js';
|
||||
import { displayVesselType } from '../../data/vesselLabels.js';
|
||||
|
||||
/** The layer id a vessel subject carries through Cockpit events and state. */
|
||||
export const VESSEL_COCKPIT_LAYER_ID = 'ais-live-vessels';
|
||||
|
||||
/** Knots to metres per second, as the AIS source uses. */
|
||||
const KNOTS_TO_MPS = 0.514444;
|
||||
|
||||
/**
|
||||
* Bridge eye height above the waterline by normalized AIS type family. AIS
|
||||
* reports no hull height, so these are representative values: deep-sea
|
||||
* cargo and tanker bridges, passenger and cruise ship bridges, harbour
|
||||
* craft, small craft. River and harbour ferries, matched by name in the
|
||||
* patterns below, sit low. Text types from non-numeric feeds fall through
|
||||
* to the patterns.
|
||||
*/
|
||||
const EYE_HEIGHT_BY_FAMILY = Object.freeze({
|
||||
CARGO: 30,
|
||||
TANKER: 30,
|
||||
PASSENGER: 25,
|
||||
MILITARY: 18,
|
||||
'HIGH-SPEED': 8,
|
||||
DREDGER: 10,
|
||||
TOWING: 7,
|
||||
TUG: 7,
|
||||
PILOT: 6,
|
||||
SAR: 6,
|
||||
'PORT TENDER': 6,
|
||||
'ANTI-POLLUTION': 7,
|
||||
'LAW ENFORCE': 7,
|
||||
MEDICAL: 7,
|
||||
'DIVE OPS': 5,
|
||||
FISHING: 6,
|
||||
SAILING: 3,
|
||||
PLEASURE: 3,
|
||||
// AIS "Other" (90-99) is mostly small craft such as river boats. Only
|
||||
// that type takes it (see isAisOther).
|
||||
OTHER: 4,
|
||||
});
|
||||
|
||||
const EYE_HEIGHT_PATTERNS = Object.freeze([
|
||||
{ pattern: /tanker|cargo|container|bulk|carrier/i, heightM: 30 },
|
||||
{ pattern: /passenger|cruise|ro-?ro/i, heightM: 25 },
|
||||
// River and harbour ferries: a low wheelhouse just above the water.
|
||||
{ pattern: /ferry/i, heightM: 4 },
|
||||
{ pattern: /tug|tow|pilot|supply|service/i, heightM: 7 },
|
||||
{ pattern: /fishing|trawler/i, heightM: 6 },
|
||||
{ pattern: /sail|pleasure|yacht/i, heightM: 3 },
|
||||
]);
|
||||
|
||||
/**
|
||||
* Whether a raw type is AIS "Other" proper: codes 90 to 99, or a feed's text
|
||||
* type. normalizeVesselType also files the reserved, wing-in-ground and
|
||||
* spare codes (1-29, 38-39, 56-57, 59) under OTHER, but they name no family.
|
||||
*/
|
||||
function isAisOther(type) {
|
||||
const text = String(type ?? '').trim();
|
||||
if (!/^\d+$/.test(text)) return true;
|
||||
const code = Number(text);
|
||||
return code >= 90 && code <= 99;
|
||||
}
|
||||
|
||||
/**
|
||||
* Representative bridge eye height for an AIS vessel type.
|
||||
* @param {string|number} type Raw AIS type: a numeric code or feed text.
|
||||
* @returns {number|null} Metres above the waterline, or null when the type
|
||||
* names no known family (the Cockpit then uses its default).
|
||||
*/
|
||||
export function vesselEyeHeightM(type) {
|
||||
const family = normalizeVesselType(type).toUpperCase();
|
||||
if (family === 'OTHER' && !isAisOther(type)) return null;
|
||||
if (Object.hasOwn(EYE_HEIGHT_BY_FAMILY, family))
|
||||
return EYE_HEIGHT_BY_FAMILY[family];
|
||||
const matched = EYE_HEIGHT_PATTERNS.find((entry) =>
|
||||
entry.pattern.test(family),
|
||||
);
|
||||
return matched ? matched.heightM : null;
|
||||
}
|
||||
|
||||
function finiteOrNull(value) {
|
||||
if (value === null || value === undefined || value === '') return null;
|
||||
const number = Number(value);
|
||||
return Number.isFinite(number) ? number : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a selected vessel record into Cockpit subject info.
|
||||
*
|
||||
* `track` is where the camera looks and `motionCourse` is where the hull
|
||||
* moves: a ship faces its true heading while current and wind set it along
|
||||
* its course over ground. Either falls back to the other when one is missing.
|
||||
* @param {object|null} record Vessel record from the vessel layer.
|
||||
* @returns {object|null} Subject info, or null without a usable fix.
|
||||
*/
|
||||
export function vesselCockpitInfo(record) {
|
||||
if (!record?.mmsi) return null;
|
||||
const latitude = Number(record.lat);
|
||||
const longitude = Number(record.lon);
|
||||
if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) return null;
|
||||
const heading = finiteOrNull(record.heading);
|
||||
const course = finiteOrNull(record.course);
|
||||
const speedKt = finiteOrNull(record.speed);
|
||||
const epochSec = finiteOrNull(record.lastPositionEpoch);
|
||||
const feedEpochSec = finiteOrNull(record.fixFeedEpoch);
|
||||
return {
|
||||
kind: 'vessel',
|
||||
layerId: VESSEL_COCKPIT_LAYER_ID,
|
||||
id: String(record.mmsi),
|
||||
mmsi: String(record.mmsi),
|
||||
name: String(record.name || record.mmsi),
|
||||
type: String(record.type || ''),
|
||||
typeLabel: displayVesselType(record.type),
|
||||
destination: String(record.destination || '').trim(),
|
||||
latitude,
|
||||
longitude,
|
||||
heading,
|
||||
course,
|
||||
speedKt,
|
||||
velocityMps: speedKt === null ? null : Math.max(0, speedKt) * KNOTS_TO_MPS,
|
||||
track: heading ?? course,
|
||||
// What `track` is: the true heading, or course over ground standing in
|
||||
// for a ship that reports none. The HUD says which.
|
||||
trackSource:
|
||||
heading !== null ? 'heading' : course !== null ? 'course' : null,
|
||||
motionCourse: course ?? heading,
|
||||
fixEpochMs: epochSec === null ? null : epochSec * 1000,
|
||||
// When this client first received the fix (its own clock) and the
|
||||
// newest fix in that refresh (the feed's clock): the fix's age without
|
||||
// clock skew, still ageing while the feed re-serves it.
|
||||
receivedAtMs: finiteOrNull(record.fixReceivedAtMs),
|
||||
feedEpochMs: feedEpochSec === null ? null : feedEpochSec * 1000,
|
||||
eyeHeightM: vesselEyeHeightM(record.type),
|
||||
stale: (record.missedRefreshes || 0) > 0,
|
||||
onGround: false,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// src/layers/vessels/cockpitSubject.test.mjs — bridge eye heights by AIS
|
||||
// type and the selected vessel as Cockpit subject info.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
VESSEL_COCKPIT_LAYER_ID,
|
||||
vesselCockpitInfo,
|
||||
vesselEyeHeightM,
|
||||
} from './cockpitSubject.js';
|
||||
|
||||
test('bridge eye height follows the AIS type family', () => {
|
||||
assert.equal(vesselEyeHeightM('71'), 30, 'cargo');
|
||||
assert.equal(vesselEyeHeightM('84'), 30, 'tanker');
|
||||
assert.equal(vesselEyeHeightM('60'), 25, 'passenger');
|
||||
assert.equal(vesselEyeHeightM('52'), 7, 'tug');
|
||||
assert.equal(vesselEyeHeightM('30'), 6, 'fishing');
|
||||
assert.equal(vesselEyeHeightM('37'), 3, 'pleasure');
|
||||
assert.equal(vesselEyeHeightM('36'), 3, 'sailing');
|
||||
});
|
||||
|
||||
test('text vessel types resolve through the same families', () => {
|
||||
assert.equal(vesselEyeHeightM('Container Ship'), 30);
|
||||
assert.equal(vesselEyeHeightM('Cruise ship'), 25);
|
||||
assert.equal(vesselEyeHeightM('Ferry'), 4);
|
||||
assert.equal(vesselEyeHeightM(99), 4, 'AIS Other');
|
||||
assert.equal(vesselEyeHeightM('90'), 4, 'AIS Other');
|
||||
assert.equal(vesselEyeHeightM('Other'), 4, 'a feed text type Other');
|
||||
assert.equal(vesselEyeHeightM('Yacht'), 3);
|
||||
});
|
||||
|
||||
test('codes filed under OTHER that are not AIS Other keep the default', () => {
|
||||
// Reserved (1-19), wing in ground (20-29), reserved (38-39), spare
|
||||
// (56-57) and local (59) codes name no family.
|
||||
for (const type of ['1', '19', '20', '29', '38', '39', '56', '57', '59'])
|
||||
assert.equal(vesselEyeHeightM(type), null, type);
|
||||
});
|
||||
|
||||
test('an unknown or missing type has no eye height of its own', () => {
|
||||
for (const type of ['', '0', '255', 'Research vessel', null]) {
|
||||
assert.equal(vesselEyeHeightM(type), null, String(type));
|
||||
}
|
||||
});
|
||||
|
||||
test('vessel info faces the true heading and moves along course over ground', () => {
|
||||
const info = vesselCockpitInfo({
|
||||
mmsi: '211000000',
|
||||
name: 'NORDIC STAR',
|
||||
type: '70',
|
||||
destination: ' KIEL ',
|
||||
lat: 54.5,
|
||||
lon: 10.2,
|
||||
speed: 12,
|
||||
course: 95,
|
||||
heading: 90,
|
||||
lastPositionEpoch: 1_700_000_000,
|
||||
missedRefreshes: 0,
|
||||
});
|
||||
assert.equal(info.kind, 'vessel');
|
||||
assert.equal(info.layerId, VESSEL_COCKPIT_LAYER_ID);
|
||||
assert.equal(info.id, '211000000');
|
||||
assert.equal(info.typeLabel, 'CARGO');
|
||||
assert.equal(info.track, 90);
|
||||
assert.equal(info.motionCourse, 95);
|
||||
assert.equal(info.destination, 'KIEL');
|
||||
assert.equal(info.eyeHeightM, 30);
|
||||
assert.equal(info.fixEpochMs, 1_700_000_000_000);
|
||||
assert.ok(Math.abs(info.velocityMps - 12 * 0.514444) < 1e-9);
|
||||
assert.equal(info.stale, false);
|
||||
assert.equal(info.onGround, false);
|
||||
});
|
||||
|
||||
test('a missing heading or course falls back to the other', () => {
|
||||
const base = { mmsi: '1', lat: 1, lon: 1, speed: 3 };
|
||||
assert.equal(vesselCockpitInfo({ ...base, course: 40 }).track, 40);
|
||||
assert.equal(vesselCockpitInfo({ ...base, heading: 50 }).motionCourse, 50);
|
||||
const neither = vesselCockpitInfo(base);
|
||||
assert.equal(neither.track, null);
|
||||
assert.equal(neither.motionCourse, null);
|
||||
});
|
||||
|
||||
test('the subject says whether its track is the heading or the course', () => {
|
||||
const base = { mmsi: '1', lat: 1, lon: 1, speed: 3 };
|
||||
assert.equal(
|
||||
vesselCockpitInfo({ ...base, heading: 50, course: 40 }).trackSource,
|
||||
'heading',
|
||||
);
|
||||
assert.equal(
|
||||
vesselCockpitInfo({ ...base, course: 40 }).trackSource,
|
||||
'course',
|
||||
);
|
||||
// A heading of 0 is due north, not a missing heading.
|
||||
assert.equal(
|
||||
vesselCockpitInfo({ ...base, heading: 0 }).trackSource,
|
||||
'heading',
|
||||
);
|
||||
assert.equal(vesselCockpitInfo(base).trackSource, null);
|
||||
});
|
||||
|
||||
test('a vessel without identity or position is not a subject', () => {
|
||||
assert.equal(vesselCockpitInfo(null), null);
|
||||
assert.equal(vesselCockpitInfo({ lat: 1, lon: 1 }), null);
|
||||
assert.equal(vesselCockpitInfo({ mmsi: '1', lat: 'x', lon: 1 }), null);
|
||||
});
|
||||
|
||||
test('a vessel missing from recent refreshes reads as stale', () => {
|
||||
const info = vesselCockpitInfo({
|
||||
mmsi: '1',
|
||||
lat: 1,
|
||||
lon: 1,
|
||||
missedRefreshes: 2,
|
||||
});
|
||||
assert.equal(info.stale, true);
|
||||
});
|
||||
@@ -1,5 +1,8 @@
|
||||
import { distance } from '../../geo/vector.js';
|
||||
import { t, msg } from '../../i18n/index.js';
|
||||
import { vesselCockpitInfo } from './cockpitSubject.js';
|
||||
import { vesselTypeCss } from '../../data/vesselLabels.js';
|
||||
import { greatCircleMeters } from '../../geoDistance.js';
|
||||
|
||||
// ingestion.js is a portable export (package.json) and writes English labels
|
||||
// into state; translate the known ones here, pass anything else through.
|
||||
@@ -20,6 +23,7 @@ import {
|
||||
AIS_STATUS_REASON,
|
||||
REFRESH_MS,
|
||||
FOCUS_EVIDENCE_DEV,
|
||||
VESSEL_LIFT_M,
|
||||
} from './policy.js';
|
||||
|
||||
export function createQueries({
|
||||
@@ -343,6 +347,59 @@ export function createQueries({
|
||||
return entries.slice(0, cap);
|
||||
},
|
||||
|
||||
/**
|
||||
* Vessels around a point for the Cockpit bridge radar, nearest first.
|
||||
* The ship whose bridge the Cockpit occupies is left out. Distances are
|
||||
* great-circle from the AIS positions, so no rendered visual is needed.
|
||||
* @param {number} lat Latitude of the centre, degrees.
|
||||
* @param {number} lon Longitude of the centre, degrees.
|
||||
* @param {number} rangeM Max distance in metres.
|
||||
* @param {number} [maxCount=60] Maximum entries to return.
|
||||
* @returns {Array<{ id: string, name: string, latitude: number,
|
||||
* longitude: number, course: number|null, heading: number|null,
|
||||
* type: string, color: string, stale: boolean }>} Empty while the
|
||||
* layer is off. A ship with no MMSI has an `unkeyed:` id.
|
||||
*/
|
||||
getRadarContacts(lat, lon, rangeM, maxCount = 60) {
|
||||
const records = state.records.all;
|
||||
if (!state.feed.enabled || !Array.isArray(records) || !records.length)
|
||||
return [];
|
||||
if (!Number.isFinite(rangeM) || rangeM <= 0) return [];
|
||||
const cap =
|
||||
Number.isFinite(maxCount) && maxCount > 0 ? Math.floor(maxCount) : 60;
|
||||
const center = { lat, lon };
|
||||
const subject = state.cockpitSubjectMmsi;
|
||||
const finite = (value) => {
|
||||
if (value === null || value === undefined || value === '') return null;
|
||||
const number = Number(value);
|
||||
return Number.isFinite(number) ? number : null;
|
||||
};
|
||||
|
||||
const entries = [];
|
||||
records.forEach((record, index) => {
|
||||
if (subject && String(record.mmsi) === subject) return;
|
||||
const distanceM = greatCircleMeters(center, {
|
||||
lat: record.lat,
|
||||
lon: record.lon,
|
||||
});
|
||||
if (!(distanceM <= rangeM)) return;
|
||||
entries.push({ record, index, distanceM });
|
||||
});
|
||||
entries.sort((a, b) => a.distanceM - b.distanceM);
|
||||
return entries.slice(0, cap).map(({ record, index }) => ({
|
||||
// A ship with no MMSI still needs a key of its own on the scope.
|
||||
id: record.mmsi ? String(record.mmsi) : `unkeyed:${index}`,
|
||||
name: String(record.name || record.mmsi || ''),
|
||||
latitude: record.lat,
|
||||
longitude: record.lon,
|
||||
course: finite(record.course),
|
||||
heading: finite(record.heading),
|
||||
type: String(record.type || ''),
|
||||
color: vesselTypeCss(record.type),
|
||||
stale: (record.missedRefreshes || 0) > 0,
|
||||
}));
|
||||
},
|
||||
|
||||
/**
|
||||
* Get positions of all currently loaded vessels.
|
||||
* @param {number} [maxCount=800] Maximum entries to return.
|
||||
@@ -458,6 +515,55 @@ export function createQueries({
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Describe the selected vessel for the Cockpit bridge view.
|
||||
* @returns {object|null} `vesselCockpitInfo` fields plus `seaSurfaceHeightM`
|
||||
* (the local geoid height the sprite datum uses) and `markHeightM` (the
|
||||
* height the sprite is drawn at), or null when the layer is off or
|
||||
* nothing is selected.
|
||||
*/
|
||||
getCockpitSubject() {
|
||||
if (!state.feed.enabled) return null;
|
||||
const info = vesselCockpitInfo(state.selectedRecord);
|
||||
if (!info) return null;
|
||||
const geoidN = components.tracking.currentGeoidN(
|
||||
info.latitude,
|
||||
info.longitude,
|
||||
);
|
||||
return {
|
||||
...info,
|
||||
seaSurfaceHeightM: vesselDatumHeightM(geoidN, 0),
|
||||
markHeightM: vesselDatumHeightM(geoidN, VESSEL_LIFT_M),
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Mark the vessel whose bridge the Cockpit camera occupies, or release it.
|
||||
* Its sprite, card and detection bracket stay hidden while it is held.
|
||||
* @param {string|number|null} mmsi Subject MMSI, or null to release.
|
||||
* @returns {boolean} Always true.
|
||||
*/
|
||||
setCockpitSubject(mmsi) {
|
||||
const next = normalizeSelectionMmsi(mmsi);
|
||||
if (state.cockpitSubjectMmsi === next) return true;
|
||||
state.cockpitSubjectMmsi = next;
|
||||
// The ridden ship's trail runs from under the eye: hidden, its history
|
||||
// kept, while the bridge holds the ship it belongs to.
|
||||
state.trail?.setVisible?.(!next || next !== state.trailMmsi);
|
||||
components.rendering.updateVisibility(true);
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Return the camera to the selected vessel's map view, as a click on it does.
|
||||
* @returns {boolean} True when a selected vessel took the camera.
|
||||
*/
|
||||
focusSelectedVessel() {
|
||||
const record = state.selectedRecord;
|
||||
if (!record || !state.feed.enabled) return false;
|
||||
return components.selection.selectAndFocusVessel(record);
|
||||
},
|
||||
|
||||
/**
|
||||
* Return a subset of vessels for the universal detection overlay.
|
||||
* Deterministic stride sampling distributes selections evenly across the
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
// src/layers/vessels/radarContacts.test.mjs — the vessels the Cockpit bridge
|
||||
// radar draws: within range, nearest first, without the ship it rides; and
|
||||
// the ridden ship's own trail, hidden while the bridge holds it.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createVesselLayer } from './index.js';
|
||||
import { vesselTypeCss } from '../../data/vesselLabels.js';
|
||||
|
||||
const noop = () => {};
|
||||
function services() {
|
||||
return {
|
||||
context: {
|
||||
clearSelectedEntityContextForLayer: noop,
|
||||
registerEntityContext: () => null,
|
||||
},
|
||||
trails: {
|
||||
createTrail: () => ({ setPositions: noop, clear: noop, destroy: noop }),
|
||||
},
|
||||
labels: {},
|
||||
picking: { unregisterPickOwner: noop },
|
||||
overlay: {
|
||||
setOverlayEntries: noop,
|
||||
setOverlaySourceVisible: noop,
|
||||
clearOverlaySource: noop,
|
||||
},
|
||||
geoid: {},
|
||||
sprites: {},
|
||||
focus: {
|
||||
focusNowMs: (now) => now,
|
||||
getFocusTarget: () => null,
|
||||
focusPassIsNeeded: () => false,
|
||||
forgetSpriteFocus: noop,
|
||||
},
|
||||
worldFocus: {},
|
||||
render: {
|
||||
holdContinuousRender: noop,
|
||||
releaseContinuousRender: noop,
|
||||
governorRequestRender: noop,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** A vessel `northNm` nautical miles north of 54.5°N 10.2°E. */
|
||||
function record(mmsi, northNm, extra = {}) {
|
||||
return {
|
||||
mmsi,
|
||||
name: `SHIP ${mmsi}`,
|
||||
type: '70',
|
||||
lat: 54.5 + northNm / 60,
|
||||
lon: 10.2,
|
||||
speed: 10,
|
||||
course: 45,
|
||||
heading: 40,
|
||||
missedRefreshes: 0,
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
function layerWith(records, options = {}) {
|
||||
const layer = createVesselLayer({
|
||||
source: { getSnapshot: noop },
|
||||
services: services(),
|
||||
});
|
||||
layer.testing._setVesselStateForTest({
|
||||
viewer: {},
|
||||
records,
|
||||
billboardCollection: { add: (entry) => ({ ...entry }), remove: noop },
|
||||
...options,
|
||||
});
|
||||
return layer;
|
||||
}
|
||||
|
||||
test('radar contacts are within range, nearest first and capped', () => {
|
||||
const layer = layerWith([
|
||||
record('3', 5),
|
||||
record('1', 1),
|
||||
record('far', 20),
|
||||
record('2', 3, { course: null, heading: '', missedRefreshes: 2 }),
|
||||
]);
|
||||
const contacts = layer.getRadarContacts(54.5, 10.2, 6 * 1852, 60);
|
||||
assert.deepEqual(
|
||||
contacts.map((entry) => entry.id),
|
||||
['1', '2', '3'],
|
||||
);
|
||||
assert.deepEqual(contacts[0], {
|
||||
id: '1',
|
||||
name: 'SHIP 1',
|
||||
latitude: 54.5 + 1 / 60,
|
||||
longitude: 10.2,
|
||||
course: 45,
|
||||
heading: 40,
|
||||
type: '70',
|
||||
color: vesselTypeCss('70'),
|
||||
stale: false,
|
||||
});
|
||||
assert.equal(contacts[1].course, null);
|
||||
assert.equal(contacts[1].heading, null);
|
||||
assert.equal(contacts[1].stale, true);
|
||||
assert.deepEqual(
|
||||
layer.getRadarContacts(54.5, 10.2, 6 * 1852, 2).map((entry) => entry.id),
|
||||
['1', '2'],
|
||||
);
|
||||
});
|
||||
|
||||
test('the ship whose bridge the Cockpit rides is not its own contact', () => {
|
||||
const layer = layerWith([record('own', 0), record('other', 2)]);
|
||||
layer.setCockpitSubject('own');
|
||||
assert.deepEqual(
|
||||
layer.getRadarContacts(54.5, 10.2, 6 * 1852).map((entry) => entry.id),
|
||||
['other'],
|
||||
);
|
||||
layer.setCockpitSubject(null);
|
||||
assert.equal(layer.getRadarContacts(54.5, 10.2, 6 * 1852).length, 2);
|
||||
});
|
||||
|
||||
test('ships with no MMSI each keep a key of their own', () => {
|
||||
const layer = layerWith([
|
||||
record('', 1, { name: 'A' }),
|
||||
record('', 2, { name: 'B' }),
|
||||
]);
|
||||
const ids = layer.getRadarContacts(54.5, 10.2, 6 * 1852).map((c) => c.id);
|
||||
assert.equal(ids.length, 2);
|
||||
assert.equal(new Set(ids).size, 2, ids.join(', '));
|
||||
assert.ok(ids.every((id) => id.startsWith('unkeyed:')));
|
||||
});
|
||||
|
||||
test("the ridden ship's trail is hidden while the bridge holds it", () => {
|
||||
const shown = [];
|
||||
const trail = {
|
||||
setPositions() {},
|
||||
clear() {},
|
||||
destroy() {},
|
||||
setVisible: (visible) => shown.push(visible),
|
||||
};
|
||||
const layer = layerWith([record('own', 0), record('other', 2)], {
|
||||
trail,
|
||||
trailMmsi: 'own',
|
||||
});
|
||||
layer.setCockpitSubject('own');
|
||||
assert.deepEqual(shown, [false]);
|
||||
layer.setCockpitSubject(null);
|
||||
assert.deepEqual(shown, [false, true]);
|
||||
});
|
||||
|
||||
test('a disabled layer has no radar contacts', () => {
|
||||
const layer = layerWith([record('1', 1)], { enabled: false });
|
||||
assert.deepEqual(layer.getRadarContacts(54.5, 10.2, 6 * 1852), []);
|
||||
});
|
||||
@@ -26,6 +26,18 @@ export function normalizeVessel(row) {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Note when a fix was first received (`fixReceivedAtMs`, this client's
|
||||
* clock) and the newest fix in that refresh (`fixFeedEpoch`, the feed's
|
||||
* clock): its age is then read on each clock separately, so neither a
|
||||
* skewed client clock nor a re-served cached row hides how old it is.
|
||||
* `receivedAtMs` stays the time the row was last seen, for retention.
|
||||
*/
|
||||
function stampFix(record, receivedAtMs, feedLatestEpoch) {
|
||||
record.fixReceivedAtMs = receivedAtMs;
|
||||
record.fixFeedEpoch = feedLatestEpoch;
|
||||
}
|
||||
|
||||
function finiteNumber(value) {
|
||||
if (value === null || value === undefined || value === '') return null;
|
||||
const number = Number(value);
|
||||
@@ -46,6 +58,18 @@ export class VesselRecords {
|
||||
effects,
|
||||
) {
|
||||
const receivedAtMs = this.now();
|
||||
// The newest fix in the batch: the feed's own "now", so a fix's age can
|
||||
// be read on the feed's clock rather than against this client's (see
|
||||
// stampFix).
|
||||
let feedLatestEpoch = null;
|
||||
for (const row of rows) {
|
||||
const epoch = finiteNumber(row?.last_position_epoch);
|
||||
if (
|
||||
epoch !== null &&
|
||||
(feedLatestEpoch === null || epoch > feedLatestEpoch)
|
||||
)
|
||||
feedLatestEpoch = epoch;
|
||||
}
|
||||
for (const record of this.unkeyed) effects.remove(record, false);
|
||||
this.unkeyed = [];
|
||||
const seen = new Set();
|
||||
@@ -53,6 +77,7 @@ export class VesselRecords {
|
||||
const next = normalizeVessel(row);
|
||||
if (!next) continue;
|
||||
next.receivedAtMs = receivedAtMs;
|
||||
stampFix(next, receivedAtMs, feedLatestEpoch);
|
||||
if (!next.mmsi) {
|
||||
effects.add(next);
|
||||
this.unkeyed.push(next);
|
||||
@@ -62,6 +87,12 @@ export class VesselRecords {
|
||||
seen.add(next.mmsi);
|
||||
const record = this.byMmsi.get(next.mmsi);
|
||||
if (record) {
|
||||
// A feed re-serving a cached row repeats its fix: the fix keeps the
|
||||
// time it was first received, so it goes on ageing.
|
||||
if (next.lastPositionEpoch !== record.lastPositionEpoch) {
|
||||
record.fixReceivedAtMs = next.fixReceivedAtMs;
|
||||
record.fixFeedEpoch = next.fixFeedEpoch;
|
||||
}
|
||||
record.reference = next.reference;
|
||||
record.receivedAtMs = next.receivedAtMs;
|
||||
record.lat = next.lat;
|
||||
|
||||
@@ -117,3 +117,33 @@ test('unkeyed records rebuild on each refresh and numeric coercion keeps unknown
|
||||
assert.equal(probe.store.all.length, 1);
|
||||
assert.equal(normalizeVessel(row('bad', { lon: Infinity })), null);
|
||||
});
|
||||
|
||||
test("each fix keeps when it was first received and its refresh's newest fix", () => {
|
||||
const probe = setup();
|
||||
probe.reconcile([
|
||||
row('111', { last_position_epoch: 1_800_000_000 }),
|
||||
row('222', { last_position_epoch: 1_800_000_030 }),
|
||||
row('333'),
|
||||
]);
|
||||
for (const mmsi of ['111', '222', '333']) {
|
||||
const record = probe.store.byMmsi.get(mmsi);
|
||||
assert.equal(record.fixFeedEpoch, 1_800_000_030, mmsi);
|
||||
assert.equal(record.fixReceivedAtMs, 1000, mmsi);
|
||||
}
|
||||
// A new fix is stamped afresh.
|
||||
probe.advance(60_000);
|
||||
probe.reconcile([row('111', { last_position_epoch: 1_800_000_050 })]);
|
||||
const record = probe.store.byMmsi.get('111');
|
||||
assert.equal(record.fixFeedEpoch, 1_800_000_050);
|
||||
assert.equal(record.fixReceivedAtMs, 61_000);
|
||||
// The same fix re-served by later polls keeps its first receipt, while
|
||||
// the row itself counts as seen for retention.
|
||||
probe.advance(30_000);
|
||||
probe.reconcile([
|
||||
row('111', { last_position_epoch: 1_800_000_050 }),
|
||||
row('222', { last_position_epoch: 1_800_000_090 }),
|
||||
]);
|
||||
assert.equal(record.fixReceivedAtMs, 61_000);
|
||||
assert.equal(record.fixFeedEpoch, 1_800_000_050);
|
||||
assert.equal(record.receivedAtMs, 91_000);
|
||||
});
|
||||
@@ -152,12 +152,37 @@ export function createRendering({
|
||||
state.icons = null;
|
||||
}
|
||||
|
||||
/** Whether the Cockpit's bridge camera rides this record's ship. */
|
||||
function isCockpitSubject(record) {
|
||||
return (
|
||||
Boolean(state.cockpitSubjectMmsi) &&
|
||||
record.mmsi === state.cockpitSubjectMmsi
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redraw the icons of the ship the bridge camera rode and the one it now
|
||||
* rides: the camera sits on the ridden ship's icon, which would fill the
|
||||
* view, so it is hidden while held.
|
||||
*/
|
||||
function syncCockpitSubjectIcon() {
|
||||
const held = state.cockpitSubjectMmsi || null;
|
||||
if (vesselState._iconHiddenMmsi === held) return;
|
||||
const before = vesselState._iconHiddenMmsi;
|
||||
vesselState._iconHiddenMmsi = held;
|
||||
for (const mmsi of [before, held]) {
|
||||
const record = mmsi ? state.records.byMmsi.get(mmsi) : null;
|
||||
if (record) syncRecordIcon(record);
|
||||
}
|
||||
}
|
||||
|
||||
/** The icon item a record draws, as it now is. */
|
||||
function iconItem(record, visual) {
|
||||
const selected = record === state.selectedRecord;
|
||||
const courseDeg = vesselCourseDeg(record);
|
||||
return {
|
||||
id: visual.iconId,
|
||||
show: !isCockpitSubject(record),
|
||||
pos: visual.position,
|
||||
image: shipIcon(record, selected),
|
||||
size: [SHIP_ICON_PX, SHIP_ICON_PX],
|
||||
@@ -341,6 +366,7 @@ export function createRendering({
|
||||
}
|
||||
|
||||
const query = state.viewer ? findQuery(state.viewer) : null;
|
||||
syncCockpitSubjectIcon();
|
||||
if (regularPass) {
|
||||
// Candidate construction stays on the original 800 ms selector cadence.
|
||||
// The 80 ms focus-only pass below never allocates label candidates.
|
||||
@@ -466,7 +492,11 @@ export function createRendering({
|
||||
const viewer = state.viewer;
|
||||
const query = viewer ? findQuery(viewer) : null;
|
||||
const selected = state.selectedRecord;
|
||||
const entries = selected
|
||||
// From the bridge, the selected vessel is the viewer; its card would be
|
||||
// pinned to the camera position.
|
||||
const selectedCarded =
|
||||
selected && selected.mmsi !== state.cockpitSubjectMmsi;
|
||||
const entries = selectedCarded
|
||||
? [components.cards.buildSelectedVesselCard(selected)]
|
||||
: [];
|
||||
const maxLabels = labelRowLimit();
|
||||
@@ -515,7 +545,7 @@ export function createRendering({
|
||||
// Greedy min-separation pass over the priority-ranked cell winners: the
|
||||
// selected card's anchor seeds the accepted set so ambient cards keep clear.
|
||||
const accepted = [];
|
||||
if (selected) {
|
||||
if (selectedCarded) {
|
||||
const screen = query.project(iconPosition(selected));
|
||||
if (screen) accepted.push({ x: screen.x, y: screen.y });
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ export function createSelection({
|
||||
function bindVesselInteraction(viewer, keyTarget) {
|
||||
state.clickSubscription = inputFor(viewer).onClick((event) => {
|
||||
if (!state.feed.enabled) return;
|
||||
// The bridge view owns the selection: a click on open water must not
|
||||
// deselect the vessel the camera is riding.
|
||||
if (state.cockpitSubjectMmsi) return;
|
||||
const pickedId = event.id;
|
||||
let record = pickedId ? state.records.byMmsi.get(pickedId) : null;
|
||||
// A vessel icon's pick is an item of the layer's icon set, whose id is
|
||||
|
||||
@@ -46,6 +46,8 @@ export function createVesselState({ source, services }) {
|
||||
/** Row ids for unkeyed (no-MMSI) records, which are rebuilt every poll. */
|
||||
|
||||
vesselState._unkeyedIconSequence = 0;
|
||||
// The ship whose icon is hidden under the bridge camera (cockpit subject).
|
||||
vesselState._iconHiddenMmsi = null;
|
||||
|
||||
vesselState.state = {
|
||||
feed: createVesselFeed(),
|
||||
@@ -73,6 +75,8 @@ export function createVesselState({ source, services }) {
|
||||
activeFocusCount: 0,
|
||||
activeLabelCount: 0,
|
||||
selectedRecord: null,
|
||||
/** @type {string|null} MMSI whose bridge the Cockpit camera occupies; its sprite and card stay hidden. */
|
||||
cockpitSubjectMmsi: null,
|
||||
/** @type {{setPositions: Function, clear: Function, destroy: Function}|null} Selected-vessel fading trail */
|
||||
trail: null,
|
||||
/** @type {{x: number, y: number, z: number}[]} Chronological ECEF trail vertices (oldest first) */
|
||||
|
||||
Reference in new issue
Block a user