Decode APRS station symbols and improve map centering
Render local APRS symbol sprites with table and overlay details while preserving markers and open popups during refresh. Open the map around the local station with a 30 km radius and fit the densest station group without distant outliers. Add localization, attribution and regression coverage.
This commit is contained in:
1 parent
2ef3c25eb3
commit
8ee4e9636a
14 files changed
+605
-21
No files matched your search
+3
-2
@@ -1,3 +1,4 @@
|
||||
import {aprsSymbol,nativeSymbol} from './aprs-symbols.mjs';
|
||||
import {AprsKiss} from './aprs-kiss.mjs';
|
||||
import {readFile,writeFile,rename,open,stat,readdir} from 'node:fs/promises';import path from 'node:path';
|
||||
import {plain,decode} from './bpq.mjs';import {Monitor} from './monitor.mjs';
|
||||
@@ -7,13 +8,13 @@ export function parseRadioAprs(frame){const lines=frame.text.split('\n'),m=lines
|
||||
export function parseIsAprs(line,date){const m=line.match(/^(\d{2}:\d{2}:\d{2})([RT])\s+(.*)$/);if(!m)return null;const packet=parseTnc2(m[3]);return packet?{...packet,at:date+'T'+m[1]+'Z',direction:m[2]==='T'?'TX':'RX',transport:'Internet',port:null,evidence:'BPQ APRS-IS log'}:null;}
|
||||
export function positionOf(body){let text=body;if(/[\/@]/.test(text[0]))text=text.slice(8);else if(/[!=]/.test(text[0]))text=text.slice(1);else return null;
|
||||
const m=text.match(/^(\d{2})(\d{2}\.\d{2})([NS])(.)(\d{3})(\d{2}\.\d{2})([EW])(.)(.*)$/);if(!m||Number(m[2])>=60||Number(m[6])>=60)return null;const lat=(+m[1]+Number(m[2])/60)*(m[3]==='S'?-1:1),lon=(+m[5]+Number(m[6])/60)*(m[7]==='W'?-1:1);return Math.abs(lat)<=90&&Math.abs(lon)<=180?{lat,lon,symbol:m[8],symset:m[4]}:null;}
|
||||
export function parseNativeStations(text,now=Date.now()){const out=[];for(const row of text.split(/\r?\n\|/)){const m=row.trim().match(/^A,(-?[\d.]+),(-?[\d.]+),([^,]+),([\s\S]*),([A-Fa-f0-9]{4}.),(\d+)$/);if(!m)continue;const lat=Number(m[1]),lon=Number(m[2]),call=m[3];if(!/^[A-Z0-9-]{2,12}$/i.test(call)||!Number.isFinite(lat)||!Number.isFinite(lon)||Math.abs(lat)>90||Math.abs(lon)>180)continue;const detail=decode(m[4]),port=Number(detail.match(/Last Heard:.*?on Port (\d+)/)?.[1])||0,lines=detail.split(/<br\s*\/?\s*>/i).map(s=>plain(s).trim()).filter(Boolean);out.push({call,position:{lat,lon},lastAt:new Date(now-Number(m[6])*1000).toISOString(),transport:port?'Radio':'Internet',port:port||null,path:lines[0]||'',lastBody:lines[1]||'',status:lines.find(s=>!s.startsWith('Last Heard')&&!s.startsWith('Distance')&&s!==lines[0]&&s!==lines[1])||'',heardRadio:port>0,positionSource:'BPQ APRS decoder',native:true});}return out;}
|
||||
export function parseNativeStations(text,now=Date.now()){const out=[];for(const row of text.split(/\r?\n\|/)){const m=row.trim().match(/^A,(-?[\d.]+),(-?[\d.]+),([^,]+),([\s\S]*),([A-Fa-f0-9]{4}.),(\d+)$/);if(!m)continue;const lat=Number(m[1]),lon=Number(m[2]),call=m[3];if(!/^[A-Z0-9-]{2,12}$/i.test(call)||!Number.isFinite(lat)||!Number.isFinite(lon)||Math.abs(lat)>90||Math.abs(lon)>180)continue;const detail=decode(m[4]),port=Number(detail.match(/Last Heard:.*?on Port (\d+)/)?.[1])||0,lines=detail.split(/<br\s*\/?\s*>/i).map(s=>plain(s).trim()).filter(Boolean);out.push({call,position:{lat,lon,...nativeSymbol(m[5])},lastAt:new Date(now-Number(m[6])*1000).toISOString(),transport:port?'Radio':'Internet',port:port||null,path:lines[0]||'',lastBody:lines[1]||'',status:lines.find(s=>!s.startsWith('Last Heard')&&!s.startsWith('Distance')&&s!==lines[0]&&s!==lines[1])||'',heardRadio:port>0,positionSource:'BPQ APRS decoder',native:true});}return out;}
|
||||
export class AprsActivity{
|
||||
constructor(config,dir,bpq,credentials,settings){this.config=config;this.bpq=bpq;this.settings=settings;this.file=path.join(dir,'aprs-activity.cache.json');this.events=[];this.stations=new Map();this.seq=0;this.cursor=0;this.monitor=new Monitor(credentials,{port:config.monitorPort});this.kiss=(config.aprsKissMonitor||[]).map(endpoint=>new AprsKiss(endpoint,(packet,port)=>this.ingest({...parseTnc2(packet),at:new Date().toISOString(),direction:'RX',transport:'Radio',port,evidence:'KISS receive-only monitor'})));this.lastNative=0;this.lastPersist=0;this.logFile=null;this.logOffset=null;this.partial='';this.errors=[];this.started=false;this.busy=false;this.init=this.restore();this.timer=setInterval(()=>{if(this.started)this.sample().catch(()=>{});},2000);this.timer.unref();}
|
||||
async restore(){try{const data=JSON.parse(await readFile(this.file,'utf8'));this.events=(data.events||[]).slice(-2000);this.seq=this.events.at(-1)?.id||0;this.logFile=data.logFile||null;this.logOffset=data.logOffset??null;this.partial=data.partial||'';this.ownPosition=data.ownPosition;for(const s of data.stations||[])if(s.call)this.stations.set(s.call,s);}catch{} }
|
||||
async start(){await this.init;if(this.starting)return this.starting;if(this.started)return this.snapshot();this.starting=(async()=>{this.started=true;await this.native();try{await this.settings.enableLogging();this.logging=true;}catch{this.logging=false;}if(this.config.monitorPort)await this.monitor.connect();await this.sample();return this.snapshot();})();try{return await this.starting;}finally{this.starting=null;}}
|
||||
ingest(event){if(!event?.type)return;if(this.events.slice(-40).some(e=>e.packet===event.packet&&e.direction===event.direction&&e.port===event.port&&Math.abs(Date.parse(e.at)-Date.parse(event.at))<1000))return;event={...event,id:++this.seq};this.events.push(event);if(this.events.length>2000)this.events.shift();if(event.direction==='RX'){const old=this.stations.get(event.from)||{call:event.from},position=positionOf(event.body)||old.position;this.stations.set(event.from,{...old,lastAt:event.at,transport:event.transport,port:event.port,path:event.path,lastBody:event.body,heardRadio:old.heardRadio||event.transport==='Radio',position,positionSource:positionOf(event.body)?'APRS position':old.positionSource});}}
|
||||
async native(){try{const html=await this.bpq('/aprsdata.txt?90|-90|-180|180|1|0|0');const home=html.match(/(?:^|\|)H,(-?[\d.]+),(-?[\d.]+),/);if(home&&Math.abs(+home[1])<=90&&Math.abs(+home[2])<=180)this.ownPosition={lat:+home[1],lon:+home[2]};for(const station of parseNativeStations(html)){const old=this.stations.get(station.call);this.stations.set(station.call,{...old,...station,heardRadio:station.heardRadio||old?.heardRadio});}this.nativeAvailable=true;}catch{this.nativeAvailable=false;}this.lastNative=Date.now();}
|
||||
async native(){try{const html=await this.bpq('/aprsdata.txt?90|-90|-180|180|1|0|0');const home=html.match(/(?:^|\|)H,(-?[\d.]+),(-?[\d.]+),/);if(home&&Math.abs(+home[1])<=90&&Math.abs(+home[2])<=180)this.ownPosition={lat:+home[1],lon:+home[2]};try{const settings=await this.settings.snapshot?.();if(settings&&this.ownPosition)this.ownPosition={...this.ownPosition,...aprsSymbol(settings.symset,settings.symbol)};}catch{}for(const station of parseNativeStations(html)){const old=this.stations.get(station.call);this.stations.set(station.call,{...old,...station,position:{...old?.position,...station.position},heardRadio:station.heardRadio||old?.heardRadio});}this.nativeAvailable=true;}catch{this.nativeAvailable=false;}this.lastNative=Date.now();}
|
||||
async readLog(){const now=new Date(),date=now.toISOString().slice(0,10),day=date.replaceAll('-','').slice(2);let file=null;for(const folder of [this.config.aprsLogDirectory,path.join(this.config.root,'Logs'),path.join(this.config.root,'logs')].filter(Boolean)){const candidate=path.join(folder,'APRS_'+day+'.log');try{await stat(candidate);file=candidate;break;}catch{}}if(!file)return;
|
||||
const info=await stat(file);if(file!==this.logFile){this.logFile=file;this.logOffset=Math.max(0,info.size-262144);this.partial='';}if(info.size<this.logOffset){this.logOffset=0;this.partial='';}if(info.size===this.logOffset)return;const handle=await open(file,'r');try{const data=Buffer.alloc(Math.min(262144,info.size-this.logOffset)),read=await handle.read(data,0,data.length,this.logOffset);this.logOffset+=read.bytesRead;const lines=(this.partial+data.subarray(0,read.bytesRead).toString('utf8')).split(/\r?\n/);this.partial=lines.pop().slice(-4096);for(const line of lines)this.ingest(parseIsAprs(line,date));}finally{await handle.close();}}
|
||||
async sample(){if(this.busy)return;this.busy=true;try{this.errors=[];for(const source of this.kiss)source.connect();if(this.config.monitorPort){const s=this.monitor.snapshot(this.cursor);if(['offline','error'].includes(s.state))await this.monitor.connect();for(const e of s.events)this.ingest(parseRadioAprs(e));this.cursor=s.latest;}
|
||||
|
||||
Reference in new issue
Block a user