-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathsourceLabels.ts
More file actions
483 lines (438 loc) · 16.7 KB
/
Copy pathsourceLabels.ts
File metadata and controls
483 lines (438 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
* Utilities for building human-readable labels from Signal K source references.
*
* Source refs look like "YDEN02.44" (connection.srcAddress) for N2K devices,
* or plain strings like "derived-data" for plugins.
*/
export interface N2kDeviceInfo {
manufacturerCode?: string
modelId?: string
modelVersion?: string
modelSerialCode?: string
softwareVersionCode?: string
deviceClass?: string
deviceFunction?: number
deviceInstance?: number
deviceInstanceLower?: number
deviceInstanceUpper?: number
installationDescription1?: string
installationDescription2?: string
manufacturerInformation?: string
canName?: string
src?: string
uniqueNumber?: number
productCode?: number
nmea2000Version?: number
certificationLevel?: number | string
loadEquivalency?: number
systemInstance?: number
pgns?: Record<string, string>
pgnInstances?: Record<string, number[]>
unknownPGNs?: Record<string, unknown>
}
export interface SourceDevice {
n2k?: N2kDeviceInfo
type?: string
[key: string]: unknown
}
export interface SourcesData {
[connectionOrKey: string]:
| SourceDevice
| { type?: string; [key: string]: unknown }
}
/**
* Parse a sourceRef like "YDEN02.44" into connection and src parts.
* Returns null for plugin sources like "derived-data" that have no dot.
*/
export function parseSourceRef(
sourceRef: string
): { connection: string; src: string } | null {
const dotIdx = sourceRef.indexOf('.')
if (dotIdx === -1) return null
return {
connection: sourceRef.slice(0, dotIdx),
src: sourceRef.slice(dotIdx + 1)
}
}
/**
* Look up the N2K device info for a given sourceRef from the sources API data.
*
* The sources API keys devices by numeric address (e.g. "44"), but sourceRefs
* may use the CAN Name (e.g. "can0.c1789101e7e0b32b"). When a direct key
* lookup fails, we search by matching the canName field.
*/
export function getDeviceInfo(
sourceRef: string,
sourcesData: SourcesData
): N2kDeviceInfo | null {
const parsed = parseSourceRef(sourceRef)
if (!parsed) return null
const connection = sourcesData[parsed.connection]
if (!connection || typeof connection !== 'object') return null
const conn = connection as Record<string, unknown>
// Direct lookup by key (numeric address)
const directDevice = conn[parsed.src]
if (directDevice && typeof directDevice === 'object') {
const d = directDevice as SourceDevice
if (d.n2k) return d.n2k
}
// Fallback: search by canName (for CAN Name-based sourceRefs)
for (const device of Object.values(conn)) {
if (!device || typeof device !== 'object') continue
const d = device as SourceDevice
if (d.n2k?.canName === parsed.src) return d.n2k
}
return null
}
/**
* Build a human-readable label for a source reference.
*
* Label format: "Manufacturer Model (sourceRef)" or "Manufacturer (sourceRef)"
* when model is not available. Data comes from the N2K bus only (PGN 60928 for
* manufacturer, PGN 126996 for modelId).
*/
export function buildSourceLabel(
sourceRef: string,
sourcesData: SourcesData | null,
sourceNames?: Record<string, string> | null
): string {
const { primary, secondary } = buildSourceLabelParts(
sourceRef,
sourcesData,
sourceNames
)
return secondary ? `${primary} (${secondary})` : primary
}
/**
* Same data as buildSourceLabel but split into two parts so the UI can
* render the human name on one line and the raw sourceRef on a second.
* On portrait phones the combined string truncates with ellipsis, hiding
* the bit that distinguishes two physical instances of the same model.
*/
export function buildSourceLabelParts(
sourceRef: string,
sourcesData: SourcesData | null,
sourceNames?: Record<string, string> | null
): { primary: string; secondary: string | null } {
// Server-supplied names (WebSocket device descriptions, merged with any
// manual aliases) take precedence over bus-derived labels. This is the
// only path available to non-admin users, who cannot read the device
// registry directly.
const name = sourceNames?.[sourceRef]
if (name) return { primary: name, secondary: sourceRef }
if (!sourcesData) return { primary: sourceRef, secondary: null }
const n2k = getDeviceInfo(sourceRef, sourcesData)
if (!n2k) return { primary: sourceRef, secondary: null }
const { manufacturerCode, modelId } = n2k
const mfr = manufacturerCode || ''
const model = modelId || ''
if (!mfr && !model) return { primary: sourceRef, secondary: null }
return {
primary: [mfr, model].filter(Boolean).join(' '),
secondary: sourceRef
}
}
/**
* Translate a numeric-form `<label>.<src>` ref (e.g. `can0.4`) to the
* canonical `<label>.<canName>` form (e.g. `can0.c050a0…`). Returns the
* raw ref unchanged when no canName is known — that lets call-sites
* compare against server-emitted canonical refs (livePreferredSources)
* without breaking on cold boot or when the source isn't an N2K device.
*
* Mirrors `buildSrcToCanonicalMap` on the server (src/deltacache.ts).
*/
export function canonicaliseSourceRef(
sourceRef: string,
sourcesData: SourcesData | null
): string {
if (!sourcesData) return sourceRef
const parsed = parseSourceRef(sourceRef)
if (!parsed) return sourceRef
const conn = sourcesData[parsed.connection] as
| Record<string, unknown>
| undefined
if (!conn || typeof conn !== 'object') return sourceRef
const dev = conn[parsed.src] as SourceDevice | undefined
if (!dev || typeof dev !== 'object') return sourceRef
const canName = dev.n2k?.canName
if (typeof canName !== 'string' || canName.length === 0) return sourceRef
// Already canonical — sourceRef looked up directly under canName key.
if (parsed.src === canName) return sourceRef
return `${parsed.connection}.${canName}`
}
/**
* True if the source is a Signal K plugin emitting deltas directly into
* the server, not a device on a bus. Detected structurally by the
* absence of a "${connection}.${address}" form — plugin sourceRefs are
* plain strings like "derived-data" or "signalk-to-nmea2000".
*
* The user usually wants to rank these explicitly: a derived-data
* fallback should sit below the real sensor; a plugin acting as
* authoritative should sit on top.
*/
export function isPluginSource(sourceRef: string): boolean {
return sourceRef.length > 0 && sourceRef.indexOf('.') === -1
}
/**
* True when the sourceRef resolves to an NMEA2000 device in the live
* sources tree. The check looks for an n2k payload (set only for
* N2K-derived devices), so NMEA0183 talkers and plugin sources return
* false even if their refs share the `${connection}.${suffix}` shape.
*
* Used by the priority-group trash action to pick between the
* N2K-specific eviction endpoint (which also walks bus-address state)
* and the generic remover for non-N2K sources.
*/
export function isN2kSource(
sourceRef: string,
sourcesData: SourcesData | null
): boolean {
if (!sourcesData) return false
return getDeviceInfo(sourceRef, sourcesData) !== null
}
export interface N2kDeviceEntry extends N2kDeviceInfo {
sourceRef: string
connection: string
/**
* The actual key under `sources[connection]` for this device — always
* the numeric N2K address as a string, regardless of useCanName.
* Used to match server-side per-source state (e.g. SOURCESTATUS).
*/
srcAddr: string
}
/**
* Extract a flat list of N2K devices from the sources API response.
* Sorted by manufacturer, then model, then bus address.
*/
export function extractN2kDevices(sourcesData: SourcesData): N2kDeviceEntry[] {
const devices: N2kDeviceEntry[] = []
for (const [connName, connData] of Object.entries(sourcesData)) {
if (!connData || typeof connData !== 'object') continue
if ((connData as Record<string, unknown>).type !== 'NMEA2000') continue
for (const [srcAddr, device] of Object.entries(connData)) {
if (srcAddr === 'type' || srcAddr === 'label') continue
if (!device || typeof device !== 'object') continue
const d = device as SourceDevice
if (!d.n2k) continue
devices.push({
...d.n2k,
sourceRef: `${connName}.${d.n2k.canName || srcAddr}`,
connection: connName,
srcAddr
})
}
}
return devices.sort((a, b) => {
const mfr = String(a.manufacturerCode ?? '').localeCompare(
String(b.manufacturerCode ?? '')
)
if (mfr !== 0) return mfr
const model = String(a.modelId ?? '').localeCompare(String(b.modelId ?? ''))
if (model !== 0) return model
return Number(a.src || 0) - Number(b.src || 0)
})
}
/**
* Build a lookup map from sourceRef → display label for all known sources.
*/
export function buildSourceLabelMap(
sourcesData: SourcesData
): Map<string, string> {
const map = new Map<string, string>()
for (const [connName, connData] of Object.entries(sourcesData)) {
if (!connData || typeof connData !== 'object') continue
for (const [key, device] of Object.entries(connData)) {
if (key === 'type' || key === 'label') continue
if (!device || typeof device !== 'object') continue
const d = device as SourceDevice
if (d.n2k) {
const sourceRef = `${connName}.${d.n2k.canName || key}`
const label = buildSourceLabel(sourceRef, sourcesData)
if (label !== sourceRef) {
map.set(sourceRef, label)
}
}
}
}
return map
}
export interface InstanceConflict {
deviceA: N2kDeviceEntry
deviceB: N2kDeviceEntry
sharedPGNs: string[]
deviceInstance: number
}
/**
* NMEA 2000 protocol/management PGNs that every device sends.
* These should NOT be counted when detecting instance conflicts because
* they are part of the network protocol, not sensor data that instruments
* distinguish by device instance.
*/
const PROTOCOL_PGNS = new Set([
'59392', // ISO Acknowledgement
'59904', // ISO Request
'60160', // ISO Transport Protocol, Data Transfer
'60416', // ISO Transport Protocol, Connection Management
'60928', // ISO Address Claim
'65240', // ISO Commanded Address
'126208', // NMEA Request/Command/Acknowledge Group Function
'126464', // PGN List (Transmit and Receive)
'126993', // Heartbeat
'126996', // Product Information
'126998' // Configuration Information
])
/**
* Manufacturer-proprietary PGN ranges. The same PGN number can carry
* entirely different payload semantics for different manufacturers
* (bytes 0-1 are the Manufacturer Code, byte 2 is the Industry Code,
* the rest is manufacturer-defined), so shared proprietary PGNs across
* devices are not a reliable signal of a real conflict.
*/
export function isProprietaryPGN(pgn: string | number): boolean {
const n = typeof pgn === 'number' ? pgn : Number(pgn)
if (!Number.isFinite(n)) return false
return (
n === 61184 ||
(n >= 65280 && n <= 65535) ||
n === 126720 ||
(n >= 130816 && n <= 131071)
)
}
/**
* Temperature/humidity PGNs where the unique key is instance + source,
* not just instance. Two devices sending the same PGN with the same
* instance but different source types are NOT in conflict.
*/
const COMPOUND_KEY_PGNS = new Set([
'130312', // Temperature
'130313', // Humidity
'130316' // Temperature Extended Range
// Note: Maretron proprietary temperature (130823) is also compound-keyed,
// but it's filtered out earlier as a proprietary PGN, so listing it here
// would be unreachable.
])
/**
* Detect N2K devices that share the same device instance and send
* overlapping data PGNs. This can confuse instruments that rely on
* instance numbers to distinguish data sources.
*
* Protocol/management PGNs (ISO Address Claim, Heartbeat, Product Info,
* etc.) are excluded — every device sends those and they do not represent
* real data conflicts. Manufacturer-proprietary PGNs are also excluded:
* the same PGN number can carry different semantics for different
* manufacturers, so sharing one is not a reliable conflict signal.
*
* When actual data instance information is available (from the sources API
* pgnInstances or n2k-discovery pgnDataInstances), PGNs where both devices
* use non-overlapping instances are also excluded — they are not in conflict.
*
* For temperature/humidity PGNs (COMPOUND_KEY_PGNS), the unique key is
* instance + source. When pgnSourceKeys data is available, these PGNs are
* compared using "instance:source" compound keys instead of instance alone.
*/
export function detectInstanceConflicts(
devices: N2kDeviceEntry[],
pgnDataInstances?: Record<string, Record<string, number[]>>,
pgnSourceKeys?: Record<string, Record<string, string[]>>
): InstanceConflict[] {
const conflicts: InstanceConflict[] = []
// Group by connection + deviceInstance to avoid false positives across buses
const byConnInstance = new Map<string, N2kDeviceEntry[]>()
for (const d of devices) {
if (d.deviceInstance === undefined) continue
const groupKey = `${d.connection}\0${d.deviceInstance}`
const group = byConnInstance.get(groupKey)
if (group) {
group.push(d)
} else {
byConnInstance.set(groupKey, [d])
}
}
for (const [key, group] of byConnInstance) {
if (group.length < 2) continue
const instance = Number(key.split('\0')[1])
for (let i = 0; i < group.length; i++) {
for (let j = i + 1; j < group.length; j++) {
const a = group[i]
const b = group[j]
const keep = (p: string) =>
!PROTOCOL_PGNS.has(p) && !isProprietaryPGN(p)
const aPGNs = a.pgns ? Object.keys(a.pgns).filter(keep) : []
const bPGNs = new Set(b.pgns ? Object.keys(b.pgns).filter(keep) : [])
const shared = aPGNs.filter((pgn) => {
if (!bPGNs.has(pgn)) return false
// Temperature / humidity PGNs are keyed by the full SK leaf
// path (which encodes the source-type enum and any instance
// index), not by the bare instance number — see
// buildPgnSourceKeysFromTree. The SK tree is authoritative
// for "currently publishing"; if we have a path-keys map at
// all, trust it. Two devices conflict only when their
// currently-published paths overlap. A device temporarily
// missing from the map (e.g. between PGN emissions) is no
// longer a conflict signal — the alternative (falling back
// to the bare instance) re-introduces the very false
// positive these compound keys exist to suppress.
if (COMPOUND_KEY_PGNS.has(pgn)) {
if (!pgnSourceKeys) return true
const aKeys = pgnSourceKeys[a.sourceRef]?.[pgn]
const bKeys = pgnSourceKeys[b.sourceRef]?.[pgn]
if (!aKeys || aKeys.length === 0) return false
if (!bKeys || bKeys.length === 0) return false
const aSet = new Set(aKeys)
return bKeys.some((k) => aSet.has(k))
}
// Check actual data instance overlap.
// Priority: sources API (pgnInstances) > n2k-discovery (pgnDataInstances)
const aInst =
a.pgnInstances?.[pgn] ?? pgnDataInstances?.[a.sourceRef]?.[pgn]
const bInst =
b.pgnInstances?.[pgn] ?? pgnDataInstances?.[b.sourceRef]?.[pgn]
if (aInst && aInst.length > 0 && bInst && bInst.length > 0) {
// Both have instance data — only conflict if instances overlap
const aSet = new Set(aInst)
return bInst.some((i) => aSet.has(i))
}
// One or both missing instance data — flag conservatively
return true
})
if (shared.length > 0) {
conflicts.push({
deviceA: a,
deviceB: b,
sharedPGNs: shared,
deviceInstance: instance
})
}
}
}
}
return conflicts
}
/**
* Build a stable key for an instance conflict pair.
* Sorts the two sourceRefs so the same pair always produces the same key.
*/
export function conflictKey(sourceRefA: string, sourceRefB: string): string {
return [sourceRefA, sourceRefB].sort().join('+')
}
/**
* Classify a removeSource/n2kRemoveSource DELETE response so the trash flow
* knows whether to keep the optimistic removal or revert it.
*
* A 404 on a source the live status snapshot no longer reports
* (`isMissingFromStatus`) is the success case, not a failure: the server is
* confirming the source is already gone, so the saved-group ref is a stale
* leftover — a device that re-registered under a new transport (a renumbered
* ttyUSB port, a fresh canName). Keeping the removal lets the ref drop out of
* the group on Save; reverting would pin the zombie ref in place forever,
* since the DELETE can never succeed for a source the server doesn't have.
*/
export function isRemoveSourceFailure(
status: number,
ok: boolean,
isMissingFromStatus: boolean
): boolean {
if (ok) return false
if (status === 404 && isMissingFromStatus) return false
return true
}