mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-08-04 16:22:48 +08:00
Bridge dome light mode mapping and flightlab seat belt flow
This commit is contained in:
@@ -8,10 +8,8 @@ import type {FlightLabTelemetryState} from '../../../shared/data/flightlab/types
|
||||
type DomeLightMode = 'off' | 'white' | 'amber'
|
||||
|
||||
const DOME_LIGHT_WEBHOOK_FALLBACK_URL = 'https://home.io.faktorxmensch.com/api/webhook/lidl_stab_3modi_8492'
|
||||
const DOME_LIGHT_ON_MODES: DomeLightMode[] = ['white', 'amber']
|
||||
|
||||
const nextDomeLightOnModeIndexByToken = new Map<string, number>()
|
||||
const lastDomeLightStateByToken = new Map<string, boolean | null>()
|
||||
const lastDomeLightModeByToken = new Map<string, DomeLightMode | null>()
|
||||
|
||||
/**
|
||||
* Receives telemetry data from an external bridge application.
|
||||
@@ -24,7 +22,22 @@ const lastDomeLightStateByToken = new Map<string, boolean | null>()
|
||||
*/
|
||||
|
||||
/** Map raw bridge field names to FlightLabTelemetryState keys */
|
||||
function parseBooleanTelemetryValue(value: unknown): boolean | null {
|
||||
if (typeof value === 'boolean') return value
|
||||
if (typeof value === 'number') return value !== 0
|
||||
if (typeof value === 'string') {
|
||||
const normalized = value.trim().toLowerCase()
|
||||
if (['1', 'true', 'on', 'yes'].includes(normalized)) return true
|
||||
if (['0', 'false', 'off', 'no'].includes(normalized)) return false
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function mapBridgeTelemetry(raw: Record<string, any>): FlightLabTelemetryState {
|
||||
const seatBeltSignsValue = parseBooleanTelemetryValue(
|
||||
raw.seat_belt_signs ?? raw.SEAT_BELT_SIGNS ?? raw.seatbelt_signs ?? raw.seat_belts_signs,
|
||||
)
|
||||
|
||||
return {
|
||||
AIRSPEED_INDICATED: raw.ias_kt ?? raw.AIRSPEED_INDICATED ?? 0,
|
||||
AIRSPEED_TRUE: raw.tas_kt ?? raw.AIRSPEED_TRUE ?? 0,
|
||||
@@ -39,6 +52,7 @@ function mapBridgeTelemetry(raw: Record<string, any>): FlightLabTelemetryState {
|
||||
GEAR_HANDLE_POSITION: !!(raw.gear_handle ?? raw.GEAR_HANDLE_POSITION ?? false),
|
||||
FLAPS_HANDLE_INDEX: raw.flaps_index ?? raw.FLAPS_HANDLE_INDEX ?? 0,
|
||||
BRAKE_PARKING_POSITION: !!(raw.parking_brake ?? raw.BRAKE_PARKING_POSITION ?? false),
|
||||
...(seatBeltSignsValue === null ? {} : {SEAT_BELT_SIGNS: seatBeltSignsValue}),
|
||||
AUTOPILOT_MASTER: !!(raw.autopilot_master ?? raw.AUTOPILOT_MASTER ?? false),
|
||||
TRANSPONDER_CODE: raw.transponder_code ?? raw.TRANSPONDER_CODE ?? 0,
|
||||
ADF_ACTIVE_FREQUENCY: raw.adf_active_freq ?? raw.ADF_ACTIVE_FREQUENCY ?? 0,
|
||||
@@ -46,42 +60,47 @@ function mapBridgeTelemetry(raw: Record<string, any>): FlightLabTelemetryState {
|
||||
}
|
||||
}
|
||||
|
||||
function resolveBooleanValue(value: unknown): boolean | null {
|
||||
if (typeof value === 'boolean') return value
|
||||
if (typeof value === 'number') {
|
||||
if (value === 1) return true
|
||||
if (value === 0) return false
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
const normalized = value.trim().toLowerCase()
|
||||
if (normalized === 'true' || normalized === '1') return true
|
||||
if (normalized === 'false' || normalized === '0') return false
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function resolveDomeLightValue(raw: Record<string, any>): boolean | null {
|
||||
const value = raw.dome_light ?? raw.DOME_LIGHT
|
||||
return resolveBooleanValue(raw.dome_light ?? raw.DOME_LIGHT)
|
||||
}
|
||||
|
||||
if (typeof value === 'boolean') return value
|
||||
if (typeof value === 'number') {
|
||||
if (value === 1) return true
|
||||
if (value === 0) return false
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
const normalized = value.trim().toLowerCase()
|
||||
if (normalized === 'true' || normalized === '1') return true
|
||||
if (normalized === 'false' || normalized === '0') return false
|
||||
}
|
||||
function resolveNavLogoLightsValue(raw: Record<string, any>): boolean | null {
|
||||
return resolveBooleanValue(raw.nav_logo_lights ?? raw.NAV_LOGO_LIGHTS)
|
||||
}
|
||||
|
||||
return null
|
||||
export function resolveDomeLightMode(raw: Record<string, any>): DomeLightMode | null {
|
||||
const domeLight = resolveDomeLightValue(raw)
|
||||
if (domeLight === null) return null
|
||||
if (!domeLight) return 'off'
|
||||
|
||||
const navLogoLights = resolveNavLogoLightsValue(raw)
|
||||
return navLogoLights ? 'white' : 'amber'
|
||||
}
|
||||
|
||||
async function forwardDomeLightToWebhook(raw: Record<string, any>, webhookUrl: string, stateKey: string) {
|
||||
const domeLight = resolveDomeLightValue(raw)
|
||||
const lastDomeLightState = lastDomeLightStateByToken.get(stateKey) ?? null
|
||||
const mode = resolveDomeLightMode(raw)
|
||||
const lastMode = lastDomeLightModeByToken.get(stateKey) ?? null
|
||||
|
||||
if (domeLight === null) return
|
||||
if (lastDomeLightState === domeLight) return
|
||||
if (mode === null) return
|
||||
if (lastMode === mode) return
|
||||
|
||||
let nextDomeLightOnModeIndex = nextDomeLightOnModeIndexByToken.get(stateKey) ?? 0
|
||||
|
||||
const mode: DomeLightMode = domeLight
|
||||
? DOME_LIGHT_ON_MODES[nextDomeLightOnModeIndex % DOME_LIGHT_ON_MODES.length]!
|
||||
: 'off'
|
||||
|
||||
if (domeLight) {
|
||||
nextDomeLightOnModeIndex = (nextDomeLightOnModeIndex + 1) % DOME_LIGHT_ON_MODES.length
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(webhookUrl, {
|
||||
try {
|
||||
const response = await fetch(webhookUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
@@ -89,27 +108,26 @@ async function forwardDomeLightToWebhook(raw: Record<string, any>, webhookUrl: s
|
||||
body: JSON.stringify({mode}),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const responseBody = await response.text().catch(() => '')
|
||||
console.error(
|
||||
`\x1b[31m[bridge:dome]\x1b[0m webhook failed status=\x1b[91m${response.status}\x1b[0m mode=\x1b[93m${mode}\x1b[0m body=${responseBody.slice(0, 180)}`,
|
||||
)
|
||||
} else {
|
||||
console.info(
|
||||
`\x1b[36m[bridge:dome]\x1b[0m dome_light=\x1b[92m${String(domeLight)}\x1b[0m mode=\x1b[93m${mode}\x1b[0m`,
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
if (!response.ok) {
|
||||
const responseBody = await response.text().catch(() => '')
|
||||
console.error(
|
||||
`\x1b[31m[bridge:dome]\x1b[0m webhook failed status=\x1b[91m${response.status}\x1b[0m mode=\x1b[93m${mode}\x1b[0m body=${responseBody.slice(0, 180)}`,
|
||||
)
|
||||
} else {
|
||||
const domeLight = resolveDomeLightValue(raw)
|
||||
const navLogoLights = resolveNavLogoLightsValue(raw)
|
||||
console.info(
|
||||
`\x1b[36m[bridge:dome]\x1b[0m dome_light=\x1b[92m${String(domeLight)}\x1b[0m nav_logo_lights=\x1b[92m${String(navLogoLights)}\x1b[0m mode=\x1b[93m${mode}\x1b[0m`,
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`\x1b[31m[bridge:dome]\x1b[0m webhook request failed mode=\x1b[93m${mode}\x1b[0m`,
|
||||
error,
|
||||
)
|
||||
} finally {
|
||||
lastDomeLightStateByToken.set(stateKey, domeLight)
|
||||
if (domeLight) {
|
||||
nextDomeLightOnModeIndexByToken.set(stateKey, nextDomeLightOnModeIndex)
|
||||
}
|
||||
}
|
||||
error,
|
||||
)
|
||||
} finally {
|
||||
lastDomeLightModeByToken.set(stateKey, mode)
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
@@ -128,7 +146,7 @@ export default defineEventHandler(async (event) => {
|
||||
const telemetry = body && typeof body === 'object' ? (body as Record<string, any>) : {}
|
||||
const telemetryKeys = Object.keys(telemetry)
|
||||
console.info(`\x1b[35m[bridge:data]\x1b[0m token=\x1b[96m${bridgeToken.slice(0, 6)}...\x1b[0m user=\x1b[92m${userId}\x1b[0m telemetryKeys=\x1b[92m${telemetryKeys.length}\x1b[0m`)
|
||||
// console.table( telemetryKeys.reduce((acc, key) => { acc[key] = telemetry[key]; return acc }, {} as Record<string, any>) )
|
||||
console.table( telemetryKeys.reduce((acc, key) => { acc[key] = telemetry[key]; return acc }, {} as Record<string, any>) )
|
||||
|
||||
const runtimeConfig = useRuntimeConfig()
|
||||
const domeLightWebhookUrl = String(runtimeConfig.domeLightWebhookUrl || '').trim() || DOME_LIGHT_WEBHOOK_FALLBACK_URL
|
||||
|
||||
Reference in New Issue
Block a user