XNSim/XNSimPortal/routes/DataCollect.js

94 lines
2.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const { startCollectData, getCollectDataStatus, stopCollectData } = require('../utils/xnCoreService');
const path = require('path');
// 启动数据采集
router.post('/start', async (req, res) => {
try {
const { collectDataInfo, dcsFilePath } = req.body;
if (!collectDataInfo || !dcsFilePath) {
return res.status(400).json({
success: false,
message: '缺少必要参数'
});
}
// 构建上传文件的完整路径
const fullPath = path.join(__dirname, '..', 'upload', dcsFilePath);
const result = startCollectData(collectDataInfo, fullPath);
if (result === '启动数据采集成功') {
res.json({
success: true,
message: result
});
} else {
res.status(500).json({
success: false,
message: result
});
}
} catch (error) {
console.error('启动数据采集失败:', error);
res.status(500).json({
success: false,
message: '启动数据采集失败: ' + error.message
});
}
});
// 获取数据采集状态
router.get('/status', async (req, res) => {
try {
const status = getCollectDataStatus();
if (typeof status === 'number') {
res.json({
success: true,
status: status, // 0-成功1-正在采集
message: status === 1 ? '正在采集' : '采集完成'
});
} else {
res.status(500).json({
success: false,
message: status
});
}
} catch (error) {
console.error('获取数据采集状态失败:', error);
res.status(500).json({
success: false,
message: '获取数据采集状态失败: ' + error.message
});
}
});
// 停止数据采集
router.post('/stop', async (req, res) => {
try {
const result = stopCollectData();
if (result === '停止数据采集成功') {
res.json({
success: true,
message: result
});
} else {
res.status(500).json({
success: false,
message: result
});
}
} catch (error) {
console.error('停止数据采集失败:', error);
res.status(500).json({
success: false,
message: '停止数据采集失败: ' + error.message
});
}
});
module.exports = router;