#include "BoltModeStructData.h" #include #include #include #include "Utils.h" BoltModeStructData::BoltModeStructData() : m_data(nullptr) { } BoltModeStructData::BoltModeStructData(BoltModeParam_t *data) : m_data(nullptr) { setData(data); } BoltModeStructData::~BoltModeStructData() { if(m_data != nullptr) { for(int i=0; i<2; i++) { m_data->format[i].diameterList.clear(); m_data->format[i].typeList.clear(); m_data->format[i].torqueList.clear(); } delete m_data; m_data = nullptr; } } Data* BoltModeStructData::clone() { Data* data = new BoltModeStructData(); data->copy(this); return data; } void BoltModeStructData::copy(Data* data) { BoltModeStructData *dt = dynamic_cast(data); if(dt->getData() == nullptr) { return; } if(m_data == nullptr) { m_data = new BoltModeParam_t(); } m_data->name = dt->getData()->name; for(int i=0; i<2; i++) { m_data->format[i].diameterList.clear(); m_data->format[i].typeList.clear(); m_data->format[i].torqueList.clear(); //m_data->format = dt->getData()->format; std::copy(dt->getData()->format[i].diameterList.begin(), dt->getData()->format[i].diameterList.end(), std::back_inserter(m_data->format[i].diameterList)); std::copy(dt->getData()->format[i].typeList.begin(), dt->getData()->format[i].typeList.end(), std::back_inserter(m_data->format[i].typeList)); std::copy(dt->getData()->format[i].torqueList.begin(), dt->getData()->format[i].torqueList.end(), std::back_inserter(m_data->format[i].torqueList)); } } void BoltModeStructData::copy(Data& data) { BoltModeStructData *dt = dynamic_cast(&data); if(dt->getData() == nullptr) { return; } if(m_data == nullptr) { m_data = new BoltModeParam_t(); } m_data->name = dt->getData()->name; for(int i=0; i<2; i++) { m_data->format[i].diameterList.clear(); m_data->format[i].typeList.clear(); m_data->format[i].torqueList.clear(); //m_data->format = dt->getData()->format; std::copy(dt->getData()->format[i].diameterList.begin(), dt->getData()->format[i].diameterList.end(), std::back_inserter(m_data->format[i].diameterList)); std::copy(dt->getData()->format[i].typeList.begin(), dt->getData()->format[i].typeList.end(), std::back_inserter(m_data->format[i].typeList)); std::copy(dt->getData()->format[i].torqueList.begin(), dt->getData()->format[i].torqueList.end(), std::back_inserter(m_data->format[i].torqueList)); } } int BoltModeStructData::length() { int length = 0; length += 10; // 扳手名称 for(int i=0; i<2; i++) { length += 1; // 制式 length += 1; // 直径数量 length += 10 * m_data->format[i].diameterList.size(); // 直径名称 length += 1; // 等级数量 length += 10 * m_data->format[i].typeList.size(); // 等级名称 length += 4 * m_data->format[i].diameterList.size() * m_data->format[i].typeList.size(); // 扭矩值 } return length; } std::string BoltModeStructData::toStr() { std::stringstream ss; ss << "name : " << m_data->name << std::endl; for(int i=0; i<2; i++) { //ss << "format : " << ((m_data->format == 0) ? (std::string("公制")) : (std::string("英制"))) << std::endl; ss << "format : " << ((i == 0) ? std::string("公制") : std::string("英制")) << std::endl; ss << "diameter : "; std::list::iterator diameterIter = m_data->format[i].diameterList.begin(); while(diameterIter != m_data->format[i].diameterList.end()) { ss << *diameterIter << ", "; diameterIter++; } ss << std::endl; std::list::iterator typeIter = m_data->format[i].typeList.begin(); while(typeIter != m_data->format[i].typeList.end()) { ss << *typeIter << ", "; typeIter++; } ss << std::endl; std::list::iterator torqueIter = m_data->format[i].torqueList.begin(); while(torqueIter != m_data->format[i].torqueList.end()) { ss << *torqueIter << ", "; torqueIter++; } ss << std::endl; } return ss.str(); } // 1. 0 共 1 字节: 制式 // 2. 1-10 共 10 字节: 扳手名称 // 3. 11 共 1 字节: 直径数量 // 4. 12-21 共10字节:直径名称 // 5. 重复4 // 6. 22 共 1 字节:等级数量 // 7. 23-32 共10字节:等级名称 // 8. 4 共4字节: 扭矩 // 9. 重复 8 std::string BoltModeStructData::toByteStr() { std::string s; // 名称 char name[11]; memset(name, 0, sizeof(name)); memcpy(name, m_data->name.c_str(), m_data->name.length() <= 10 ? m_data->name.length() : 10); name[10] = '\0'; s.append(name, 10); for(unsigned char i=0; i<2; i++) { // 制式 //s.append(Utils::charToByteStr(m_data->format)); s.append(Utils::charToByteStr(i)); // 螺栓直径数量 unsigned char diameterCount = m_data->format[i].diameterList.size(); s.append(Utils::charToByteStr(diameterCount)); std::list::iterator diameterIter = m_data->format[i].diameterList.begin(); while(diameterIter != m_data->format[i].diameterList.end()) { char diameter[11]; memset(diameter, 0, sizeof(diameter)); memcpy(diameter, (*diameterIter).c_str(), (*diameterIter).length() <= 10 ? (*diameterIter).length() : 10); diameter[10] = '\0'; s.append(diameter, 10); diameterIter++; } // 螺栓等级数量 unsigned char typeCount = m_data->format[i].typeList.size(); s.append(Utils::charToByteStr(typeCount)); std::list::iterator typeIter = m_data->format[i].typeList.begin(); while(typeIter != m_data->format[i].typeList.end()) { char type[11]; memset(type, 0, sizeof(type)); memcpy(type, (*typeIter).c_str(), (*typeIter).length() <= 10 ? (*typeIter).length() : 10); type[10] = '\0'; s.append(type, 10); typeIter++; } // 扭矩 // 检查扭矩数量是否正确,如果缺少,后续补0,如果有多,调整数量 int torqueCount = m_data->format[i].torqueList.size(); if(torqueCount < diameterCount * typeCount) { for(int i=torqueCount; iformat[i].torqueList.push_back(0); } } torqueCount = diameterCount * typeCount; //s.append(Utils::charToByteStr(torqueCount)); -- 协议中无扭矩数量 std::list::iterator torqueIter = m_data->format[i].torqueList.begin(); int nCount = 0; while(torqueIter != m_data->format[i].torqueList.end()) { int torque = *torqueIter; s.append(Utils::intToByteStr(torque)); nCount++; if(nCount >= torqueCount) { break; } torqueIter++; } } return s; } std::string BoltModeStructData::toHexStr() { std::string s = toByteStr(); return Utils::strToHexStr(s); } bool BoltModeStructData::fromByteStr(std::string str) { if(str.length() < 13) // 名称10,制式1,直径数量1,型号数量1,。。。制式1,直径数量1,型号数量1 { return false; } unsigned int pos = 0; // name m_data->name = str.substr(pos, 10); pos += 10; for(int i=0; i<2; i++) { // format //m_data->format = str.at(pos); unsigned char format = str.at(pos); pos++; // diameter unsigned char diameterCount = str.at(pos); pos++; for(int i = 0; iformat[i].diameterList.push_back(diameter); } // type unsigned char typeCount = str.at(pos); pos++; for(int i = 0; iformat[i].typeList.push_back(type); } // torque for(int i=0; iformat[i].torqueList.push_back(Utils::byteStrToInt(torque)); } } return true; } void BoltModeStructData::setData(BoltModeParam_t* data) { if(m_data != nullptr) { delete m_data; m_data = nullptr; } m_data = data; } void BoltModeStructData::copyData(BoltModeParam_t* data) { if(m_data == nullptr) { m_data = new BoltModeParam_t(); } m_data->name = data->name; for(int i=0; i<2; i++) { m_data->format[i].diameterList.clear(); m_data->format[i].typeList.clear(); m_data->format[i].torqueList.clear(); //m_data->format = data->format; std::copy(data->format[i].diameterList.begin(), data->format[i].diameterList.end(), std::back_inserter(m_data->format[i].diameterList)); std::copy(data->format[i].typeList.begin(), data->format[i].typeList.end(), std::back_inserter(m_data->format[i].typeList)); std::copy(data->format[i].torqueList.begin(), data->format[i].torqueList.end(), std::back_inserter(m_data->format[i].torqueList)); } }