diff --git a/CloudServer/server.js b/CloudServer/server.js index baa19a1..298e1fd 100644 --- a/CloudServer/server.js +++ b/CloudServer/server.js @@ -1963,6 +1963,39 @@ function ensureSilenceWav() { execSync(`ffmpeg -y -v quiet -f lavfi -i anullsrc=r=48000:cl=mono -t 1 -c:a pcm_s16le "${silence1sWavPath}"`); } +function resolveStationAudioCode(rawCode, rawName) { + const code1 = String(rawCode || '').trim().toLowerCase().replace(/_/g, '-'); + const name = String(rawName || '').trim(); + const pick = (c) => { + if (!c) return null; + const v = String(c).trim().toLowerCase().replace(/_/g, '-'); + return v && v !== 'unk' && v !== 'unknown' ? v : null; + }; + // Level 1: raw destCode (already provided) + let result = pick(code1); + let levelHit = result ? 1 : 0; + // Level 2: extract from destination name string (parens / dash / pattern) + if (!result && name) { + const extracted = getStationCodeFromName(name); + result = pick(extracted); + if (result) levelHit = 2; + } + // Level 3: fuzzy match station name to registered stations and use their code + if (!result && name) { + const reg = findStationByFlexibleName(name); + if (reg && reg.code) { + result = pick(reg.code); + if (result) levelHit = 3; + } + } + if (result && (name || rawCode)) { + log('DEBUG', `[Station-Audio-Code] resolved destCode=${result} (level=${levelHit}) rawCode=${JSON.stringify(rawCode || null)} name=${JSON.stringify(name || null)}`); + } else { + log('WARN', `[Station-Audio-Code] FAILED to resolve station audio code: rawCode=${JSON.stringify(rawCode || null)} name=${JSON.stringify(name || null)} → sequence will skip this station audio (silence / missing wav)`); + } + return result || null; +} + function getOrCreateHlsStreamState(streamName) { if (!hlsStreams[streamName]) { hlsStreams[streamName] = { @@ -2688,7 +2721,7 @@ function handleTrainApproaching(ws, data) { const finalPlatform = cleanPlatform || '1'; const lineCode = data.lineCode || (stationCode ? stationCode.split('-')[0] : 'UNK'); - const destCode = data.destCode || getStationCodeFromName(data.destination) || 'UNK'; + const destCode = resolveStationAudioCode(data.destCode, data.destination) || 'UNK'; log('INFO', `Train ${trainId} approaching ${stationName}. Broadcasting pre-arrival...`); @@ -2804,9 +2837,10 @@ function handleTrainArrival(ws, data) { const finalPlatform = cleanPlatform || '1'; const lineCode = data.lineCode || (stationCode ? stationCode.split('-')[0] : 'UNK'); - const destCode = data.destCode || getStationCodeFromName(data.destination) || 'UNK'; + const destCode = resolveStationAudioCode(data.destCode, data.destination) || 'UNK'; const nextStationRaw = data.nextStation ? String(data.nextStation).trim() : ''; - const nextCodeRaw = data.nextCode ? String(data.nextCode).trim() : (nextStationRaw ? getStationCodeFromName(nextStationRaw) : ''); + const nextCodeRaw = resolveStationAudioCode(data.nextCode, nextStationRaw) || + (nextStationRaw ? getStationCodeFromName(nextStationRaw) : ''); const hasNextStation = nextStationRaw && nextStationRaw !== '' && nextCodeRaw && nextCodeRaw !== 'UNK' && nextCodeRaw !== ''; const finalNextCode = hasNextStation ? nextCodeRaw : null; @@ -2885,9 +2919,10 @@ function handleTrainDeparture(ws, data) { const beep = data.departureBeep || data.beep || 'fc1'; const lineCode = data.lineCode || (stationCode ? stationCode.split('-')[0] : 'UNK'); - const destCode = data.destCode || getStationCodeFromName(data.destination) || 'UNK'; + const destCode = resolveStationAudioCode(data.destCode, data.destination) || 'UNK'; const nextStationRaw = data.nextStation ? String(data.nextStation).trim() : ''; - const nextCodeRaw = data.nextCode ? String(data.nextCode).trim() : (nextStationRaw ? getStationCodeFromName(nextStationRaw) : ''); + const nextCodeRaw = resolveStationAudioCode(data.nextCode, nextStationRaw) || + (nextStationRaw ? String(data.nextCode || '').trim() || getStationCodeFromName(nextStationRaw) : ''); const hasNextStation = nextStationRaw && nextStationRaw !== '' && nextCodeRaw && nextCodeRaw !== 'UNK' && nextCodeRaw !== ''; const finalNextCode = hasNextStation ? nextCodeRaw : null; diff --git a/station_broadcast.lua b/station_broadcast.lua index 8617d38..2ee9164 100644 --- a/station_broadcast.lua +++ b/station_broadcast.lua @@ -1,6 +1,6 @@ -- 1. Configuration & Constants -local APP_VERSION_TEXT = "v1.0.109" +local APP_VERSION_TEXT = "v1.0.110" local CONFIG = { CLOUD_URL = "ws://stabadmin.fse-media.group", @@ -4044,18 +4044,19 @@ function Main.loop() Utils.log("INFO", string.format("Train Alert: %s (ETA: %ds, Delay: %dm, Cancelled: %s, Reason: %s)", trainName, etaSeconds, displayDelayMinutes, tostring(cancelled), tostring(reason or ""))) if Utils.isLocalStationMaster() then Network.send({ - type = "train_alert", - stationName = State.stationName, - stationCode = State.stationCode, - stationPlatform = platform, - trainId = trainName, - destination = destination, - eta = tostring(etaSeconds) .. "s", - etaSeconds = etaSeconds, - delay = displayDelayMinutes, - cancelled = cancelled, - reason = reason - }) + type = "train_alert", + stationName = State.stationName, + stationCode = State.stationCode, + stationPlatform = platform, + trainId = trainName, + destination = destination, + destCode = Utils.getStationCode(destination), + eta = tostring(etaSeconds) .. "s", + etaSeconds = etaSeconds, + delay = displayDelayMinutes, + cancelled = cancelled, + reason = reason + }) end end end @@ -4087,6 +4088,7 @@ function Main.loop() stationPlatform = platform, trainId = trainName, destination = destination, + destCode = Utils.getStationCode(destination), eta = string.format("%.1fs", etaSeconds), etaSeconds = etaSeconds, delay = displayDelayMinutes, @@ -4216,6 +4218,7 @@ function Main.loop() stationPlatform = platform or State.stationPlatform or "1", trainId = tName, destination = dest, + destCode = Utils.getStationCode(dest), eta = etaStr, etaSeconds = etaSeconds, delay = delay @@ -4251,6 +4254,7 @@ function Main.loop() stationPlatform = platform or State.stationPlatform or "1", trainId = tName, destination = dest, + destCode = Utils.getStationCode(dest), eta = etaStr, etaSeconds = etaSeconds, delay = delay