feat: Add Line Audio Generation and Line List Manager

This commit is contained in:
2026-08-01 11:20:58 +08:00
parent b140aafa2d
commit e66f39284f
2 changed files with 145 additions and 42 deletions
+73 -13
View File
@@ -1763,52 +1763,109 @@ function findStationByFlexibleName(targetName) {
function handleTrainArrival(ws, data) {
const trainId = data.trainId;
const stationName = getCorrectStationName(data.stationName);
const existingIndex = activeTrains.findIndex(t => t.trainId === trainId);
const stationCode = data.stationCode;
const platform = data.platform || '1';
const lineCode = data.lineCode || 'UNK';
const destCode = data.destCode || 'UNK';
const nextCode = data.nextCode || 'UNK';
const existingIndex = activeTrains.findIndex(t => t.trainId === trainId);
const trainData = {
trainId,
stationName,
status: 'arrived',
lastUpdated: Date.now()
};
if (existingIndex >= 0) {
activeTrains[existingIndex] = trainData;
} else {
activeTrains.push(trainData);
}
log('INFO', `Train ${trainId} arrived at ${stationName}`);
log('INFO', `Train ${trainId} arrived at ${stationName}. Broadcasting...`);
saveData();
broadcastUpdate();
ws.send(JSON.stringify({
type: 'train_arrival_ack',
success: true,
message: 'Train arrival processed successfully',
trainId
}));
// Broadcast Audio Logic
const seq = [
'ksts',
`ja-${platform}`, 'ja-zt', `ja-${lineCode}`, 'ja-xl', `ja-${destCode}`, 'ja-to', 'ja-nextsta', `ja-${nextCode}`, 'ja-warn',
`ch-${platform}`, 'ch-zt', `ch-${lineCode}`, 'ch-line', 'ch-to', `ch-${destCode}`, 'ch-nextsta', `ch-${nextCode}`, 'ch-warn'
];
enqueueHlsPush(stationName, seq);
const audioId = `arrival-${stationCode}-${Date.now()}`;
const built = buildSequenceWavFile(seq);
if (built) {
enqueueStationAudioTask(stationName, ws, {
audioFormat: built.audioFormat,
streamUrl: built.streamUrl,
durationSec: built.durationSec,
priority: 1,
interruptMode: 'ifLower',
source: 'train_arrival',
audioId,
consoleStreamStart: { type: 'stream_start', station: stationName, seq, streamName: getStationStreamName(stationName), hlsUrl: getStationHlsUrl(getStationStreamName(stationName)) },
consoleStreamEnd: { type: 'stream_end', station: stationName }
});
}
}
function handleTrainDeparture(ws, data) {
const trainId = data.trainId;
const stationName = getCorrectStationName(data.stationName);
const existingIndex = activeTrains.findIndex(t => t.trainId === trainId);
const stationCode = data.stationCode;
const beep = data.beep || 'fc1';
const existingIndex = activeTrains.findIndex(t => t.trainId === trainId);
if (existingIndex >= 0) {
activeTrains.splice(existingIndex, 1);
}
log('INFO', `Train ${trainId} departed from ${stationName}`);
log('INFO', `Train ${trainId} departing from ${stationName}. Broadcasting departure...`);
saveData();
broadcastUpdate();
ws.send(JSON.stringify({
type: 'train_departure_ack',
success: true,
message: 'Train departure processed successfully',
trainId
}));
// Departure Audio Logic
const seq = [
beep,
'ja-doorwarn',
'ch-doorwarn'
];
enqueueHlsPush(stationName, seq);
const audioId = `departure-${stationCode}-${Date.now()}`;
const built = buildSequenceWavFile(seq);
if (built) {
enqueueStationAudioTask(stationName, ws, {
audioFormat: built.audioFormat,
streamUrl: built.streamUrl,
durationSec: built.durationSec,
priority: 1,
interruptMode: 'ifLower',
source: 'train_departure',
audioId,
consoleStreamStart: { type: 'stream_start', station: stationName, seq, streamName: getStationStreamName(stationName), hlsUrl: getStationHlsUrl(getStationStreamName(stationName)) },
consoleStreamEnd: { type: 'stream_end', station: stationName }
});
}
}
function handleStationTimeRequest(ws, data) {
@@ -2460,6 +2517,7 @@ app.post('/api/tts/upload_temp', upload.fields([{ name: 'zhAudio', maxCount: 1 }
// Import TTS temp files to station folder
app.post('/api/tts/import', express.json(), (req, res) => {
const stationCode = String(req.body?.stationCode || '').trim();
const type = String(req.body?.type || 'station').trim();
const zhTemp = String(req.body?.zhTemp || '').trim();
const jaTemp = String(req.body?.jaTemp || req.body?.enTemp || '').trim();
if (!stationCode || !zhTemp || !jaTemp) return res.status(400).json({ success: false, message: 'Missing parameters' });
@@ -2467,8 +2525,10 @@ app.post('/api/tts/import', express.json(), (req, res) => {
const zhSource = path.join(uploadDir, zhTemp);
const jaSource = path.join(uploadDir, jaTemp);
const safeStationCode = String(stationCode).replace(/\//g, '-');
const zhDest = path.join(audioStationDir, `ch-${safeStationCode}.wav`);
const jaDest = path.join(audioStationDir, `ja-${safeStationCode}.wav`);
const targetDir = type === 'line' ? audioGeneralDir : audioStationDir;
const zhDest = path.join(targetDir, `ch-${safeStationCode}.wav`);
const jaDest = path.join(targetDir, `ja-${safeStationCode}.wav`);
try {
if (fs.existsSync(zhSource)) {