diff --git a/index.html b/index.html index 924b85588..fcc5f79cf 100644 --- a/index.html +++ b/index.html @@ -26,7 +26,7 @@ with the sources: src/materialSymbolsSubset.test.mjs fails when a source names a glyph this list is missing, because a missing glyph does not draw a box — the ligature never forms and the element renders the literal word. --> - +
diff --git a/src/data/entityContext.js b/src/data/entityContext.js index 78fe791bc..5b3ee7df4 100644 --- a/src/data/entityContext.js +++ b/src/data/entityContext.js @@ -53,7 +53,9 @@ const TITLE_FIELDS = [ 'registration', ]; /** Fields that identify an item, for matching a click or a search. */ -const KEY_FIELDS = ['icao24', 'mmsi', 'noradId', 'usgsId', 'id']; +// `key` is a layer's own key where it differs from the record's id (a +// transit vehicle is keyed by feed and vehicle, and named by its fleet number). +const KEY_FIELDS = ['icao24', 'mmsi', 'noradId', 'usgsId', 'id', 'key']; /** * How many records to ask a layer for when all are wanted. Layers read a * missing or unbounded count as their own small default, so this is a number @@ -103,6 +105,7 @@ const HIDDEN_FACTS = new Set([ 'scale', 'routeId', 'building', + 'key', ]); /** Facts that lead the list when a record has them, in this order. */ @@ -318,6 +321,27 @@ const isEpochMs = (value) => */ /** Codes the feeds use, in words. */ const CODE_WORDS = Object.freeze({ + motion: { + moving: 'Moving', + stopped: 'Stopped', + waiting: 'Waiting for update', + 'no-fix': 'No recent fix', + }, + // GTFS-RT vehicle states, as the operator last reported them. + status: { + IN_TRANSIT_TO: 'Heading to next stop', + INCOMING_AT: 'Arriving at a stop', + STOPPED_AT: 'Stopped at a stop', + }, + occupancy: { + EMPTY: 'Empty', + MANY_SEATS_AVAILABLE: 'Many seats free', + FEW_SEATS_AVAILABLE: 'Few seats free', + STANDING_ROOM_ONLY: 'Standing room only', + CRUSHED_STANDING_ROOM_ONLY: 'Crowded', + FULL: 'Full', + NOT_ACCEPTING_PASSENGERS: 'Not taking passengers', + }, classification: { TD: 'Tropical depression', TS: 'Tropical storm', @@ -995,7 +1019,7 @@ const AIRCRAFT_CLASS_NAMES = { */ export function layerBreakdown(dataManager, layerId, { rect = null } = {}) { const kind = entityKind(layerId).kind; - if (!['aircraft', 'vessel', 'satellite', 'earthquake'].includes(kind)) return null; + if (!['aircraft', 'vessel', 'satellite', 'earthquake', 'vehicle'].includes(kind)) return null; let records = layerRecords(dataManager, layerId); if (rect) { const wraps = rect.west > rect.east; @@ -1024,6 +1048,22 @@ export function layerBreakdown(dataManager, layerId, { rect = null } = {}) { splits.push({ title: 'Type', parts: tallyBy(records, (record) => AIRCRAFT_CLASS_NAMES[record.aircraftClass] || plainWord(text(record.aircraftClass))) }); } else if (kind === 'vessel') { splits.push({ title: 'Type', parts: tallyBy(records, (record) => shipTypeName(record.shipType)) }); + } else if (kind === 'vehicle') { + // What the display shows each vehicle doing, then what kind it is, who + // runs it, the operator's own report and crowding where the feed has it. + // Feeds of vehicle positions carry no timetable, so early or late is not + // something these records can say. + const word = (field) => (record) => CODE_WORDS[field][record[field]] || ''; + splits.push({ title: 'Motion', parts: tallyBy(records, word('motion')) }); + splits.push({ title: 'Mode', parts: tallyBy(records, (record) => plainWord(text(record.mode))) }); + const operators = tallyBy(records, (record) => text(record.operator)); + if (operators.length > 1) splits.push({ title: 'Operator', parts: operators }); + const reported = records.filter((record) => CODE_WORDS.status[record.status]); + if (reported.length) + splits.push({ title: 'Last reported', parts: tallyBy(reported, word('status')) }); + const crowded = records.filter((record) => CODE_WORDS.occupancy[record.occupancy]); + if (crowded.length) + splits.push({ title: 'Crowding', parts: tallyBy(crowded, word('occupancy')) }); } else if (kind === 'satellite') { splits.push({ title: 'Class', parts: tallyBy(records, (record) => plainWord(text(record.satelliteClass || record.group))) }); } else { diff --git a/src/data/entityContext.test.mjs b/src/data/entityContext.test.mjs index bfaf8fc1b..2bbe1a4d0 100644 --- a/src/data/entityContext.test.mjs +++ b/src/data/entityContext.test.mjs @@ -50,6 +50,58 @@ test('a click identity finds its record by key, past the first 2000 records', () assert.equal(record?.callsign, 'FLT2400'); }); +test('a transit click finds its vehicle by the layer key and reads its state', () => { + const buses = manager({ + transit: { + name: 'Transit', + records: [ + { + id: 'M634', + key: 'capmetro-austin:M634', + route: '801', + operator: 'CapMetro', + status: 'IN_TRANSIT_TO', + lat: 30.2, + lon: -97.7, + }, + ], + }, + }); + const card = describeEntity(buses, { + layerId: 'transit', + id: 'capmetro-austin:M634', + lat: 30.21, + lon: -97.71, + }); + assert.equal(card?.title, 'M634'); + const facts = Object.fromEntries(card.facts.map((fact) => [fact.label, fact.value])); + assert.equal(facts.Operator, 'CapMetro'); + assert.equal(facts.Status, 'Heading to next stop'); + assert.equal(facts.Key, undefined, 'the layer key is not shown'); +}); + +test('transit vehicles break down by motion, mode and reported state', () => { + const at = { lat: 30.2, lon: -97.7 }; + const buses = manager({ + transit: { + name: 'Transit', + records: [ + { id: '1', mode: 'bus', motion: 'moving', status: 'IN_TRANSIT_TO', ...at }, + { id: '2', mode: 'bus', motion: 'stopped', status: 'STOPPED_AT', ...at }, + { id: '3', mode: 'rail', motion: 'moving', occupancy: 'FULL', ...at }, + ], + }, + }); + const breakdown = layerBreakdown(buses, 'transit'); + assert.equal(breakdown.total, 3); + const split = (title) => + Object.fromEntries(breakdown.splits.find((item) => item.title === title).parts.map((part) => [part.label, part.count])); + assert.deepEqual(split('Motion'), { Moving: 2, Stopped: 1 }); + assert.deepEqual(split('Mode'), { Bus: 2, Rail: 1 }); + assert.deepEqual(split('Last reported'), { 'Heading to next stop': 1, 'Stopped at a stop': 1 }); + assert.deepEqual(split('Crowding'), { Full: 1 }); +}); + test('a moving item never borrows a nearby craft when its key is unknown', () => { const record = findRecord(dm, { layerId: 'flights', id: 'zzzzzz', label: 'NOPE', lat: 34.0004, lon: -118.05 }); assert.equal(record, null); diff --git a/src/layers/transit/queries.js b/src/layers/transit/queries.js index 28aff8b63..738a1c17c 100644 --- a/src/layers/transit/queries.js +++ b/src/layers/transit/queries.js @@ -5,12 +5,23 @@ import { transitDetectionClass, transitDetectionId, transitDetectionMetric, + vehicleReportTimeMs, } from './policy.js'; import { TRANSIT_ENABLED_FEEDS, getRegisteredTransitFeed, + transitRouteLabel, } from '../../data/transitFeeds.js'; import { transitModeTier } from '../../data/transitPresetStyle.js'; +import { displayMotion } from './movement.js'; + +/** The display's motion word as a record field for the Context card. */ +const MOTION_FIELD = Object.freeze({ + 'EN ROUTE': 'moving', + STOPPED: 'stopped', + WAITING: 'waiting', + 'NO FIX': 'no-fix', +}); /** Regions, not feeds: one region can carry several feeds (Rio has two). */ const TRANSIT_REGION_COUNT = new Set( @@ -211,10 +222,14 @@ export function createQueries({ state, parts }) { * query engine. On demand only (once per spoken query); [] while the * layer is off. * @param {number} [maxCount=Infinity] Most records to return. - * @returns {Array} {id, label, routeId, mode, lat, lon, speedMps, courseDeg, status, occupancy}. + * @returns {Array} {id, label, routeId, mode, lat, lon, speedMps, courseDeg, status, occupancy}, + * plus `key` (the layer's own vehicle key, which a map click carries), + * `route`, `operator`, `place`, `observedMs` and `motion` (what the + * display shows: moving, stopped, waiting or no-fix) for the Context card. */ getAnalystRecords(maxCount = Infinity) { if (!state._enabled) return []; + const now = Date.now(); const num = (v) => (Number.isFinite(v) ? v : null); const result = []; for (const entry of state._vehicles.values()) { @@ -222,6 +237,8 @@ export function createQueries({ state, parts }) { const record = entry.record || {}; if (!Number.isFinite(record.lat) || !Number.isFinite(record.lon)) continue; + const feed = feedForEntry(state, entry); + const reportedMs = vehicleReportTimeMs(record, entry.fetchedAt); result.push({ id: record.label || record.id || entry.key, label: record.label || null, @@ -233,6 +250,15 @@ export function createQueries({ state, parts }) { courseDeg: num(entry.courseDeg ?? record.bearing), status: record.status || null, occupancy: record.occupancy || null, + key: entry.key, + route: + record.routeId && feed + ? transitRouteLabel(feed, record.routeId) + : record.routeId || null, + operator: feed?.name || null, + place: feed?.region || null, + observedMs: Number.isFinite(reportedMs) ? reportedMs : null, + motion: MOTION_FIELD[displayMotion(entry, now).word] || null, }); } return result; diff --git a/src/ui/styles/entity-card.css b/src/ui/styles/entity-card.css index bea47e724..6cfcb06f9 100644 --- a/src/ui/styles/entity-card.css +++ b/src/ui/styles/entity-card.css @@ -464,6 +464,23 @@ white-space: nowrap; } +/* A moving item's state on its row: green moving, amber stopped, grey + while its feed has nothing new. */ +.view-context__row-motion { + flex: none; + margin-right: 6px; + color: rgba(199, 214, 222, 0.5); + font-size: 14px; +} + +.view-context__row-motion[data-motion='moving'] { + color: #5dff9f; +} + +.view-context__row-motion[data-motion='stopped'] { + color: #ffb86b; +} + .view-context__row-meta { flex: none; color: rgba(199, 214, 222, 0.55); @@ -1144,7 +1161,8 @@ border-radius: 8px; } -.cctv-preview__frame img { +.cctv-preview__frame img, +.cctv-preview__live { width: 100%; height: 100%; object-fit: cover; diff --git a/src/ui/views/panels/ViewContext.vue b/src/ui/views/panels/ViewContext.vue index fc4c97c06..07f76e722 100644 --- a/src/ui/views/panels/ViewContext.vue +++ b/src/ui/views/panels/ViewContext.vue @@ -19,6 +19,16 @@ import AltitudeGraph from './AltitudeGraph.vue'; // that item's card. It reads again when the camera settles, and every few // seconds so moving things stay current. const store = useEntityCardStore(); + +// What a moving item is doing, as the layer's own display shows it (transit +// vehicles report it): an icon on its row, its words on hover. +const MOTION_STATES = { + moving: { icon: 'play_arrow', label: () => t('Moving') }, + stopped: { icon: 'pause', label: () => t('Stopped', { context: 'vehicle motion' }) }, + waiting: { icon: 'schedule', label: () => t('Waiting for update') }, + 'no-fix': { icon: 'location_off', label: () => t('No recent fix') }, +}; +const motionOf = (item) => MOTION_STATES[item.identity?.record?.motion] || null; const { view } = storeToRefs(store); const tick = ref(0); let timer = null; @@ -348,6 +358,14 @@ const distance = (km) =>