#include "stdafx.h"
|
#include "CRecipeList.h"
|
#include "Common.h"
|
#include "ToolUnits.h"
|
#include <fstream>
|
|
|
namespace SERVO {
|
CRecipeList::CRecipeList()
|
{
|
m_nToatlGroupCount = 0;
|
m_nCurrentGroupCount = 0;
|
}
|
|
CRecipeList::CRecipeList(int unitNo)
|
{
|
m_nUnitNo = unitNo;
|
}
|
|
CRecipeList::~CRecipeList()
|
{
|
|
}
|
|
int CRecipeList::getUnitNo()
|
{
|
return m_nUnitNo;
|
}
|
|
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) {
|
reset();
|
return MRLRC_GROUP_COUNT_NG;
|
}
|
if (currentGroup == 0) {
|
reset();
|
}
|
if (m_nCurrentGroupCount + 1 > currentGroup) {
|
return MRLRC_DUPLICATION_GROUP_COUNT_NG;
|
}
|
if (m_nCurrentGroupCount + 1 < currentGroup) {
|
return ORDER_BY_GROUP_COUNT_NG;
|
}
|
m_nCurrentGroupCount++;
|
|
for (int i = 0; i < size; i += 4) {
|
int index = CToolUnits::toInt16(&pszData[i]);
|
short id = CToolUnits::toInt16(&pszData[i + 2]);
|
if (index != 0 && id != 0) {
|
addRecipe(index, id);
|
}
|
}
|
|
if (m_nCurrentGroupCount == m_nToatlGroupCount) {
|
m_nToatlGroupCount = 0;
|
m_nCurrentGroupCount = 0;
|
return MRLRC_CURRENT_RECIPE_COMPLETE;
|
}
|
|
|
return MRLRC_CONTINUE;
|
}
|
|
int CRecipeList::addRecipe(int index, short id)
|
{
|
for (auto item : m_ids) {
|
if (item.second == id) {
|
return -1;
|
}
|
}
|
|
m_ids[index] = id;
|
return (int)m_ids.size();
|
}
|
|
std::map<int, short>& CRecipeList::getIds()
|
{
|
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*>(¶msSize), 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*>(¶msSize), 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;
|
}
|
}
|