XNSim/XNSimPortal/routes/model-dev.js

198 lines
5.9 KiB
JavaScript

const express = require('express');
const router = express.Router();
const {
getATAChapters,
getModelsByChapterId,
getModelVersionsByClassName,
saveModelVersion
} = require('../utils/model-utils');
const {
modelCodeGen,
modelCodeZip,
modelCodeUnzip,
modelCodeCompile
} = require('../utils/xnCoreService');
// 获取所有ATA章节
router.get('/ata-chapters', (req, res) => {
try {
const chapters = getATAChapters();
res.json(chapters);
} catch (error) {
console.error('处理ATA章节请求时出错:', error);
res.status(500).json({ error: '获取ATA章节数据失败', details: error.message });
}
});
// 获取指定章节ID的模型
router.get('/chapter-models/:chapterId', (req, res) => {
try {
const { chapterId } = req.params;
const { planeName } = req.query;
if (!chapterId) {
return res.status(400).json({ error: '缺少章节ID参数' });
}
if (!planeName) {
return res.status(400).json({ error: '缺少飞机名称参数' });
}
const models = getModelsByChapterId(chapterId, planeName);
res.json(models);
} catch (error) {
console.error('处理章节模型请求时出错:', error);
res.status(500).json({ error: '获取章节模型数据失败', details: error.message });
}
});
// 获取指定ClassName的模型版本
router.get('/model-versions/:className', (req, res) => {
try {
const className = req.params.className;
const { planeName } = req.query;
if (!className) {
return res.status(400).json({ error: '模型类名不能为空' });
}
if (!planeName) {
return res.status(400).json({ error: '飞机名称不能为空' });
}
const versions = getModelVersionsByClassName(className, planeName);
res.json(versions);
} catch (error) {
console.error(`获取模型版本失败: ${error.message}`);
res.status(500).json({ error: '获取模型版本失败', details: error.message });
}
});
// 保存模型版本信息
router.post('/model-versions', (req, res) => {
try {
const versionData = req.body;
if (!versionData || !versionData.ClassName) {
return res.status(400).json({ error: '缺少必要的模型版本数据' });
}
// 验证必填字段
const requiredFields = ['ClassName', 'Name', 'Version', 'Author', 'PlaneName', 'ConfID'];
for (const field of requiredFields) {
if (!versionData[field]) {
return res.status(400).json({ error: `${field} 是必填字段` });
}
}
// 验证 CmdList 是否为有效的 JSON 字符串
if (versionData.CmdList !== undefined) {
try {
JSON.parse(versionData.CmdList);
} catch (e) {
return res.status(400).json({ error: 'CmdList 必须是有效的 JSON 字符串' });
}
}
const result = saveModelVersion(versionData);
res.json(result);
} catch (error) {
console.error(`保存模型版本失败: ${error.message}`);
res.status(500).json({ error: '保存模型版本失败', details: error.message });
}
});
// 生成模型代码
router.post('/model-code-gen', (req, res) => {
try {
const { className, version, planeName } = req.body;
if (!className) {
return res.status(400).json({ error: '模型类名不能为空' });
}
if (!version) {
return res.status(400).json({ error: '版本号不能为空' });
}
if (!planeName) {
return res.status(400).json({ error: '飞机名称不能为空' });
}
const result = modelCodeGen(className, version, planeName);
res.json({ result });
} catch (error) {
console.error(`生成模型代码失败: ${error.message}`);
res.status(500).json({ error: '生成模型代码失败', details: error.message });
}
});
// 压缩模型代码
router.post('/model-code-zip', (req, res) => {
try {
const { className, version, planeName } = req.body;
if (!className) {
return res.status(400).json({ error: '模型类名不能为空' });
}
if (!version) {
return res.status(400).json({ error: '版本号不能为空' });
}
if (!planeName) {
return res.status(400).json({ error: '飞机名称不能为空' });
}
const result = modelCodeZip(className, version, planeName);
res.json(result);
} catch (error) {
console.error(`压缩模型代码失败: ${error.message}`);
res.status(500).json({ error: '压缩模型代码失败', details: error.message });
}
});
// 解压模型代码
router.post('/model-code-unzip', (req, res) => {
try {
const { className, version, planeName, srcPath } = req.body;
if (!className) {
return res.status(400).json({ error: '模型类名不能为空' });
}
if (!version) {
return res.status(400).json({ error: '版本号不能为空' });
}
if (!planeName) {
return res.status(400).json({ error: '飞机名称不能为空' });
}
if (!srcPath) {
return res.status(400).json({ error: '源文件路径不能为空' });
}
const result = modelCodeUnzip(className, version, planeName, srcPath);
res.json({ result });
} catch (error) {
console.error(`解压模型代码失败: ${error.message}`);
res.status(500).json({ error: '解压模型代码失败', details: error.message });
}
});
// 编译模型代码
router.post('/model-code-compile', (req, res) => {
try {
const { className, version, planeName } = req.body;
if (!className) {
return res.status(400).json({ error: '模型类名不能为空' });
}
if (!version) {
return res.status(400).json({ error: '版本号不能为空' });
}
if (!planeName) {
return res.status(400).json({ error: '飞机名称不能为空' });
}
const result = modelCodeCompile(className, version, planeName);
res.json({ result });
} catch (error) {
console.error(`编译模型代码失败: ${error.message}`);
res.status(500).json({ error: '编译模型代码失败', details: error.message });
}
});
module.exports = router;