From 3c33ce0779e18782d148f9314313041c14cca5a5 Mon Sep 17 00:00:00 2001
From: chenluhua1980 <Chenluhua@qq.com>
Date: 星期四, 11 十二月 2025 10:22:58 +0800
Subject: [PATCH] 1.新增变量和编辑变量;

---
 SourceCode/Bond/Servo/HsmsPassive.cpp |  125 +++++++++++++++++++++++++++++++++--------
 1 files changed, 100 insertions(+), 25 deletions(-)

diff --git a/SourceCode/Bond/Servo/HsmsPassive.cpp b/SourceCode/Bond/Servo/HsmsPassive.cpp
index 3315b32..d6f97cb 100644
--- a/SourceCode/Bond/Servo/HsmsPassive.cpp
+++ b/SourceCode/Bond/Servo/HsmsPassive.cpp
@@ -8,6 +8,8 @@
 #include <time.h>  
 #include <stdlib.h>  
 #include <string.h>  
+#include <algorithm>
+#include <set>
 #include <regex>
 
 
@@ -373,6 +375,17 @@
 	return m_variabels;
 }
 
+unsigned int CHsmsPassive::getMaxVariableId() const
+{
+	unsigned int maxId = 0;
+	for (auto item : m_variabels) {
+		if (item && item->getVarialbleId() > maxId) {
+			maxId = item->getVarialbleId();
+		}
+	}
+	return maxId;
+}
+
 SERVO::CVariable* CHsmsPassive::getVariable(int variableId)
 {
 	for (auto item : m_variabels) {
@@ -453,7 +466,93 @@
 
 	if (filepath.empty()) return -2;
 
-	// 鍐欏洖鏂囦欢锛屼繚鎸佸師缂栫爜锛圲TF-8 鎴栨湰鍦扮紪鐮侊級
+	return writeVariablesToFile(filepath);
+}
+
+void CHsmsPassive::setVariableValue(const char* pszName, __int64 value)
+{
+	auto v = getVariable(pszName);
+	if (v != nullptr) {
+		v->setValue(value);
+	}
+}
+
+void CHsmsPassive::setVariableValue(const char* pszName, const char* value)
+{
+	auto v = getVariable(pszName);
+	if (v != nullptr) {
+		v->setValue(value);
+	}
+}
+
+void CHsmsPassive::setVariableValue(const char* pszName, std::vector<SERVO::CVariable>& vars)
+{
+	auto v = getVariable(pszName);
+	if (v != nullptr) {
+		v->setValue(vars);
+	}
+}
+
+static bool isValidFormat(const std::string& fmt)
+{
+	static const std::set<std::string> allow = { "U1","U2","I2","A20","A50","L" };
+	return allow.count(fmt) > 0;
+}
+
+int CHsmsPassive::addVariable(const char* pszName, const char* pszFormat, const char* pszRemark, int& outId)
+{
+	if (pszName == nullptr || pszFormat == nullptr) return -1;
+	std::string fmt = pszFormat;
+	std::transform(fmt.begin(), fmt.end(), fmt.begin(), ::toupper);
+	if (!isValidFormat(fmt)) return -2;
+
+	Lock();
+	int maxId = 0;
+	for (auto v : m_variabels) {
+		if (v != nullptr && v->getVarialbleId() > maxId) {
+			maxId = v->getVarialbleId();
+		}
+	}
+	outId = maxId + 1;
+
+	SERVO::CVariable* pNew = new SERVO::CVariable(std::to_string(outId).c_str(), pszName, fmt.c_str(), pszRemark ? pszRemark : "");
+	m_variabels.push_back(pNew);
+	auto filepath = m_strVariableFilepath;
+	Unlock();
+
+	if (filepath.empty()) return -3;
+	return writeVariablesToFile(filepath);
+}
+
+int CHsmsPassive::updateVariable(int variableId, const char* pszName, const char* pszFormat, const char* pszRemark)
+{
+	if (pszName == nullptr || pszFormat == nullptr) return -1;
+	std::string fmt = pszFormat;
+	std::transform(fmt.begin(), fmt.end(), fmt.begin(), ::toupper);
+	if (!isValidFormat(fmt)) return -2;
+
+	Lock();
+	auto it = std::find_if(m_variabels.begin(), m_variabels.end(), [=](SERVO::CVariable* v) {
+		return v != nullptr && v->getVarialbleId() == variableId;
+		});
+	if (it == m_variabels.end()) {
+		Unlock();
+		return -4;
+	}
+	(*it)->setName(pszName);
+	(*it)->setFormat(fmt.c_str());
+	(*it)->setRemark(pszRemark ? pszRemark : "");
+	auto filepath = m_strVariableFilepath;
+	Unlock();
+
+	if (filepath.empty()) return -3;
+	return writeVariablesToFile(filepath);
+}
+
+int CHsmsPassive::writeVariablesToFile(const std::string& filepath)
+{
+	if (filepath.empty()) return -3;
+
 	CFile file;
 	if (!file.Open(filepath.c_str(), CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone)) {
 		return -3;
@@ -497,30 +596,6 @@
 	file.Close();
 
 	return 0;
-}
-
-void CHsmsPassive::setVariableValue(const char* pszName, __int64 value)
-{
-	auto v = getVariable(pszName);
-	if (v != nullptr) {
-		v->setValue(value);
-	}
-}
-
-void CHsmsPassive::setVariableValue(const char* pszName, const char* value)
-{
-	auto v = getVariable(pszName);
-	if (v != nullptr) {
-		v->setValue(value);
-	}
-}
-
-void CHsmsPassive::setVariableValue(const char* pszName, std::vector<SERVO::CVariable>& vars)
-{
-	auto v = getVariable(pszName);
-	if (v != nullptr) {
-		v->setValue(vars);
-	}
 }
 
 int CHsmsPassive::loadReports(const char* pszFilepath)

--
Gitblit v1.9.3