chenluhua1980
2025-12-11 aadeadede11fabacebc5378ac0548794f5f49a74
SourceCode/Bond/Servo/HsmsPassive.cpp
@@ -8,6 +8,8 @@
#include <time.h>  
#include <stdlib.h>  
#include <string.h>  
#include <algorithm>
#include <set>
#include <regex>
@@ -228,6 +230,9 @@
int CHsmsPassive::loadVarialbles(const char* pszFilepath)
{
   m_strVariableFilepath = pszFilepath;
   m_bVariableUtf8 = false;
   m_bVariableUtf8Bom = false;
   // 先读原始字节,后续再按 UTF-8/BOM 或本地编码转换
   CFile file;
   if (!file.Open(pszFilepath, CFile::modeRead | CFile::shareDenyNone)) {
@@ -251,6 +256,8 @@
   // UTF-8 BOM
   if (nLen >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
      offset = 3;
      m_bVariableUtf8 = true;
      m_bVariableUtf8Bom = true;
   }
   // UTF-16 LE BOM
@@ -281,6 +288,7 @@
         MultiByteToWideChar(CP_UTF8, 0, buffer.data() + off,
            static_cast<int>(buffer.size() - off), temp.data(), need);
         content = temp.c_str();
         m_bVariableUtf8 = true;
         return true;
      };
@@ -367,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) {
@@ -397,6 +416,59 @@
   m_variabels.clear();
}
CStringA WideToUtf8(const CStringW& ws)
{
   int need = WideCharToMultiByte(CP_UTF8, 0, ws, -1, nullptr, 0, nullptr, nullptr);
   if (need <= 0) return "";
   CStringA out;
   LPSTR buf = out.GetBufferSetLength(need - 1);
   WideCharToMultiByte(CP_UTF8, 0, ws, -1, buf, need, nullptr, nullptr);
   out.ReleaseBuffer();
   return out;
}
CStringA WideToAnsi(const CStringW& ws)
{
   int need = WideCharToMultiByte(CP_ACP, 0, ws, -1, nullptr, 0, nullptr, nullptr);
   if (need <= 0) return "";
   CStringA out;
   LPSTR buf = out.GetBufferSetLength(need - 1);
   WideCharToMultiByte(CP_ACP, 0, ws, -1, buf, need, nullptr, nullptr);
   out.ReleaseBuffer();
   return out;
}
static CStringA AnsiToUtf8(const std::string& s)
{
   int wlen = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, nullptr, 0);
   if (wlen <= 0) return "";
   CStringW ws;
   LPWSTR wbuf = ws.GetBufferSetLength(wlen - 1);
   MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, wbuf, wlen);
   ws.ReleaseBuffer();
   return WideToUtf8(ws);
}
int CHsmsPassive::deleteVariable(int variableId)
{
   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 -1;
   }
   delete *it;
   m_variabels.erase(it);
   auto filepath = m_strVariableFilepath;
   Unlock();
   if (filepath.empty()) return -2;
   return writeVariablesToFile(filepath);
}
void CHsmsPassive::setVariableValue(const char* pszName, __int64 value)
{
   auto v = getVariable(pszName);
@@ -421,8 +493,116 @@
   }
}
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;
   }
   // header
   const std::string headerAnsi = "SVID,SV Name,SV Format,SV Remark\r\n";
   if (m_bVariableUtf8) {
      if (m_bVariableUtf8Bom) {
         const BYTE bom[3] = { 0xEF, 0xBB, 0xBF };
         file.Write(bom, 3);
      }
      CStringA header = AnsiToUtf8(headerAnsi);
      file.Write(header.GetString(), header.GetLength());
   }
   else {
      file.Write(headerAnsi.data(), (UINT)headerAnsi.size());
   }
   for (auto v : m_variabels) {
      if (v == nullptr) continue;
      std::string lineAnsi;
      lineAnsi.reserve(256);
      lineAnsi += std::to_string(v->getVarialbleId());
      lineAnsi.push_back(',');
      lineAnsi += v->getName();
      lineAnsi.push_back(',');
      lineAnsi += SERVO::CVariable::formatToString(v->getFormat());
      lineAnsi.push_back(',');
      lineAnsi += v->getRemark();
      lineAnsi.append("\r\n");
      if (m_bVariableUtf8) {
         CStringA outLine = AnsiToUtf8(lineAnsi);
         file.Write(outLine.GetString(), outLine.GetLength());
      }
      else {
         file.Write(lineAnsi.data(), (UINT)lineAnsi.size());
      }
   }
   file.Close();
   return 0;
}
int CHsmsPassive::loadReports(const char* pszFilepath)
{
   m_strReportFilepath = pszFilepath;
   m_bReportUtf8 = false;
   m_bReportUtf8Bom = false;
   // 兼容 UTF-8/BOM 与本地编码读取
   CFile file;
   if (!file.Open(pszFilepath, CFile::modeRead | CFile::shareDenyNone)) {
@@ -446,6 +626,8 @@
   // UTF-8 BOM
   if (nLen >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
      offset = 3;
      m_bReportUtf8 = true;
      m_bReportUtf8Bom = true;
   }
   // UTF-16 LE BOM
@@ -475,6 +657,7 @@
         MultiByteToWideChar(CP_UTF8, 0, buffer.data() + off,
            static_cast<int>(buffer.size() - off), temp.data(), need);
         content = temp.c_str();
         m_bReportUtf8 = true;
         return true;
      };
@@ -551,6 +734,17 @@
   return m_reports;
}
unsigned int CHsmsPassive::getMaxReportId() const
{
   unsigned int maxId = 0;
   for (auto item : m_reports) {
      if (item && item->getReportId() > maxId) {
         maxId = item->getReportId();
      }
   }
   return maxId;
}
SERVO::CReport* CHsmsPassive::getReport(int rptid)
{
   for (auto item : m_reports) {
@@ -575,6 +769,49 @@
   return false;
}
int CHsmsPassive::deleteReport(int rptid)
{
   if (!removeReport(rptid)) {
      return -1;
   }
   return writeReportsToFile(m_strReportFilepath);
}
int CHsmsPassive::addReport(int rptid, const std::vector<unsigned int>& vids)
{
   if (getReport(rptid) != nullptr) {
      return -1;
   }
   SERVO::CReport* pReport = new SERVO::CReport(rptid, vids);
   for (auto vid : vids) {
      SERVO::CVariable* pVariable = getVariable((int)vid);
      if (pVariable != nullptr) {
         pReport->addVariable(pVariable);
      }
   }
   m_reports.push_back(pReport);
   return writeReportsToFile(m_strReportFilepath);
}
int CHsmsPassive::updateReport(int rptid, const std::vector<unsigned int>& vids)
{
   for (auto iter = m_reports.begin(); iter != m_reports.end(); ++iter) {
      if ((*iter)->getReportId() == rptid) {
         delete (*iter);
         SERVO::CReport* pReport = new SERVO::CReport(rptid, vids);
         for (auto vid : vids) {
            SERVO::CVariable* pVariable = getVariable((int)vid);
            if (pVariable != nullptr) {
               pReport->addVariable(pVariable);
            }
         }
         *iter = pReport;
         return writeReportsToFile(m_strReportFilepath);
      }
   }
   return -1;
}
void CHsmsPassive::clearAllReport()
{
   for (auto item : m_reports) {
@@ -583,6 +820,59 @@
   m_reports.clear();
}
int CHsmsPassive::writeReportsToFile(const std::string& filepath)
{
   if (filepath.empty()) return -1;
   CFile file;
   if (!file.Open(filepath.c_str(), CFile::modeCreate | CFile::modeWrite)) {
      return -1;
   }
   if (m_bReportUtf8 && m_bReportUtf8Bom) {
      const BYTE bom[3] = { 0xEF, 0xBB, 0xBF };
      file.Write(bom, 3);
   }
   // header
   const std::string headerAnsi = "RPTID,(VID1,VID2,...)\r\n";
   if (m_bReportUtf8) {
      CStringA header = AnsiToUtf8(headerAnsi);
      file.Write(header.GetString(), header.GetLength());
   }
   else {
      file.Write(headerAnsi.data(), (UINT)headerAnsi.size());
   }
   for (auto rpt : m_reports) {
      if (rpt == nullptr) continue;
      std::string line;
      line.reserve(64);
      line += std::to_string(rpt->getReportId());
      line += ",(";
      const auto& vids = rpt->getVids();
      for (size_t i = 0; i < vids.size(); ++i) {
         line += std::to_string(vids[i]);
         if (i + 1 < vids.size()) {
            line.push_back(',');
         }
      }
      line += ")\r\n";
      if (m_bReportUtf8) {
         CStringA out = AnsiToUtf8(line);
         file.Write(out.GetString(), out.GetLength());
      }
      else {
         file.Write(line.data(), (UINT)line.size());
      }
   }
   file.Close();
   return 0;
}
int CHsmsPassive::loadCollectionEvents(const char* pszFilepath)
{
   CFile file;