1079 lines
32 KiB
JavaScript
1079 lines
32 KiB
JavaScript
/**
|
||
* XNCoreService
|
||
* XNCore服务后端工具
|
||
*/
|
||
const ffi = require('ffi-napi');
|
||
const ref = require('ref-napi');
|
||
const path = require('path');
|
||
const os = require('os');
|
||
|
||
// 获取XNCore环境变量
|
||
const xnCorePath = process.env.XNCore || '';
|
||
|
||
// 根据操作系统类型确定动态库扩展名和路径
|
||
const isWindows = os.platform() === 'win32';
|
||
const libExtension = isWindows ? '.dll' : '.so';
|
||
const libPrefix = isWindows ? '' : 'lib';
|
||
|
||
// Login库配置
|
||
const loginLibName = `${libPrefix}Login${libExtension}`;
|
||
const loginLibPath = path.join(xnCorePath, 'lib', loginLibName);
|
||
|
||
// MonitorServer库配置
|
||
const monitorLibName = `${libPrefix}XNMonitorServer${libExtension}`;
|
||
const monitorLibPath = path.join(xnCorePath, 'lib', monitorLibName);
|
||
|
||
// InterfaceGenServer库配置
|
||
const interfaceGenLibName = `${libPrefix}XNInterfaceGenServer${libExtension}`;
|
||
const interfaceGenLibPath = path.join(xnCorePath, 'lib', interfaceGenLibName);
|
||
|
||
// ModelGenServer库配置
|
||
const modelGenLibName = `${libPrefix}XNModelGenServer${libExtension}`;
|
||
const modelGenLibPath = path.join(xnCorePath, 'lib', modelGenLibName);
|
||
|
||
// ServiceGenServer库配置
|
||
const serviceGenLibName = `${libPrefix}XNServiceGenServer${libExtension}`;
|
||
const serviceGenLibPath = path.join(xnCorePath, 'lib', serviceGenLibName);
|
||
|
||
// 定义Buffer类型
|
||
const BufferType = ref.refType(ref.types.void);
|
||
const StringType = ref.types.CString;
|
||
const IntType = ref.types.int;
|
||
|
||
// 定义动态库函数接口
|
||
let loginLib;
|
||
let monitorLib;
|
||
let interfaceGenLib;
|
||
let modelGenLib;
|
||
let serviceGenLib;
|
||
|
||
try {
|
||
loginLib = ffi.Library(loginLibPath, {
|
||
'validateUser': ['int', [StringType, 'int', StringType, 'int']],
|
||
'getUserInfo': ['int', ['int', StringType, 'int']],
|
||
'cleanup': ['void', []],
|
||
'registerUser': ['int', [StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'changePassword': ['int', ['int', StringType, 'int', StringType, 'int']],
|
||
'updateUserInfo': ['int', ['int', StringType, 'int']],
|
||
'updateUserAccessLevel': ['int', ['int', 'int']],
|
||
'updateUserIcon': ['int', ['int', StringType, 'int']],
|
||
'getAllUsersSimpleInfo': ['int', [StringType, 'int']],
|
||
'resetPassword': ['int', ['int']],
|
||
'deleteUser': ['int', ['int']]
|
||
});
|
||
} catch (error) {
|
||
console.error(`加载 ${loginLibName} 失败:`, error);
|
||
}
|
||
|
||
try {
|
||
monitorLib = ffi.Library(monitorLibPath, {
|
||
'XN_Initialize': ['int', [StringType, 'int', 'int', 'int', StringType, 'int']],
|
||
'XN_Cleanup': ['void', []],
|
||
'XN_StartMonitorSystemInfo': ['int', [StringType, 'int']],
|
||
'XN_GetSystemInfo': ['int', [StringType, 'int']],
|
||
'XN_GetAllThreadInfo': ['int', [StringType, 'int']],
|
||
'XN_StopMonitorSystemInfo': ['void', []],
|
||
'XN_StartMonitorModelInfo': ['int', [StringType, 'int']],
|
||
'XN_GetModelInfo': ['int', [StringType, 'int']],
|
||
'XN_StopMonitorModelInfo': ['void', []],
|
||
'XN_InitializeEngineControl': ['int', [StringType, 'int']],
|
||
'XN_PauseEngine': ['void', [StringType, 'int']],
|
||
'XN_ResumeEngine': ['void', [StringType, 'int']],
|
||
'XN_StopEngine': ['void', [StringType, 'int']],
|
||
'XN_StartDataMonitor': ['int', [StringType, 'int', StringType, 'int']],
|
||
'XN_StopDataMonitor': ['void', [StringType, 'int', StringType, 'int']],
|
||
'XN_GetDataMonitorInfo': ['int', [StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XN_InjectDataInterface': ['int', [StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XN_StartInjectContinuous': ['int', [StringType, 'int', StringType, 'int', 'double', StringType, 'int']],
|
||
'XN_StopInjectContinuous': ['int', [StringType, 'int', StringType, 'int']],
|
||
'XN_InjectDataInterfaceFromCsv': ['int', [StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XN_StopCsvDataInject': ['int', [StringType, 'int']],
|
||
'XN_GetCsvDataInjectStatus': ['int', [StringType, 'int']],
|
||
'XN_StartCollectData': ['int', [StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XN_GetCollectDataStatus': ['int', [StringType, 'int']],
|
||
'XN_StopCollectData': ['int', [StringType, 'int']]
|
||
});
|
||
} catch (error) {
|
||
console.error(`加载 ${monitorLibName} 失败:`, error);
|
||
}
|
||
|
||
try {
|
||
interfaceGenLib = ffi.Library(interfaceGenLibPath, {
|
||
'XNInterfaceGen_Step1_InitParams': ['int', ['int', StringType, 'int']],
|
||
'XNInterfaceGen_Step2_GetInterfaceData': ['int', [StringType, 'int']],
|
||
'XNInterfaceGen_Step3_CreateConfigDir': ['int', [StringType, 'int']],
|
||
'XNInterfaceGen_Step4_GenerateIDL': ['int', [StringType, 'int']],
|
||
'XNInterfaceGen_Step5_GenerateFastDDS': ['int', [StringType, 'int']],
|
||
'XNInterfaceGen_Step6_GenerateDDSInterface': ['int', [StringType, 'int']],
|
||
'XNInterfaceGen_Step7_GenerateCMakeLists': ['int', [StringType, 'int']],
|
||
'XNInterfaceGen_Step8_BuildAndInstall': ['int', [StringType, 'int']],
|
||
'XNInterfaceGen_Step9_SudoLdconfig': ['int', [StringType, 'int']]
|
||
});
|
||
} catch (error) {
|
||
console.error(`加载 ${interfaceGenLibName} 失败:`, error);
|
||
}
|
||
|
||
try {
|
||
modelGenLib = ffi.Library(modelGenLibPath, {
|
||
'XNModelCodeGen': ['int', [StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XNModelCodeZip': ['int', [StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XNModelCodeUnzip': ['int', [StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XNModelCodeCompile': ['int', [StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int']]
|
||
});
|
||
} catch (error) {
|
||
console.error(`加载 ${modelGenLibName} 失败:`, error);
|
||
}
|
||
|
||
try {
|
||
serviceGenLib = ffi.Library(serviceGenLibPath, {
|
||
'XNServiceCodeGen': ['int', [StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XNServiceCodeZip': ['int', [StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XNServiceCodeUnzip': ['int', [StringType, 'int', StringType, 'int', StringType, 'int', StringType, 'int']],
|
||
'XNServiceCodeCompile': ['int', [StringType, 'int', StringType, 'int', StringType, 'int']]
|
||
});
|
||
} catch (error) {
|
||
console.error(`加载 ${serviceGenLibName} 失败:`, error);
|
||
}
|
||
|
||
// 注册进程退出时的清理函数
|
||
function performCleanup() {
|
||
console.log('正在执行清理操作...');
|
||
try {
|
||
// 清理 loginLib
|
||
if (loginLib) {
|
||
loginLib.cleanup();
|
||
}
|
||
|
||
// 清理 monitorLib
|
||
if (monitorLib) {
|
||
// 停止所有监控
|
||
stopMonitorSystemInfo();
|
||
stopMonitorModelInfo();
|
||
// 停止所有数据监控和注入
|
||
stopDataMonitor('');
|
||
stopInjectContinuous('');
|
||
// 清理监控服务器资源
|
||
monitorLib.XN_Cleanup();
|
||
}
|
||
console.log('清理操作完成');
|
||
} catch (error) {
|
||
console.error('清理操作失败:', error);
|
||
}
|
||
}
|
||
|
||
// 转换字符串为Buffer的函数
|
||
function stringToBuffer(str) {
|
||
try {
|
||
if (!str) {
|
||
return { buffer: Buffer.alloc(0), length: 0 };
|
||
}
|
||
const buffer = Buffer.from(str, 'utf8');
|
||
return { buffer: buffer, length: buffer.length };
|
||
} catch (error) {
|
||
console.error('字符串转Buffer失败:', error);
|
||
return { buffer: Buffer.alloc(0), length: 0 };
|
||
}
|
||
}
|
||
|
||
// 初始化监控服务器
|
||
function initializeMonitor(domainId, confID, forceGen = 0) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
// 创建错误消息缓冲区
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_Initialize(domainId, domainId.length, confID, forceGen, errorMsg, errorMsg.length);
|
||
|
||
if (result !== 0) {
|
||
return `初始化失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '初始化成功';
|
||
} catch (error) {
|
||
return `初始化失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 清理监控服务器资源
|
||
async function cleanupMonitor() {
|
||
if (!monitorLib) {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
monitorLib.XN_Cleanup();
|
||
} catch (error) {
|
||
console.error('清理监控服务器资源失败:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 启动监控系统信息
|
||
function startMonitorSystemInfo() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_StartMonitorSystemInfo(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `启动失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '启动成功';
|
||
} catch (error) {
|
||
return `启动失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 获取系统信息
|
||
function getSystemInfo() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(8192);
|
||
const result = monitorLib.XN_GetSystemInfo(infoMsg, infoMsg.length);
|
||
|
||
if (result !== 0) {
|
||
return `获取失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return infoMsg.toString('utf8').replace(/\0/g, '');
|
||
} catch (error) {
|
||
return `获取失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 获取所有线程信息
|
||
function getAllThreadInfo() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(8192);
|
||
const result = monitorLib.XN_GetAllThreadInfo(infoMsg, infoMsg.length);
|
||
|
||
if (result !== 0) {
|
||
return `获取失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return infoMsg.toString('utf8').replace(/\0/g, '');
|
||
} catch (error) {
|
||
return `获取失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 停止监控系统信息
|
||
function stopMonitorSystemInfo() {
|
||
if (!monitorLib) {
|
||
return;
|
||
}
|
||
try {
|
||
monitorLib.XN_StopMonitorSystemInfo();
|
||
} catch (error) {
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 启动监控模型信息
|
||
function startMonitorModelInfo() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_StartMonitorModelInfo(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `启动失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '启动成功';
|
||
} catch (error) {
|
||
return `启动失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 获取模型信息
|
||
function getModelInfo() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(16384);
|
||
const result = monitorLib.XN_GetModelInfo(infoMsg, infoMsg.length);
|
||
|
||
if (result !== 0) {
|
||
return `获取失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return infoMsg.toString('utf8').replace(/\0/g, '');
|
||
} catch (error) {
|
||
return `获取失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 停止监控模型信息
|
||
function stopMonitorModelInfo() {
|
||
if (!monitorLib) {
|
||
return;
|
||
}
|
||
try {
|
||
monitorLib.XN_StopMonitorModelInfo();
|
||
} catch (error) {
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 初始化引擎控制
|
||
function initializeEngineControl() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_InitializeEngineControl(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `初始化引擎控制失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '初始化引擎控制成功';
|
||
} catch (error) {
|
||
return `初始化引擎控制失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 暂停引擎
|
||
function pauseEngine() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
monitorLib.XN_PauseEngine(errorMsg, errorMsg.length);
|
||
const error = errorMsg.toString('utf8').replace(/\0/g, '');
|
||
return error ? `暂停引擎失败: ${error}` : '暂停引擎成功';
|
||
} catch (error) {
|
||
return `暂停引擎失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 恢复引擎
|
||
function resumeEngine() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
monitorLib.XN_ResumeEngine(errorMsg, errorMsg.length);
|
||
const error = errorMsg.toString('utf8').replace(/\0/g, '');
|
||
return error ? `恢复引擎失败: ${error}` : '恢复引擎成功';
|
||
} catch (error) {
|
||
return `恢复引擎失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 停止引擎
|
||
async function stopEngine() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
monitorLib.XN_StopEngine(errorMsg, errorMsg.length);
|
||
const error = errorMsg.toString('utf8').replace(/\0/g, '');
|
||
return error ? `停止引擎失败: ${error}` : '停止引擎成功';
|
||
} catch (error) {
|
||
return `停止引擎失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 启动数据监控
|
||
function startDataMonitor(structName) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_StartDataMonitor(structName, structName.length, errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `启动数据监控失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '启动数据监控成功';
|
||
} catch (error) {
|
||
return `启动数据监控失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 停止数据监控
|
||
function stopDataMonitor(structName) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
monitorLib.XN_StopDataMonitor(structName, structName.length, errorMsg, errorMsg.length);
|
||
const error = errorMsg.toString('utf8').replace(/\0/g, '');
|
||
return error ? `停止数据监控失败: ${error}` : '停止数据监控成功';
|
||
} catch (error) {
|
||
return `停止数据监控失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 获取数据监控信息
|
||
function getDataMonitorInfo(structName, interfaceName, bufferSize = 8192) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(1024);
|
||
const dataBuffer = Buffer.alloc(bufferSize); // 使用指定的缓冲区大小
|
||
const result = monitorLib.XN_GetDataMonitorInfo(
|
||
structName, structName.length,
|
||
interfaceName, interfaceName.length,
|
||
dataBuffer, dataBuffer.length,
|
||
infoMsg, infoMsg.length
|
||
);
|
||
|
||
if (result !== 0) {
|
||
return `获取数据监控信息失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return {
|
||
data: dataBuffer.toString('utf8').replace(/\0/g, ''),
|
||
info: infoMsg.toString('utf8').replace(/\0/g, '')
|
||
};
|
||
} catch (error) {
|
||
return `获取数据监控信息失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 注入数据接口
|
||
function injectDataInterface(structName, interfaceNameAndData) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_InjectDataInterface(
|
||
structName, structName.length,
|
||
interfaceNameAndData, interfaceNameAndData.length,
|
||
infoMsg, infoMsg.length
|
||
);
|
||
|
||
if (result !== 0) {
|
||
return `注入数据接口失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '注入数据接口成功';
|
||
} catch (error) {
|
||
return `注入数据接口失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 持续注入数据接口
|
||
function startInjectContinuous(structName, interfaceNameAndData, frequency) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_StartInjectContinuous(
|
||
structName, structName.length,
|
||
interfaceNameAndData, interfaceNameAndData.length,
|
||
frequency,
|
||
infoMsg, infoMsg.length
|
||
);
|
||
|
||
if (result !== 0) {
|
||
return `启动持续注入数据失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '启动持续注入数据成功';
|
||
} catch (error) {
|
||
return `启动持续注入数据失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 停止持续注入数据接口
|
||
function stopInjectContinuous(structName) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_StopInjectContinuous(
|
||
structName, structName.length,
|
||
infoMsg, infoMsg.length
|
||
);
|
||
|
||
if (result !== 0) {
|
||
return `停止持续注入数据失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '停止持续注入数据成功';
|
||
} catch (error) {
|
||
return `停止持续注入数据失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 从CSV文件注入数据
|
||
function injectDataInterfaceFromCsv(structName, csvFilePath) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_InjectDataInterfaceFromCsv(
|
||
structName,
|
||
structName.length,
|
||
csvFilePath,
|
||
csvFilePath.length,
|
||
errorMsg,
|
||
errorMsg.length
|
||
);
|
||
|
||
if (result !== 0) {
|
||
return `注入失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '注入成功';
|
||
} catch (error) {
|
||
return `注入失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 停止CSV数据注入
|
||
function stopCsvDataInject() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_StopCsvDataInject(errorMsg, errorMsg.length);
|
||
|
||
if (result !== 0) {
|
||
return `停止失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '停止成功';
|
||
} catch (error) {
|
||
return `停止失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 获取CSV数据注入状态
|
||
function getCsvDataInjectStatus() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_GetCsvDataInjectStatus(infoMsg, infoMsg.length);
|
||
|
||
if (result === -1) {
|
||
return `获取状态失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return result; // 直接返回result值
|
||
} catch (error) {
|
||
return `获取状态失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 启动数据采集
|
||
function startCollectData(collectDataInfo, dcsFilePath) {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_StartCollectData(
|
||
collectDataInfo, collectDataInfo.length,
|
||
dcsFilePath, dcsFilePath.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
|
||
if (result !== 0) {
|
||
return `启动数据采集失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '启动数据采集成功';
|
||
} catch (error) {
|
||
return `启动数据采集失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 获取数据采集状态
|
||
function getCollectDataStatus() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const infoMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_GetCollectDataStatus(infoMsg, infoMsg.length);
|
||
|
||
if (result === -1) {
|
||
return `获取状态失败: ${infoMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return result; // 返回状态:0-成功,1-正在采集
|
||
} catch (error) {
|
||
return `获取状态失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 停止数据采集
|
||
function stopCollectData() {
|
||
if (!monitorLib) {
|
||
return '监控服务器库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = monitorLib.XN_StopCollectData(errorMsg, errorMsg.length);
|
||
|
||
if (result !== 0) {
|
||
return `停止数据采集失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '停止数据采集成功';
|
||
} catch (error) {
|
||
return `停止数据采集失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 重置用户密码
|
||
function resetPassword(userId) {
|
||
if (!loginLib) {
|
||
return '登录库未加载';
|
||
}
|
||
try {
|
||
const result = loginLib.resetPassword(userId);
|
||
if (result === 0) {
|
||
return '密码重置成功';
|
||
} else {
|
||
return `密码重置失败,错误码:${result}`;
|
||
}
|
||
} catch (error) {
|
||
return `密码重置失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 删除用户
|
||
function deleteUser(userId) {
|
||
if (!loginLib) {
|
||
return '登录库未加载';
|
||
}
|
||
try {
|
||
const result = loginLib.deleteUser(userId);
|
||
if (result === 0) {
|
||
return '用户删除成功';
|
||
} else {
|
||
return `用户删除失败,错误码:${result}`;
|
||
}
|
||
} catch (error) {
|
||
return `用户删除失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 更新用户头像
|
||
function updateUserIcon(userId, iconBase64) {
|
||
if (!loginLib) {
|
||
return '登录库未加载';
|
||
}
|
||
try {
|
||
const iconBuffer = stringToBuffer(iconBase64);
|
||
const result = loginLib.updateUserIcon(userId, iconBuffer.buffer, iconBuffer.length);
|
||
if (result === 0) {
|
||
return '头像更新成功';
|
||
} else {
|
||
return `头像更新失败,错误码:${result}`;
|
||
}
|
||
} catch (error) {
|
||
return `头像更新失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// ========== XNInterfaceGenServer 封装函数 ==========
|
||
|
||
function interfaceGenStep1InitParams(configID) {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step1_InitParams(
|
||
configID,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `Step1: 初始化接口生成参数失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step1: 初始化接口生成参数成功';
|
||
} catch (error) {
|
||
return `Step1: 初始化接口生成参数失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function interfaceGenStep2GetInterfaceData() {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step2_GetInterfaceData(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `Step2: 获取接口数据失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step2: 获取接口数据成功';
|
||
} catch (error) {
|
||
return `Step2: 获取接口数据失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function interfaceGenStep3CreateConfigDir() {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step3_CreateConfigDir(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `Step3: 创建构型接口目录失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step3: 创建构型接口目录成功';
|
||
} catch (error) {
|
||
return `Step3: 创建构型接口目录失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function interfaceGenStep4GenerateIDL() {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step4_GenerateIDL(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `Step4: 生成IDL文件失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step4: 生成IDL文件成功';
|
||
} catch (error) {
|
||
return `Step4: 生成IDL文件失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function interfaceGenStep5GenerateFastDDS() {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step5_GenerateFastDDS(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `Step5: 生成FastDDS接口代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step5: 生成FastDDS接口代码成功';
|
||
} catch (error) {
|
||
return `Step5: 生成FastDDS接口代码失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function interfaceGenStep6GenerateDDSInterface() {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step6_GenerateDDSInterface(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `Step6: 生成DDS接口代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step6: 生成DDS接口代码成功';
|
||
} catch (error) {
|
||
return `Step6: 生成DDS接口代码失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function interfaceGenStep7GenerateCMakeLists() {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step7_GenerateCMakeLists(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `Step7: 生成CMakeLists文件失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step7: 生成CMakeLists文件成功';
|
||
} catch (error) {
|
||
return `Step7: 生成CMakeLists文件失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function interfaceGenStep8BuildAndInstall() {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step8_BuildAndInstall(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `Step8: CMake构建并安装失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step8: CMake构建并安装成功';
|
||
} catch (error) {
|
||
return `Step8: CMake构建并安装失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function interfaceGenStep9SudoLdconfig() {
|
||
if (!interfaceGenLib) {
|
||
return '接口库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = interfaceGenLib.XNInterfaceGen_Step9_SudoLdconfig(errorMsg, errorMsg.length);
|
||
if (result !== 0) {
|
||
return `Step9: 执行sudo ldconfig失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return 'Step9: 执行sudo ldconfig成功';
|
||
} catch (error) {
|
||
return `Step9: 执行sudo ldconfig失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// ========== XNModelGenServer 封装函数 ==========
|
||
|
||
// 生成模型代码
|
||
function modelCodeGen(className, version, planeName) {
|
||
if (!modelGenLib) {
|
||
return '模型生成库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = modelGenLib.XNModelCodeGen(
|
||
className, className.length,
|
||
version, version.length,
|
||
planeName, planeName.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `生成模型代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '生成模型代码成功';
|
||
} catch (error) {
|
||
return `生成模型代码失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 压缩模型代码
|
||
function modelCodeZip(className, version, planeName) {
|
||
if (!modelGenLib) {
|
||
return '模型生成库未加载';
|
||
}
|
||
try {
|
||
const dstPath = Buffer.alloc(1024);
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = modelGenLib.XNModelCodeZip(
|
||
className, className.length,
|
||
version, version.length,
|
||
planeName, planeName.length,
|
||
dstPath, dstPath.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `压缩模型代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return {
|
||
success: true,
|
||
dstPath: dstPath.toString('utf8').replace(/\0/g, ''),
|
||
message: '压缩模型代码成功'
|
||
};
|
||
} catch (error) {
|
||
return {
|
||
success: false,
|
||
message: `压缩模型代码失败: ${error.message}`
|
||
};
|
||
}
|
||
}
|
||
|
||
// 解压模型代码
|
||
function modelCodeUnzip(className, version, planeName, srcPath) {
|
||
if (!modelGenLib) {
|
||
return '模型生成库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = modelGenLib.XNModelCodeUnzip(
|
||
className, className.length,
|
||
version, version.length,
|
||
planeName, planeName.length,
|
||
srcPath, srcPath.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `解压模型代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '解压模型代码成功';
|
||
} catch (error) {
|
||
return `解压模型代码失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 编译模型代码
|
||
function modelCodeCompile(className, version, planeName) {
|
||
if (!modelGenLib) {
|
||
return '模型生成库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = modelGenLib.XNModelCodeCompile(
|
||
className, className.length,
|
||
version, version.length,
|
||
planeName, planeName.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `编译模型代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '编译模型代码成功';
|
||
} catch (error) {
|
||
return `编译模型代码失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// ========== XNServiceGenServer 封装函数 ==========
|
||
|
||
// 生成服务代码
|
||
function serviceCodeGen(className, version) {
|
||
if (!serviceGenLib) {
|
||
return '服务生成库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = serviceGenLib.XNServiceCodeGen(
|
||
className, className.length,
|
||
version, version.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `生成服务代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '生成服务代码成功';
|
||
} catch (error) {
|
||
return `生成服务代码失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 压缩服务代码
|
||
function serviceCodeZip(className, version) {
|
||
if (!serviceGenLib) {
|
||
return '服务生成库未加载';
|
||
}
|
||
try {
|
||
const dstPath = Buffer.alloc(1024);
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = serviceGenLib.XNServiceCodeZip(
|
||
className, className.length,
|
||
version, version.length,
|
||
dstPath, dstPath.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `压缩服务代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return {
|
||
success: true,
|
||
dstPath: dstPath.toString('utf8').replace(/\0/g, ''),
|
||
message: '压缩服务代码成功'
|
||
};
|
||
} catch (error) {
|
||
return {
|
||
success: false,
|
||
message: `压缩服务代码失败: ${error.message}`
|
||
};
|
||
}
|
||
}
|
||
|
||
// 解压服务代码
|
||
function serviceCodeUnzip(className, version, srcPath) {
|
||
if (!serviceGenLib) {
|
||
return '服务生成库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = serviceGenLib.XNServiceCodeUnzip(
|
||
className, className.length,
|
||
version, version.length,
|
||
srcPath, srcPath.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `解压服务代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '解压服务代码成功';
|
||
} catch (error) {
|
||
return `解压服务代码失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
// 编译服务代码
|
||
function serviceCodeCompile(className, version) {
|
||
if (!serviceGenLib) {
|
||
return '服务生成库未加载';
|
||
}
|
||
try {
|
||
const errorMsg = Buffer.alloc(1024);
|
||
const result = serviceGenLib.XNServiceCodeCompile(
|
||
className, className.length,
|
||
version, version.length,
|
||
errorMsg, errorMsg.length
|
||
);
|
||
if (result !== 0) {
|
||
return `编译服务代码失败: ${errorMsg.toString('utf8').replace(/\0/g, '')}`;
|
||
}
|
||
return '编译服务代码成功';
|
||
} catch (error) {
|
||
return `编译服务代码失败: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
module.exports = {
|
||
loginLib,
|
||
monitorLib,
|
||
interfaceGenLib,
|
||
modelGenLib,
|
||
serviceGenLib,
|
||
performCleanup,
|
||
stringToBuffer,
|
||
initializeMonitor,
|
||
cleanupMonitor,
|
||
startMonitorSystemInfo,
|
||
getSystemInfo,
|
||
getAllThreadInfo,
|
||
stopMonitorSystemInfo,
|
||
startMonitorModelInfo,
|
||
getModelInfo,
|
||
stopMonitorModelInfo,
|
||
initializeEngineControl,
|
||
pauseEngine,
|
||
resumeEngine,
|
||
stopEngine,
|
||
startDataMonitor,
|
||
stopDataMonitor,
|
||
getDataMonitorInfo,
|
||
injectDataInterface,
|
||
startInjectContinuous,
|
||
stopInjectContinuous,
|
||
injectDataInterfaceFromCsv,
|
||
stopCsvDataInject,
|
||
getCsvDataInjectStatus,
|
||
startCollectData,
|
||
getCollectDataStatus,
|
||
stopCollectData,
|
||
resetPassword,
|
||
deleteUser,
|
||
updateUserIcon,
|
||
interfaceGenStep1InitParams,
|
||
interfaceGenStep2GetInterfaceData,
|
||
interfaceGenStep3CreateConfigDir,
|
||
interfaceGenStep4GenerateIDL,
|
||
interfaceGenStep5GenerateFastDDS,
|
||
interfaceGenStep6GenerateDDSInterface,
|
||
interfaceGenStep7GenerateCMakeLists,
|
||
interfaceGenStep8BuildAndInstall,
|
||
interfaceGenStep9SudoLdconfig,
|
||
modelCodeGen,
|
||
modelCodeZip,
|
||
modelCodeUnzip,
|
||
modelCodeCompile,
|
||
serviceCodeGen,
|
||
serviceCodeZip,
|
||
serviceCodeUnzip,
|
||
serviceCodeCompile
|
||
}; |