2025-05-15 16:59:12 +08:00
|
|
|
const { getDBConnection } = require('./file-utils');
|
|
|
|
|
|
|
|
// 获取所有配置
|
2025-05-16 09:21:11 +08:00
|
|
|
function getConfigurations(planeName) {
|
2025-05-15 16:59:12 +08:00
|
|
|
try {
|
|
|
|
const db = getDBConnection(true);
|
|
|
|
|
2025-05-16 09:21:11 +08:00
|
|
|
let query = `
|
2025-05-15 16:59:12 +08:00
|
|
|
SELECT * FROM Configuration
|
2025-05-16 09:21:11 +08:00
|
|
|
`;
|
|
|
|
|
|
|
|
const params = [];
|
|
|
|
if (planeName) {
|
|
|
|
query += ` WHERE PlaneName = ?`;
|
|
|
|
params.push(planeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
query += ` ORDER BY ConfID ASC`;
|
|
|
|
|
|
|
|
const configs = db.prepare(query).all(...params);
|
2025-05-15 16:59:12 +08:00
|
|
|
|
|
|
|
return configs;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('获取配置列表失败:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 根据ID获取配置
|
|
|
|
function getConfigurationById(confId) {
|
|
|
|
try {
|
|
|
|
const db = getDBConnection(true);
|
|
|
|
|
|
|
|
const config = db.prepare(`
|
|
|
|
SELECT * FROM Configuration
|
|
|
|
WHERE ConfID = ?
|
|
|
|
`).get(confId);
|
|
|
|
|
|
|
|
return config;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`获取配置ID ${confId} 失败:`, error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建新配置
|
|
|
|
function createConfiguration(configData) {
|
|
|
|
try {
|
|
|
|
// 验证必填字段
|
|
|
|
const requiredFields = ['ConfName', 'WorkPath', 'DomainID'];
|
|
|
|
for (const field of requiredFields) {
|
|
|
|
if (!configData[field]) {
|
|
|
|
throw new Error(`${field} 是必填字段`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = getDBConnection();
|
|
|
|
|
|
|
|
const result = db.prepare(`
|
|
|
|
INSERT INTO Configuration (
|
|
|
|
PlaneName, ConfName, OSName, OSVersion, RTXVersion,
|
|
|
|
CPUAffinity, WorkPath, ModelsPath, ServicesPath,
|
|
|
|
DomainID, ConsoleDebug, ConsoleInfo, ConsoleWarning,
|
|
|
|
ConsoleError, LogDebug, LogInfo, LogWarning, LogError
|
|
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
`).run(
|
|
|
|
configData.PlaneName || 'C909',
|
|
|
|
configData.ConfName,
|
|
|
|
configData.OSName || 'Debian 11',
|
|
|
|
configData.OSVersion || '5.10.0-32-rt-amd64',
|
|
|
|
configData.RTXVersion || 'preempt-rt',
|
|
|
|
configData.CPUAffinity || '0,1',
|
|
|
|
configData.WorkPath,
|
|
|
|
configData.ModelsPath || 'Models/',
|
|
|
|
configData.ServicesPath || 'Services/',
|
|
|
|
configData.DomainID,
|
|
|
|
configData.ConsoleDebug !== undefined ? configData.ConsoleDebug : 1,
|
|
|
|
configData.ConsoleInfo !== undefined ? configData.ConsoleInfo : 1,
|
|
|
|
configData.ConsoleWarning !== undefined ? configData.ConsoleWarning : 1,
|
|
|
|
configData.ConsoleError !== undefined ? configData.ConsoleError : 1,
|
|
|
|
configData.LogDebug !== undefined ? configData.LogDebug : 0,
|
|
|
|
configData.LogInfo !== undefined ? configData.LogInfo : 1,
|
|
|
|
configData.LogWarning !== undefined ? configData.LogWarning : 1,
|
|
|
|
configData.LogError !== undefined ? configData.LogError : 1
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
confId: result.lastInsertRowid,
|
|
|
|
message: '配置创建成功'
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error('创建配置失败:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新配置
|
|
|
|
function updateConfiguration(configData) {
|
|
|
|
try {
|
|
|
|
// 验证必填字段
|
|
|
|
if (!configData.ConfID) {
|
|
|
|
throw new Error('ConfID 是必填字段');
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = getDBConnection();
|
|
|
|
|
|
|
|
// 检查配置是否存在
|
|
|
|
const existingConfig = db.prepare(`
|
|
|
|
SELECT COUNT(*) as count FROM Configuration
|
|
|
|
WHERE ConfID = ?
|
|
|
|
`).get(configData.ConfID);
|
|
|
|
|
|
|
|
if (existingConfig.count === 0) {
|
|
|
|
throw new Error('要更新的配置不存在');
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = db.prepare(`
|
|
|
|
UPDATE Configuration
|
|
|
|
SET PlaneName = ?,
|
|
|
|
ConfName = ?,
|
|
|
|
OSName = ?,
|
|
|
|
OSVersion = ?,
|
|
|
|
RTXVersion = ?,
|
|
|
|
CPUAffinity = ?,
|
|
|
|
WorkPath = ?,
|
|
|
|
ModelsPath = ?,
|
|
|
|
ServicesPath = ?,
|
|
|
|
DomainID = ?,
|
|
|
|
ConsoleDebug = ?,
|
|
|
|
ConsoleInfo = ?,
|
|
|
|
ConsoleWarning = ?,
|
|
|
|
ConsoleError = ?,
|
|
|
|
LogDebug = ?,
|
|
|
|
LogInfo = ?,
|
|
|
|
LogWarning = ?,
|
|
|
|
LogError = ?
|
|
|
|
WHERE ConfID = ?
|
|
|
|
`).run(
|
|
|
|
configData.PlaneName || 'C909',
|
|
|
|
configData.ConfName,
|
|
|
|
configData.OSName || 'Debian 11',
|
|
|
|
configData.OSVersion || '5.10.0-32-rt-amd64',
|
|
|
|
configData.RTXVersion || 'preempt-rt',
|
|
|
|
configData.CPUAffinity || '0,1',
|
|
|
|
configData.WorkPath,
|
|
|
|
configData.ModelsPath || 'Models/',
|
|
|
|
configData.ServicesPath || 'Services/',
|
|
|
|
configData.DomainID,
|
|
|
|
configData.ConsoleDebug !== undefined ? configData.ConsoleDebug : 1,
|
|
|
|
configData.ConsoleInfo !== undefined ? configData.ConsoleInfo : 1,
|
|
|
|
configData.ConsoleWarning !== undefined ? configData.ConsoleWarning : 1,
|
|
|
|
configData.ConsoleError !== undefined ? configData.ConsoleError : 1,
|
|
|
|
configData.LogDebug !== undefined ? configData.LogDebug : 0,
|
|
|
|
configData.LogInfo !== undefined ? configData.LogInfo : 1,
|
|
|
|
configData.LogWarning !== undefined ? configData.LogWarning : 1,
|
|
|
|
configData.LogError !== undefined ? configData.LogError : 1,
|
|
|
|
configData.ConfID
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
changes: result.changes,
|
|
|
|
message: '配置更新成功'
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error('更新配置失败:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除配置
|
|
|
|
function deleteConfiguration(confId) {
|
|
|
|
try {
|
|
|
|
const db = getDBConnection();
|
|
|
|
|
|
|
|
const result = db.prepare('DELETE FROM Configuration WHERE ConfID = ?').run(confId);
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
changes: result.changes,
|
|
|
|
message: result.changes > 0 ? '配置删除成功' : '配置不存在或已被删除'
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error('删除配置失败:', error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getConfigurations,
|
|
|
|
getConfigurationById,
|
|
|
|
createConfiguration,
|
|
|
|
updateConfiguration,
|
|
|
|
deleteConfiguration
|
|
|
|
};
|