| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114 |
- #include "stdafx.h"
- #include "LNHandle.h"
- #include <Simplelog.h>
- #include "AppService.h"
- #include "Device.h"
- #include <yyjson.h>
- #include "MGWSServer.h"
- #include "Device.h"
- #include "ResistAlarm.h"
- #include <gbk2utf8.h>
- using namespace jsonxx;
- CTask::CTask(LPBYTE pPack, int nPackLen, SOCKADDR* psa, void* pClientContext)
- : addr(*psa)
- {
- pdata = new BYTE[nPackLen];
- memcpy(pdata, pPack, nPackLen);
- nlen = nPackLen;
- }
- void CTask::join()
- {
- delete this;
- }
- CTask::~CTask()
- {
- delete[] pdata;
- pdata = nullptr;
- nlen = 0;
- }
- CLNHandle::CLNHandle()
- {
- m_dl_sync = new uint8_t[100];
- m_dl_len = CHjDataConver::conver_sendpack(m_dl_sync, nullptr, 0, 0, 0, E_ZL_PROTOCAL::DL_SYNC, OR_DATA_INFO(0,0, 1, 3, OPT_TYPE::OPT_SYNC));
- for (auto i = 0; i < MAX_HANDLE_THREAD_NUM; i++)
- pThread[i] = nullptr;
- Create();
- }
- CLNHandle::~CLNHandle()
- {
- delete[] m_dl_sync;
- m_dl_sync = nullptr;
- m_dl_len = 0;
- Close();
- }
- BOOL CLNHandle::HandlePack(LPBYTE pPack, int nPackLen, SOCKADDR* psa, void* pClientContext /*= NULL*/)
- {
- if (!m_bWork) return true;
- auto pContext = ((CLNContext*)pClientContext);
- if ((pPack == NULL || nPackLen == 0) && pClientContext)
- {
- pContext->SetIPAdress();
- SPDLOG_INFO("10090 发现新连接:{}", pContext->GetIPAdress());
- pContext->Send(m_dl_sync, m_dl_len);
- return TRUE;
- }
- if (CHjDataConver::conver_recvpack(pPack, nPackLen) == false)
- return FALSE;
- LPHJDATAHEAD2 lpHead = (HJDATAHEAD2*)pPack;
- auto addr = (SOCKADDR_IN*)psa;
- auto it = m_mapUser.find(addr->sin_addr.S_un.S_addr);
- if (lpHead->protocol == E_ZL_PROTOCAL::ZL_KEEP)
- return TRUE;
- //SPDLOG_INFO("packno:{} recv {}", lpHead->packno, uint8_t(lpHead->protocol));
- switch (lpHead->protocol)
- {
- case E_ZL_PROTOCAL::ZL_MO:
- HandleMoSync(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- case E_ZL_PROTOCAL::ZL_MP:
- HandleMpSync(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- case E_ZL_PROTOCAL::ZL_SVG:
- HandleSvgSync(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- case E_ZL_PROTOCAL::ZL_REFER:
- HandleReferSync(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- case E_ZL_PROTOCAL::ZL_ALARM_SET:
- HandleAlarmSetSync(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- case E_ZL_PROTOCAL::ZL_ALARM_UNACK:
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- break;
- case E_ZL_PROTOCAL::ZL_RESIST_DATA:
- HandleResistData(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- case E_ZL_PROTOCAL::ZL_TEMP_HUMI:
- HandleTempHumiData(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- case E_ZL_PROTOCAL::ZL_MOVE:
- HandleMove(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- case E_ZL_PROTOCAL::ZL_ALARM_DATA:
- HandleAlarmData(lpHead, (char*)pPack + sizeof(HJDATAHEAD2), lpHead->len, (CLNContext*)pClientContext);
- break;
- default:
- SPDLOG_WARN("收到未解析的命令:{}", uint8_t(lpHead->protocol));
- break;
- }
- return TRUE;
- }
- void CLNHandle::SendAllClientSync()
- {
- auto pAcceptor = CAppService::Instance()->GetAcceptor();
- if (!pAcceptor) return;
- pAcceptor->SendDataAllContexts(m_dl_sync, m_dl_len);
- }
- void CLNHandle::SendSubNotify(string momp)
- {
- jsonxx::Object obj;
- obj << "momp" << momp;
- auto data = obj.json();
- BYTE send[100];
- int len = CHjDataConver::conver_sendpack(send, (BYTE*)data.c_str(), data.length(), 0, 0, E_ZL_PROTOCAL::DL_SUB_NOTIFY, OR_DATA_INFO(0, 0, 1, 3, OPT_TYPE::OPT_ADD));
- auto pAcceptor = CAppService::Instance()->GetAcceptor();
- if (!pAcceptor) return;
- pAcceptor->SendDataAllContexts(send, len);
- }
- void CLNHandle::HandleMoSync(LPHJDATAHEAD2 lpHead, char* json, int json_len, CLNContext* pContext)
- {
- jsonxx::Object doc;
- doc.parse(string(json, json_len));
- if (doc.empty()) return;
- auto id = doc.get<string>("id");
-
- ASSERT(id.compare("100000") == 0);
- auto pObj = CMonitorObjectMng::Instance()->GetTreeByID(id);
- auto child = doc.get<jsonxx::Array>("child");
- auto n = child.size();
- for (int i = 0; i < n; i++)
- {
- auto obj = child.get<Object>(i);
- CompareMoSync(obj, pObj);
- }
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- }
- void CLNHandle::HandleMpSync(LPHJDATAHEAD2 lpHead, char* json, int json_len, CLNContext* pContext)
- {
- using namespace jsonxx;
- jsonxx::Array doc;
- doc.parse(string(json, json_len));
- if (doc.empty()) return;
- size_t n = doc.size();
- for (size_t i = 0; i < n; i++)
- {
- auto obj = doc.get<Object>(i);
- CompareMpSync(obj);
- }
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- }
- BOOL CLNHandle::HandleSvgSync(LPHJDATAHEAD2 lpHead, char* json, int json_len, CLNContext* pContext)
- {
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- auto strSvgFolderPath = g_strExepath + "svg\\";
- string strFileName(json);
- auto strSvgPath = strSvgFolderPath + strFileName + ".svg";
- BOOL bSave = true;
- WIN32_FILE_ATTRIBUTE_DATA attrs = { 0 };
- if (0 != ::GetFileAttributesEx(strSvgPath.c_str(), ::GetFileExInfoStandard, &attrs))
- {
- auto len = ((uint64_t)attrs.nFileSizeHigh << 32) + attrs.nFileSizeLow;
- if (len == json_len - 64)
- bSave = false; //大小相等不存储
- }
- if (bSave)
- {
- CFile file;
- if (file.Open(strSvgPath.c_str(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary) == FALSE)
- {
- SPDLOG_ERROR("打开文件{}失败", strSvgPath);
- return FALSE;
- }
- else
- {
- file.Write(json + 64, json_len - 64);
- file.Close();
- }
- }
- return TRUE;
- }
- BOOL CLNHandle::HandleReferSync(LPHJDATAHEAD2 lpHead, char* json, int json_len, CLNContext* pContext)
- {
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- auto strSvgFolderPath = g_strExepath + "refer\\";
- string strFileName(json);
- auto strSvgPath = strSvgFolderPath + strFileName + ".json";
- BOOL bSave = true;
- WIN32_FILE_ATTRIBUTE_DATA attrs = { 0 };
- if (0 != ::GetFileAttributesEx(strSvgPath.c_str(), ::GetFileExInfoStandard, &attrs))
- {
- auto len = ((uint64_t)attrs.nFileSizeHigh << 32) + attrs.nFileSizeLow;
- if (len == json_len - 64)
- bSave = false; //大小相等不存储
- }
- if (bSave)
- {
- CFile file;
- if (file.Open(strSvgPath.c_str(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary) == FALSE)
- {
- SPDLOG_ERROR("打开文件{}失败", strSvgPath);
- return FALSE;
- }
- else
- {
- file.Write(json + 64, json_len - 64);
- file.Close();
- }
- }
- return TRUE;
- }
- BOOL CLNHandle::HandleResistData(LPHJDATAHEAD2 lpHead, char* json, int len, CLNContext* pContext)
- {
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- static string sz_utf_fix = ANSItoUTF8("定位测力曲线");
- static string sz_utf_invert = ANSItoUTF8("反位测力曲线");
- static string sz_utf_convert = ANSItoUTF8("转换阻力曲线");
- Object doc;
- string strJson(json, len);
- doc.parse(strJson);
- if (doc.empty())
- {
- SPDLOG_WARN("json格式未正确 {}", strJson);
- return FALSE;
- }
- auto station = doc.get<string>("station");
- auto mo = doc.get<string>("mo");
- auto mp = doc.get<string>("mp");
- auto time = doc.get<Array>("time");
- auto fix_data = doc.get<Array>("fix_data");
- auto invert_data = doc.get<Array>("invert_data");
- auto convert_data = doc.get<Array>("convert_data");
- size_t n = time.size();
- size_t n1 = fix_data.size();
- size_t n2 = invert_data.size();
- size_t n3 = convert_data.size();
- ASSERT(n == n1 && n == n2 && n == n3);
- if (n != n1 || n != n2 || n != n3)
- {
- SPDLOG_WARN("time fix_data invert_data convert_data not = {}", strJson);
- return FALSE;
- }
- if (n < 1)
- {
- SPDLOG_WARN("size < 1 {}", strJson);
- return FALSE;
- }
- auto momp = fmt::format("{}.{}", mo, mp);
- auto tt = time.get<string>(0);
- int year, month, day;
- auto s_num = sscanf_s(tt.c_str(), "%d-%d-%d ", &year, &month, &day);
- ASSERT(s_num == 3);
- auto tablename = fmt::format("rm_resistance_{:0>4}{:0>2}{:0>2}", year, month, day);
- auto ii = fix_data.get<Value>(0);
- auto pDevice = CDeviceMng::Instance()->Find(momp);
- if (pDevice) pDevice->updateActive();
- static auto mg_server = CAppService::Instance()->GetMgServer();
- auto bSub = mg_server->IsClientSubReal(momp); //判断客户端是否订阅
- char* json_res = nullptr;
- yyjson_mut_doc* doc_y = nullptr;
- yyjson_mut_val* points_fix = nullptr;
- yyjson_mut_val* points_invert = nullptr;
- yyjson_mut_val* points_convert = nullptr;
- if (bSub)
- {
- doc_y = yyjson_mut_doc_new(nullptr);
- auto root = yyjson_mut_obj(doc_y);
- yyjson_mut_doc_set_root(doc_y, root);
- auto data = yyjson_mut_arr(doc_y);
- yyjson_mut_obj_add_val(doc_y, root, "data", data);
- auto curve_fix = yyjson_mut_obj(doc_y);//定
- auto curve_invert = yyjson_mut_obj(doc_y);//反
- auto curve_convert = yyjson_mut_obj(doc_y);//转换
- yyjson_mut_arr_add_val(data, curve_fix);
- yyjson_mut_arr_add_val(data, curve_invert);
- yyjson_mut_arr_add_val(data, curve_convert);
- yyjson_mut_obj_add_str(doc_y, root, "cmd", "new_data_notify");
- yyjson_mut_obj_add_strcpy(doc_y, root, "tag", (momp + ".resist").c_str());
- yyjson_mut_obj_add_str(doc_y, root, "data_fmt", "curve");
- yyjson_mut_obj_add_str(doc_y, root, "unit", "N");
- yyjson_mut_obj_add_strcpy(doc_y, root, "up", ANSItoUTF8(station).c_str());
- yyjson_mut_obj_add_strcpy(doc_y, root, "tag_name", pDevice ? (pDevice->mo_name + "." + pDevice->mp_name).c_str() : momp.c_str());
- yyjson_mut_obj_add_str(doc_y, curve_fix, "name", sz_utf_fix.c_str());
- yyjson_mut_obj_add_str(doc_y, curve_invert, "name", sz_utf_invert.c_str());
- yyjson_mut_obj_add_str(doc_y, curve_convert, "name", sz_utf_convert.c_str());
- points_fix = yyjson_mut_arr(doc_y);
- yyjson_mut_obj_add_val(doc_y, curve_fix, "points", points_fix);
- points_invert = yyjson_mut_arr(doc_y);
- yyjson_mut_obj_add_val(doc_y, curve_invert, "points", points_invert);
- points_convert = yyjson_mut_arr(doc_y);
- yyjson_mut_obj_add_val(doc_y, curve_convert, "points", points_convert);
- }
- CString sql;
- for (size_t i = 0; i < n; i++)
- {
- auto t = time.get<string>(i);
- int f = int(fix_data.get<Number>(i));
- int v = int(invert_data.get<Number>(i));
- int c = int(convert_data.get<Number>(i));
- sql += fmt::format("INSERT INTO {}(momp,acquisitiontime,data0,data1,data2) VALUES ('{}','{}',{},{},{});",
- tablename, momp, t, f, v, c).c_str();
- if (bSub)
- {
- int hour, min, second, fra;
- auto s_num = sscanf_s(t.c_str(), "%d-%d-%d %d:%d:%d.%d", &year, &month, &day, &hour, &min, &second, &fra);
- if (s_num == 7)
- {
- uint64_t ttt = CTime(year, month, day, hour, min, second).GetTime() * 1000 + fra;
- {
- auto item = yyjson_mut_arr(doc_y);
- yyjson_mut_arr_add_val(points_fix, item);
- yyjson_mut_arr_add_uint(doc_y, item, ttt);
- yyjson_mut_arr_add_int(doc_y, item, f);
- }
- {
- auto item = yyjson_mut_arr(doc_y);
- yyjson_mut_arr_add_val(points_invert, item);
- yyjson_mut_arr_add_uint(doc_y, item, ttt);
- yyjson_mut_arr_add_int(doc_y, item, v);
- }
- {
- auto item = yyjson_mut_arr(doc_y);
- yyjson_mut_arr_add_val(points_convert, item);
- yyjson_mut_arr_add_uint(doc_y, item, ttt);
- yyjson_mut_arr_add_int(doc_y, item, c);
- }
- }
- else
- ASSERT(false);
- }
- }
- auto pConn = CDBConnectPool::Instance()->GetConnection();
- COdbcStatement stmt(pConn);
- auto iret = pConn->SetAutoCommit(false);
- iret = stmt.Prepare(sql);
- if (ISODBCSUCCESS(stmt.Execute()))
- {
- }
- else
- {
- SPDLOG_ERROR("语句执行错误{}", sql);
- }
- iret = pConn->Commit();
- pConn->SetAutoCommit(true);
- if (bSub)
- {
- size_t json_len;
- json_res = yyjson_mut_write(doc_y, 0, &json_len);
- mg_server->SendClientSubReal(momp, json_res, json_len);
- }
- //释放
- CDBConnectPool::Instance()->FreeConnection(pConn);
- if (json_res) free(json_res);
- yyjson_mut_doc_free(doc_y);
- return TRUE;
- }
- BOOL CLNHandle::HandleTempHumiData(LPHJDATAHEAD2 lpHead, char* json, int json_len, CLNContext* pContext)
- {
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- Object doc;
- string strJson(json, json_len);
- doc.parse(strJson);
- if (doc.empty())
- {
- SPDLOG_WARN("json格式未正确 {}", strJson);
- return FALSE;
- }
- auto station = doc.get<string>("station");
- auto mo = doc.get<string>("mo");
- auto mp = doc.get<string>("mp");
- auto time = doc.get<string>("time");
- int temp = doc.get<Number>("temp");
- int humi = doc.get<Number>("humi");
- int year, month, day;
- auto s_num = sscanf_s(time.c_str(), "%d-%d-%d ", &year, &month, &day);
- ASSERT(s_num == 3);
- auto tablename = fmt::format("rm_temphumidity_{:0>4}{:0>2}", year, month);
- CString sql = fmt::format("INSERT INTO {}(mo,mp,acquisitiontime,temperature,humidity) VALUES ('{}','{}','{}',{},{});",
- tablename, mo, mp, time, temp, humi).c_str();
- if (!CDBConnectPool::Instance()->DBExecuteSQL(sql))
- {
- SPDLOG_ERROR("语句执行错误{}", sql);
- return FALSE;
- }
- return TRUE;
- }
- BOOL CLNHandle::HandleAlarmSetSync(LPHJDATAHEAD2 lpHead, char* json, int json_len, CLNContext* pContext)
- {
- BOOL bRet = false;
- do {
- Array arr;
- arr.parse(string(json, json_len));
- if (arr.empty())
- {
- ASSERT(0);
- break;
- }
- size_t n = arr.size();
- for (int i = 0; i < n; i++)
- {
- auto obj = arr.get<Object>(i);
- auto mo = obj.get<string>("mo");
- auto mp = obj.get<string>("mp");
- auto station = obj.get<string>("station");
- uint8_t no = obj.get<Number>("no");
- eZL_ALARMTYPE type = (eZL_ALARMTYPE)obj.get<Number>("type");
- auto pBaseInfo = CResistAlarmMng::Instance()->Find(mo, mp, no, type);
- static auto InsertAlarmSetToDB = [](string& mo, string& mp, uint8_t no, eZL_ALARMTYPE type, string& conf) {
- time_t tt;
- time(&tt);
- CString sql = fmt::format("INSERT INTO[rm_alarm_set]([mo], [mp], [no], [type], [conf], [time]) VALUES('{}', '{}', {}, {}, '{}', '{}')",
- mo, mp, no, uint8_t(type), conf, tt).c_str();
- if (FALSE == CDBConnectPool::Instance()->DBExecuteSQL(sql))
- {
- ASSERT(0);
- SPDLOG_ERROR("插入语句执行错误:{}", sql);
- return FALSE;
- }
- return TRUE;
- };
- static auto UpdateAlarmSetToDB = [](string& mo, string& mp, uint8_t no, eZL_ALARMTYPE type, string& conf) {
- time_t tt;
- time(&tt);
- CString sql = fmt::format("UPDATE rm_alarm_set SET [conf] = '{}',[time] = {} WHERE mo='{}' AND mp='{}' AND no = {} AND type = {}",
- conf, tt, mo, mp, no, uint8_t(type)).c_str();
- if (FALSE == CDBConnectPool::Instance()->DBExecuteSQL(sql))
- {
- ASSERT(0);
- SPDLOG_ERROR("更新语句执行错误:{}", sql);
- return FALSE;
- }
- return TRUE;
- };
- string strConfig;
- Array conf_array;
- Object conf_obj;
- if (type == eZL_ALARMTYPE::MAX_OVER_LIMIT || type == eZL_ALARMTYPE::FRICTION_OVER_LIMIT)
- {
- conf_array = obj.get<Array>("conf");
- strConfig = conf_array.json();
- }
- else if (type == eZL_ALARMTYPE::SUOBI_LOCK_LIMIT || type == eZL_ALARMTYPE::CONVERT_LIMIT)
- {
- conf_obj = obj.get<Object>("conf");
- strConfig = conf_obj.json();
- }
- else
- ASSERT(FALSE);
- if (pBaseInfo == nullptr)
- {//不存在,新增
- //插入数据库
- if (FALSE == InsertAlarmSetToDB(mo, mp, no, type, strConfig))
- break;
- switch (type)
- {
- case eZL_ALARMTYPE::MAX_OVER_LIMIT:
- {
- auto pInfo = new MAX_OVER_LIMIT_INFO;
- pBaseInfo = pInfo;
- pInfo->no = no;
- pInfo->type = type;
- bool enable = false;
- short alarm_high_limit = MAXSHORT;
- short warn_high_limit = MAXSHORT;
- short f_alarm_high_limit = MAXSHORT;
- short f_warn_high_limit = MAXSHORT;
- size_t n = conf_array.size();
- for (size_t i = 0; i < n; i++)
- {
- auto obj = conf_array.get<Object>(i);
- auto key = obj.get<string>("name");
- auto value = obj.get<string>("val");
- if (key.compare("enable") == 0)
- {
- enable = (value.compare("true") == 0);
- }
- else if (key.compare("lock_alarm_high_limit") == 0)
- {
- alarm_high_limit = atoi(value.c_str());
- }
- else if (key.compare("lock_warn_high_limit") == 0)
- {
- warn_high_limit = atoi(value.c_str());
- }
- else if (key.compare("d_alarm_high_limit") == 0)
- {
- alarm_high_limit = atoi(value.c_str());
- }
- else if (key.compare("d_warn_high_limit") == 0)
- {
- warn_high_limit = atoi(value.c_str());
- }
- else if (key.compare("f_alarm_high_limit") == 0)
- {
- f_alarm_high_limit = atoi(value.c_str());
- }
- else if (key.compare("f_warn_high_limit") == 0)
- {
- f_warn_high_limit = atoi(value.c_str());
- }
- else if (key.compare("keep_alarm_high_limit") == 0)
- {
- f_alarm_high_limit = atoi(value.c_str());
- }
- else if (key.compare("keep_warn_high_limit") == 0)
- {
- f_warn_high_limit = atoi(value.c_str());
- }
- else
- {
- ASSERT(0);
- }
- }
- pInfo->alarm_high_limit = alarm_high_limit;
- pInfo->warn_high_limit = warn_high_limit;
- pInfo->f_alarm_high_limit = f_alarm_high_limit;
- pInfo->f_warn_high_limit = f_warn_high_limit;
- CResistAlarmMng::Instance()->Insert(mo, mp, no, uint8_t(type), pInfo);
- }
- break;
- case eZL_ALARMTYPE::FRICTION_OVER_LIMIT:
- {
- auto pInfo = new FRICTION_OVER_LIMIT_INFO;
- pBaseInfo = pInfo;
- pInfo->no = no;
- pInfo->type = type;
- size_t n = conf_array.size();
- for (size_t i = 0; i < n; i++)
- {
- auto obj = conf_array.get<Object>(i);
- auto name = obj.get<string>("name");
- auto val = obj.get<string>("val");
- if (name.compare("enable") == 0)
- {
- pInfo->enable = val.compare("true") == 0 ? 1 : 0;
- }
- else if (name.compare("up_alarm_low_limit") == 0)
- {
- pInfo->up_alarm_low_limit = atoi(val.c_str());
- }
- else if (name.compare("up_warn_low_limit") == 0)
- {
- pInfo->up_warn_low_limit = atoi(val.c_str());
- }
- else if (name.compare("dw_alarm_high_limit") == 0)
- {
- pInfo->dw_alarm_high_limit = atoi(val.c_str());
- }
- else if (name.compare("dw_warn_high_limit") == 0)
- {
- pInfo->dw_warn_high_limit = atoi(val.c_str());
- }
- else
- ASSERT(0);
- }
- CResistAlarmMng::Instance()->Insert(mo, mp, no, uint8_t(type), pInfo);
- }
- break;
- case eZL_ALARMTYPE::SUOBI_LOCK_LIMIT:
- {
- auto pInfo = new SUOBI_OVER_LIMIT_INFO;
- pBaseInfo = pInfo;
- pInfo->no = no;
- pInfo->type = type;
- pInfo->enable = conf_obj.get<Boolean>("enable");
- pInfo->alarm_low_limit = conf_obj.get<Number>("alarm_low_limit");
- pInfo->warn_low_limit = conf_obj.get<Number>("warn_low_limit");
- pInfo->alarm_high_limit = conf_obj.get<Number>("alarm_high_limit");
- pInfo->warn_high_limit = conf_obj.get<Number>("warn_high_limit");
- CResistAlarmMng::Instance()->Insert(mo, mp, no, uint8_t(type), pInfo);
- }
- break;
- case eZL_ALARMTYPE::CONVERT_LIMIT:
- {
- auto pInfo = new CONVERT_RESIST_OVER_LIMIT;
- pBaseInfo = pInfo;
- pInfo->no = no;
- pInfo->type = type;
- pInfo->enable = conf_obj.get<Boolean>("enable");
- pInfo->dw_alarm_low_limit = conf_obj.get<Number>("dw_alarm_low_limit");
- pInfo->dw_warn_low_limit = conf_obj.get<Number>("dw_warn_low_limit");
- pInfo->up_alarm_high_limit = conf_obj.get<Number>("up_alarm_high_limit");
- pInfo->up_warn_high_limit = conf_obj.get<Number>("up_warn_high_limit");
- CResistAlarmMng::Instance()->Insert(mo, mp, no, uint8_t(type), pInfo);
- }
- break;
- default:
- ASSERT(0);
- break;
- }
- }
- else
- {//存在 对比数值
- switch (type)
- {
- case eZL_ALARMTYPE::MAX_OVER_LIMIT:
- {
- bool enable = false;
- short alarm_high_limit = MAXSHORT;
- short warn_high_limit = MAXSHORT;
- short f_alarm_high_limit = MAXSHORT;
- short f_warn_high_limit = MAXSHORT;
- size_t n = conf_array.size();
- for (size_t i = 0; i < n; i++)
- {
- auto obj = conf_array.get<Object>(i);
- auto key = obj.get<string>("name");
- auto value = obj.get<string>("val");
- if (key.compare("enable") == 0)
- {
- enable = (value.compare("true") == 0);
- }
- else if (key.compare("lock_alarm_high_limit") == 0)
- {
- alarm_high_limit = atoi(value.c_str());
- }
- else if (key.compare("lock_warn_high_limit") == 0)
- {
- warn_high_limit = atoi(value.c_str());
- }
- else if (key.compare("d_alarm_high_limit") == 0)
- {
- alarm_high_limit = atoi(value.c_str());
- }
- else if (key.compare("d_warn_high_limit") == 0)
- {
- warn_high_limit = atoi(value.c_str());
- }
- else if (key.compare("f_alarm_high_limit") == 0)
- {
- f_alarm_high_limit = atoi(value.c_str());
- }
- else if (key.compare("f_warn_high_limit") == 0)
- {
- f_warn_high_limit = atoi(value.c_str());
- }
- else if (key.compare("keep_alarm_high_limit") == 0)
- {
- f_alarm_high_limit = atoi(value.c_str());
- }
- else if (key.compare("keep_warn_high_limit") == 0)
- {
- f_warn_high_limit = atoi(value.c_str());
- }
- else
- {
- ASSERT(0);
- }
- }
- auto pInfo = (MAX_OVER_LIMIT_INFO*)pBaseInfo;
- if (enable != pInfo->enable || alarm_high_limit != pInfo->alarm_high_limit || warn_high_limit != pInfo->warn_high_limit ||
- f_alarm_high_limit != pInfo->f_alarm_high_limit || f_warn_high_limit != pInfo->f_warn_high_limit)
- {
- if (UpdateAlarmSetToDB(mo, mp, no, type, strConfig))
- {
- pInfo->alarm_high_limit = alarm_high_limit;
- pInfo->warn_high_limit = warn_high_limit;
- pInfo->f_alarm_high_limit = f_alarm_high_limit;
- pInfo->f_warn_high_limit = f_warn_high_limit;
- pInfo->enable = enable;
- }
- }
- }
- break;
- case eZL_ALARMTYPE::FRICTION_OVER_LIMIT:
- {
- bool enable = false;
- short up_alarm_low_limit = MAXSHORT;
- short up_warn_low_limit = MAXSHORT;
- short dw_alarm_high_limit = MAXSHORT;
- short dw_warn_high_limit = MAXSHORT;
- size_t n = conf_array.size();
- for (size_t i = 0; i < n; i++)
- {
- auto obj = conf_array.get<Object>(i);
- auto name = obj.get<string>("name");
- auto val = obj.get<string>("val");
- if (name.compare("enable") == 0)
- {
- enable = val.compare("true") == 0 ? 1 : 0;
- }
- else if (name.compare("up_alarm_low_limit") == 0)
- {
- up_alarm_low_limit = atoi(val.c_str());
- }
- else if (name.compare("up_warn_low_limit") == 0)
- {
- up_warn_low_limit = atoi(val.c_str());
- }
- else if (name.compare("dw_alarm_high_limit") == 0)
- {
- dw_alarm_high_limit = atoi(val.c_str());
- }
- else if (name.compare("dw_warn_high_limit") == 0)
- {
- dw_warn_high_limit = atoi(val.c_str());
- }
- else
- ASSERT(0);
- }
- auto pInfo = (FRICTION_OVER_LIMIT_INFO*)pBaseInfo;
- if (enable != pInfo->enable || up_alarm_low_limit != pInfo->up_alarm_low_limit || up_warn_low_limit != pInfo->up_warn_low_limit ||
- dw_alarm_high_limit != pInfo->dw_alarm_high_limit || dw_warn_high_limit != pInfo->dw_warn_high_limit)
- {
- if (UpdateAlarmSetToDB(mo, mp, no, type, strConfig))
- {
- pInfo->up_alarm_low_limit = up_alarm_low_limit;
- pInfo->up_warn_low_limit = up_warn_low_limit;
- pInfo->dw_alarm_high_limit = dw_alarm_high_limit;
- pInfo->dw_warn_high_limit = dw_warn_high_limit;
- pInfo->enable = enable;
- }
- }
- }
- break;
- case eZL_ALARMTYPE::SUOBI_LOCK_LIMIT:
- {
- auto pInfo = (SUOBI_OVER_LIMIT_INFO*)pBaseInfo;
- bool enable = conf_obj.get<Boolean>("enable");
- int alarm_low_limit = conf_obj.get<Number>("alarm_low_limit");
- int warn_low_limit = conf_obj.get<Number>("warn_low_limit");
- int alarm_high_limit = conf_obj.get<Number>("alarm_high_limit");
- int warn_high_limit = conf_obj.get<Number>("warn_high_limit");
- if (enable != pInfo->enable || alarm_low_limit != pInfo->alarm_low_limit || warn_low_limit != pInfo->warn_low_limit ||
- alarm_high_limit != pInfo->alarm_high_limit || warn_high_limit != pInfo->warn_high_limit)
- {
- if (UpdateAlarmSetToDB(mo, mp, no, type, strConfig))
- {
- pInfo->alarm_low_limit = alarm_low_limit;
- pInfo->warn_low_limit = warn_low_limit;
- pInfo->alarm_high_limit = alarm_high_limit;
- pInfo->warn_high_limit = warn_high_limit;
- pInfo->enable = enable;
- }
- }
- }
- break;
- case eZL_ALARMTYPE::CONVERT_LIMIT:
- {
- auto pInfo = (CONVERT_RESIST_OVER_LIMIT*)pBaseInfo;
- bool enable = conf_obj.get<Boolean>("enable");
- int dw_alarm_low_limit = conf_obj.get<Number>("dw_alarm_low_limit");
- int dw_warn_low_limit = conf_obj.get<Number>("dw_warn_low_limit");
- int up_alarm_high_limit = conf_obj.get<Number>("up_alarm_high_limit");
- int up_warn_high_limit = conf_obj.get<Number>("up_warn_high_limit");
- if (enable != pInfo->enable || dw_alarm_low_limit != pInfo->dw_alarm_low_limit || dw_warn_low_limit != pInfo->dw_warn_low_limit ||
- up_alarm_high_limit != pInfo->up_alarm_high_limit || up_warn_high_limit != pInfo->up_warn_high_limit)
- {
- if (UpdateAlarmSetToDB(mo, mp, no, type, strConfig))
- {
- pInfo->dw_alarm_low_limit = dw_alarm_low_limit;
- pInfo->dw_warn_low_limit = dw_warn_low_limit;
- pInfo->up_alarm_high_limit = up_alarm_high_limit;
- pInfo->up_warn_high_limit = up_warn_high_limit;
- pInfo->enable = enable;
- }
- }
- }
- break;
- default:
- ASSERT(0);
- break;
- }
- }
- }
- } while (false);
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- return bRet;
- }
- BOOL CLNHandle::HandleMove(LPHJDATAHEAD2 lpHead, char* json, int json_len, CLNContext* pContext)
- {
- BOOL bRet = false;
- do {
- Array arr;
- arr.parse(string(json, json_len));
- if (arr.empty())
- {
- ASSERT(0);
- break;
- }
- size_t n = arr.size();
- CString sql;
- for (int i = 0; i < n; i++)
- {
- auto obj = arr.get<Object>(i);
- auto mo = obj.get<string>("mo");
- auto mp = obj.get<string>("mp");
- auto show_time = obj.get<string>("show_time");
- auto start_time = obj.get<string>("start_time");
- auto end_time = obj.get<string>("end_time");
- auto mark = obj.get<string>("mark");
- int curr_val = obj.get<Number>("curr_val");
- int show_val = obj.get<Number>("show_val");
- uint8_t idx = obj.get<Number>("idx");
- uint8_t posi = obj.get<Number>("posi");
- int year, month, day;
- auto num = sscanf_s(show_time.c_str(), "%d-%d-%d ", &year, &month, &day);
- auto tablename = fmt::format("rm_move_{:0>4}{:0>2}", year, month);
- sql += fmt::format("IF NOT EXISTS (SELECT TOP 1 * FROM {0} WHERE mo = '{1}' and mp = '{2}' and show_time = '{3}')\
- BEGIN\
- INSERT INTO {0}([mo], [mp], [show_time], [start_time], [end_time], [curr_val], [show_val], [idx], [posi], [mark])\
- VALUES('{1}', '{2}', '{3}', '{4}', '{5}', {6}, {7}, {8}, {9}, '{10}')\
- END ", tablename, mo, mp, show_time, start_time, end_time, curr_val, show_val, idx, posi, mark).c_str();
- }
- if (CDBConnectPool::Instance()->DBExecuteSQL(sql))
- {
- bRet = true;
- SPDLOG_INFO("{} 同步move {}条记录,语句长度:{}", pContext->GetIPAdressNew(), n, sql.GetLength());
- }
- else
- SPDLOG_ERROR("{} 同步move {}条记录失败!语句长度:{}", pContext->GetIPAdressNew(), n, sql.GetLength());
- } while (false);
-
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- return bRet;
- }
- BOOL CLNHandle::HandleAlarmData(LPHJDATAHEAD2 lpHead, char* json, int json_len, CLNContext* pContext)
- {
- BOOL bRet = false;
- do {
- Array arr;
- arr.parse(string(json, json_len));
- if (arr.empty())
- {
- ASSERT(0);
- break;
- }
- const OPT_TYPE opt_type = lpHead->datainfo.opt;
- size_t n = arr.size();
- CString sql;
- ALARM_INFO* pInfo = nullptr;
- for (int i = 0; i < n; i++)
- {
- auto obj = arr.get<Object>(i);
- auto event_id = obj.get<string>("event_id");
- if (opt_type == OPT_TYPE::OPT_ADD)
- {
- pInfo = new ALARM_INFO;
- pInfo->event_id = event_id;
- auto pResistServer = CResistAlarmMng::Instance();
- lock_guard<mutex> lock(pResistServer->m_mtxAlarm);
- pResistServer->m_lstUnConfirmAlarm.push_back(pInfo);
- }
- else
- {
- //TODO
- continue;
- }
- pInfo->id = obj.get<Number>("id");
- pInfo->mo = obj.get<string>("mo");
- pInfo->mp = obj.get<string>("mp");
- pInfo->no = obj.get<Number>("no");
- pInfo->type = eZL_ALARMTYPE(obj.get<Number>("type"));
- pInfo->level = obj.get<Number>("level");
- auto alarm_time = obj.get<string>("time");
- //int year, month, day, hour, min, second;
- //auto num = sscanf_s(alarm_time.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &min, &second);
- pInfo->desc = obj.get<string>("desc");
- pInfo->suggest = obj.get<string>("suggest");
- pInfo->val = obj.get<Number>("val");
- pInfo->ack_result = obj.get<Number>("ack_result");
- pInfo->ack_name = obj.get<string>("ack_name");
- pInfo->ack_time = obj.get<Number>("ack_time");
- pInfo->rel_id = obj.get<string>("rel_id");
- CString ss;
- if (opt_type == OPT_TYPE::OPT_ADD)
- {
- ss.Format("INSERT INTO [rm_alarm]([ID],[mo],[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id]) "\
- "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s');",
- pInfo->id, pInfo->mo.c_str(), pInfo->mp.c_str(), pInfo->no, pInfo->type,
- alarm_time.c_str(), pInfo->level, pInfo->desc.c_str(), pInfo->suggest.c_str(), pInfo->val,
- pInfo->event_id.c_str(), pInfo->rel_id.c_str());
- sql += ss;
- }
- }
- if (CDBConnectPool::Instance()->DBExecuteSQL(sql))
- {
- bRet = true;
- SPDLOG_INFO("{} 新增 alarm {}条记录,语句长度:{}", pContext->GetIPAdressNew(), n, sql.GetLength());
- }
- else
- SPDLOG_ERROR("{} 新增 alarm {}条记录失败!语句长度:{}", pContext->GetIPAdressNew(), n, sql.GetLength());
- } while (false);
- HandleRecvPack(pContext, lpHead->protocol, lpHead->packno);
- return bRet;
- }
- void CLNHandle::CompareMoSync(jsonxx::Object obj, CMonitorObject *pObject)
- {
- using namespace jsonxx;
- auto id = obj.get<string>("id");
- auto name = obj.get<string>("name");
- auto type = obj.get<string>("type");
- auto up = obj.get<string>("up");
- ASSERT(up.compare(pObject->id) == 0);
- bool bHave = false;
- CMonitorObject* pObj = nullptr;
- for (auto& it : pObject->m_lstMo)
- {
- if (it->id.compare(id) == 0)
- {
- bHave = true;
- if (name.compare(it->name) != 0) //修改名称
- {
- CMonitorObjectMng::ReNameMO(id, name);
- it->name = name;
- }
- pObj = it;
- break;
- }
- }
- if (!bHave)
- {
- if (type.compare("mo.mp") == 0)
- CMonitorObjectMng::AddMO2(id, name, type, up);
- else
- CMonitorObjectMng::AddMO(id, name, type, up);
- pObj = new CMonitorObject;
- pObj->id = id;
- pObj->name = name;
- pObj->type = type;
- pObj->up = up;
- pObject->m_lstMo.push_back(pObj);
- }
-
- if (!obj.has<Array>("child"))
- return;
- auto child = obj.get<jsonxx::Array>("child");
- auto n = child.size();
- for (int i = 0; i < n; i++)
- {
- auto o = child.get<Object>(i);
- CompareMoSync(o, pObj);
- }
- }
- void CLNHandle::CompareMpSync(jsonxx::Object obj)
- {
- auto mo = obj.get<string>("mo");
- auto mp = obj.get<string>("mp");
- auto name1 = obj.get<string>("name1");
- auto name2 = obj.get<string>("name2");
- auto name3 = obj.get<string>("name3");
- auto direct1 = obj.get<string>("direct1");
- auto direct2 = obj.get<string>("direct2");
- auto station = obj.get<string>("station");
- auto mo_name = obj.get<string>("mo_name");
- auto mp_name = obj.get<string>("mp_name");
- //auto taskid = fmt::format("{}.{}.{}", station, mo, mp);
- auto taskid = fmt::format("{}.{}", mo, mp);
- auto pDevice = CDeviceMng::Instance()->Find(taskid);
- if (pDevice)
- {
- if (pDevice->name1.compare(name1) == 0 && pDevice->name2.compare(name2) == 0 && pDevice->name3.compare("name3")
- && pDevice->direct1.compare(direct1) == 0 && pDevice->direct2.compare(direct2) == 0)
- int ii = 0;
- else
- {
- //update
- CString sql = fmt::format("UPDATE rm_map SET [name1]='{}', [name2]='{}',[name3]='{}',[direct1]='{}',[direct2]='{}' WHERE [mo]='{}' AND [mp]='{}';",
- name1, name2, name3, direct1, direct2, mo, mp).c_str();
- if (!CDBConnectPool::Instance()->DBExecuteSQL(sql))
- {
- SPDLOG_ERROR("语句执行错误:{}", sql);
- }
- else
- {
- pDevice->name1 = name1;
- pDevice->name2 = name2;
- pDevice->name3 = name3;
- pDevice->direct1 = direct1;
- pDevice->direct2 = direct2;
- pDevice->mo_name = mo_name;
- pDevice->mp_name = mp_name;
- }
- }
- }
- else
- {
- //new
- CString sql = fmt::format("INSERT INTO [rm_map] ([mo],[mp],[name1],[name2],[name3],[direct1],[direct2]) VALUES('{}','{}','{}','{}','{}','{}','{}');",
- mo, mp, name1, name2, name3, direct1, direct2).c_str();
- if (!CDBConnectPool::Instance()->DBExecuteSQL(sql))
- {
- SPDLOG_ERROR("语句执行错误:{}", sql);
- }
- //else //即使数据库执行失败,内存还是更新, 以便后面实时数据
- {
- pDevice = new CDevice(taskid, station, mo, mp);
- CDeviceMng::Instance()->Insert(pDevice);
- pDevice->name1 = name1;
- pDevice->name2 = name2;
- pDevice->name3 = name3;
- pDevice->direct1 = direct1;
- pDevice->direct2 = direct2;
- pDevice->mo_name = mo_name;
- pDevice->mp_name = mp_name;
- }
- }
- }
- BOOL CLNHandle::HandleRecvPack(CLNContext* pContext, E_ZL_PROTOCAL protocal, uint32_t packno)
- {
- uint8_t bSend[100] = { 0 };
- auto len = CHjDataConver::conver_sendpack(bSend, nullptr, 0, 0, packno, protocal, OR_DATA_INFO(0, 1, 1, 3, OPT_TYPE::OPT_RECV));
- auto ret = pContext->Send(bSend, len) != SOCKET_ERROR;
- //if (ret)
- // SPDLOG_INFO("packno:{} send", packno);
- //else
- // SPDLOG_INFO("packno:{} send fail", packno);
- return ret;
- }
|