/* APRS descriptions: OH7LZB / aprs-symbol-index, CC BY-SA 4.0. See vendor/aprs-symbols/README.md. */
(()=>{
const names={"/!":"Police station","/#":"Digipeater","/$":"Telephone","/%":"DX cluster","/&":"HF gateway","/'":"Small aircraft","/(":"Mobile satellite station","/)":"Wheelchair, handicapped","/*":"Snowmobile","/+":"Red Cross","/,":"Boy Scouts","/-":"House","/.":"Red X","//":"Red dot","/0":"Numbered circle: 0","/1":"Numbered circle: 1","/2":"Numbered circle: 2","/3":"Numbered circle: 3","/4":"Numbered circle: 4","/5":"Numbered circle: 5","/6":"Numbered circle: 6","/7":"Numbered circle: 7","/8":"Numbered circle: 8","/9":"Numbered circle: 9","/:":"Fire","/;":"Campground, tent","/<":"Motorcycle","/=":"Railroad engine","/>":"Car","/?":"File server","/@":"Hurricane predicted path","/A":"Aid station","/B":"BBS","/C":"Canoe","/E":"Eyeball","/F":"Farm vehicle, tractor","/G":"Grid square, 3 by 3","/H":"Hotel","/I":"TCP/IP network station","/K":"School","/L":"PC user","/M":"Mac apple","/N":"NTS station","/O":"Balloon","/P":"Police car","/R":"Recreational vehicle","/S":"Space Shuttle","/T":"SSTV","/U":"Bus","/V":"ATV, Amateur Television","/W":"Weather service site","/X":"Helicopter","/Y":"Sailboat","/Z":"Windows flag","/[":"Human","/\\":"DF triangle","/]":"Mailbox, post office","/^":"Large aircraft","/_":"Weather station","/`":"Satellite dish antenna","/a":"Ambulance","/b":"Bicycle","/c":"Incident command post","/d":"Fire station","/e":"Horse, equestrian","/f":"Fire truck","/g":"Glider","/h":"Hospital","/i":"IOTA, islands on the air","/j":"Jeep","/k":"Truck","/l":"Laptop","/m":"Mic-E repeater","/n":"Node, black bulls-eye","/o":"Emergency operations center","/p":"Dog","/q":"Grid square, 2 by 2","/r":"Repeater tower","/s":"Ship, power boat","/t":"Truck stop","/u":"Semi-trailer truck, 18-wheeler","/v":"Van","/w":"Water station","/x":"X / Unix","/y":"House, yagi antenna","/z":"Shelter","\\!":"Emergency","\\#":"Digipeater, green star","\\$":"Bank or ATM","\\&":"Gateway station","\\'":"Crash / incident site","\\(":"Cloudy","\\)":"Firenet MEO, MODIS Earth Observation","\\*":"Snow","\\+":"Church","\\,":"Girl Scouts","\\-":"House, HF antenna","\\.":"Ambiguous, question mark inside circle","\\/":"Waypoint destination","\\0":"Circle, IRLP / Echolink/WIRES","\\8":"802.11 WiFi or other network node","\\9":"Gas station","\\:":"Hail","\\;":"Park, picnic area","\\<":"Advisory, single red flag","\\>":"Red car","\\?":"Info kiosk","\\@":"Hurricane, Tropical storm","\\A":"White box","\\B":"Blowing snow","\\C":"Coast Guard","\\D":"Drizzling rain","\\E":"Smoke, Chimney","\\F":"Freezing rain","\\G":"Snow shower","\\H":"Haze","\\I":"Rain shower","\\J":"Lightning","\\K":"Kenwood HT","\\L":"Lighthouse","\\N":"Navigation buoy","\\O":"Rocket","\\P":"Parking","\\Q":"Earthquake","\\R":"Restaurant","\\S":"Satellite","\\T":"Thunderstorm","\\U":"Sunny","\\V":"VORTAC, Navigational aid","\\W":"NWS site","\\X":"Pharmacy","\\[":"Wall Cloud","\\^":"Aircraft","\\_":"Weather site","\\`":"Rain","\\a":"Red diamond","\\b":"Blowing dust, sand","\\c":"CD triangle, RACES, CERTS, SATERN","\\d":"DX spot","\\e":"Sleet","\\f":"Funnel cloud","\\g":"Gale, two red flags","\\h":"Store","\\i":"Black box, point of interest","\\j":"Work zone, excavating machine","\\k":"SUV, ATV","\\m":"Value sign, 3 digit display","\\n":"Red triangle","\\o":"Small circle","\\p":"Partly cloudy","\\r":"Restrooms","\\s":"Ship, boat","\\t":"Tornado","\\u":"Truck","\\v":"Van","\\w":"Flooding","\\y":"Skywarn","\\z":"Shelter","\\{":"Fog"};
function decode(position){
const {symbol,symset}=position||{};
if(typeof symbol!=='string'||symbol.length!==1||symbol<'!'||symbol>'~'||!/^[/\\A-Z0-9]$/.test(symset||''))return null;
const table=symset==='/'?'primary':'alternate',overlay=/^[A-Z0-9]$/.test(symset)?symset:null;
const index=symbol.charCodeAt(0)-33;
return {symbol,symset,table,overlay,index,name:names[(table==='primary'?'/':'\\')+symbol]||'APRS',code:symset+symbol};
}
function html(position){
const info=decode(position);if(!info)return '?';
const col=info.index%16,row=Math.floor(info.index/16);
return ''+(info.overlay?''+info.overlay+'':'')+'';
}
// APRS101 chapter 10 and aprs.org/aprs12/mic-e-types.txt.
function packetDetails(body=''){
body=String(body);const result={format:null,comment:body,speedKmh:null,course:null,altitudeM:null,device:null};
if(/^[`']/.test(body)){
result.format='Mic-E';result.comment='';
if(body.length<9||!decode({symset:body[8],symbol:body[7]}))return result;
const [sp,dc,se]=[4,5,6].map(i=>body.charCodeAt(i)-28);
if(sp>=0&&sp<=99&&dc>=0&&dc<=99&&se>=0&&se<=99){
let speed=sp*10+Math.floor(dc/10),course=(dc%10)*100+se;
if(speed>=800)speed-=800;if(course>=400)course-=400;
if(speed>=0&&speed<=799&&course>=0&&course<=360){result.speedKmh=Math.round(speed*1.852);result.course=course||null;}
}
let tail=body.slice(9),prefix=tail[0];
if(prefix==='>'||prefix===']'){
result.device=prefix==='>'?'Kenwood TH-D7':'Kenwood TM-D700';tail=tail.slice(1);
if(tail.endsWith('=')){result.device=prefix==='>'?'Kenwood TH-D72':'Kenwood TM-D710';tail=tail.slice(0,-1);}
else if(prefix==='>'&&tail.endsWith('^')){result.device='Kenwood TH-D74';tail=tail.slice(0,-1);}
}else if(prefix==='`'||prefix==="'"){
tail=tail.slice(1);
const devices={'_ ':'Yaesu VX-8','_"':'Yaesu FTM-350','_#':'Yaesu VX-8G','_$':'Yaesu FT1D','_%':'Yaesu FTM-400DR','_)':'Yaesu FTM-100D','_(':'Yaesu FT2D','_0':'Yaesu FT3D','_3':'Yaesu FT5D','_1':'Yaesu FTM-300D','(5':'AnyTone D578UV','(8':'AnyTone D878UV','|3':'Byonics TinyTrak3','|4':'Byonics TinyTrak4',':4':'SCS DR-7400',':8':'SCS DR-7800'};
const device=devices[tail.slice(-2)];if(device){result.device=device;tail=tail.slice(0,-2);}
}else if(prefix===' ')tail=tail.slice(1);
const altitude=tail.match(/^([!-{]{3})\}/);
if(altitude){result.altitudeM=[...altitude[1]].reduce((n,c)=>n*91+c.charCodeAt(0)-33,0)-10000;tail=tail.slice(4);}
result.comment=tail;
}else if(/^[!=/@]/.test(body)){
result.format='APRS';const payload=body.slice(body[0]==='@'||body[0]==='/'?8:1);
if(/^\d{4}\.\d{2}[NS].\d{5}\.\d{2}[EW]./.test(payload))result.comment=payload.slice(19);
else if(/^[/\A-Z0-9][!-{]{8}[!-~][!-{]{3}/.test(payload))result.comment=payload.slice(13);
else result.comment='';
}else if(body.startsWith('>'))result.comment=body.slice(1).replace(/^\d{6}[zh/]/,'');
// Control bytes belong to the encoded frame, never the operator's description.
result.comment=result.comment.replace(/[\x00-\x1f\x7f-\x9f]/g,' ').trim();return result;
}
window.BPQAprsPacket={decode:packetDetails};
window.BPQAprsSymbols={decode,html};
})();