mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-08-05 00:46:00 +08:00
first version looks ok
This commit is contained in:
49
server/api/copilot/simbrief.get.ts
Normal file
49
server/api/copilot/simbrief.get.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { createError, defineEventHandler, getQuery } from 'h3'
|
||||
|
||||
// Lädt aktuellen SimBrief OFP. Public API: kein Token nötig, Username oder PilotID reicht.
|
||||
// Doku: https://www.simbrief.com/api/xml.fetcher.php
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = getQuery(event)
|
||||
const username = (Array.isArray(query.username) ? query.username[0] : query.username)?.toString().trim()
|
||||
const userid = (Array.isArray(query.userid) ? query.userid[0] : query.userid)?.toString().trim()
|
||||
if (!username && !userid) {
|
||||
throw createError({statusCode: 400, statusMessage: 'username or userid required'})
|
||||
}
|
||||
|
||||
const params = new URLSearchParams({json: '1'})
|
||||
if (username) params.set('username', username)
|
||||
if (userid) params.set('userid', userid)
|
||||
|
||||
const url = `https://www.simbrief.com/api/xml.fetcher.php?${params.toString()}`
|
||||
const fetcher = (globalThis as any).$fetch as (target: string, options?: Record<string, unknown>) => Promise<any>
|
||||
const ofp = await fetcher(url, {method: 'GET'})
|
||||
|
||||
if (!ofp || ofp.fetch?.status !== 'Success') {
|
||||
throw createError({statusCode: 404, statusMessage: 'No SimBrief OFP found'})
|
||||
}
|
||||
|
||||
return {
|
||||
callsign: ofp.atc?.callsign,
|
||||
flightNumber: ofp.general?.flight_number,
|
||||
aircraftReg: ofp.aircraft?.reg,
|
||||
aircraftIcao: ofp.aircraft?.icao_code,
|
||||
departure: ofp.origin?.icao_code,
|
||||
destination: ofp.destination?.icao_code,
|
||||
altn: ofp.alternate?.icao_code,
|
||||
route: ofp.general?.route,
|
||||
crzFL: ofp.general?.initial_altitude ? `FL${Math.round(Number(ofp.general.initial_altitude) / 100)}` : undefined,
|
||||
costIndex: ofp.general?.costindex,
|
||||
zfw: ofp.weights?.est_zfw ? (Number(ofp.weights.est_zfw) / 1000).toFixed(1) : undefined,
|
||||
blockFuel: ofp.fuel?.plan_ramp ? (Number(ofp.fuel.plan_ramp) / 1000).toFixed(1) : undefined,
|
||||
tripFuel: ofp.fuel?.enroute_burn ? (Number(ofp.fuel.enroute_burn) / 1000).toFixed(1) : undefined,
|
||||
sid: ofp.api_params?.sid_ident || ofp.general?.sid_ident,
|
||||
rwy: ofp.origin?.plan_rwy,
|
||||
transAlt: ofp.origin?.trans_alt,
|
||||
transLevel: ofp.destination?.trans_level,
|
||||
atc: {
|
||||
initial_alt: ofp.atc?.initial_alt,
|
||||
route: ofp.atc?.route,
|
||||
},
|
||||
raw: ofp,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user