You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.6 KiB
C++

1 year ago
#include "SensorModeStructData.h"
#include <sstream>
#include <iomanip>
#include <ctime>
#include "Utils.h"
SensorModeStructData::SensorModeStructData()
: m_prefixLength(2) // 前导长度此处为表示数量的字节数2
{
}
SensorModeStructData::SensorModeStructData(SensorModeParam_t *data)
: m_prefixLength(2) // 前导长度此处为表示数量的字节数2
{
setData(*data);
// m_data.type = data->type;
// m_data.datas.clear();
// //m_data.datas.insert(data->datas.begin(), data->datas.end()); // std::map
// std::copy(data->datas.begin(), data->datas.end(), std::back_inserter(m_data.datas));
}
Data* SensorModeStructData::clone()
{
Data* data = new SensorModeStructData(&m_data);
return data;
}
void SensorModeStructData::copy(Data* data)
{
SensorModeStructData *dt = dynamic_cast<SensorModeStructData *>(data);
m_data.datas.clear();
//m_data.datas.insert(dt->getData().datas.begin(), dt->getData().datas.end()); // std::map
std::copy(dt->getData().datas.begin(), dt->getData().datas.end(), std::back_inserter(m_data.datas));
}
void SensorModeStructData::copy(Data& data)
{
SensorModeStructData *dt = dynamic_cast<SensorModeStructData *>(&data);
m_data.datas.clear();
//m_data.datas.insert(dt->getData().datas.begin(), dt->getData().datas.end()); // std::map
std::copy(dt->getData().datas.begin(), dt->getData().datas.end(), std::back_inserter(m_data.datas));
}
int SensorModeStructData::length()
{
return (m_prefixLength + (10 + 4) * m_data.datas.size());
}
std::string SensorModeStructData::toStr()
{
std::stringstream ss;
std::list<SensorModeParam>::iterator iter = m_data.datas.begin();
while(iter != m_data.datas.end())
{
ss << "name:" << (*iter).getName() << ", value:" << (*iter).getValue() << std::endl;
iter++;
}
return ss.str();
}
// 1. 第0-1个字节/共2字节 数量
// 2. 第2-11字节/共10字节 扳手名称
// 3. 第12-15字节/共4字节 限定扭矩值
// 重复2和3
std::string SensorModeStructData::toByteStr()
{
std::string s;
unsigned short int nCount = m_data.datas.size();
s.append(Utils::shortToByteStr(nCount));
std::list<SensorModeParam>::iterator iter = m_data.datas.begin();
while(iter != m_data.datas.end())
{
char name[11];
unsigned int value;
memset(name, 0, sizeof(name));
memcpy(name, (*iter).getName().c_str(), (*iter).getName().length() <= 10 ? (*iter).getName().length() : 10);
name[10] = '\0';
value = (*iter).getValue();
s.append(name, 10);
s.append(Utils::intToByteStr(value));
iter++;
}
return s;
}
std::string SensorModeStructData::toHexStr()
{
std::string s = toByteStr();
return Utils::strToHexStr(s);
}
bool SensorModeStructData::fromByteStr(std::string str)
{
if(str.length() < m_prefixLength)
{
return false;
}
std::string s;
s = str.substr(0, 2);
unsigned short int nCount = Utils::byteStrToShort(s);
if(str.length() < (m_prefixLength + (10 + 4) * nCount))
{
return false;
}
for(unsigned short int i=0; i<nCount; i++)
{
std::string name = str.substr(m_prefixLength + (10 + 4) * i, 10);
std::string strValue = str.substr(m_prefixLength + (10 + 4) * i + 10, 4);
unsigned int value = Utils::byteStrToInt(strValue);
SensorModeParam param(name, value);
m_data.datas.push_back(param);
}
return true;
}
void SensorModeStructData::setData(SensorModeParam_t& data)
{
m_data.datas.clear();
std::copy(data.datas.begin(), data.datas.end(), std::back_inserter(m_data.datas));
}