Skip to content

REMINDER: Restoring pgnInstances: Active Instance Tracking via FullSignalK (TypeBox & Source UX) #2483

@dirkwa

Description

@dirkwa

Restoring pgnInstances: Active Instance Tracking via FullSignalK

Context

The source-priority-ux branch originally built on top of the TypeBox PR (#2325), which migrated FullSignalK from @signalk/signalk-schema (npm package) into @signalk/server-api as TypeScript source. This allowed us to extend handleNmea2000Source with continuous pgnInstances tracking.

When we rebased source-priority-ux onto master without TypeBox, we lost this capability because FullSignalK is back in @signalk/signalk-schema and cannot be modified.

What was lost

In the TypeBox version, packages/server-api/src/fullsignalk.ts had this addition inside handleNmea2000Source:

// Accumulate data instances per PGN for conflict detection.
if (source.instance !== undefined) {
  const pi = (existing.n2k.pgnInstances ??= {} as Record<string, number[]>)
  const pgn = String(source.pgn)
  const arr = (pi[pgn] ??= [])
  const inst = Number(source.instance)
  if (!isNaN(inst) && !arr.includes(inst)) {
    arr.push(inst)
  }
}

This ran on every N2K delta, continuously building up which data instances each device uses per PGN. The result was available via /signalk/v1/api/sources in the pgnInstances field of each device's n2k object.

Current fallback

sourceLabels.ts already has a fallback path:

const aInst = a.pgnInstances?.[pgn] ?? pgnDataInstances?.[a.src ?? '']?.[pgn]

The pgnDataInstances comes from n2k-discovery.ts, which listens to the N2K bus for a fixed duration when the user clicks "Discover" in the Source Discovery UI. This is a point-in-time snapshot, not continuous.

What to restore when TypeBox merges

When the TypeBox PR merges and FullSignalK is back in server-api as TypeScript:

  1. Re-add the pgnInstances tracking block to handleNmea2000Source in packages/server-api/src/fullsignalk.ts (code above)

  2. The N2kDeviceInfo interface in packages/server-admin-ui/src/utils/sourceLabels.ts already has the pgnInstances field — no change needed

  3. The sourceLabels.ts conflict detection already prefers pgnInstances over pgnDataInstances — no change needed

  4. The Sources REST API (/signalk/v1/api/sources) will automatically include pgnInstances because it returns FullSignalK.retrieve().sources

In short: just restore the one code block in handleNmea2000Source and everything else reconnects automatically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions