From f60e4939f540c4fb14284d1ac61a763b66086808 Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Wed, 18 Nov 2026 11:05:24 +0100 Subject: [PATCH 01/10] feat: log each meter report and the reply enqueued for it The agent, central, the gateway fallback and standalone now log one readable line per step: the reading received, the decision, and the state reply enqueued, plus the real reason a report is rejected. --- docs/deployment-guide.md | 12 +++++++++ src/agent/index.ts | 19 +++++++++---- src/services/grpc.ts | 3 ++- src/services/mqtt.ts | 13 ++++++--- src/utils/report-log.ts | 44 +++++++++++++++++++++++++++++++ tests/services/report-log.test.ts | 37 ++++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 src/utils/report-log.ts create mode 100644 tests/services/report-log.test.ts diff --git a/docs/deployment-guide.md b/docs/deployment-guide.md index 1f54523..cb95101 100644 --- a/docs/deployment-guide.md +++ b/docs/deployment-guide.md @@ -353,6 +353,18 @@ docker compose -f compose.gateway.yaml logs -f console docker compose -f compose.gateway.yaml logs -f agent ``` +**Following a meter report.** Each step logs one line with the reading and the reply: + +| Where | Lines | +|---|---| +| Gateway `logs -f agent` | `Listening for meter reports on …`, `Report received from ChirpStack: devEui=… nonce=… energy=…kWh …`, `Report … is now …`, `Reply enqueued to ChirpStack for … (queue id …): state=ON …` | +| Central `logs -f console` | `Report received: …`, `Reading recorded: …`, `Reply decided (the gateway agent enqueues it): state=…` or `Report rejected: ` | +| Gateway `logs -f console` | The same as central, but only while central is unreachable and the gateway decides locally | +| Standalone `logs -f console` | `Report received: …`, `Reading recorded: …`, `Reply enqueued: state=…` | + +`Uplink skipped (fPort …)` means the meter sent on a port other than 2, which isn't billed. +The full signature and meter key are shortened in the logs; keys and tokens never appear. + | Symptom | Cause and fix | |---|---| | `Replace the placeholder values in .env: …` | Template values are still in `.env`. Fill in the named settings. | diff --git a/src/agent/index.ts b/src/agent/index.ts index ad6ffe6..a9fde1a 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -15,6 +15,8 @@ import { mqttConnection } from '../lib/mqtt-config'; import { applicationId } from '../lib/chirpstack-config'; import { managedQueue } from '../services/grpc'; import { createLogger } from '../utils/logger'; +import { describeReport, describeReply, errorText } from '../utils/report-log'; +import type { ChirpstackQueue } from '../central/coordinator'; import { RateLimit } from '../central/auth'; import { UpdaterClient } from './updater-client'; import { TunnelMux, gatewayTarget } from '../central/tunnels'; @@ -41,14 +43,19 @@ export async function runAgent():Promise<()=>Promise> { const updater=new UpdaterClient(process.env.UPDATER_SOCKET||path.join(path.dirname(config.credentialPath),'updater.sock'),process.env.UPDATER_SECRET_PATH||path.join(path.dirname(config.credentialPath),'updater-secret')); let updatesEnabled=false;const updateStatuses=new Map(); const stateDirectory=path.dirname(config.credentialPath); - const journal=new OperationJournal(path.join(stateDirectory,'operations.db'),config.queueLimit,event=>logger.info('Operation status',event)); + const journal=new OperationJournal(path.join(stateDirectory,'operations.db'),config.queueLimit,event=>logger.info(`Report ${event.id} is now ${event.state}`)); const local=new LocalDecider(gatewayFallbackPort(),readOwnerSecret(config.fallbackSecretPath),config.decisionTimeoutMs); let socket:WebSocket|undefined,stopped=false,reconnect:ReturnType|undefined; let connectedAt=0,attempt=0,mqttState:Health['mqtt']='connecting',lastError:Health['lastError']='none'; const central=new SocketDecider(()=>socket,config.decisionTimeoutMs); const replication=new Replication(journal,central,local); const healthy=()=>socket?.readyState===WebSocket.OPEN&&Date.now()-connectedAt>=config.recoveryHealthyMs; - const coordinator=new UplinkCoordinator(journal,replication.guarded('central'),replication.guarded('local'),managedQueue, + // Log every reply the agent puts in ChirpStack's queue, and every failed attempt. + const queue:ChirpstackQueue={list:devEui=>managedQueue.list(devEui),enqueue:async decision=>{ + try{const id=await managedQueue.enqueue(decision);logger.info(`Reply enqueued to ChirpStack for ${decision.devEui} (queue id ${id}): ${describeReply(decision.bytes)}`);return id;} + catch(error){logger.warn(`Reply for ${decision.devEui} may not have been enqueued (${errorText(error)}); it is held, never resent: ${describeReply(decision.bytes)}`);throw error;} + }}; + const coordinator=new UplinkCoordinator(journal,replication.guarded('central'),replication.guarded('local'),queue, {centralHealthy:healthy,timeoutMs:config.decisionTimeoutMs,reconcileSnapshot:(owner,outcome)=>replication.observe(owner,outcome)}); const sendHealth=()=>{ if(socket?.readyState!==WebSocket.OPEN)return; @@ -108,7 +115,7 @@ export async function runAgent():Promise<()=>Promise> { const application=applicationId(),topic=`application/${application}/device/+/event/up`; mqtt.on('connect',()=>{mqtt.subscribe(topic,{qos:1},(error,granted)=>{ if(error||granted?.some(grant=>grant.qos===128)){mqttState='disconnected';lastError='mqtt';mqtt.stream.destroy();} - else mqttState='connected';sendHealth(); + else {mqttState='connected';logger.info(`Listening for meter reports on ${topic}`);}sendHealth(); });}); mqtt.on('reconnect',()=>{mqttState='connecting';});mqtt.on('close',()=>{mqttState='disconnected';}); mqtt.on('error',()=>{lastError='mqtt';logger.warn('MQTT connection unavailable; reconnecting.');}); @@ -119,8 +126,10 @@ export async function runAgent():Promise<()=>Promise> { const parsed:unknown=JSON.parse(packet.payload.toString()); if(typeof parsed!=='object'||parsed===null||Array.isArray(parsed)){done();return;} const event=parsed as UplinkEvent; - if(event.fPort!==2||typeof event.deviceInfo?.devEui!=='string'||typeof event.deviceInfo?.applicationId!=='string'||event.deviceInfo.devEui.toLowerCase()!==match[2].toLowerCase()||event.deviceInfo.applicationId.toLowerCase()!==application){done();return;} - try{eventIdentity(event);}catch{logger.warn('Invalid billing event ignored.');done();return;} + if(event.fPort!==2||typeof event.deviceInfo?.devEui!=='string'||typeof event.deviceInfo?.applicationId!=='string'||event.deviceInfo.devEui.toLowerCase()!==match[2].toLowerCase()||event.deviceInfo.applicationId.toLowerCase()!==application){ + logger.info(`Uplink skipped (${event.fPort!==2?`fPort ${String(event.fPort)}; only fPort 2 is billed`:'device or application does not match its topic'}): ${describeReport(event)}`);done();return;} + try{eventIdentity(event);}catch{logger.warn(`Invalid billing event ignored: ${describeReport(event)}`);done();return;} + logger.info(`Report received from ChirpStack: ${describeReport(event)}`); // receive persists synchronously before returning its processing promise. const processing=coordinator.receive(gateway,event); done();void processing.then(sendHealth).catch(()=>{lastError='storage';logger.error('Managed operation unavailable; inspect gateway status.');}); diff --git a/src/services/grpc.ts b/src/services/grpc.ts index fcfa53c..f00a926 100644 --- a/src/services/grpc.ts +++ b/src/services/grpc.ts @@ -3,6 +3,7 @@ import { DeviceServiceClient } from "@chirpstack/chirpstack-api/api/device_grpc_ import { DeviceQueueItem, EnqueueDeviceQueueItemRequest, GetDeviceQueueItemsRequest } from "@chirpstack/chirpstack-api/api/device_pb"; import type { ChirpstackQueue } from '../central/coordinator'; import { createLogger } from "../utils/logger"; +import { describeReply } from "../utils/report-log"; const logger = createLogger("[grpc]"); @@ -27,7 +28,7 @@ export function enqueue(devEui: string, data: number[]): Promise { return new Promise((resolve, reject) => { client().enqueue(enqueueReq, metadata, (err, resp) => { if (err || !resp) { reject(err || new Error("Missing enqueue response")); return; } - logger.info(`${data} has been enqueued with id: ` + resp.getId()); + logger.info(`Reply enqueued to ChirpStack for ${devEui} (queue id ${resp.getId()}): ${describeReply(data)}`); resolve(); }); }); diff --git a/src/services/mqtt.ts b/src/services/mqtt.ts index c1bd9c5..2c633e2 100644 --- a/src/services/mqtt.ts +++ b/src/services/mqtt.ts @@ -15,6 +15,7 @@ import { uplinkPayload } from '../lib/uplink-payload'; import { runHook, notifyHook, verifyPayloadSignature } from '../lib/utils'; import { getLatestTransactionNonce } from '../lib/sync'; import { createMeterLogger, createLogger } from '../utils/logger'; +import { describeReport, describeReply, errorText } from '../utils/report-log'; import { meterIdFromPublicKeyContractCall } from '../utils/contracts'; const logger = createLogger('[mqtt]'); @@ -84,6 +85,7 @@ export async function handleMessage(blob: Buffer, downlink: DownlinkSink = chirp } devEui = message.deviceInfo.devEui; log = createMeterLogger({ devEui }); + log.info(`Report received: ${describeReport(message)}`); if (deviceLocks.has(devEui!)) { void notifyHook('onMessageDropped', 'locked', devEui!); return; @@ -145,9 +147,14 @@ export async function handleMessage(blob: Buffer, downlink: DownlinkSink = chirp } void notifyHook('onIsOnStateComputed', meter.tokenId, is_on); const state = decoded.nonce === 0 ? { nonce: meter.latestNonce, is_on } : { is_on }; + log.info(newTransaction ? `Reading recorded: meter ${meter.tokenId} nonce ${decoded.nonce}` + : decoded.nonce === 0 ? `Meter ${meter.tokenId} asked for its nonce (latest ${meter.latestNonce})` + : `Repeat of meter ${meter.tokenId} nonce ${decoded.nonce}: acknowledged, not billed again`); try { - await downlink({ devEui: devEui!, fPort: 2, confirmed: false, - bytes: encode(state as State, decoded.extensions.latitude ?? 0, decoded.extensions.longitude ?? 0) }); + const bytes = encode(state as State, decoded.extensions.latitude ?? 0, decoded.extensions.longitude ?? 0); + await downlink({ devEui: devEui!, fPort: 2, confirmed: false, bytes }); + // Managed roles hand the reply to the gateway agent, which enqueues it; standalone enqueued it already. + log.info(`${managedBilling.getStore() ? 'Reply decided (the gateway agent enqueues it)' : 'Reply enqueued'}: ${describeReply(bytes)}`); void notifyHook('onStateEnqueued', state, decoded.extensions.latitude ?? 0, decoded.extensions.longitude ?? 0); } finally { // Delivery services run after the downlink attempt and cannot prevent it. @@ -161,7 +168,7 @@ export async function handleMessage(blob: Buffer, downlink: DownlinkSink = chirp } } } catch (error) { - log.error('Error handling MQTT message:', error); + log.error(`Report rejected: ${errorText(error)}`); void notifyHook('onMessageError', error); } finally { if (lockedMeter) meterLocks.delete(lockedMeter); diff --git a/src/utils/report-log.ts b/src/utils/report-log.ts new file mode 100644 index 0000000..e9b440e --- /dev/null +++ b/src/utils/report-log.ts @@ -0,0 +1,44 @@ +import { uplinkPayload } from '../lib/uplink-payload'; +import { decodePayload } from '../lib/decode'; + +/** One readable line per meter report and per state reply, for `docker compose logs`. + * Only meter readings and reply contents: never keys, tokens or the full signature. */ + +type Uplink = { deviceInfo?: { devEui?: unknown }; fCnt?: unknown; fPort?: unknown; deduplicationId?: unknown; + data?: unknown; dataHex?: unknown; dataEncoding?: unknown }; + +const short = (hex: string) => hex.length > 16 ? `${hex.slice(0, 8)}…${hex.slice(-8)}` : hex; + +export function describeReport(event: Uplink): string { + const parts = [`devEui=${String(event.deviceInfo?.devEui ?? '?')}`]; + if (typeof event.fCnt === 'number') parts.push(`fCnt=${event.fCnt}`); + if (typeof event.fPort === 'number') parts.push(`fPort=${event.fPort}`); + if (typeof event.deduplicationId === 'string') parts.push(`id=${event.deduplicationId}`); + try { + const bytes = uplinkPayload(event).bytes; + try { + const reading = decodePayload(bytes), ext = reading.extensions; + parts.push(`nonce=${reading.nonce}`, `energy=${reading.energy}kWh`); + if (ext.voltage !== undefined) parts.push(`voltage=${ext.voltage}V`); + if (ext.latitude !== undefined && ext.longitude !== undefined) parts.push(`location=${ext.latitude},${ext.longitude}`); + if (ext.deviceId) parts.push(`meterKey=${short(ext.deviceId)}`); + parts.push(`signature=${short(reading.signature)}`); + } catch { parts.push('reading=undecodable'); } + parts.push(`bytes=${bytes.length}`); + } catch { parts.push('payload=missing or invalid'); } + return parts.join(' '); +} + +/** State reply: is_on(1) [nonce(4)] latitude(f32) longitude(f32). */ +export function describeReply(bytes: number[]): string { + const buf = Buffer.from(bytes), parts = [`state=${buf[0] === 1 ? 'ON' : buf[0] === 0 ? 'OFF' : `?(${buf[0]})`}`]; + if (buf.length === 13) parts.push(`nonce=${buf.readUInt32BE(1)}`); + if (buf.length === 9 || buf.length === 13) { + const at = buf.length - 8; + parts.push(`location=${+buf.readFloatBE(at).toFixed(5)},${+buf.readFloatBE(at + 4).toFixed(5)}`); + } + parts.push(`hex=${buf.toString('hex').toUpperCase()}`); + return parts.join(' '); +} + +export const errorText = (error: unknown) => error instanceof Error ? error.message : String(error); diff --git a/tests/services/report-log.test.ts b/tests/services/report-log.test.ts new file mode 100644 index 0000000..2830e7b --- /dev/null +++ b/tests/services/report-log.test.ts @@ -0,0 +1,37 @@ +import { describeReport, describeReply } from '../../src/utils/report-log'; +import { encodeTransaction, encode } from '../../src/lib/encode'; +import { createLogger } from '../../src/utils/logger'; + +const reading = Buffer.from(encodeTransaction({ nonce: 42, energy: 1.5, signature: 'ab'.repeat(64), voltage: 230.4, + deviceId: 'cd'.repeat(32), longitude: 7.49508, latitude: 9.05785 })); +const event = { deviceInfo: { devEui: '0011223344556677' }, fCnt: 17, fPort: 2, deduplicationId: 'dedup-1', data: reading.toString('base64') }; + +test('a report is described by its reading, with the signature and meter key shortened', () => { + const text = describeReport(event); + expect(text).toContain('devEui=0011223344556677'); + expect(text).toContain('fCnt=17'); + expect(text).toContain('nonce=42'); + expect(text).toContain('energy=1.5kWh'); + expect(text).toContain('voltage=230.4V'); + expect(text).toContain('location=9.05785,7.49508'); + expect(text).toContain('signature=abababab…abababab'); + expect(text).toContain('bytes=112'); + expect(text).not.toContain('ab'.repeat(20)); + expect(describeReport({ ...event, data: 'AAAA' })).toContain('reading=undecodable'); + expect(describeReport({ deviceInfo: { devEui: 'x' } })).toContain('payload=missing or invalid'); +}); + +test('replies are described by state, nonce and location', () => { + expect(describeReply(encode({ is_on: true }, 9.5, 7.25))).toBe('state=ON location=9.5,7.25 hex=014118000040E80000'); + expect(describeReply(encode({ is_on: false, nonce: 41 } as never, 0, 0))).toMatch(/^state=OFF nonce=41 location=0,0 hex=/); +}); + +test('managed logs keep these lines readable instead of redacting them', () => { + const role = process.env.CONSOLE_ROLE, spy = jest.spyOn(console, 'log').mockImplementation(() => {}); + try { + process.env.CONSOLE_ROLE = 'gateway'; + createLogger('[agent]').info(`Report received from ChirpStack: ${describeReport(event)}`); + const line = String(spy.mock.calls[0][0]); + expect(line).toContain('nonce=42');expect(line).toContain('energy=1.5kWh');expect(line).not.toContain('[redacted'); + } finally { spy.mockRestore(); if (role === undefined) delete process.env.CONSOLE_ROLE; else process.env.CONSOLE_ROLE = role; } +}); From 1e725562eb56fb5191b3ab71fb5e06bff1556500 Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Wed, 18 Nov 2026 11:55:59 +0100 Subject: [PATCH 02/10] feat: per-tab gateway selection and clearer decision logs Each browser tab keeps its own target (a gateway or central): Terminal and ChirpStack name it in their addresses, ChirpStack logins are kept per target, and the shared selection cookie is gone. Logs now say why the agent decided a report on the gateway instead of central, why central refused an agent request, and why the power-state check failed. Non-billing ports show raw bytes, repeated states are logged once, short placeholders are no longer redacted and keyed RPC URLs are hidden. --- docs/deployment-guide.md | 2 +- src/agent/index.ts | 11 +++++- src/central/coordinator.ts | 13 +++++-- src/central/hub.ts | 22 +++++------ src/central/operations.ts | 4 +- src/central/replication.ts | 6 ++- src/central/rpc.ts | 9 ++++- src/index.ts | 6 ++- src/lib/utils.ts | 2 +- src/public/js/chirpstack-bridge.js | 11 ++++-- src/public/js/gateways.js | 21 +++++++++-- src/public/js/terminal.js | 4 +- src/services/chirpstack.ts | 24 ++++++------ src/services/mqtt.ts | 2 +- src/services/server.ts | 14 +++++-- src/services/socket-router.ts | 3 +- src/utils/logger.ts | 5 ++- src/utils/report-log.ts | 5 +++ tests/services/central-link.test.ts | 23 ++++++------ tests/services/chirpstack.test.ts | 8 ++++ tests/services/reconciliation.test.ts | 20 ++++++++++ tests/services/report-log.test.ts | 22 +++++++++++ tests/ui/gateways.spec.cjs | 53 +++++++++++++++++++++------ 23 files changed, 219 insertions(+), 71 deletions(-) diff --git a/docs/deployment-guide.md b/docs/deployment-guide.md index cb95101..288fd0a 100644 --- a/docs/deployment-guide.md +++ b/docs/deployment-guide.md @@ -279,7 +279,7 @@ a gateway) and Terminal and ChirpStack work on the central machine instead of a | **Terminal** | SSH into the central server itself. Sign in with a Linux user and password on that server. | | **ChirpStack** | Opens the dashboard set in `CENTRAL_CHIRPSTACK_URL`. | -Choosing a gateway switches both back to that gateway. Open windows close when +Choosing a gateway switches both back to that gateway. Each browser tab keeps its own choice, so you can work on one gateway in one tab and another gateway (or central) in a second tab; a new tab starts on **Central server**. Open windows close when you switch, so you never type into the wrong machine. Settings in central's `.env` (run `./auto-compose.sh` after changing them): diff --git a/src/agent/index.ts b/src/agent/index.ts index a9fde1a..a436980 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -43,7 +43,14 @@ export async function runAgent():Promise<()=>Promise> { const updater=new UpdaterClient(process.env.UPDATER_SOCKET||path.join(path.dirname(config.credentialPath),'updater.sock'),process.env.UPDATER_SECRET_PATH||path.join(path.dirname(config.credentialPath),'updater-secret')); let updatesEnabled=false;const updateStatuses=new Map(); const stateDirectory=path.dirname(config.credentialPath); - const journal=new OperationJournal(path.join(stateDirectory,'operations.db'),config.queueLimit,event=>logger.info(`Report ${event.id} is now ${event.state}`)); + const lastStates=new Map(); + const journal=new OperationJournal(path.join(stateDirectory,'operations.db'),config.queueLimit,event=>{ + // The journal can record the same state twice (for example the decision); log each change once. + if(lastStates.get(event.id)===event.state)return; + lastStates.set(event.id,event.state);if(lastStates.size>1000)lastStates.delete(lastStates.keys().next().value!); + const by=event.owner==='central'?' (decided by central)':event.owner==='local'?' (decided by this gateway: central unreachable)':''; + logger.info(`Report ${event.id} is now ${event.state}${by}`); + }); const local=new LocalDecider(gatewayFallbackPort(),readOwnerSecret(config.fallbackSecretPath),config.decisionTimeoutMs); let socket:WebSocket|undefined,stopped=false,reconnect:ReturnType|undefined; let connectedAt=0,attempt=0,mqttState:Health['mqtt']='connecting',lastError:Health['lastError']='none'; @@ -56,7 +63,7 @@ export async function runAgent():Promise<()=>Promise> { catch(error){logger.warn(`Reply for ${decision.devEui} may not have been enqueued (${errorText(error)}); it is held, never resent: ${describeReply(decision.bytes)}`);throw error;} }}; const coordinator=new UplinkCoordinator(journal,replication.guarded('central'),replication.guarded('local'),queue, - {centralHealthy:healthy,timeoutMs:config.decisionTimeoutMs,reconcileSnapshot:(owner,outcome)=>replication.observe(owner,outcome)}); + {centralHealthy:healthy,timeoutMs:config.decisionTimeoutMs,reconcileSnapshot:(owner,outcome)=>replication.observe(owner,outcome),log:message=>logger.warn(message)}); const sendHealth=()=>{ if(socket?.readyState!==WebSocket.OPEN)return; const counts=journal.counts(gateway); diff --git a/src/central/coordinator.ts b/src/central/coordinator.ts index cfd7ff4..a5a3fce 100644 --- a/src/central/coordinator.ts +++ b/src/central/coordinator.ts @@ -12,11 +12,13 @@ export interface CoordinatorOptions { centralHealthy:()=>boolean; timeoutMs:number; reconcileSnapshot?:(owner:Owner,outcome:Outcome)=>Promise; + /** Explains why a report is not decided by central. */ + log?:(message:string)=>void; } function deadline(promise:Promise,ms:number):Promise { return new Promise((resolve,reject)=>{ const timeout=setTimeout(()=>reject(new Error('Decision deadline elapsed.')),ms); - promise.then(value=>{clearTimeout(timeout);resolve(value);},()=>{clearTimeout(timeout);reject(new Error('Decider unavailable.'));}); + promise.then(value=>{clearTimeout(timeout);resolve(value);},error=>{clearTimeout(timeout);reject(new Error(`Decider unavailable: ${error instanceof Error?error.message:String(error)}`));}); }); } const terminal = (state:string) => ['enqueued','queue-observed','superseded','rejected'].includes(state); @@ -55,9 +57,14 @@ export class UplinkCoordinator { } catch {return;} } let chosen:Owner|null=null; - for(const owner of this.options.centralHealthy()?['central','local'] as const:['local'] as const) { + const healthy=this.options.centralHealthy(); + if(!healthy)this.options.log?.(`Report ${id}: central link is not ready (offline or connected under 15 s); this gateway decides`); + for(const owner of healthy?['central','local'] as const:['local'] as const) { try {await deadline(this.decider(owner).offer(row.gateway,row.event),this.options.timeoutMs);chosen=owner;break;} - catch {/* Offers never process billing, so pre-commit fallback is safe. */} + catch(error) { + // Offers never process billing, so pre-commit fallback is safe. + this.options.log?.(`Report ${id}: ${owner==='central'?'central':'this gateway'} did not take it (${error instanceof Error?error.message:String(error)})${owner==='central'?'; this gateway decides':''}`); + } } if(!chosen || !this.journal.own(id,chosen))return; row=this.journal.get(id)!; diff --git a/src/central/hub.ts b/src/central/hub.ts index de28bf2..15c457d 100644 --- a/src/central/hub.ts +++ b/src/central/hub.ts @@ -75,16 +75,9 @@ export class GatewayHub { socket.on('error',()=>socket.terminate()); }); server.once('close',()=>{for(const gateway of this.rotations.keys())this.clearRotation(gateway);for(const socket of this.sockets.values())socket.terminate();wss.close();}); - app.get('/api/gateways',(req,res)=>res.json({selected:this.selected(req),gateways:this.identities.gateways().map(g=>({...g,online:this.sockets.get(g.id)?.readyState===WebSocket.OPEN,health:this.identities.health(g.id),updates:this.identities.updates(g.id)}))})); + app.get('/api/gateways',(_req,res)=>res.json({gateways:this.identities.gateways().map(g=>({...g,online:this.sockets.get(g.id)?.readyState===WebSocket.OPEN,health:this.identities.health(g.id),updates:this.identities.updates(g.id)}))})); app.get('/api/devices/gateways',(_req,res)=>res.json({devices:this.identities.devices()})); app.post('/api/devices/:devEui/gateway',(req,res)=>{try{if(req.body?.gateway!==null&&typeof req.body?.gateway!=='string')throw new Error();this.identities.assignDevice(req.params.devEui,req.body.gateway,res.locals.centralUser.id);res.json({devices:this.identities.devices()});}catch{res.status(400).json({error:'Choose an observed device and available gateway, or null to restore discovery.'});}}); - app.post('/api/gateways/select',(req,res)=>{ - const secure=this.development?'':'; Secure'; - // null deselects: Terminal and ChirpStack then work on the central server itself. - if(req.body?.id===null){res.setHeader('Set-Cookie',`console_gateway=; Path=/; Max-Age=0; HttpOnly; SameSite=Strict${secure}`);res.json({selected:null});return;} - if(typeof req.body?.id!=='string'||!this.identities.gateways().some(g=>g.id===req.body.id&&!g.revoked)){res.status(400).json({error:'Choose an available gateway.'});return;} - res.setHeader('Set-Cookie',`console_gateway=${req.body.id}; Path=/; HttpOnly; SameSite=Strict${secure}`);res.json({selected:req.body.id}); - }); app.post('/api/gateways',(req,res)=>{ try{const g=this.identities.createGateway(String(req.body?.name||''),res.locals.centralUser.id);res.status(201).json({...g,token:this.identities.enrollment(g.id,res.locals.centralUser.id)});} catch{res.status(400).json({error:'Provide a valid gateway name.'});} @@ -118,9 +111,16 @@ export class GatewayHub { catch{res.status(409).json({error:'Credential rotation is already pending or unavailable.'});} }); } - selected(req:http.IncomingMessage):string|null { - const id=(req.headers.cookie||'').split(';').map(v=>v.trim()).find(v=>v.startsWith('console_gateway='))?.slice(16); - return id&&this.identities.gateways().some(g=>g.id===id&&!g.revoked)?id:null; + /** Each browser tab names its own target, so tabs never change each other's. + * Empty or "central" means the central server; otherwise an available gateway. + * Returns null for a gateway that is unknown, deleted or revoked. */ + target(raw:string|null|undefined):{gateway:string|null}|null { + if(!raw||raw==='central')return {gateway:null}; + return this.identities.gateways().some(g=>g.id===raw&&!g.revoked)?{gateway:raw}:null; + } + /** The target a WebSocket (such as Terminal) asked for in its address: /terminal?gateway=ID. */ + socketTarget(req:http.IncomingMessage):{gateway:string|null}|null { + return this.target(new URL(req.url||'/','http://console.invalid').searchParams.get('gateway')); } open(gateway:string,purpose:TunnelPurpose,host:string,port:number):Promise{ const tunnels=this.tunnels.get(gateway);if(!tunnels)return Promise.reject(new Error('Gateway is offline.'));return tunnels.open(purpose,host,port); diff --git a/src/central/operations.ts b/src/central/operations.ts index 14341bb..d0e49ee 100644 --- a/src/central/operations.ts +++ b/src/central/operations.ts @@ -12,7 +12,7 @@ export interface Operation { queueBefore: string[]; queueId: string | null; sequence: number; uncertainCounted: boolean; } export interface GatewayCounts { uncertain: number; reconciled: number; superseded: number; pending: number } -export type OperationLog = (event: { gateway: string; id: string; state: OperationState }) => void; +export type OperationLog = (event: { gateway: string; id: string; state: OperationState; owner?: Owner | null }) => void; type Row = Omit & { event: string; decision: string | null; attempted: number; queueBefore: string; uncertainCounted: number; }; @@ -145,6 +145,6 @@ export class OperationJournal { } replays(target:Owner):Outcome[] {return (this.db.prepare('SELECT outcome FROM agent_replays WHERE target=? ORDER BY rowid').all(target) as Array<{outcome:string}>).map(row=>JSON.parse(row.outcome) as Outcome);} replayed(target:Owner,id:string):void {this.db.prepare('DELETE FROM agent_replays WHERE id=? AND target=?').run(id,target);} - private emit(id: string): void { const row=this.get(id); if(row) this.log({gateway:row.gateway,id:row.id,state:row.state}); } + private emit(id: string): void { const row=this.get(id); if(row) this.log({gateway:row.gateway,id:row.id,state:row.state,owner:row.owner}); } close(): void { this.db.close(); } } diff --git a/src/central/replication.ts b/src/central/replication.ts index 12dd5bb..630071f 100644 --- a/src/central/replication.ts +++ b/src/central/replication.ts @@ -21,7 +21,11 @@ export class Replication { } guarded(owner:Owner):Decider { const target=owner==='central'?this.central:this.local; - return {offer:async(gateway,event)=>{await this.flush(owner);await target.offer(gateway,event);}, + return {offer:async(gateway,event)=>{ + // A decider must first learn the reports the other side decided; until then it cannot take new ones. + try{await this.flush(owner);} + catch(error){throw new Error(`replaying ${this.journal.replays(owner).length} report(s) decided elsewhere failed: ${error instanceof Error?error.message:String(error)}`);} + await target.offer(gateway,event);}, commit:(gateway,id)=>target.commit(gateway,id),outcome:(gateway,id)=>target.outcome(gateway,id),reconcile:target.reconcile?.bind(target)}; } } diff --git a/src/central/rpc.ts b/src/central/rpc.ts index 285d061..9a99832 100644 --- a/src/central/rpc.ts +++ b/src/central/rpc.ts @@ -3,6 +3,10 @@ import WebSocket from 'ws'; import type { Decider, Outcome, BillingSnapshot } from './processor'; import { parseMessage, UplinkEvent, MAX_MESSAGE_BYTES } from './protocol'; import { eventIdentity } from './operations'; +import { createLogger } from '../utils/logger'; +import { errorText } from '../utils/report-log'; + +const logger=createLogger('[central]'); export type OperationRequest = {v:1;type:'operation';requestId:string;action:'offer'|'commit'|'outcome'|'reconcile';id:string;event?:UplinkEvent;outcome?:Outcome}; export type OperationResponse = {v:1;type:'operation-result';requestId:string;id:string;outcome:Outcome|null;error:boolean}; @@ -51,7 +55,10 @@ export async function serveOperation(processor:Decider,gateway:string,request:Op else if(request.action==='outcome')outcome=await processor.outcome(gateway,request.id); else if(processor.reconcile)await processor.reconcile(gateway,request.event!,request.outcome!); else throw new Error('Reconciliation is unavailable.'); - } catch {error=true;} + } catch(failure) { + error=true; + logger.warn(`Refused ${request.action} of report ${request.id} from gateway ${gateway}: ${errorText(failure)}`); + } return {v:1,type:'operation-result',requestId:request.requestId,id:request.id,outcome,error}; } diff --git a/src/index.ts b/src/index.ts index 480ce9e..3840113 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,9 @@ else { const sessions=new Map(); attachTerminal(server,undefined,{ connection:async request=>{ - const gateway=runtime.hub.selected(request); + const target=runtime.hub.socketTarget(request); + if(!target)throw new Error('That gateway is no longer available.'); + const gateway=target.gateway; // No gateway selected: a shell on the central server itself (the container shares the host network). if(!gateway)return centralSsh(); const sock=await runtime.hub.open(gateway,'ssh','ssh',22); @@ -18,7 +20,7 @@ else { }, audit:(request,event,session)=>{ if(event==='start'){ - const user=runtime.auth.session(request),gateway=runtime.hub.selected(request)||undefined; + const user=runtime.auth.session(request),gateway=runtime.hub.socketTarget(request)?.gateway||undefined; if(user)sessions.set(session,{user:user.id,gateway}); } const actor=sessions.get(session); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 8a44a90..750789c 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -55,7 +55,7 @@ export async function runHook(hook: K, ...args: Parameter if (fn) functionReturn = await (fn as any).call(ext, ...args); } catch (error) { errors.push(error); - logger.error(`Extension hook ${hook} failed:`, error); + logger.error(`Extension hook ${hook} failed: ${error instanceof Error ? error.message : String(error)}`); } if (typeof functionReturn === "boolean" && hook === "isOnStateCompute") { diff --git a/src/public/js/chirpstack-bridge.js b/src/public/js/chirpstack-bridge.js index 809198a..d298fdb 100644 --- a/src/public/js/chirpstack-bridge.js +++ b/src/public/js/chirpstack-bridge.js @@ -2,10 +2,14 @@ (function () { 'use strict'; if (!location.pathname.startsWith('/chirpstack/')) return; + // Central serves each target under its own path (/chirpstack/~GATEWAY_ID/), so two + // tabs can show two ChirpStacks; standalone uses /chirpstack/. + const scope = /^\/chirpstack\/~([\w-]{1,64})\//.exec(location.pathname); + const base = scope ? `/chirpstack/~${scope[1]}` : '/chirpstack'; function proxied(value) { const url = new URL(String(value), location.href); - if (url.origin === location.origin && !url.pathname.startsWith('/chirpstack/')) { - url.pathname = '/chirpstack' + url.pathname; + if (url.origin === location.origin && !url.pathname.startsWith(base + '/')) { + url.pathname = base + (url.pathname.startsWith('/chirpstack/') ? url.pathname.slice('/chirpstack'.length) : url.pathname); } return url.href; } @@ -35,7 +39,8 @@ for (const name of ['localStorage', 'sessionStorage']) { try { const storage = window[name]; - const prefix = 'm3tering.chirpstack.'; + // Each target keeps its own ChirpStack login: one gateway's token never reaches another. + const prefix = scope ? `m3tering.chirpstack.${scope[1]}.` : 'm3tering.chirpstack.'; const keys = () => Object.keys(storage).filter(key => key.startsWith(prefix)); const scoped = { get length() { return keys().length; }, diff --git a/src/public/js/gateways.js b/src/public/js/gateways.js index 138200b..d93e7ac 100644 --- a/src/public/js/gateways.js +++ b/src/public/js/gateways.js @@ -2,7 +2,18 @@ const $=id=>document.getElementById(id); const switcher=$('gateway-switcher'),menu=$('gateway-menu'),options=$('gateway-options'),dialog=$('gateway-dialog'),list=$('gateway-list'),confirmDialog=$('gateway-confirm'); const NETWORK_APPS=['terminal','chirpstack'],TOKEN_LIFETIME=10*60000,CENTRAL_NAME='Central server'; - let gateways=[],selected=null,busy=false,again=false,editing=null,focusRename=false,tokenTimer=null; + // The target belongs to this tab only (sessionStorage): other tabs keep their own gateway or central. + const TARGET_KEY='m3tering.console.target'; + const loadTarget=()=>{try{return sessionStorage.getItem(TARGET_KEY)||null;}catch{return null;}}; + let gateways=[],selected=loadTarget(),busy=false,again=false,editing=null,focusRename=false,tokenTimer=null; + // Terminal and ChirpStack name this tab's target in their addresses; "central" means the central server. + window.ConsoleTarget=()=>selected||'central'; + function applyTarget(){ + try{if(selected)sessionStorage.setItem(TARGET_KEY,selected);else sessionStorage.removeItem(TARGET_KEY);}catch{/* still works for this page */} + const frame=$('chirpstack-frame'),src=`/chirpstack/~${encodeURIComponent(selected||'central')}/`; + // Unload the previous target's ChirpStack; the next launch loads this one. + if(frame&&frame.dataset.src!==src){frame.dataset.src=src;if(frame.hasAttribute('src')){frame.src='about:blank';frame.removeAttribute('src');}} + } const openUpdates=new Set(); const error=message=>{$('gateway-error').textContent=message||'';}; @@ -120,8 +131,7 @@ // null deselects the gateway. if(id===selected){setMenu(false);switcher.focus();return;} for(const app of NETWORK_APPS)Desktop.close(app); - try{await api('/api/gateways/select',{id});await refresh();setMenu(false);switcher.focus();} - catch(e){$('gateway-menu-summary').textContent=e.message;} + selected=id;applyTarget();render();setMenu(false);switcher.focus(); } function stopEditing(){editing=null;if(list.contains(document.activeElement))document.activeElement.blur();} function startEditing(id){editing=id;focusRename=true;render();} @@ -195,7 +205,9 @@ async function refresh(){ if(busy){again=true;return;}busy=true; - try{const result=await api('/api/gateways');gateways=result.gateways;selected=result.selected; + try{const result=await api('/api/gateways');gateways=result.gateways; + // A gateway deleted or revoked elsewhere can't stay this tab's target. + if(selected&&!gateways.some(g=>g.id===selected&&!g.revoked)){for(const app of NETWORK_APPS)Desktop.close(app);selected=null;applyTarget();} if(editing&&!gateways.some(g=>g.id===editing))editing=null;render();} catch(e){error(e.message);} finally{busy=false;if(again){again=false;void refresh();}} @@ -262,5 +274,6 @@ window.addEventListener('desktop:open',event=>{if(NETWORK_APPS.includes(event.detail.id))title(event.detail.id);}); // Keep titles of already-open network apps in step with a rename. const retitle=()=>{for(const app of NETWORK_APPS)if($(app)&&!$(app).hidden)title(app);}; + applyTarget(); const timer=setInterval(()=>{void refresh().then(retitle);},5000);window.addEventListener('pagehide',()=>clearInterval(timer));refresh(); })(); diff --git a/src/public/js/terminal.js b/src/public/js/terminal.js index 8ad323f..c25bb24 100644 --- a/src/public/js/terminal.js +++ b/src/public/js/terminal.js @@ -55,7 +55,9 @@ function connectTerminal(event) { const password = document.getElementById('password').value; status.textContent = 'Connecting to terminal…'; const protocol = location.protocol === 'https:' ? 'wss' : 'ws'; - const connection = new WebSocket(`${protocol}://${location.host}/terminal`); + // On central, this tab's own target (a gateway ID or "central"); other tabs keep theirs. + const target = typeof window.ConsoleTarget === 'function' ? `?gateway=${encodeURIComponent(window.ConsoleTarget())}` : ''; + const connection = new WebSocket(`${protocol}://${location.host}/terminal${target}`); socket = connection; let failure = ''; const timeout = setTimeout(() => { failure = 'Terminal connection timed out. Check that SSH is running on the Console host.'; connection.close(); }, 35000); diff --git a/src/services/chirpstack.ts b/src/services/chirpstack.ts index 7444843..1fb8a04 100644 --- a/src/services/chirpstack.ts +++ b/src/services/chirpstack.ts @@ -21,14 +21,14 @@ function cleanHeaders(headers: IncomingHttpHeaders): IncomingHttpHeaders { return result; } -export function rewriteChirpstackText(body: string, html: boolean): string { +export function rewriteChirpstackText(body: string, html: boolean, base = prefix): string { if (!html) { // Vite / CRA asset references in bundles and stylesheets (including lazy chunks). - return body.replace(/(["'`(])\/(assets|static)\//g, `$1${prefix}/$2/`) - .replace(/(["'`(])\/(?!chirpstack\/)([^"'`()\s]+\.(?:png|svg|ico|jpe?g|webp|gif|woff2?)(?:\?[^"'`()\s]*)?)(?=["'`)])/g, `$1${prefix}/$2`); + return body.replace(/(["'`(])\/(assets|static)\//g, `$1${base}/$2/`) + .replace(/(["'`(])\/(?!chirpstack\/)([^"'`()\s]+\.(?:png|svg|ico|jpe?g|webp|gif|woff2?)(?:\?[^"'`()\s]*)?)(?=["'`)])/g, `$1${base}/$2`); } const rewritten = body.replace(/\b(src|href|action)=(['"])\/(?!\/|chirpstack(?:\/|['"]))([^'"]*)\2/gi, - (_, attr, quote, path) => `${attr}=${quote}${prefix}/${path}${quote}`); + (_, attr, quote, path) => `${attr}=${quote}${base}/${path}${quote}`); // Runs before the upstream application: remaps gRPC-Web and isolates storage. return rewritten.replace(/]*)?>/i, '$&'); } @@ -44,17 +44,19 @@ function unavailable(res: Response, configuration = false): void { export function proxyChirpstack(req: Request, res: Response): void { proxy(req,res); } -export function proxyChirpstackThrough(req:Request,res:Response,target:URL,agent?:http.Agent):void { - proxy(req,res,{target,agent}); +export function proxyChirpstackThrough(req:Request,res:Response,target:URL,agent?:http.Agent,base?:string):void { + proxy(req,res,{target,agent,base}); } -function proxy(req:Request,res:Response,transport?:{target:URL;agent?:http.Agent}):void { +/** `base` is the public path ChirpStack is served under: /chirpstack, or /chirpstack/~TARGET on central. */ +function proxy(req:Request,res:Response,transport?:{target:URL;agent?:http.Agent;base?:string}):void { + const base = transport?.base || prefix; res.setHeader('Cache-Control', 'no-store'); res.setHeader('Referrer-Policy', 'no-referrer'); res.setHeader('X-Frame-Options', 'SAMEORIGIN'); res.setHeader('Content-Security-Policy', "frame-ancestors 'self'"); if (!isSameOrigin(req)) { res.status(403).send('Cross-origin requests are not permitted'); return; } - if (req.originalUrl.split('?')[0] === prefix) { - res.redirect(307, `${prefix}/${req.originalUrl.includes('?') ? req.originalUrl.slice(req.originalUrl.indexOf('?')) : ''}`); + if (req.originalUrl.split('?')[0] === base) { + res.redirect(307, `${base}/${req.originalUrl.includes('?') ? req.originalUrl.slice(req.originalUrl.indexOf('?')) : ''}`); return; } let target: URL; @@ -79,7 +81,7 @@ function proxy(req:Request,res:Response,transport?:{target:URL;agent?:http.Agent if (responseHeaders.location) { try { const location = new URL(responseHeaders.location, new URL(target.origin + req.url)); - if (location.origin === target.origin) responseHeaders.location = prefix + location.pathname + location.search + location.hash; + if (location.origin === target.origin) responseHeaders.location = base + location.pathname + location.search + location.hash; } catch { delete responseHeaders.location; } } const type = String(responseHeaders['content-type'] || ''); @@ -106,7 +108,7 @@ function proxy(req:Request,res:Response,transport?:{target:URL;agent?:http.Agent chunks.push(chunk); }); response.on('end', () => { - if (!res.destroyed && !res.writableEnded) res.end(rewriteChirpstackText(Buffer.concat(chunks).toString('utf8'), html)); + if (!res.destroyed && !res.writableEnded) res.end(rewriteChirpstackText(Buffer.concat(chunks).toString('utf8'), html, base)); }); }); upstream.setTimeout(60_000, () => upstream.destroy(new Error('ChirpStack upstream timeout'))); diff --git a/src/services/mqtt.ts b/src/services/mqtt.ts index 2c633e2..c685ad8 100644 --- a/src/services/mqtt.ts +++ b/src/services/mqtt.ts @@ -142,7 +142,7 @@ export async function handleMessage(blob: Buffer, downlink: DownlinkSink = chirp let is_on = false; try { is_on = await runHook('isOnStateCompute', meter.tokenId); } catch (error) { - log.error('Could not determine meter power state:', error); + log.error(`Could not determine meter power state, so the reply is OFF: ${errorText(error)}`); void notifyHook('onIsOnStateComputeError', meter.tokenId, error); } void notifyHook('onIsOnStateComputed', meter.tokenId, is_on); diff --git a/src/services/server.ts b/src/services/server.ts index dfe1c77..5ac0677 100644 --- a/src/services/server.ts +++ b/src/services/server.ts @@ -30,11 +30,19 @@ app.set('views', './src/views'); central?.auth.mount(app,central.hub.machineRoutes); const gatewayHub=central?.hub; app.use('/chirpstack', gatewayHub ? (req,res) => { - const gateway=gatewayHub.selected(req); + // Each tab names its target in the path, /chirpstack/~GATEWAY_ID/ or /chirpstack/~central/, + // so two tabs can show two different ChirpStacks at once. + const match=/^\/~([\w-]{1,64})(?=\/|\?|$)/.exec(req.url); + const name=match?.[1]||'central',base=`/chirpstack/~${name}`; + if(match)req.url=req.url.slice(match[0].length)||'/'; + if(match&&!req.url.startsWith('/'))req.url='/'+req.url; + const target=gatewayHub.target(name); + if(!target){res.status(404).type('text').send('That gateway is no longer available. Choose another in the gateway menu.');return;} + const gateway=target.gateway; if(!gateway){ // No gateway selected: show the ChirpStack dashboard configured for central. let dashboard:URL|undefined,invalid=false;try{dashboard=centralChirpstackUrl();}catch{invalid=true;} - if(dashboard){proxyChirpstackThrough(req,res,dashboard);return;} + if(dashboard){proxyChirpstackThrough(req,res,dashboard,undefined,base);return;} const heading=invalid?'CENTRAL_CHIRPSTACK_URL is not a valid address':'No ChirpStack dashboard set'; res.status(409).type('html').send(`ChirpStack

${heading}

Select a gateway to open its ChirpStack, or set CENTRAL_CHIRPSTACK_URL in central's .env to your online ChirpStack dashboard's address (such as https://chirpstack.example.com, with no path) and run ./auto-compose.sh.

`); return; @@ -43,7 +51,7 @@ app.use('/chirpstack', gatewayHub ? (req,res) => { if(req.destroyed||res.destroyed){stream.destroy();return;} const agent=new http.Agent({keepAlive:false});agent.createConnection=()=>stream; res.once('close',()=>{agent.destroy();stream.destroy();}); - proxyChirpstackThrough(req,res,new URL(stream.upstreamOrigin||'http://chirpstack:8080'),agent); + proxyChirpstackThrough(req,res,new URL(stream.upstreamOrigin||'http://chirpstack:8080'),agent,base); }).catch(()=>{if(!res.headersSent)res.status(502).send('Gateway ChirpStack is unavailable.');else res.destroy();}); } : proxyChirpstack); app.use(requireSameOrigin); diff --git a/src/services/socket-router.ts b/src/services/socket-router.ts index 4288025..7342477 100644 --- a/src/services/socket-router.ts +++ b/src/services/socket-router.ts @@ -14,7 +14,8 @@ export function registerSocket(server: http.Server, path: string, wss: WebSocket if (!routes) { routes = new Map(); servers.set(server, routes); server.on('upgrade', (request, socket, head) => { - const target = routes!.get(request.url || ''); + // Match on the path: a socket may carry its target in the query (/terminal?gateway=ID). + const target = routes!.get((request.url || '').split('?')[0]); const guard=sessionGuards.get(server); if (!target || (!target.machine && (!isSameOrigin(request, true) || (guard && !guard(request)))) || (target.authorize && !target.authorize(request))) { socket.end('HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n'); return; diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 6d20ad3..57b8108 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -16,7 +16,10 @@ function managedLog(value:unknown,depth=0):unknown { if(typeof value==='string'){ let text=value; for(const [name,secret] of Object.entries(process.env)){ - if(secret&&/(?:PASSWORD|SECRET|TOKEN|PRIVATE_KEY|CREDENTIAL)/i.test(name))text=text.split(secret).join('[redacted]'); + // Very short values (such as a template's "...") are not real secrets and would mangle ordinary text. + if(secret&&secret.length>=8&&/(?:PASSWORD|SECRET|TOKEN|PRIVATE_KEY|CREDENTIAL)/i.test(name))text=text.split(secret).join('[redacted]'); + // RPC providers often put an API key in the URL path or query; plain public endpoints stay readable. + else if(secret&&/RPC/i.test(name)&&/^https?:\/\/[^/]+\/./i.test(secret))text=text.split(secret).join('[rpc url redacted]'); } return text.replace(/(?:0x)?[a-fA-F0-9]{64,}/g,'[redacted bytes]') .replace(/[A-Za-z0-9+/]{80,}={0,2}/g,'[redacted payload]') diff --git a/src/utils/report-log.ts b/src/utils/report-log.ts index e9b440e..adb7f5d 100644 --- a/src/utils/report-log.ts +++ b/src/utils/report-log.ts @@ -16,6 +16,11 @@ export function describeReport(event: Uplink): string { if (typeof event.deduplicationId === 'string') parts.push(`id=${event.deduplicationId}`); try { const bytes = uplinkPayload(event).bytes; + // Only fPort 2 carries signed readings; other ports use their own formats. + if (event.fPort !== undefined && event.fPort !== 2) { + parts.push(`data=${short(bytes.toString('hex').toUpperCase())}`, `bytes=${bytes.length}`); + return parts.join(' '); + } try { const reading = decodePayload(bytes), ext = reading.extensions; parts.push(`nonce=${reading.nonce}`, `energy=${reading.energy}kWh`); diff --git a/tests/services/central-link.test.ts b/tests/services/central-link.test.ts index 0f064fa..0c78221 100644 --- a/tests/services/central-link.test.ts +++ b/tests/services/central-link.test.ts @@ -76,19 +76,20 @@ test('rename updates the list; delete disconnects the agent and removes the gate }finally{await f.close();} }); -test('selecting null clears the gateway so apps target the central server',async()=>{ +test('each tab names its own target: central, an available gateway, or nothing that has gone',async()=>{ const f=await fixture(); try{ - const post=(body:unknown)=>fetch(f.base+'/api/gateways/select',{method:'POST',headers:{cookie:f.cookie,origin:f.base,'Content-Type':'application/json'},body:JSON.stringify(body)}); - const chosen=await post({id:f.enrolled.gateway}); - expect(chosen.status).toBe(200);expect(chosen.headers.get('set-cookie')).toContain(`console_gateway=${f.enrolled.gateway}`); - const cleared=await post({id:null}); - expect(cleared.status).toBe(200);expect(await cleared.json()).toEqual({selected:null}); - expect(cleared.headers.get('set-cookie')).toMatch(/^console_gateway=; Path=\/; Max-Age=0; HttpOnly; SameSite=Strict/); - const list=await (await fetch(f.base+'/api/gateways',{headers:{cookie:f.cookie}})).json() as {selected:unknown}; - expect(list.selected).toBeNull(); - expect((await post({id:'not-a-gateway'})).status).toBe(400); - expect((await post({})).status).toBe(400); + expect(f.hub.target(null)).toEqual({gateway:null}); + expect(f.hub.target('central')).toEqual({gateway:null}); + expect(f.hub.target(f.enrolled.gateway)).toEqual({gateway:f.enrolled.gateway}); + expect(f.hub.target('not-a-gateway')).toBeNull(); + expect(f.hub.socketTarget({url:`/terminal?gateway=${f.enrolled.gateway}`} as never)).toEqual({gateway:f.enrolled.gateway}); + expect(f.hub.socketTarget({url:'/terminal'} as never)).toEqual({gateway:null}); + // There is no shared, cookie-based selection any more. + const list=await (await fetch(f.base+'/api/gateways',{headers:{cookie:f.cookie}})).json() as Record; + expect(list).not.toHaveProperty('selected'); + expect((await fetch(f.base+'/api/gateways/select',{method:'POST',headers:{cookie:f.cookie,origin:f.base,'Content-Type':'application/json'},body:'{"id":null}'})).status).toBe(404); + f.store.revoke(f.enrolled.gateway,'test');expect(f.hub.target(f.enrolled.gateway)).toBeNull(); }finally{await f.close();} }); diff --git a/tests/services/chirpstack.test.ts b/tests/services/chirpstack.test.ts index 8707818..8e24134 100644 --- a/tests/services/chirpstack.test.ts +++ b/tests/services/chirpstack.test.ts @@ -54,3 +54,11 @@ test('a direct dashboard origin is proxied without a tunnel agent', async () => expect(await response.text()).toBe('dashboard /tenants cookie=none'); } finally { server.close(); upstream.close(); } }); + +test('a per-target base keeps asset and link rewrites under that target on central', () => { + const base = '/chirpstack/~gateway-1'; + const html = rewriteChirpstackText('t', true, base); + expect(html).toContain('src="/chirpstack/~gateway-1/assets/app.js"'); + expect(html).toContain('href="/chirpstack/~gateway-1/tenants"'); + expect(rewriteChirpstackText('import("/assets/chunk.js")', false, base)).toBe('import("/chirpstack/~gateway-1/assets/chunk.js")'); +}); diff --git a/tests/services/reconciliation.test.ts b/tests/services/reconciliation.test.ts index 5da5fc5..7a4394f 100644 --- a/tests/services/reconciliation.test.ts +++ b/tests/services/reconciliation.test.ts @@ -67,3 +67,23 @@ test('reconciliation cannot attach another device decision or meter snapshot to expect(await processor.outcome('gateway',report.deduplicationId!)).toBeNull(); }finally{processor.close();} }); + +test('when central cannot take a report the agent logs why, including a failed replay of earlier decisions',async()=>{ + const journal=new OperationJournal(':memory:'); + const central=new DurableProcessor(':memory:',async()=>{},()=>snapshot,async()=>{}); + const local=new DurableProcessor(':memory:',async(_event,sink)=>sink(decision),()=>snapshot); + jest.spyOn(central,'reconcile').mockRejectedValue(new Error('meter token does not match the contract')); + let healthy=false;const messages:string[]=[]; + const replication=new Replication(journal,central,local); + const queue={list:jest.fn(async()=>[]),enqueue:jest.fn(async()=>'queue-1')}; + const coordinator=new UplinkCoordinator(journal,replication.guarded('central'),replication.guarded('local'),queue, + {centralHealthy:()=>healthy,timeoutMs:50,reconcileSnapshot:(owner,outcome)=>replication.observe(owner,outcome),log:message=>messages.push(message)}); + try{ + await coordinator.receive('gateway',report); + expect(messages).toEqual([`Report ${report.deduplicationId}: central link is not ready (offline or connected under 15 s); this gateway decides`]); + healthy=true;messages.length=0; + await coordinator.receive('gateway',{...report,deduplicationId:'replay-2'}); + expect(messages).toEqual(['Report replay-2: central did not take it (Decider unavailable: replaying 1 report(s) decided elsewhere failed: meter token does not match the contract); this gateway decides']); + expect(journal.get('replay-2')?.owner).toBe('local'); + }finally{await coordinator.stop();journal.close();central.close();local.close();} +}); diff --git a/tests/services/report-log.test.ts b/tests/services/report-log.test.ts index 2830e7b..03ec7bc 100644 --- a/tests/services/report-log.test.ts +++ b/tests/services/report-log.test.ts @@ -19,6 +19,10 @@ test('a report is described by its reading, with the signature and meter key sho expect(text).not.toContain('ab'.repeat(20)); expect(describeReport({ ...event, data: 'AAAA' })).toContain('reading=undecodable'); expect(describeReport({ deviceInfo: { devEui: 'x' } })).toContain('payload=missing or invalid'); + // Other ports have their own formats: shown as raw bytes, never as fake readings. + const other = describeReport({ ...event, fPort: 4 }); + expect(other).toContain('fPort=4');expect(other).toContain('bytes=112');expect(other).toMatch(/data=[0-9A-F]{8}…[0-9A-F]{8}/); + expect(other).not.toContain('nonce=');expect(other).not.toContain('energy='); }); test('replies are described by state, nonce and location', () => { @@ -35,3 +39,21 @@ test('managed logs keep these lines readable instead of redacting them', () => { expect(line).toContain('nonce=42');expect(line).toContain('energy=1.5kWh');expect(line).not.toContain('[redacted'); } finally { spy.mockRestore(); if (role === undefined) delete process.env.CONSOLE_ROLE; else process.env.CONSOLE_ROLE = role; } }); + +test('a template placeholder such as API_TOKEN=... does not redact ordinary text, real secrets still are', () => { + const saved = { ...process.env }, spy = jest.spyOn(console, 'log').mockImplementation(() => {}); + try { + Object.assign(process.env, { CONSOLE_ROLE: 'central', API_TOKEN: '...', CENTRAL_ADMIN_PASSWORD: 'a-real-password-1' }); + createLogger('[index]').info('Starting application initialization... with a-real-password-1'); + expect(String(spy.mock.calls[0][0])).toBe('[info] [index] Starting application initialization... with [redacted]'); + } finally { spy.mockRestore(); process.env = saved; } +}); + +test('RPC addresses with a key in the path are hidden; plain public endpoints stay readable', () => { + const saved = { ...process.env }, spy = jest.spyOn(console, 'log').mockImplementation(() => {}); + try { + Object.assign(process.env, { CONSOLE_ROLE: 'gateway', MAINNET_RPC: 'https://mainnet.example.io/v3/abc123key', ARBITRUM_RPC: 'https://arb.example.org' }); + createLogger('[x]').info('failed https://mainnet.example.io/v3/abc123key and https://arb.example.org'); + expect(String(spy.mock.calls[0][0])).toBe('[info] [x] failed [rpc url redacted] and https://arb.example.org'); + } finally { spy.mockRestore(); process.env = saved; } +}); diff --git a/tests/ui/gateways.spec.cjs b/tests/ui/gateways.spec.cjs index a3caa9e..6254e83 100644 --- a/tests/ui/gateways.spec.cjs +++ b/tests/ui/gateways.spec.cjs @@ -1,13 +1,15 @@ const { test,expect }=require('@playwright/test'); +// Start a tab on a gateway (main page only, and only before the tab has chosen anything itself). +const pick=(page,id)=>page.addInitScript(target=>{if(window.top!==window||sessionStorage.getItem('picked'))return;sessionStorage.setItem('picked','1');sessionStorage.setItem('m3tering.console.target',target);},id); + test('central selector identifies the gateway and exposes uncertainty without sending relay commands',async({page})=>{ - let selected='gateway-one'; const gateways=[ {id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected',queueDepth:1,uncertain:3,reconciled:2,superseded:1}}, {id:'gateway-two',name:'South gateway',online:false,revoked:false,enrolled:true,health:{mqtt:'disconnected',queueDepth:0,uncertain:0,reconciled:0,superseded:0}}, ]; - await page.route('**/api/gateways',route=>route.fulfill({json:{selected,gateways}})); - await page.route('**/api/gateways/select',route=>{selected=route.request().postDataJSON().id;return route.fulfill({json:{selected}});}); + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways}})); + await pick(page,'gateway-one'); await page.goto('/?central=1'); // Status lives only in the gateway menu: nothing floats over the desktop. await expect(page.locator('#gateway-status')).toHaveCount(0); @@ -28,6 +30,7 @@ test('central selector identifies the gateway and exposes uncertainty without se await expect(page.locator('#chirpstack')).toBeHidden(); await page.evaluate(()=>Desktop.launch('chirpstack')); await expect(page.locator('#chirpstack .window-title')).toContainText('South gateway'); + await expect(page.locator('#chirpstack-frame')).toHaveAttribute('src','/chirpstack/~gateway-two/'); await page.locator('#gateway-switcher').click(); await page.getByRole('button',{name:'Manage gateways'}).click(); await expect(page.locator('#gateway-dialog')).toBeVisible(); @@ -39,32 +42,60 @@ test('central selector identifies the gateway and exposes uncertainty without se }); test('the gateway can be deselected so apps open on the central server',async({page})=>{ - let selected='gateway-one';const posted=[]; const gateways=[{id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}]; - await page.route('**/api/gateways',route=>route.fulfill({json:{selected,gateways}})); - await page.route('**/api/gateways/select',route=>{selected=route.request().postDataJSON().id;posted.push(selected);return route.fulfill({json:{selected}});}); + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways}})); + await pick(page,'gateway-one'); await page.goto('/?central=1'); + const target=()=>page.evaluate(()=>sessionStorage.getItem('m3tering.console.target')); await expect(page.locator('#gateway-switcher-name')).toHaveText('North gateway'); await page.evaluate(()=>Desktop.launch('chirpstack')); await page.locator('#gateway-switcher').click(); await page.getByRole('option',{name:/Central server/}).click(); - expect(posted).toEqual([null]); + expect(await target()).toBeNull(); await expect(page.locator('#gateway-switcher-name')).toHaveText('Central server'); await expect(page.locator('#gateway-switcher')).toHaveAttribute('data-state','central'); await expect(page.locator('#chirpstack')).toBeHidden(); await page.evaluate(()=>Desktop.launch('chirpstack')); await expect(page.locator('#chirpstack .window-title')).toContainText('Central server'); + await expect(page.locator('#chirpstack-frame')).toHaveAttribute('src','/chirpstack/~central/'); await page.locator('#gateway-switcher').click(); await expect(page.locator('#gateway-current')).toContainText('open on the central server'); await expect(page.getByRole('option',{name:/Central server/})).toHaveAttribute('aria-selected','true'); await page.getByRole('option',{name:/North gateway/}).click(); - expect(posted).toEqual([null,'gateway-one']); + expect(await target()).toBe('gateway-one'); await expect(page.locator('#gateway-switcher-name')).toHaveText('North gateway'); + // A reload keeps this tab's choice. + await page.reload();await expect(page.locator('#gateway-switcher-name')).toHaveText('North gateway'); +}); + +test('two tabs keep their own targets: switching in one never changes the other',async({context})=>{ + const gateways=[ + {id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}, + {id:'gateway-two',name:'South gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}, + ]; + await context.route('**/api/gateways',route=>route.fulfill({json:{gateways}})); + const first=await context.newPage(),second=await context.newPage(); + await first.goto('/?central=1');await second.goto('/?central=1'); + await first.locator('#gateway-switcher').click();await first.getByRole('option',{name:/North gateway/}).click(); + await second.locator('#gateway-switcher').click();await second.getByRole('option',{name:/South gateway/}).click(); + // Let the first tab refresh its gateway list: its choice must survive. + await first.waitForTimeout(5500); + await expect(first.locator('#gateway-switcher-name')).toHaveText('North gateway'); + await expect(second.locator('#gateway-switcher-name')).toHaveText('South gateway'); + for(const [page,id] of [[first,'gateway-one'],[second,'gateway-two']]){ + await page.evaluate(()=>Desktop.launch('chirpstack')); + await expect(page.locator('#chirpstack-frame')).toHaveAttribute('src',`/chirpstack/~${id}/`); + expect(await page.evaluate(()=>window.ConsoleTarget())).toBe(id); + } + // A third, new tab starts on the central server rather than inheriting either choice. + const third=await context.newPage();await third.goto('/?central=1'); + await expect(third.locator('#gateway-switcher-name')).toHaveText('Central server'); }); test('on a phone the top bar fits and the gateway menu is a full-width sheet',async({page})=>{ await page.setViewportSize({width:390,height:844}); - await page.route('**/api/gateways',route=>route.fulfill({json:{selected:'g',gateways:[{id:'g',name:'A gateway with a rather long site name',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}]}})); + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways:[{id:'g',name:'A gateway with a rather long site name',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}]}})); + await pick(page,'g'); await page.route('**/api/account',route=>route.fulfill({json:{username:'operator'}})); await page.goto('/?central=1'); const topbar=await page.locator('.topbar').boundingBox(); @@ -101,7 +132,7 @@ test('gateways can be added, renamed and deleted from the management dialog',asy gateways=[...gateways,{id:'gateway-new',name,online:false,revoked:false,enrolled:false,lastHeartbeat:null,version:null,health:null,updates:[]}]; return route.fulfill({status:201,json:{id:'gateway-new',name,token:'T'.repeat(43)}}); } - return route.fulfill({json:{selected:null,gateways}}); + return route.fulfill({json:{gateways}}); }); await page.route('**/api/gateways/*/rename',route=>{ const id=route.request().url().split('/').at(-2),{name}=route.request().postDataJSON();calls.push(['rename',id,name]); @@ -147,7 +178,7 @@ test('gateways can be added, renamed and deleted from the management dialog',asy test('the account dialog changes the password and reports mistakes',async({page})=>{ const posts=[]; - await page.route('**/api/gateways',route=>route.fulfill({json:{selected:null,gateways:[]}})); + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways:[]}})); await page.route('**/api/account',route=>route.fulfill({json:{username:'admin'}})); await page.route('**/api/account/password',route=>{const body=route.request().postDataJSON();posts.push(body); return body.current==='right-pass'?route.fulfill({json:{changed:true}}):route.fulfill({status:400,json:{error:'Your current password is incorrect.'}});}); From a1584c3606a29a007ffe79752f39f3f331d548ce Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Wed, 18 Nov 2026 12:34:45 +0100 Subject: [PATCH 03/10] fix: let central start a meter's history on first contact A gateway that billed a meter standalone before joining central could never hand its fallback decisions to central: the first replayed nonce looked like a gap, so every later report fell back to the gateway. Central now starts a meter it has never seen at the oldest signed record (signature and token still checked); later gaps are refused. --- src/central/billing.ts | 24 +++++++++++++++---- tests/services/billing-import.test.ts | 34 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/services/billing-import.test.ts diff --git a/src/central/billing.ts b/src/central/billing.ts index 6b29156..01cfc5e 100644 --- a/src/central/billing.ts +++ b/src/central/billing.ts @@ -5,6 +5,7 @@ import { managedBilling } from './billing-context'; import type { UplinkEvent } from './protocol'; import { verifyPayloadSignature, distributeManaged } from '../lib/utils'; import { meterIdFromPublicKeyContractCall, meterLatestNonceContractCall } from '../utils/contracts'; +import { createLogger } from '../utils/logger'; export function billingSnapshot(event:UplinkEvent):BillingSnapshot { return readBillingSnapshot(event.deviceInfo.devEui); @@ -14,7 +15,13 @@ export function createBillingProcessor(filename:string):DurableProcessor { const context={id:event.deduplicationId!,distribution:[]}; await managedBilling.run(context,()=>handleMessage(Buffer.from(JSON.stringify(event)),sink)); void distributeManaged(context.distribution).catch(()=>{}); - },billingSnapshot,async snapshot=>{ + },billingSnapshot,importBillingSnapshot); +} + +const logger=createLogger('[central]'); +/** Accept another decider's billing state (a gateway's fallback decisions) after + * checking it against the meter's signatures, its token and the chain. */ +export async function importBillingSnapshot(snapshot:BillingSnapshot):Promise { const meter=snapshot.meter;if(!meter)return; const known=getMeterByPublicKey(meter.publicKey); const token=known?.tokenId ?? await meterIdFromPublicKeyContractCall(meter.publicKey); @@ -22,15 +29,22 @@ export function createBillingProcessor(filename:string):DurableProcessor { const chainNonce=await meterLatestNonceContractCall(token); if(!Number.isSafeInteger(chainNonce)||chainNonce<0)throw new Error('Cannot verify reconciliation against the chain.'); let supportedNonce=Math.max(known?.latestNonce??-1,chainNonce); - for(const record of [...snapshot.transactions].sort((a,b)=>a.nonce-b.nonce)){ + const records=[...snapshot.transactions].sort((a,b)=>a.nonce-b.nonce); + // First contact: central has never seen this meter, for example a gateway that ran + // standalone before joining central. Its history is not here, so start from the oldest + // meter-signed record (still checked below). Once central knows the meter, gaps are refused. + if(!known&&records.length&&records[0].nonce>supportedNonce+1){ + logger.warn(`Meter ${token} is new to central: starting its history at nonce ${records[0].nonce} (chain has ${chainNonce}); earlier readings stay with the gateway that billed them.`); + supportedNonce=records[0].nonce-1; + } + for(const record of records){ const bytes=Buffer.from(record.raw,'hex'); if(record.identifier!==token || record.nonce!==bytes.readUInt32BE(0) || bytes.length<74 || !verifyPayloadSignature(bytes,Buffer.from(meter.publicKey.replace(/^0x/,''),'hex')))throw new Error('Invalid signed reconciliation record.'); - if(record.nonce>supportedNonce+1)throw new Error('Reconciliation has a gap before this nonce.'); + if(record.nonce>supportedNonce+1)throw new Error(`Reconciliation has a gap before nonce ${record.nonce} (central can confirm up to ${supportedNonce}).`); supportedNonce=Math.max(supportedNonce,record.nonce); } - if(meter.latestNonce>supportedNonce)throw new Error('Reconciliation nonce is not supported by signed records or the chain.'); + if(meter.latestNonce>supportedNonce)throw new Error(`Reconciliation nonce ${meter.latestNonce} is not supported by signed records or the chain (up to ${supportedNonce}).`); mergeBillingSnapshot(snapshot); void distributeManaged(snapshot.transactions).catch(()=>{}); - }); } diff --git a/tests/services/billing-import.test.ts b/tests/services/billing-import.test.ts new file mode 100644 index 0000000..bab776b --- /dev/null +++ b/tests/services/billing-import.test.ts @@ -0,0 +1,34 @@ +jest.mock('../../src/store/sqlite', () => ({ getMeterByPublicKey: jest.fn(), mergeBillingSnapshot: jest.fn(), readBillingSnapshot: jest.fn() })); +jest.mock('../../src/utils/contracts', () => ({ meterIdFromPublicKeyContractCall: jest.fn(async () => 7), meterLatestNonceContractCall: jest.fn(async () => 100) })); +jest.mock('../../src/lib/utils', () => ({ verifyPayloadSignature: jest.fn(() => true), distributeManaged: jest.fn(async () => {}) })); +jest.mock('../../src/services/mqtt', () => ({ handleMessage: jest.fn() })); +import { importBillingSnapshot } from '../../src/central/billing'; +import { getMeterByPublicKey, mergeBillingSnapshot } from '../../src/store/sqlite'; +import { verifyPayloadSignature } from '../../src/lib/utils'; +import type { BillingSnapshot } from '../../src/central/processor'; + +const publicKey = `0x${'a'.repeat(64)}`; +const record = (nonce: number) => { const raw = Buffer.alloc(74); raw.writeUInt32BE(nonce, 0); return { nonce, identifier: 7, receivedAt: 1, raw: raw.toString('hex') }; }; +const snapshot = (...nonces: number[]): BillingSnapshot => ({ meter: { devEui: 'fadf417b946e25ef', publicKey, tokenId: 7, latestNonce: Math.max(...nonces) }, transactions: nonces.map(record) }); +beforeEach(() => { jest.clearAllMocks(); jest.spyOn(console, 'log').mockImplementation(() => {}); }); +afterEach(() => jest.restoreAllMocks()); + +test('a meter new to central starts its history at the oldest signed record, then gaps are refused again', async () => { + (getMeterByPublicKey as jest.Mock).mockReturnValue(undefined); + // The chain has nonce 100; the gateway, which ran standalone first, replays 1246. + await importBillingSnapshot(snapshot(1246)); + expect(mergeBillingSnapshot).toHaveBeenCalledTimes(1); + // Now central knows the meter at 1246: the next record must follow on. + (getMeterByPublicKey as jest.Mock).mockReturnValue({ publicKey, tokenId: 7, devEui: 'fadf417b946e25ef', latestNonce: 1246 }); + await importBillingSnapshot(snapshot(1247)); + await expect(importBillingSnapshot(snapshot(1250))).rejects.toThrow('gap before nonce 1250 (central can confirm up to 1246)'); + expect(mergeBillingSnapshot).toHaveBeenCalledTimes(2); +}); + +test('first contact still checks signatures and the token', async () => { + (getMeterByPublicKey as jest.Mock).mockReturnValue(undefined); + (verifyPayloadSignature as jest.Mock).mockReturnValueOnce(false); + await expect(importBillingSnapshot(snapshot(1246))).rejects.toThrow('Invalid signed reconciliation record'); + await expect(importBillingSnapshot({ ...snapshot(1246), meter: { ...snapshot(1246).meter!, tokenId: 8 } })).rejects.toThrow('token does not match'); + expect(mergeBillingSnapshot).not.toHaveBeenCalled(); +}); From d6d07345f8f771dc987887c1375b9e287525c749 Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Wed, 18 Nov 2026 12:57:10 +0100 Subject: [PATCH 04/10] feat: plain compose logs per role and a role-aware Streamr app auto-compose.sh records COMPOSE_FILE and COMPOSE_PROJECT_NAME in .env after a successful deploy, so plain 'docker compose logs -f' follows the running role. The Streamr app now counts only unpublished readings on central, scopes to the tab's gateway, explains that central publishes on managed gateways, refreshes itself and shows the last automatic publish. Central learns a meter's gateway from replayed reports too. Documents the known ENS verifier issue that keeps meters OFF. --- auto-compose.sh | 23 ++++++++- docs/deployment-guide.md | 73 +++++++++++++++++++++++++++-- src/central/distribution.ts | 14 ++++++ src/central/hub.ts | 3 +- src/lib/core/streamr/index.ts | 3 +- src/lib/core/streamr/state.ts | 5 ++ src/lib/core/streamr/ui.ts | 77 +++++++++++++++++++------------ src/public/js/actions.js | 2 + src/public/js/gateways.js | 2 + src/public/js/streamr.js | 39 ++++++++++++++++ src/services/server.ts | 23 ++++++++- src/types.ts | 5 +- src/views/layouts/main.hbs | 1 + tests/services/deployment.test.ts | 22 ++++++++- tests/ui/gateways.spec.cjs | 22 +++++++++ tests/ui/serve.cjs | 2 +- tests/ui/streamr.test.ts | 38 ++++++++++++++- 17 files changed, 310 insertions(+), 44 deletions(-) create mode 100644 src/lib/core/streamr/state.ts create mode 100644 src/public/js/streamr.js diff --git a/auto-compose.sh b/auto-compose.sh index 28040aa..a55ce0c 100755 --- a/auto-compose.sh +++ b/auto-compose.sh @@ -253,7 +253,28 @@ if [[ "$role" == central ]]; then "${compose[@]}" up --detach tls-proxy || { echo 'Console is running, but the TLS proxy failed to start; check ports 80/443.' >&2; exit 1; } fi "${compose[@]}" ps -echo 'Console updated. Database, extension volumes and .env were preserved.' +# Plain `docker compose logs -f`, `ps` and so on in this folder follow the role that is now running: +# Compose reads COMPOSE_FILE and COMPOSE_PROJECT_NAME from .env. Only these two lines are touched; +# rewriting in place keeps the file's owner-only permissions. +set_env_value() { + local name=$1 value=$2 tmp + [[ "$(env_value "$name")" == "$value" ]] && return 0 + tmp=$(mktemp) || return 1 + if grep -q "^[[:space:]]*$name[[:space:]]*=" .env; then + sed "s|^[[:space:]]*$name[[:space:]]*=.*|$name=$value|" .env > "$tmp" + else + { cat .env; [[ -z "$(tail -c 1 .env)" ]] || echo + [[ "$name" != COMPOSE_FILE ]] || echo "# Set by ./auto-compose.sh: plain 'docker compose logs -f' follows the role running here." + echo "$name=$value"; } > "$tmp" + fi + cat "$tmp" > .env; rm -f "$tmp" +} +if set_env_value COMPOSE_FILE "$compose_file" && set_env_value COMPOSE_PROJECT_NAME "$project"; then + echo "Logs: run 'docker compose logs -f' here (it follows $compose_file)." +else + echo "Note: could not record the role in .env; use: docker compose -f $compose_file logs -f" >&2 +fi +echo 'Console updated. Database, extension volumes and your .env settings were preserved.' if [[ "$role" == gateway ]]; then echo 'Gateway agent is running. Its card on central should show Online within a minute.' if [[ -n "$(env_value AGENT_ENROLLMENT_TOKEN)" ]]; then diff --git a/docs/deployment-guide.md b/docs/deployment-guide.md index 288fd0a..0ed164e 100644 --- a/docs/deployment-guide.md +++ b/docs/deployment-guide.md @@ -164,6 +164,19 @@ same meter. Fix the error shown and run the script again. The gateway's own LAN desktop at `http://:3000` keeps working, without a login. +## The Streamr app + +| Where | What it shows | Publish Now | +|---|---|---| +| Standalone | Readings stored on this Console, waiting to be published | Publishes them to every configured stream | +| Central, **Central server** selected | Readings from every gateway not yet published to every stream, plus any with an uncertain outcome | Publishes what is waiting, for every gateway | +| Central, a gateway selected | The same, but only for meters served by that gateway | Same as above: central always publishes for every gateway together | +| A managed gateway's own Console | How many readings are kept locally; central publishes them | Hidden | + +The window refreshes when you open it, after **Publish Now**, when you switch gateway and every 30 +seconds while open. **Last publish** covers both the hourly job and **Publish Now**. Central never +resends a reading whose earlier publish had an uncertain outcome. + ## Managing gateways on central Open **Gateways** from the top bar. Each gateway is a card. @@ -344,13 +357,23 @@ are listed in [`.env.example`](../.env.example) and ## Troubleshooting -Useful commands, using the compose file for the machine's role -(`compose.yaml`, `compose.central.yaml` or `compose.gateway.yaml`): +**Logs on any machine.** In the Console folder, plain Compose commands follow whatever role is +running there: central (Console and HTTPS proxy), a managed gateway (Console and agent) or standalone. + +```sh +docker compose logs -f # every service of this role +docker compose logs -f agent # one service +docker compose ps +``` + +This works because `./auto-compose.sh` keeps two lines in `.env` up to date after each successful +deploy: `COMPOSE_FILE` (the role's compose file) and `COMPOSE_PROJECT_NAME`. It doesn't change +anything else in the file. The explicit form still works and is what you need for the gateway +updater, whose compose file is only added by the script: ```sh -docker compose -f compose.gateway.yaml ps -docker compose -f compose.gateway.yaml logs -f console docker compose -f compose.gateway.yaml logs -f agent +docker compose -f compose.gateway.yaml -f compose.updater.yaml logs -f updater ``` **Following a meter report.** Each step logs one line with the reading and the reply: @@ -379,8 +402,50 @@ The full signature and meter key are shortened in the logs; keys and tokens neve | Gateway: `Port 3011 is already used by …` | Another program uses the gateway Console's local fallback port. Add `GATEWAY_FALLBACK_PORT=3101` (any free port) to `.env` and rerun. Older setups used 3001, which clashes with ChirpStack's Basic Station bridge. | | Gateway: agent online once, then **Offline** for good | Usually the gateway Console couldn't start, often because of a port clash. Check `logs console` and rerun `./auto-compose.sh`; it now says what's wrong. | | Gateway: `This gateway is not enrolled yet` | Add the `AGENT_ENROLLMENT_TOKEN` line from central. | +| Every reply is `state=OFF`; logs show `Failed to resolve ENS name: base-sepolia.verifier.unruggable.eth` | Known issue, see [Every meter is switched OFF](#known-issue-every-meter-is-switched-off). | | Gateway: `Agent enrollment failed` in agent logs | The token expired or was already used. Get a new one from the gateway's card. | | Gateway card stays **Waiting to connect** | The gateway can't reach central on port 443, or `CENTRAL_URL` is wrong. Try `curl -I https://console.example.com` from the gateway. | | Gateway card shows **Offline** after a revoke/delete | Expected. To reconnect, see [Reconnecting a revoked or deleted gateway](#managing-gateways-on-central). | | `Secret file must be owner-only` in logs | Someone changed permissions in `.data/agent/`. Run `./auto-compose.sh` again; it repairs them. | | Script says *standalone was NOT restarted* | The agent had started handling reports when something failed. Fix the error shown and rerun; don't start standalone by hand. | + +## Known issue: every meter is switched OFF + +**Status:** open, not fixed in this repository (checked 2026-11-18). + +**Symptom.** Every state reply is OFF (`hex=000000000000000000`), whoever decides it. The deciding +Console logs: + +```text +[error] [utils] Extension hook isOnStateCompute failed: Failed to resolve ENS name: base-sepolia.verifier.unruggable.eth +[error] [Device-…] Could not determine meter power state, so the reply is OFF: 1 extension(s) failed in isOnStateCompute +``` + +**Cause.** The ON/OFF rule (`core/is_on`) switches a meter ON only when its revenue covers what it +owes. Revenue is read through the verifiers listed in the CCIP revenue reader contract +(`CCIP_REVENUE_READER_ADDRESS`, default `0xD648cdF47e9534B2FCfb18C1E94CA9AAff07BA0E` on Sepolia). +That contract lists exactly one verifier, `base-sepolia.verifier.unruggable.eth` (target +`0xC08E39789A93230069c956e76B0c10cbC83D903e`). That ENS name has no address record, neither +on Sepolia (no resolver) nor on Ethereum mainnet (a resolver, but no address for any chain), so +the revenue lookup fails and the safe default, OFF, is used. + +This is not a Console bug and not specific to one machine: central and gateways give the same +result. Check it from any Console with: + +```sh +docker compose -f compose.central.yaml exec console node -e "const {JsonRpcProvider}=require('ethers');new JsonRpcProvider(process.env.MAINNET_RPC).resolveName('base-sepolia.verifier.unruggable.eth').then(a=>console.log('resolved',a))" +``` + +`resolved null` means the issue is still present. + +**Possible fixes (not done yet).** + +- Have the revenue reader contract's owner update its verifier entry to a name (or verifier) that + currently resolves. Unruggable's [verifier deployments](https://gateway-docs.unruggable.com/verifiers/deployments) + page says its verifiers are deployed on Ethereum mainnet and Sepolia. +- Point `CCIP_REVENUE_READER_ADDRESS` at a revenue reader whose verifiers resolve. +- Add a Console setting that maps a verifier name to a known verifier address, so a broken ENS + record can be bypassed. + +Until then, meters stay OFF. Billing still records every reading; only the state reply is affected. + diff --git a/src/central/distribution.ts b/src/central/distribution.ts index ce18d8a..d646b99 100644 --- a/src/central/distribution.ts +++ b/src/central/distribution.ts @@ -20,8 +20,22 @@ export class PublicationStore { private finish(destination:string,records:TransactionRecord[],state:string):void { this.db.transaction(()=>{for(const r of records)this.db.prepare('UPDATE managed_publications SET state=? WHERE destination=? AND tokenId=? AND nonce=?').run(state,destination,r.identifier,r.nonce);})(); } + /** Records not yet submitted to every destination, and how many have an uncertain outcome. */ + status(destinations:string[],records:TransactionRecord[]):{pending:number;uncertain:number}{ + const state=this.db.prepare('SELECT state FROM managed_publications WHERE destination=? AND tokenId=? AND nonce=?'); + let pending=0,uncertain=0; + for(const record of records){ + const states=destinations.map(d=>(state.get(d,record.identifier,record.nonce) as {state:string}|undefined)?.state); + if(states.some(s=>s===undefined))pending++;else if(states.some(s=>s!=='submitted'))uncertain++; + } + return {pending,uncertain}; + } close():void{this.db.close();} } +/** Central only: publication progress for these records; null elsewhere. */ +export function publicationStatus(destinations:string[],records:TransactionRecord[]):{pending:number;uncertain:number}|null{ + return process.env.CONSOLE_ROLE==='central'&&publications?publications.status(destinations,records):null; +} export function publishManaged(destination:string,records:TransactionRecord[],send:(records:TransactionRecord[])=>Promise):Promise{ if(process.env.CONSOLE_ROLE==='gateway')return Promise.resolve(null); if(!managedRole())return send(records); diff --git a/src/central/hub.ts b/src/central/hub.ts index 15c457d..30b16ee 100644 --- a/src/central/hub.ts +++ b/src/central/hub.ts @@ -54,7 +54,8 @@ export class GatewayHub { if(typeof value==='object'&&value!==null&&'type' in value&&value.type==='operation'){ let message;try{message=parseOperation(data.toString());if(message.type!=='operation')throw new Error();}catch{socket.close(1008,'Invalid operation');return;} inFlight++; - void serveOperation(this.processor(),gateway,message).then(reply=>{if(!reply.error&&message.action==='offer'&&message.event)this.identities.observeDevice(message.event.deviceInfo.devEui,gateway);if(socket.readyState===WebSocket.OPEN)socket.send(JSON.stringify(reply));}).catch(()=>socket.close(1011,'Operation storage unavailable')).finally(()=>inFlight--); + void serveOperation(this.processor(),gateway,message).then(reply=>{// Learn which gateway serves a meter, also from reports it decided while central was away. + if(!reply.error&&(message.action==='offer'||message.action==='reconcile')&&message.event)this.identities.observeDevice(message.event.deviceInfo.devEui,gateway);if(socket.readyState===WebSocket.OPEN)socket.send(JSON.stringify(reply));}).catch(()=>socket.close(1011,'Operation storage unavailable')).finally(()=>inFlight--); return; } try { diff --git a/src/lib/core/streamr/index.ts b/src/lib/core/streamr/index.ts index aa25d86..2603d88 100644 --- a/src/lib/core/streamr/index.ts +++ b/src/lib/core/streamr/index.ts @@ -6,6 +6,7 @@ import type { Hooks, TransactionRecord } from "../../../types"; import { getSyncedPendingTransactions } from "../../sync"; import { createLogger } from "../../../utils/logger"; import { managedRole, publishManaged } from '../../../central/distribution'; +import { recordPublish } from "./state"; const logger = createLogger("[streamr]"); @@ -44,7 +45,7 @@ export default class implements Hooks { 2000, ); }), - ); + ).then(() => recordPublish('success', pendingTransactions.length), error => { recordPublish('error'); throw error; }); } }, { name: "streamr-publish-pending-transactions", noOverlap: true }, diff --git a/src/lib/core/streamr/state.ts b/src/lib/core/streamr/state.ts new file mode 100644 index 0000000..40e6241 --- /dev/null +++ b/src/lib/core/streamr/state.ts @@ -0,0 +1,5 @@ +/** Shared by the hourly job and the Publish Now button, so the app shows whichever ran last. */ +export const lastPublish: { time: Date | null; status: 'success' | 'error' | null; count: number } = { time: null, status: null, count: 0 }; +export function recordPublish(status: 'success' | 'error', count = 0): void { + lastPublish.time = new Date(); lastPublish.status = status; lastPublish.count = count; +} diff --git a/src/lib/core/streamr/ui.ts b/src/lib/core/streamr/ui.ts index 48d9336..bda6db2 100644 --- a/src/lib/core/streamr/ui.ts +++ b/src/lib/core/streamr/ui.ts @@ -1,7 +1,8 @@ -import { getAllTransactionRecords } from "../../../store/sqlite"; +import { getAllTransactionRecords, getMeterByDevEui } from "../../../store/sqlite"; import { buildBatchPayload, retry } from "../../utils"; -import { managedRole, publishManaged } from '../../../central/distribution'; -import type { UIHooks, UIAppIcon, UIAppWindow, UIAction, TransactionRecord } from "../../../types"; +import { managedRole, publishManaged, publicationStatus } from '../../../central/distribution'; +import { lastPublish, recordPublish } from './state'; +import type { UIHooks, UIAppIcon, UIAppWindow, UIAction, TransactionRecord, UIStatusContext } from "../../../types"; import { StreamrClient } from "@streamr/sdk"; import { createLogger } from "../../../utils/logger"; @@ -24,9 +25,7 @@ function escapeHtml(value: string): string { */ export default class implements UIHooks { private streamIds: string[] = process.env.STREAMR_STREAM_ID ? process.env.STREAMR_STREAM_ID.split(",") : []; - private cronSchedule: string = process.env.STREAMR_CRONSCHEDULE || "Not configured"; - private lastPublishTime: Date | null = null; - private lastPublishStatus: "success" | "error" | null = null; + private cronSchedule: string = process.env.STREAMR_CRONSCHEDULE || "0 * * * *"; getAppIcon(): UIAppIcon { return { @@ -38,10 +37,9 @@ export default class implements UIHooks { } async getAppWindow(): Promise { - const pendingCount = (await this.getPendingTransactions()).length; - const streamIds = this.streamIds; - const cronSchedule = this.cronSchedule; - + // First paint; /js/streamr.js keeps it current and follows the tab's gateway on central. + const status = await this.status(); + const gateway = status.role === 'gateway'; return { id: "streamr", title: "Streamr Publisher", @@ -50,21 +48,23 @@ export default class implements UIHooks {
STREAMR NETWORK

Keep the data flowing.

Publish your pending meter transactions to connected streams.

- ${pendingCount} pending + ${gateway ? "Central publishes" : `${status.pendingCount} pending`}
+

${escapeHtml(status.scope)}

-
Pending transactions${pendingCount}
-
Last publish${this.lastPublishTime ? this.lastPublishTime.toLocaleString() : "Never"} - ${this.lastPublishStatus ? `${this.lastPublishStatus === "success" ? "Published successfully" : "Publish failed"}` : ""}
+
${gateway ? "Readings kept here" : "Waiting to publish"}${gateway ? status.recordCount : status.pendingCount} + ${status.uncertainCount ? `${status.uncertainCount} with an uncertain outcome (not resent automatically)` : ""}
+
Last publish${status.lastPublishTime ? new Date(status.lastPublishTime).toLocaleString() : "Never"} + ${status.lastPublishStatus ? (status.lastPublishStatus === "success" ? "Published successfully" : "Publish failed") : ""}

Configuration

-
Publish schedule${escapeHtml(cronSchedule)}
+
Publish schedule${escapeHtml(this.cronSchedule)}

Connected streams

-
    ${streamIds.map((id) => `
  • ${escapeHtml(id)}
  • `).join("") || '
  • No streams configured.
  • '}
+
    ${this.streamIds.map((id) => `
  • ${escapeHtml(id)}
  • `).join("") || '
  • No streams configured.
  • '}
- - + +
`, }; @@ -77,7 +77,7 @@ export default class implements UIHooks { label: "Publish Now", buttonClass: "is-warning", handler: async () => { - if (process.env.CONSOLE_ROLE === 'gateway') return { message: 'Publication is owned by the central Console. Local reports are retained for reconciliation.' }; + if (process.env.CONSOLE_ROLE === 'gateway') return { message: 'Central publishes for managed gateways. Readings here are kept for reconciliation.' }; const pendingTransactions = await this.getPendingTransactions(); if (pendingTransactions.length === 0) { @@ -92,15 +92,14 @@ export default class implements UIHooks { }), ); - this.lastPublishTime = new Date(); - this.lastPublishStatus = "success"; + recordPublish("success", pendingTransactions.length); return { - message: managedRole() ? 'Central publication checked. Previously submitted or uncertain transactions are not resubmitted.' : `Published ${pendingTransactions.length} transactions to ${this.streamIds.length} stream(s)`, + message: managedRole() ? 'Published what was waiting, for every gateway. Readings already published, or with an uncertain outcome, are not sent again.' : `Published ${pendingTransactions.length} transactions to ${this.streamIds.length} stream(s)`, data: { count: pendingTransactions.length }, }; } catch (error: any) { - this.lastPublishStatus = "error"; + recordPublish("error"); throw new Error(`Failed to publish: ${error.message}`); } }, @@ -108,14 +107,32 @@ export default class implements UIHooks { ]; } - async getStatusData(): Promise> { - const pendingTransactions = await this.getPendingTransactions(); + async getStatusData(context?: UIStatusContext): Promise> { + return this.status(context); + } + + /** What the window shows. Standalone: every stored reading is waiting. Central: readings not + * yet submitted to every stream, optionally only one gateway's meters. Gateway: central publishes. */ + async status(context: UIStatusContext = {}) { + const role = (process.env.CONSOLE_ROLE || "standalone") as "standalone" | "central" | "gateway"; + let records = await this.getPendingTransactions(); + if (context.devEuis) { + const tokens = new Set(context.devEuis.map((devEui) => getMeterByDevEui(devEui)?.tokenId).filter((id) => id !== undefined)); + records = records.filter((record) => tokens.has(record.identifier)); + } + const progress = publicationStatus(this.streamIds.map((id) => `streamr:${id}`), records); + const scope = role === "gateway" ? "This gateway is managed by central, which publishes its readings to Streamr." + : role === "central" ? (context.gateway ? `Showing readings from ${context.gatewayName || "this gateway"}'s meters. Central publishes for every gateway together.` + : "Showing readings from every gateway. Central publishes them on the schedule below.") + : ""; return { - pendingCount: pendingTransactions.length, - streamIds: this.streamIds, - cronSchedule: this.cronSchedule, - lastPublishTime: this.lastPublishTime, - lastPublishStatus: this.lastPublishStatus, + role, scope, gateway: context.gateway || null, + recordCount: records.length, + pendingCount: role === "gateway" ? 0 : progress ? progress.pending : records.length, + uncertainCount: progress ? progress.uncertain : 0, + canPublish: role !== "gateway" && this.streamIds.length > 0, + streamIds: this.streamIds, cronSchedule: this.cronSchedule, + lastPublishTime: lastPublish.time, lastPublishStatus: lastPublish.status, }; } diff --git a/src/public/js/actions.js b/src/public/js/actions.js index cc36904..b41fcfc 100644 --- a/src/public/js/actions.js +++ b/src/public/js/actions.js @@ -51,6 +51,8 @@ async function invokeAction(moduleId, actionId, buttonElement) { buttonElement.disabled = false; buttonElement.removeAttribute('aria-busy'); } + // Let the app refresh what it shows (and re-disable the button if nothing is left). + document.dispatchEvent(new CustomEvent('app:action', { detail: { moduleId, actionId } })); } } diff --git a/src/public/js/gateways.js b/src/public/js/gateways.js index d93e7ac..1be0168 100644 --- a/src/public/js/gateways.js +++ b/src/public/js/gateways.js @@ -13,6 +13,8 @@ const frame=$('chirpstack-frame'),src=`/chirpstack/~${encodeURIComponent(selected||'central')}/`; // Unload the previous target's ChirpStack; the next launch loads this one. if(frame&&frame.dataset.src!==src){frame.dataset.src=src;if(frame.hasAttribute('src')){frame.src='about:blank';frame.removeAttribute('src');}} + // Apps that show per-gateway data (such as Streamr) refresh for the new target. + document.dispatchEvent(new CustomEvent('console:target',{detail:{target:selected||'central'}})); } const openUpdates=new Set(); diff --git a/src/public/js/streamr.js b/src/public/js/streamr.js new file mode 100644 index 0000000..0ba2045 --- /dev/null +++ b/src/public/js/streamr.js @@ -0,0 +1,39 @@ +/* Keeps the Streamr window current: on open, after Publish Now, when this tab's + * gateway changes (central), and every 30 seconds while it is visible. */ +(() => { + const win = document.getElementById('streamr'); + if (!win) return; + const field = name => win.querySelector(`[data-streamr-field="${name}"]`); + const set = (name, text) => { const node = field(name); if (node) node.textContent = text; }; + let busy = false; + async function refresh() { + if (busy) return; busy = true; + try { + const headers = typeof window.ConsoleTarget === 'function' ? { 'X-Console-Target': window.ConsoleTarget() } : {}; + const response = await fetch('/api/apps/streamr/status', { headers }); + if (response.status === 401) { location.assign('/login'); return; } + if (!response.ok) return; + const s = await response.json(), gateway = s.role === 'gateway'; + set('scope', s.scope || ''); + set('badge', gateway ? 'Central publishes' : `${s.pendingCount} pending`); + set('pending-label', gateway ? 'Readings kept here' : 'Waiting to publish'); + set('pending', String(gateway ? s.recordCount : s.pendingCount)); + set('uncertain', s.uncertainCount ? `${s.uncertainCount} with an uncertain outcome (not resent automatically)` : ''); + set('last', s.lastPublishTime ? new Date(s.lastPublishTime).toLocaleString() : 'Never'); + set('last-status', s.lastPublishStatus ? (s.lastPublishStatus === 'success' ? 'Published successfully' : 'Publish failed') : ''); + const publish = field('publish'); + // Leave a running publish alone; actions.js restores the button when it finishes. + if (publish && publish.getAttribute('aria-busy') !== 'true') { + publish.hidden = gateway; + publish.disabled = !s.canPublish || s.pendingCount === 0; + } + } catch { /* keep the last values */ } finally { busy = false; } + } + window.StreamrApp = { refresh }; + window.addEventListener('desktop:open', event => { if (event.detail?.id === 'streamr') void refresh(); }); + document.addEventListener('app:action', event => { if (event.detail?.moduleId === 'streamr') void refresh(); }); + document.addEventListener('console:target', () => void refresh()); + const timer = setInterval(() => { if (!win.hidden) void refresh(); }, 30000); + window.addEventListener('pagehide', () => clearInterval(timer)); + void refresh(); +})(); diff --git a/src/services/server.ts b/src/services/server.ts index 5ac0677..67353d4 100644 --- a/src/services/server.ts +++ b/src/services/server.ts @@ -4,7 +4,8 @@ import express, { Express, Request, Response } from 'express'; import { requireSameOrigin } from './auth'; import { proxyChirpstack, proxyChirpstackThrough } from './chirpstack'; import { createLogger } from '../utils/logger'; -import { getUIComponents, invokeUIAction } from '../lib/utils'; +import { getUIComponents, invokeUIAction, getUIExtension } from '../lib/utils'; +import type { UIStatusContext } from '../types'; import { central, config } from '../central/runtime'; import { centralChirpstackUrl } from '../central/config'; import { protectSessionSockets } from './socket-router'; @@ -73,6 +74,26 @@ app.get('/', async (req: Request, res: Response, next) => { } catch (error) { next(error); } }); +// Live status for an app window. On central the tab names its target in X-Console-Target, +// so an app can show one gateway's meters (or everything, for the central server). +app.get('/api/apps/:moduleId/status', async (req: Request, res: Response, next) => { + try { + const ext = getUIExtension(req.params.moduleId); + if (!ext?.getStatusData) { res.status(404).json({ error: 'This app has no status.' }); return; } + const context: UIStatusContext = {}; + if (central) { + const target = central.hub.target(String(req.headers['x-console-target'] || '')); + if (!target) { res.status(404).json({ error: 'That gateway is no longer available.' }); return; } + if (target.gateway) { + context.gateway = target.gateway; + context.gatewayName = central.identities.gateways().find(g => g.id === target.gateway)?.name; + context.devEuis = central.identities.devices().filter(d => d.gateway === target.gateway).map(d => d.devEui); + } + } + res.json(await ext.getStatusData(context)); + } catch (error) { next(error); } +}); + // API endpoint to invoke UI actions app.post('/api/actions/:moduleId/:actionId', async (req: Request, res: Response, next) => { try { diff --git a/src/types.ts b/src/types.ts index 72418f0..217f244 100644 --- a/src/types.ts +++ b/src/types.ts @@ -143,6 +143,7 @@ export interface UIAction { * UI Hooks interface for extending the console UI * Modules can implement this to add icons, windows, and actions to the web interface */ +export type UIStatusContext = { gateway?: string; gatewayName?: string; devEuis?: string[] }; export type UIHooks = { /** Return an app icon to display on the desktop */ getAppIcon?: () => UIAppIcon | Promise; @@ -150,6 +151,6 @@ export type UIHooks = { getAppWindow?: () => UIAppWindow | Promise; /** Return available actions for this module */ getActions?: () => UIAction[] | Promise; - /** Return metadata/status data for display */ - getStatusData?: () => Record | Promise>; + /** Return metadata/status data for display. On central, scoped to the tab's gateway when one is selected. */ + getStatusData?: (context?: UIStatusContext) => Record | Promise>; }; diff --git a/src/views/layouts/main.hbs b/src/views/layouts/main.hbs index 5d175d4..4e1c954 100644 --- a/src/views/layouts/main.hbs +++ b/src/views/layouts/main.hbs @@ -25,6 +25,7 @@ + {{#if central}}{{/if}} diff --git a/tests/services/deployment.test.ts b/tests/services/deployment.test.ts index fec1a18..b3cdfdf 100644 --- a/tests/services/deployment.test.ts +++ b/tests/services/deployment.test.ts @@ -28,7 +28,9 @@ esac expect(calls).toContain('pull console');expect(calls).toContain('stop console');expect(calls).toContain('rm --force console'); expect(calls).not.toMatch(/down|--volumes|system prune|build console/); expect(calls.indexOf('pull console')).toBeLessThan(calls.indexOf('stop console')); - expect(fs.readFileSync(path.join(dir,'.env'),'utf8')).toBe(environment); + // Only the two role lines are added on success, so plain `docker compose logs -f` follows this role. + expect(fs.readFileSync(path.join(dir,'.env'),'utf8')).toBe(fail?environment:environment+"# Set by ./auto-compose.sh: plain 'docker compose logs -f' follows the role running here.\nCOMPOSE_FILE=compose.yaml\nCOMPOSE_PROJECT_NAME=existing\n"); + if(!fail)expect(run.stdout).toContain("Logs: run 'docker compose logs -f' here"); expect(fs.readFileSync(path.join(dir,'.data','sentinel'),'utf8')).toBe('keep'); if(fail){expect(calls).toContain('logs --tail 80 console');expect(calls).toContain('--force-recreate console');expect(calls).not.toContain('image rm');} else expect(calls).toContain('image rm sha256:old'); @@ -131,7 +133,7 @@ test('gateway role enrolls: secrets are owner-only, standalone stops before Cons expect(calls).not.toContain('-f compose.yaml'); expect(modes).toEqual({fallback:0o600,updater:0o600,agentDir:0o700}); expect(fallback).toBe('keep-this-existing-secret'.padEnd(43,'k')); - expect(env).toBe(environment); + expect(env).toBe(environment+"# Set by ./auto-compose.sh: plain 'docker compose logs -f' follows the role running here.\nCOMPOSE_FILE=compose.gateway.yaml\nCOMPOSE_PROJECT_NAME=site\n"); expect(run.stdout).toContain('remove the AGENT_ENROLLMENT_TOKEN line'); }); test('gateway role refuses to start without enrollment or with a bad central URL, before touching Docker', () => { @@ -279,3 +281,19 @@ test('GATEWAY_FALLBACK_PORT in .env moves the check to that port',()=>{ expect(runGateway(gatewayEnv+'GATEWAY_FALLBACK_PORT=3101\n',{credential:true,busyPort:'3011'}).run.status).toBe(0); expect(runGateway(gatewayEnv+'GATEWAY_FALLBACK_PORT=3101\n',{credential:true,busyPort:'3101'}).run.status).toBe(2); }); + +test('the role lines in .env are updated in place, keep the file owner-only, and are not duplicated', () => { + const dir=fs.mkdtempSync(path.join(os.tmpdir(),'console-role-')); + try { + for(const file of ['auto-compose.sh','compose.yaml','compose.central.yaml'])fs.copyFileSync(file,path.join(dir,file)); + fs.writeFileSync(path.join(dir,'.env'),'CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\nCOMPOSE_FILE=compose.yaml\n',{mode:0o600}); + fs.mkdirSync(path.join(dir,'.data'));fs.writeFileSync(path.join(dir,'.data','central.db'),'db'); + fs.writeFileSync(path.join(dir,'docker'),"#!/bin/bash\ncase \"$*\" in *'ps -q console'*) echo new-container ;; esac\n",{mode:0o755}); + const run=()=>spawnSync('bash',['auto-compose.sh','--project-name','central'],{cwd:dir,encoding:'utf8',env:{...process.env,PATH:dir+':'+process.env.PATH}}); + expect(run().status).toBe(0);expect(run().status).toBe(0); + const env=fs.readFileSync(path.join(dir,'.env'),'utf8'); + expect(env.match(/^COMPOSE_FILE=/gm)).toHaveLength(1);expect(env).toContain('COMPOSE_FILE=compose.central.yaml'); + expect(env.match(/^COMPOSE_PROJECT_NAME=central$/gm)).toHaveLength(1); + expect(fs.statSync(path.join(dir,'.env')).mode&0o777).toBe(0o600); + } finally {fs.rmSync(dir,{recursive:true,force:true});} +}); diff --git a/tests/ui/gateways.spec.cjs b/tests/ui/gateways.spec.cjs index 6254e83..584913f 100644 --- a/tests/ui/gateways.spec.cjs +++ b/tests/ui/gateways.spec.cjs @@ -208,3 +208,25 @@ test('the account dialog changes the password and reports mistakes',async({page} await page.getByRole('button',{name:'Close',exact:true}).click(); await expect(page.locator('#account-dialog')).toBeHidden(); }); + +test('Streamr follows this tab\'s target on central and refreshes when it changes',async({page})=>{ + const gateways=[{id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}]; + const targets=[]; + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways}})); + await page.route('**/api/apps/streamr/status',route=>{ + const target=route.request().headers()['x-console-target'];targets.push(target); + const gateway=target==='gateway-one'; + return route.fulfill({json:{role:'central',scope:gateway?"Showing readings from North gateway's meters. Central publishes for every gateway together.":'Showing readings from every gateway. Central publishes them on the schedule below.', + recordCount:gateway?2:5,pendingCount:gateway?0:3,uncertainCount:gateway?0:1,canPublish:true,lastPublishTime:null,lastPublishStatus:null}}); + }); + await page.goto('/?central=1'); + await page.evaluate(()=>Desktop.launch('streamr')); + await expect(page.locator('[data-streamr-field="scope"]')).toContainText('every gateway'); + await expect(page.locator('[data-streamr-field="pending"]')).toHaveText('3'); + await expect(page.locator('[data-streamr-field="uncertain"]')).toContainText('1 with an uncertain outcome'); + await page.locator('#gateway-switcher').click();await page.getByRole('option',{name:/North gateway/}).click(); + await page.evaluate(()=>Desktop.launch('streamr')); + await expect(page.locator('[data-streamr-field="scope"]')).toContainText("North gateway's meters"); + await expect(page.locator('[data-streamr-field="pending"]')).toHaveText('0'); + expect(targets).toContain('central');expect(targets).toContain('gateway-one'); +}); diff --git a/tests/ui/serve.cjs b/tests/ui/serve.cjs index d9ac864..aa51a44 100644 --- a/tests/ui/serve.cjs +++ b/tests/ui/serve.cjs @@ -14,7 +14,7 @@ app.get('/', (req, res) => { for (let i = 0; i < count; i++) { const id = i ? `extension-${i}` : 'streamr'; icons.push({ id, label: i ? `Extension ${i}` : 'Streamr', iconHtml: '' }); - windows.push({ id, title: i ? `Extension ${i}` : 'Streamr Publisher', contentHtml: '

Configuration

Pending transactions: 2

' }); + windows.push({ id, title: i ? `Extension ${i}` : 'Streamr Publisher', contentHtml: (i ? '' : '

2') + '

Configuration

Pending transactions: 2

' }); } res.render('index', { icons, windows, central:req.query.central==='1' }); }); diff --git a/tests/ui/streamr.test.ts b/tests/ui/streamr.test.ts index 53bbff9..5d73700 100644 --- a/tests/ui/streamr.test.ts +++ b/tests/ui/streamr.test.ts @@ -2,7 +2,8 @@ import StreamrUI from '../../src/lib/core/streamr/ui'; import { getAllTransactionRecords } from '../../src/store/sqlite'; jest.mock('@streamr/sdk', () => ({ StreamrClient: jest.fn() })); -jest.mock('../../src/store/sqlite', () => ({ getAllTransactionRecords: jest.fn(() => []) })); +jest.mock('../../src/store/sqlite', () => ({ getAllTransactionRecords: jest.fn(() => []), getMeterByDevEui: jest.fn((devEui: string) => devEui === 'aaaaaaaaaaaaaaaa' ? { tokenId: 1 } : devEui === 'bbbbbbbbbbbbbbbb' ? { tokenId: 2 } : null) })); +import { configurePublications } from '../../src/central/distribution'; describe('Streamr desktop presentation', () => { const originalStream = process.env.STREAMR_STREAM_ID; @@ -31,4 +32,39 @@ describe('Streamr desktop presentation', () => { expect(window.contentHtml).not.toContain('