#include "stdafx.h" #include "MonitorObject.h" #include #include #include #include #include "Device.h" #include #include #include using namespace std; CMonitorObject::CMonitorObject() { } CMonitorObject::~CMonitorObject() { check = 0x00; for (auto& it : m_lstMo) { delete it; it = nullptr; } m_lstMo.clear(); } CMonitorObjectMng::CMonitorObjectMng() { } CMonitorObjectMng::~CMonitorObjectMng() { if (m_treeroot) delete m_treeroot; m_treeroot = nullptr; m_mapImeiIdxMoMp.clear(); m_mapMoMpImeiIdx.clear(); } BOOL CMonitorObjectMng::LoadMoMap() { char mo[50]; char mp[50]; char imei[50]; char name1[20]; char name2[20]; char name3[20]; char direct1[20]; char direct2[20]; short idx; int zzj_no = 0; int epos = 0; uint8_t binstall_1 = 0; uint8_t binstall_2 = 0; uint8_t binstall_3 = 0; CString sql = "SELECT mo,mp,ISNULL(IMEI, ''),ISNULL(idx, 0),ISNULL(zl_1_loca, ''),ISNULL(zl_2_loca, ''), '转换阻力曲线',ISNULL(direct1, ''),ISNULL(direct2, ''),zzjno,epos,install_1,install_2,install_3 FROM rm_map"; COdbcStatement stmt; if (!CDBConnectPool::Instance()->DBQuery(stmt, sql)) { ASSERT(FALSE); CSimpleLog::Error("执行语句失败" + sql); return FALSE; } int nCol = 1; stmt.BindCharCol(nCol++, mo, sizeof(mo)); stmt.BindCharCol(nCol++, mp, sizeof(mp)); stmt.BindCharCol(nCol++, imei, sizeof(imei)); stmt.BindSmallIntCol(nCol++, &idx); stmt.BindCharCol(nCol++, name1, sizeof(name1)); stmt.BindCharCol(nCol++, name2, sizeof(name2)); stmt.BindCharCol(nCol++, name3, sizeof(name3)); stmt.BindCharCol(nCol++, direct1, sizeof(direct1)); stmt.BindCharCol(nCol++, direct2, sizeof(direct2)); stmt.BindIntCol(nCol++, &zzj_no); stmt.BindIntCol(nCol++, &epos); stmt.BindTinyIntCol(nCol++, &binstall_1); stmt.BindTinyIntCol(nCol++, &binstall_2); stmt.BindTinyIntCol(nCol++, &binstall_3); stringstream smomp; stringstream simei; while (true) { memset(imei, 0, sizeof(imei)); if (stmt.FetchNext() != 0) { break; } if (name1[0x00] != 0) sprintf_s(name1, 20, "%s%s", name1, "测力曲线"); if (name2[0x00] != 0) sprintf_s(name2, 20, "%s%s", name2, "测力曲线"); for (int i = 0x00; i < 16; i++) if (imei[i] == 0x20) imei[i] = 0x00; //0x20 代表空格 smomp.str(""); simei.str(""); smomp << mo << '.' << mp; if (imei[0x00] != 0x00) simei << imei << '.' << idx; string momp = smomp.str(); //m_mapMoMpZZJNO[momp] = zzj_no; //m_mapMoMpZZJEPOS[momp][0] = (DAOCHA_POSITION)epos; m_mapMoMpImeiIdx[momp] = simei.str(); if (imei[0x00] != 0x00) m_mapImeiIdxMoMp[simei.str()] = smomp.str(); { auto& it = m_mapMoMpInfo[momp]; if (it == nullptr) it = new ST_MOMP_INFO; it->binstall_1 = binstall_1; it->binstall_2 = binstall_2; it->binstall_3 = binstall_3; it->name1 = name1; it->name1utf = ANSItoUTF8(name1); it->name2 = name2; it->name2utf = ANSItoUTF8(name2); it->name3 = name3; it->name3utf = ANSItoUTF8(name3); it->out_name = direct1; it->in_name = direct2; it->imei = imei; it->idx = idx; it->zzjno = zzj_no; it->mo = mo; it->mp = mp; it->mapPos[time(0)] = (DAOCHA_POSITION)epos;; if (!it->imei.empty()) it->pDeivce = CDeviceMng::Instance()->Insert(imei); } /* { auto& it = m_mapMoMpName[momp]; if (it.size() == 0) { it.emplace_back(name1); it.emplace_back(name2); it.emplace_back(name3); it.emplace_back(direct1); //伸出 it.emplace_back(direct2); //缩进 } else if (it.size() == 3) { it[0] = name1; it[1] = name2; it[2] = name3; it.emplace_back(direct1); it.emplace_back(direct2); } else if (it.size() == 5) { it[0] = name1; it[1] = name2; it[2] = name3; it[3] = direct1; it[4] = direct2; } else { it.clear(); it.emplace_back(name1); it.emplace_back(name2); it.emplace_back(name3); it.emplace_back(direct1); it.emplace_back(direct2); } } { auto& it = m_mapMoMpDirect[momp]; if (it.size() == 0) { it.push_back(direct1); it.push_back(direct2); } else if (it.size() == 2) { it[0] = direct1; it[1] = direct2; } else { it.clear(); it.push_back(direct1); it.push_back(direct2); } } */ } return m_mapMoMpInfo.size() + 1; } void CMonitorObjectMng::GetChild(std::list& lst, CMonitorObject* parent) { for (auto it = lst.begin(); it != lst.end(); ++it) { auto p = *it; if (p == nullptr) continue; if (p->id.compare(p->up) == 0) continue; if (p->up.compare(parent->id) == 0) { if (p->type.compare("mo") == 0) { for (auto& ik : m_mapMoMpImeiIdx) { //int ii = ik.first.find('.'); //int jj = ik.first.find(p->id); string strName = p->id + '.'; if (ik.first.find(strName) != -1) { auto pObject = new CMonitorObject; pObject->id = ik.first; pObject->name = p->name + "." + ik.first.substr(ik.first.find('.') + 1); pObject->type = "mo.mp"; pObject->up = parent->id; pObject->parent = parent; pObject->eqpno = m_mapMoMpInfo[pObject->id]->zzjno; parent->m_lstMo.push_back(pObject); } } } else { p->parent = parent; parent->m_lstMo.push_back(p); *it = nullptr; GetChild(lst, p); } } } } bool CMonitorObjectMng::GetStationNameByMomP(CMonitorObject* pInfo, const std::string& momp, std::string& station, std::string& momp_name) { if (pInfo == nullptr) return false; if (pInfo->type.compare("mo.mp") == 0 && pInfo->id.compare(momp) == 0) { station = pInfo->up; momp_name = pInfo->name; return true; } for (const auto& it : pInfo->m_lstMo) { if (GetStationNameByMomP(it, momp, station, momp_name)) return true; } return false; } void CMonitorObjectMng::Clear() { if (m_treeroot) delete m_treeroot; m_treeroot = nullptr; m_mapImeiIdxMoMp.clear(); m_mapMoMpImeiIdx.clear(); for (auto& it : m_mapMoMpInfo) delete it.second; m_mapMoMpInfo.clear(); } BOOL CMonitorObjectMng::LoadMonitorTree() { Clear(); if (!LoadMoMap()) return FALSE; char id[100]; char type[50]; char up[100]; char name[100]; CString sql = "SELECT [id],[type],[up],[name] FROM [rm_mo]"; COdbcStatement stmt; if (!CDBConnectPool::Instance()->DBQuery(stmt, sql)) { CSimpleLog::Error("执行语句失败" + sql); return FALSE; } int nCol = 1; stmt.BindCharCol(nCol++, id, sizeof(id)); stmt.BindCharCol(nCol++, type, sizeof(type)); stmt.BindCharCol(nCol++, up, sizeof(up)); stmt.BindCharCol(nCol++, name, sizeof(name)); list lstMo; while (true) { if (stmt.FetchNext() != 0) { break; } auto p = new CMonitorObject; p->id = id; p->type = type; p->up = up; p->name = name; //赋值节点名称 if (p->type.compare("mo") == 0) { for (auto& it : m_mapMoMpInfo) { if (it.second->mo.compare(id) == 0) it.second->name = fmt::format("{}.{}", name, it.second->mp); } } if (p->id.compare(up) == 0) { swap(m_treeroot, p); delete p; p = nullptr; } else { lstMo.push_back(p); } } bool bret = true; if (m_treeroot == nullptr) { assert(0); bret = false; goto l; } GetChild(lstMo, m_treeroot); l: for (auto it = lstMo.begin(); it != lstMo.end(); ++it) delete (*it); lstMo.clear(); //test //{ // string momp = "HZHDJD21.X"; // string name, name2; // GetStationNameByMomP(momp, name, name2); //} return TRUE; } BOOL CMonitorObjectMng::LoadHistoryData() { for (auto& it : m_mapMoMpImeiIdx) { string imei, idx; spiltByPoint(it.second, imei, idx); if (imei.length() > 5) CDeviceMng::Instance()->Insert(imei); } return TRUE; } bool CMonitorObjectMng::IMEI2MOMP(const std::string imei, const int idx, std::string& mo, std::string& mp) { auto& it = m_mapImeiIdxMoMp.find(concatStringByPoint(imei, to_string(idx))); if (it == m_mapImeiIdxMoMp.end()) return false; return spiltByPoint(it->second, mo, mp); } bool CMonitorObjectMng::IMEI2MOMP(const std::string& imei_idx, std::string& mo_mp) { auto& it = m_mapImeiIdxMoMp.find(imei_idx); if (it == m_mapImeiIdxMoMp.end()) return false; mo_mp = it->second; return true; } bool CMonitorObjectMng::MOMP2IMEI(const std::string& mo, const std::string& mp, std::string& imei, int& idx) { auto& it = m_mapMoMpImeiIdx.find(concatStringByPoint(mo, mp)); if (it == m_mapMoMpImeiIdx.end()) return false; string idx2; if (!spiltByPoint(it->second, imei, idx2)) return false; idx = atoi(idx2.c_str()); return true; } bool CMonitorObjectMng::MOMP2IMEI(const std::string& momp, std::string& imeiidx) { auto& it = m_mapMoMpImeiIdx.find(momp); if (it == m_mapMoMpImeiIdx.end()) return false; if (it->second.length() <= 0) return false; imeiidx = it->second; return true; } bool CMonitorObjectMng::MOMP2IMEI(const std::string& momp, std::string& imei, int& idx) { auto& it = m_mapMoMpImeiIdx.find(momp); if (it == m_mapMoMpImeiIdx.end()) return false; string idx2; if (!spiltByPoint(it->second, imei, idx2)) return false; idx = atoi(idx2.c_str()); return true; } bool CMonitorObjectMng::GetStationNameByMomP(const std::string& momp, std::string& station, std::string& momp_name) { return GetStationNameByMomP(m_treeroot, momp, station, momp_name); } bool CMonitorObjectMng::GetStationNameByMomP(const std::string& mo, const std::string& mp, std::string& station, std::string& momp_name) { return GetStationNameByMomP(m_treeroot, mo + "." + mp, station, momp_name); } bool CMonitorObjectMng::GetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3) { //auto& it = m_mapMoMpName.find(momp); //if (it == m_mapMoMpName.end()) return false; //if (it->second.size() < 3) return false; //name1 = it->second[0]; //name2 = it->second[1]; //name3 = it->second[2]; //return true; auto it = GetMoMpInfo(momp); if (it) { name1 = it->name1; name2 = it->name2; name3 = it->name3; return true; } return false; } bool CMonitorObjectMng::GetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3, std::string& out_name, std::string& in_name) { //auto& it = m_mapMoMpName.find(momp); //if (it == m_mapMoMpName.end()) return false; //if (it->second.size() < 5) return false; //name1 = it->second[0]; //name2 = it->second[1]; //name3 = it->second[2]; //out_name = it->second[3]; //in_name = it->second[4]; auto it = GetMoMpInfo(momp); if (it) { name1 = it->name1; name2 = it->name2; name3 = it->name3; out_name = it->out_name; in_name = it->in_name; return true; } return false; } void CMonitorObjectMng::SetNameByMoMp(const std::string& momp, std::string& name1, std::string& name2, std::string& name3) { //auto& it = m_mapMoMpName.find(momp); //if (it == m_mapMoMpName.end()) //{ // std::vector vct; // vct.push_back(name1); // vct.push_back(name2); // vct.push_back(name3); // m_mapMoMpName[momp] = vct; //} //else //{ // it->second[0] = name1; // it->second[1] = name2; // it->second[2] = name3; //} { auto it = GetMoMpInfo(momp); if (it) { it->name1 = name1; it->name2 = name2; it->name3 = name3; it->name1utf = ANSItoUTF8(name1); it->name2utf = ANSItoUTF8(name2); it->name3utf = ANSItoUTF8(name3); } } } bool CMonitorObjectMng::GetDirectByMoMp(const std::string& momp, std::string& out_name, std::string& in_name) { auto it = GetMoMpInfo(momp); if (it == nullptr) return false; out_name = it->out_name; in_name = it->in_name; return true; } void CMonitorObjectMng::SetDirectByMoMp(const std::string& momp, std::string& direct1, std::string& direct2) { //auto& it = m_mapMoMpDirect.find(momp); //if (it == m_mapMoMpDirect.end()) //{ // std::vector vct; // vct.push_back(direct1); // vct.push_back(direct2); // m_mapMoMpDirect[momp] = vct; //} //else //{ // it->second[0] = direct1; // it->second[1] = direct2; //} { auto it = GetMoMpInfo(momp); if (it) { it->out_name = direct1; it->in_name = direct2; //it->name1utf = ANSItoUTF8(direct1); //it->name2utf = ANSItoUTF8(direct2); } } } bool CMonitorObjectMng::spiltByPoint(const std::string&src, std::string& dst1, std::string& dst2) { auto pos = src.find('.'); if (pos == -1) return false; dst1 = src.substr(0, pos); dst2 = src.substr(pos + 1); return true; } std::string CMonitorObjectMng::concatStringByPoint(const std::string& src1, const std::string& src2) { stringstream ss; ss << src1 << "." << src2; return ss.str(); } CMonitorObject* CMonitorObjectMng::GetTreeByID(CMonitorObject* p, const string& id) { if (p->id.compare(id) == 0) { return p; } else { for (const auto& it : p->m_lstMo) { auto pObj = GetTreeByID(it, id); if (pObj) return pObj; } } return nullptr; } CMonitorObject* CMonitorObjectMng::GetTreeByEpqID(CMonitorObject* p, uint16_t id) { if (p->eqpno == id) { return p; } else { for (const auto& it : p->m_lstMo) { auto pObj = GetTreeByEpqID(it, id); if (pObj) return pObj; } } return nullptr; } CMonitorObject* CMonitorObjectMng::GetTreeByID(const string& id) { CMonitorObject* pObject = nullptr; do { if (m_treeroot == nullptr) break; return GetTreeByID(m_treeroot, id); } while (false); return pObject; } CMonitorObject* CMonitorObjectMng::GetTreeByEpqID(uint16_t id) { CMonitorObject* pObject = nullptr; do { if (m_treeroot == nullptr) break; return GetTreeByEpqID(m_treeroot, id); } while (false); return pObject; } void CMonitorObjectMng::GetAllObjByType(CMonitorObject* p, std::vector& vctObj, const string& type) { if (p->type.compare(type) == 0) { vctObj.push_back(p); } for (auto& it : p->m_lstMo) { GetAllObjByType(it, vctObj, type); } } //获取所有Type对象 bool CMonitorObjectMng::GetAllObjByType(std::vector& vctObj, const string& type) { do { if (m_treeroot == nullptr) break; GetAllObjByType(m_treeroot, vctObj, type); } while (false); return !vctObj.empty(); } uint32_t CMonitorObjectMng::GeneralMoPack(uint8_t** pack, int* len) { if (m_treeroot == nullptr) return 0; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_obj(doc); yyjson_mut_doc_set_root(doc, root); GeneralMoPack(m_treeroot, doc, root); size_t json_len; auto json = yyjson_mut_write(doc, 0, &json_len); yyjson_mut_doc_free(doc); int new_len = json_len + sizeof(HJDATAHEAD2) + 2; *pack = new uint8_t[new_len]; memset(*pack, 0, new_len); auto no = CAppService::Instance()->GetPackNo(); *len = CHjDataConver::conver_sendpack(*pack, (LPBYTE)json, json_len, 0, no, E_ZL_PROTOCAL::ZL_MO, OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_SYNC)); free(json); return no; } void CMonitorObjectMng::GeneralMoPack(CMonitorObject* pObject, yyjson_mut_doc* doc, yyjson_mut_val* val) { yyjson_mut_obj_add_str(doc, val, "id", pObject->id.c_str()); yyjson_mut_obj_add_str(doc, val, "name", pObject->name.c_str()); yyjson_mut_obj_add_str(doc, val, "type", pObject->type.c_str()); yyjson_mut_obj_add_str(doc, val, "up", pObject->up.c_str()); yyjson_mut_val* arr = nullptr; if (pObject->m_lstMo.size() > 0) { arr = yyjson_mut_arr(doc); yyjson_mut_obj_add_val(doc, val, "child", arr); } for (auto& it : pObject->m_lstMo) { auto object = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(arr, object); GeneralMoPack(it, doc, object); } } uint32_t CMonitorObjectMng::GeneralMpPack(uint8_t** pack, int* len) { if (m_mapMoMpInfo.size() == 0) return 0; auto doc = yyjson_mut_doc_new(nullptr); auto root = yyjson_mut_arr(doc); yyjson_mut_doc_set_root(doc, root); for (const auto& it : m_mapMoMpInfo) { auto ss = it.second; auto obj = yyjson_mut_obj(doc); yyjson_mut_arr_add_val(root, obj); string mo, mp, momp = it.first; spiltByPoint(momp, mo, mp); string station, mompname; CMonitorObjectMng::Instance()->GetStationNameByMomP(momp, station, mompname); string mo_name, mp_name; spiltByPoint(mompname, mo_name, mp_name); yyjson_mut_obj_add_strcpy(doc, obj, "station", station.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "mo_name", mo_name.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "mp_name", mp_name.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "mo", mo.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "mp", mp.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "name1", ss->name1.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "name2", ss->name2.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "name3", ss->name3.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "direct1", ss->out_name.c_str()); yyjson_mut_obj_add_strcpy(doc, obj, "direct2", ss->in_name.c_str()); } size_t json_len; auto json = yyjson_mut_write(doc, 0, &json_len); yyjson_mut_doc_free(doc); *len = json_len + sizeof(HJDATAHEAD2) + 2; *pack = new uint8_t[*len]; auto no = CAppService::Instance()->GetPackNo(); CHjDataConver::conver_sendpack(*pack, (LPBYTE)json, json_len, 0, no, E_ZL_PROTOCAL::ZL_MP, OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_SYNC)); free(json); return no; } void CMonitorObjectMng::UpdateZZJEPOS(const string& momp, DAOCHA_POSITION epos) { string mo, mp; spiltByPoint(momp, mo, mp); CString sql = fmt::format("UPDATE rm_map SET epos = {} where mo = '{}' and mp = '{}'", int(epos), mo, mp).c_str(); auto ret = CDBConnectPool::Instance()->DBExecuteSQL(sql); if (ret) { auto lstEPOS = m_mapMoMpInfo[momp]; lstEPOS->mapPos[time(0)] = (DAOCHA_POSITION)epos; if (lstEPOS->mapPos.size() > 10) { lstEPOS->mapPos.erase(lstEPOS->mapPos.begin()); } } else { CSimpleLog::Error("更新语句错误:" + sql); } } //删除ZZJ历史记录,保留最新的一个 void CMonitorObjectMng::ClearZZJHistroyEPOS(time_t t) { for (auto& item : m_mapMoMpInfo) { if (item.second == nullptr) continue; auto& epos = item.second->mapPos; if (epos.size() <= 1) continue; std::map mapPos; mapPos[epos.rbegin()->first] = epos.rbegin()->second; epos.swap(mapPos); mapPos.clear(); } } CMonitorObjectMng CMonitorObjectMng::obj;