chenluhua1980
2026-01-06 4d9d8d22e3666076988c30afb4e7c6fe365c19aa
SourceCode/Bond/Servo/CRecipeList.cpp
@@ -2,6 +2,7 @@
#include "CRecipeList.h"
#include "Common.h"
#include "ToolUnits.h"
#include <fstream>
namespace SERVO {
@@ -26,7 +27,7 @@
      return m_nUnitNo;
   }
   int CRecipeList::addRecipePacket(int totalGroup, int currentGroup, const char* pszData, size_t size)
   int CRecipeList::addRecipePacket(int totalCount, int totalGroup, int currentGroup, const char* pszData, size_t size)
   {
      if (m_nToatlGroupCount == 0) m_nToatlGroupCount = totalGroup;
      if (m_nToatlGroupCount != totalGroup) {
@@ -47,10 +48,14 @@
      for (int i = 0; i < size; i += 4) {
         int index = CToolUnits::toInt16(&pszData[i]);
         short id = CToolUnits::toInt16(&pszData[i + 2]);
         addRecipe(index, id);
         if (index != 0 && id != 0) {
            addRecipe(index, id);
         }
      }
      if (m_nCurrentGroupCount == m_nToatlGroupCount) {
         m_nToatlGroupCount = 0;
         m_nCurrentGroupCount = 0;
         return MRLRC_CURRENT_RECIPE_COMPLETE;
      }
@@ -67,7 +72,7 @@
      }
      m_ids[index] = id;
      return 0;
      return (int)m_ids.size();
   }
   std::map<int, short>& CRecipeList::getIds()
@@ -75,10 +80,136 @@
      return m_ids;
   }
   std::unordered_map<short, std::vector<uint8_t>>& CRecipeList::getParamsRawData()
   {
      return m_paramsRawData;
   }
   void CRecipeList::reset()
   {
      m_nToatlGroupCount = 0;
      m_nCurrentGroupCount = 0;
      m_ids.clear();
      m_paramsRawData.clear();
   }
   void CRecipeList::reset2()
   {
      m_nToatlGroupCount = 0;
      m_nCurrentGroupCount = 0;
   }
   int CRecipeList::addParamsPacket(int totalCount, int totalGroup, int currentGroup,
      short unitId, short recipeId,
      const char* pszData, size_t size)
   {
      if (m_nToatlGroupCount == 0) m_nToatlGroupCount = totalGroup;
      if (m_nToatlGroupCount != totalGroup) {
         reset2();
         return MRLRC_GROUP_COUNT_NG;
      }
      if (currentGroup == 0) {
         reset2();
      }
      if (m_nCurrentGroupCount + 1 > currentGroup) {
         return MRLRC_DUPLICATION_GROUP_COUNT_NG;
      }
      if (m_nCurrentGroupCount + 1 < currentGroup) {
         return ORDER_BY_GROUP_COUNT_NG;
      }
      m_nCurrentGroupCount++;
      m_paramsRawData[recipeId].insert(m_paramsRawData[recipeId].end(), (uint8_t*)(pszData), (uint8_t*)(pszData) + size);
      if (m_nCurrentGroupCount == m_nToatlGroupCount) {
         // 解释数据就交给应用层吧
         reset2();
         return MRLRC_CURRENT_RECIPE_COMPLETE;
      }
      return MRLRC_CONTINUE;
   }
   // 序列化
   bool CRecipeList::serialize(const std::string& filename) const
   {
      std::ofstream ofs(filename, std::ios::binary);
      if (!ofs) return false;
      // 写基本成员
      ofs.write(reinterpret_cast<const char*>(&m_nUnitNo), sizeof(m_nUnitNo));
      ofs.write(reinterpret_cast<const char*>(&m_nToatlGroupCount), sizeof(m_nToatlGroupCount));
      ofs.write(reinterpret_cast<const char*>(&m_nCurrentGroupCount), sizeof(m_nCurrentGroupCount));
      // д m_ids
      size_t idsSize = m_ids.size();
      ofs.write(reinterpret_cast<const char*>(&idsSize), sizeof(idsSize));
      for (auto& kv : m_ids) {
         ofs.write(reinterpret_cast<const char*>(&kv.first), sizeof(kv.first));
         ofs.write(reinterpret_cast<const char*>(&kv.second), sizeof(kv.second));
      }
      // д m_paramsRawData
      size_t paramsSize = m_paramsRawData.size();
      ofs.write(reinterpret_cast<const char*>(&paramsSize), sizeof(paramsSize));
      for (auto& kv : m_paramsRawData) {
         // д key
         ofs.write(reinterpret_cast<const char*>(&kv.first), sizeof(kv.first));
         // 写 vector 大小
         size_t vecSize = kv.second.size();
         ofs.write(reinterpret_cast<const char*>(&vecSize), sizeof(vecSize));
         // 写 vector 内容
         if (!kv.second.empty()) {
            ofs.write(reinterpret_cast<const char*>(kv.second.data()), vecSize);
         }
      }
      return true;
   }
   // 反序列化
   bool CRecipeList::deserialize(const std::string& filename)
   {
      std::ifstream ifs(filename, std::ios::binary);
      if (!ifs) return false;
      reset(); // 清空旧数据
      // 读基本成员
      ifs.read(reinterpret_cast<char*>(&m_nUnitNo), sizeof(m_nUnitNo));
      ifs.read(reinterpret_cast<char*>(&m_nToatlGroupCount), sizeof(m_nToatlGroupCount));
      ifs.read(reinterpret_cast<char*>(&m_nCurrentGroupCount), sizeof(m_nCurrentGroupCount));
      // 读 m_ids
      size_t idsSize = 0;
      ifs.read(reinterpret_cast<char*>(&idsSize), sizeof(idsSize));
      for (size_t i = 0; i < idsSize; ++i) {
         int key;
         short value;
         ifs.read(reinterpret_cast<char*>(&key), sizeof(key));
         ifs.read(reinterpret_cast<char*>(&value), sizeof(value));
         m_ids[key] = value;
      }
      // 读 m_paramsRawData
      size_t paramsSize = 0;
      ifs.read(reinterpret_cast<char*>(&paramsSize), sizeof(paramsSize));
      for (size_t i = 0; i < paramsSize; ++i) {
         short key;
         size_t vecSize = 0;
         ifs.read(reinterpret_cast<char*>(&key), sizeof(key));
         ifs.read(reinterpret_cast<char*>(&vecSize), sizeof(vecSize));
         std::vector<uint8_t> buffer(vecSize);
         if (vecSize > 0) {
            ifs.read(reinterpret_cast<char*>(buffer.data()), vecSize);
         }
         m_paramsRawData[key] = std::move(buffer);
      }
      return true;
   }
}