93 lines
2.6 KiB
C++
93 lines
2.6 KiB
C++
#pragma once
|
|
#include <string>
|
|
#include <map>
|
|
#include <list>
|
|
#include <shared_mutex>
|
|
#include "../Communication/BaseData.h"
|
|
#include "../DataManage/ClientInfo.h"
|
|
|
|
//size_t ptrSize = sizeof(nullptr); //指针大小
|
|
//void* startPtr = &m_DoluyoChillerStat.m_startFlag + 1;
|
|
//size_t count = ((size_t)&m_DoluyoChillerStat.m_endFlag - (size_t)startPtr) / ptrSize;
|
|
//InsertMp(startPtr, count);
|
|
|
|
class Base {
|
|
protected:
|
|
void InsertMp(void* startPtr, size_t count = 1, const std::string& suff = "") {
|
|
size_t ptrSize = sizeof(nullptr); //指针大小
|
|
for (size_t i = 0; i < count; ++i) {
|
|
BaseData* bd = *((BaseData**)((char*)startPtr + ptrSize * i));
|
|
std::string key = bd->GetCode() + suff;
|
|
if (m_baseMp.find(key) != m_baseMp.end()) {
|
|
printf("%s is repeated...\n", key.data());
|
|
}
|
|
else { m_baseMp.insert(make_pair(key, bd)); }
|
|
}
|
|
}
|
|
public:
|
|
Base() {}
|
|
virtual ~Base() {
|
|
std::unique_lock<std::shared_mutex> lock(m_mtx);
|
|
auto item = m_baseMp.begin();
|
|
while (item != m_baseMp.end()) {
|
|
delete item->second;
|
|
++item;
|
|
}
|
|
m_baseMp.clear();
|
|
}
|
|
|
|
virtual void SendToClients(WRITETYPE type, const std::string& addKey = "") {
|
|
std::list<Item> its;
|
|
{
|
|
std::shared_lock<std::shared_mutex> lock(m_mtx); //读锁
|
|
auto baseItem = m_baseMp.begin();
|
|
while (baseItem != m_baseMp.end()) {
|
|
std::string tempKey(baseItem->first + addKey);
|
|
its.emplace_back(Item{ tempKey,baseItem->second->GetValueStr(),baseItem->second->GetDataType() });
|
|
++baseItem;
|
|
}
|
|
}
|
|
ClientWrapper::Instance()->PushAllClient(WriteData(type, its));
|
|
|
|
}
|
|
|
|
void Update(const ReadData& rd, WRITETYPE type) {
|
|
|
|
if (m_baseMp.find(rd.nameKey) != m_baseMp.end()) {
|
|
std::unique_lock<std::shared_mutex> lock(m_mtx);
|
|
if (rd.valueType == iBOOL) {
|
|
m_baseMp[rd.nameKey]->SetValue((bool)stoi(rd.strValue));
|
|
}
|
|
else if (rd.valueType == iINT) {
|
|
m_baseMp[rd.nameKey]->SetValue(stoi(rd.strValue));
|
|
}
|
|
else if (rd.valueType == iUINT) {
|
|
m_baseMp[rd.nameKey]->SetValue((unsigned int)stoi(rd.strValue));
|
|
}
|
|
else if (rd.valueType == iFLOAT) {
|
|
m_baseMp[rd.nameKey]->SetValue((float)stoi(rd.strValue));
|
|
}
|
|
else if (rd.valueType == iSHORT) {
|
|
m_baseMp[rd.nameKey]->SetValue((short)stoi(rd.strValue));
|
|
}
|
|
else if (rd.valueType == iUSHORT) {
|
|
m_baseMp[rd.nameKey]->SetValue((unsigned short)stoi(rd.strValue));
|
|
}
|
|
else if (rd.valueType == iSTRING) {
|
|
m_baseMp[rd.nameKey]->SetValue(rd.strValue);
|
|
}
|
|
}
|
|
else {
|
|
printf("error, %s do not find...", rd.nameKey.c_str());
|
|
}
|
|
|
|
SendToClients(type); //客户端更新后在发送给客户端
|
|
}
|
|
|
|
|
|
protected:
|
|
std::map<std::string, BaseData*> m_baseMp;
|
|
public:
|
|
std::shared_mutex m_mtx;
|
|
};
|