ResistAlarm.cpp 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238
  1. #include "stdafx.h"
  2. #include "ResistAlarm.h"
  3. #include "MonitorObject.h"
  4. #include "Device.h"
  5. #include <ODBC/DBConnectPool.h>
  6. #include <rapidjson/document.h>
  7. #include <rapidjson/writer.h>
  8. #include <rapidjson/stringbuffer.h>
  9. #include <Simplelog.h>
  10. #include <sstream>
  11. #include <gbk2utf8.h>
  12. #include "AppService.h"
  13. #include <yyjson.h>
  14. #include <315ClientManager.h>
  15. #include "SkylightMng.h"
  16. CResistAlarm::CResistAlarm()
  17. {
  18. }
  19. CResistAlarm::~CResistAlarm()
  20. {
  21. }
  22. CResistAlarmMng::CResistAlarmMng()
  23. {
  24. }
  25. CResistAlarmMng::~CResistAlarmMng()
  26. {
  27. }
  28. BOOL CResistAlarmMng::Start()
  29. {
  30. LoadAlarmSet();
  31. if (FALSE == LoadAlarmID()) return FALSE;
  32. LoadUnAck();
  33. m_bWork = true;
  34. m_pThreadAlarmSet = new thread(CResistAlarmMng::ThreadProcAlarmSet, (DWORD_PTR)this);
  35. if (m_pThreadAlarmSet == nullptr) return FALSE;
  36. m_pThreadDevice = new thread(CResistAlarmMng::ThreadProcDevice, (DWORD_PTR)this);
  37. if (m_pThreadDevice == nullptr) return FALSE;
  38. m_pThreadMove = new thread(CResistAlarmMng::ThreadProcMove, (DWORD_PTR)this);
  39. if (m_pThreadMove == nullptr) return FALSE;
  40. return TRUE;
  41. }
  42. void CResistAlarmMng::Stop()
  43. {
  44. m_bWork = false;
  45. if (m_pThreadAlarmSet)
  46. {
  47. m_pThreadAlarmSet->join();
  48. delete m_pThreadAlarmSet;
  49. m_pThreadAlarmSet = nullptr;
  50. }
  51. if (m_pThreadDevice)
  52. {
  53. m_pThreadDevice->join();
  54. delete m_pThreadDevice;
  55. m_pThreadDevice = nullptr;
  56. }
  57. if (m_pThreadMove)
  58. {
  59. m_pThreadMove->join();
  60. delete m_pThreadMove;
  61. m_pThreadMove = nullptr;
  62. }
  63. for (auto& it : m_alarm_set)
  64. {
  65. if (it.second)
  66. {
  67. if (it.second->type == eZL_ALARMTYPE::MAX_OVER_LIMIT)
  68. {
  69. delete (MAX_OVER_LIMIT_INFO*)it.second;
  70. it.second = nullptr;
  71. }
  72. else if (it.second->type == eZL_ALARMTYPE::FRICTION_OVER_LIMIT)
  73. {
  74. delete (FRICTION_OVER_LIMIT_INFO*)it.second;
  75. it.second = nullptr;
  76. }
  77. else if (it.second->type == eZL_ALARMTYPE::CONVERT_LIMIT)
  78. {
  79. delete (CONVERT_RESIST_OVER_LIMIT*)it.second;
  80. it.second = nullptr;
  81. }
  82. else if (it.second->type == eZL_ALARMTYPE::SUOBI_LOCK_LIMIT)
  83. {
  84. delete (SUOBI_OVER_LIMIT_INFO*)it.second;
  85. it.second = nullptr;
  86. }
  87. else
  88. {
  89. assert(0);
  90. }
  91. }
  92. }
  93. m_alarm_set.clear();
  94. for (auto& it : m_lstUnConfirmAlarm) delete it;
  95. m_lstUnConfirmAlarm.clear();
  96. }
  97. //BASE_INFO* CResistAlarmMng::Find(string momp, uint8_t no, uint8_t type)
  98. //{
  99. // stringstream ss;
  100. // ss << momp << '.' << to_string(no) << '.' << to_string(type);
  101. // auto it = m_alarm_set.find(ss.str());
  102. // if (it != m_alarm_set.end())
  103. // {
  104. // return it->second;
  105. // }
  106. // return nullptr;
  107. //}
  108. BASE_INFO* CResistAlarmMng::Find(string mo, string mp, uint8_t no, eZL_ALARMTYPE type)
  109. {
  110. stringstream ss;
  111. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string((uint8_t)type);
  112. auto it = m_alarm_set.find(ss.str());
  113. if (it != m_alarm_set.end())
  114. {
  115. return it->second;
  116. }
  117. return nullptr;
  118. }
  119. //bool CResistAlarmMng::Insert(const string& momp, uint8_t no, uint8_t type, BASE_INFO* info)
  120. //{
  121. // auto it = m_alarm_set.insert(make_pair(momp + '.' + to_string(no) + '.' + to_string(type), info));
  122. // return it.second;
  123. //}
  124. bool CResistAlarmMng::Insert(string mo, string mp, uint8_t no, uint8_t type, BASE_INFO* info)
  125. {
  126. auto it = m_alarm_set.insert(make_pair(mo + '.' + mp + '.' + to_string(no) + '.' + to_string(type), info));
  127. return it.second;
  128. }
  129. //bool CResistAlarmMng::ConfirmAlarm(string mo, string mp, uint8_t no, uint8_t type, const SYSTEMTIME& st)
  130. //{
  131. // lock_guard<mutex> lock(m_mtxAlarm);
  132. // auto it = m_lstUnConfirmAlarm.begin();
  133. // for (it; it != m_lstUnConfirmAlarm.end(); ++it)
  134. // {
  135. // const auto& pInfo = *it;
  136. // if (pInfo->mo.compare(mo) == 0 && pInfo->mp.compare(mp) == 0 && pInfo->no == no && pInfo->type == type && pInfo->time.wYear == st.wYear &&
  137. // pInfo->time.wMonth == st.wMonth && pInfo->time.wDay == st.wDay && pInfo->time.wHour == st.wHour && pInfo->time.wMinute == st.wMinute)
  138. // {
  139. // m_lstUnConfirmAlarm.erase(it);
  140. // return true;
  141. // }
  142. // }
  143. // return false;
  144. //}
  145. bool CResistAlarmMng::AckAlarm(int alarm_id, string& name, CTime& time)
  146. {
  147. lock_guard<mutex> lock(m_mtxAlarm);
  148. auto it = m_lstUnConfirmAlarm.begin();
  149. for (it; it != m_lstUnConfirmAlarm.end(); ++it)
  150. {
  151. auto& pInfo = *it;
  152. if (alarm_id == pInfo->id)
  153. {
  154. pInfo->ack_result = 1;
  155. pInfo->ack_name = name;
  156. pInfo->ack_time = time;
  157. //g_p315ClientManager->GetTcpClient()->SendAlarmData(pInfo->no, 2, CTime(pInfo->time).GetTime(), time.GetTime(), (WORD)pInfo->type, pInfo->val, 0, 0);
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. bool CResistAlarmMng::HandleAlarm(int alarm_id)
  164. {
  165. lock_guard<mutex> lock(m_mtxAlarm);
  166. auto it = m_lstUnConfirmAlarm.begin();
  167. for (it; it != m_lstUnConfirmAlarm.end(); ++it)
  168. {
  169. const auto& pInfo = *it;
  170. if (pInfo->id == alarm_id && pInfo->ack_result == 1 && pInfo->recoveryTime.wYear > 2000)
  171. {
  172. m_lstUnConfirmAlarm.erase(it);
  173. return true;
  174. }
  175. }
  176. return true;
  177. }
  178. void CResistAlarmMng::GeneralAlarm(string mo, string mp, uint8_t no, eZL_ALARMTYPE type, uint8_t level, SYSTEMTIME& tAlarm)
  179. {
  180. ALARM_INFO* pAlarmInfo = nullptr;
  181. bool bNew = false;
  182. {
  183. std::lock_guard<mutex> lock(m_mtxAlarm);
  184. for (const auto& alarm : m_lstUnConfirmAlarm)
  185. {
  186. if (alarm->type == type && alarm->mo.compare(mo) == 0 && alarm->recoveryTime.wYear < 2000
  187. && alarm->mp.compare(mp) == 0 && no == alarm->no) //跟上次报警时间超过1小时,则算新报警
  188. {
  189. pAlarmInfo = alarm;
  190. break;
  191. }
  192. }
  193. }
  194. if (pAlarmInfo == nullptr)
  195. {
  196. bNew = true;
  197. pAlarmInfo = new ALARM_INFO;
  198. pAlarmInfo->event_id = hjfunc_GetGUID();
  199. pAlarmInfo->id = ++m_nAlarmID;
  200. pAlarmInfo->level = 1;
  201. pAlarmInfo->mo = mo;
  202. pAlarmInfo->mp = mp;
  203. pAlarmInfo->no = no;
  204. pAlarmInfo->time = tAlarm;
  205. pAlarmInfo->type = type;
  206. pAlarmInfo->val = 0;
  207. char szInfo[200];
  208. sprintf_s(szInfo, sizeof(szInfo), "传感器异常时间:%04d-%02d-%02d %02d:%02d:%02d",
  209. tAlarm.wYear, tAlarm.wMonth, tAlarm.wDay, tAlarm.wHour, tAlarm.wMinute, tAlarm.wSecond);
  210. pAlarmInfo->desc = szInfo;
  211. lock_guard<mutex> lock(m_mtxAlarm);
  212. m_lstUnConfirmAlarm.push_back(pAlarmInfo);
  213. }
  214. //send
  215. if (bNew) //不再推送
  216. {
  217. rapidjson::StringBuffer buffer;
  218. auto ret = AlarmInfo2Pack(pAlarmInfo, buffer);
  219. const char* output = buffer.GetString();
  220. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  221. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  222. }
  223. //save
  224. if (bNew)
  225. {
  226. CTime ctAlarmTime(tAlarm);
  227. CString sql;
  228. sql.Format("INSERT INTO [rm_alarm]([ID],[mo],[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id],posi,loworhigh,referval,[sunroof]) "\
  229. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  230. pAlarmInfo->id, pAlarmInfo->mo.c_str(), pAlarmInfo->mp.c_str(), pAlarmInfo->no, (int)pAlarmInfo->type,
  231. (LPCSTR)(ctAlarmTime.Format("%Y-%m-%d %H:%M:%S")), pAlarmInfo->level, pAlarmInfo->desc.c_str(), pAlarmInfo->suggest.c_str(), pAlarmInfo->val,
  232. pAlarmInfo->event_id.c_str(), pAlarmInfo->rel_id.c_str(), uint8_t(pAlarmInfo->posi), uint8_t(pAlarmInfo->loworhigh), pAlarmInfo->refer_val, pAlarmInfo->sunroof);
  233. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  234. CSimpleLog::Error("执行语句失败" + sql);
  235. else
  236. {
  237. uint8_t* pack = nullptr; int len = 0;
  238. auto packno = CResistAlarmMng::GeneralNewAlarmData(pAlarmInfo, &pack, &len);
  239. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  240. //g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->no, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, (WORD)pAlarmInfo->type, pAlarmInfo->val, 0, 0);
  241. delete[] pack;
  242. pack = nullptr;
  243. }
  244. }
  245. }
  246. //报警恢复, 报警恢复逻辑与受理确认逻辑分开
  247. void CResistAlarmMng::RecoverAlarm(const string mo, const string mp, const uint8_t no, const eZL_ALARMTYPE type, const uint8_t level, const SYSTEMTIME& tAlarm)
  248. {
  249. vector<uint32_t> vctAlarmID;
  250. vector<string> vctEventID;
  251. //ALARM_INFO tmpalarm;
  252. {
  253. std::lock_guard<mutex> lock(m_mtxAlarm);
  254. for (auto it = m_lstUnConfirmAlarm.begin(); it != m_lstUnConfirmAlarm.end();)
  255. {
  256. auto pAlarm = *it;
  257. if (pAlarm->type == type && pAlarm->mo.compare(mo) == 0 && pAlarm->mp.compare(mp) == 0 && no == pAlarm->no && level == pAlarm->level
  258. && pAlarm->recoveryTime.wYear < 2000)
  259. {
  260. vctAlarmID.emplace_back(pAlarm->id);
  261. vctEventID.emplace_back(pAlarm->event_id);
  262. //CopyMemory(&tmpalarm, *it, sizeof(tmpalarm)); 有string 不能拷贝
  263. //tmpalarm = *pAlarm;
  264. if (pAlarm->ack_result == 1)//如果已确认则可以删除
  265. {
  266. it = m_lstUnConfirmAlarm.erase(it);
  267. continue;
  268. }
  269. else
  270. memcpy(&pAlarm->recoveryTime, &tAlarm, sizeof(SYSTEMTIME));
  271. }
  272. it++;
  273. }
  274. }
  275. if (vctAlarmID.size() == 0)
  276. return;
  277. CTime ctAlarm(tAlarm);
  278. string recovery_time = ctAlarm.Format("%Y-%m-%d %H:%M:%S");
  279. //g_p315ClientManager->GetTcpClient()->SendAlarmData(tmpalarm.no, 2, CTime(tmpalarm.time).GetTime(), ctAlarm.GetTime(), (WORD)tmpalarm.type, tmpalarm.val, 0, 0);
  280. //更新数据库
  281. {
  282. CString sql;
  283. for (auto& it : vctAlarmID)
  284. {
  285. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), it);
  286. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  287. }
  288. //for (auto& it : vctEventID)
  289. //{
  290. // sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE [event_id] = '%s';", recovery_time.c_str(), it);
  291. // CDBConnectPool::Instance()->DBExecuteSQL(sql);
  292. //}
  293. }
  294. //恢复暂时不通知
  295. //auto doc = yyjson_mut_doc_new(nullptr);
  296. //auto root = yyjson_mut_obj(doc);
  297. //yyjson_mut_doc_set_root(doc, root);
  298. //yyjson_mut_obj_add_str(doc, root, "cmd", "alm_ack");
  299. //yyjson_mut_obj_add_uint(doc, root, "alarm_id", alarm_id);
  300. //yyjson_mut_obj_add_strcpy(doc, root, "ack_time", recovery_time.c_str());
  301. //yyjson_mut_obj_add_strcpy(doc, root, "ack_name", ANSItoUTF8("系统").c_str());
  302. //size_t len;
  303. //auto json = yyjson_mut_write(doc, 0, &len);
  304. //if (json)
  305. //{
  306. // CAppService::Instance()->GetMgServer()->SendToAllClient(json, len);
  307. // free(json);
  308. //}
  309. //yyjson_mut_doc_free(doc);
  310. }
  311. eZL_MP_STAT CResistAlarmMng::GetAlarmStat(string& mo, string& mp, SYSTEMTIME& stAlarm)
  312. {
  313. eZL_MP_STAT stat = eZL_MP_STAT::MP_STAT_UNKNOW;
  314. lock_guard<mutex> lock(m_mtxAlarm);
  315. auto it = m_lstUnConfirmAlarm.rbegin();
  316. for (it; it != m_lstUnConfirmAlarm.rend(); ++it)
  317. {
  318. const auto& pInfo = *it;
  319. if (pInfo->ack_result) continue;//受理了暂时放过
  320. if (pInfo->mo.compare(mo) == 0 && pInfo->mp.compare(mp) == 0 && pInfo->recoveryTime.wYear < 2000)
  321. {
  322. if (pInfo->type == eZL_ALARMTYPE::EQUIP_OFFLINE || pInfo->type == eZL_ALARMTYPE::SENSOR_ABNORMAL)
  323. {
  324. stat = eZL_MP_STAT::MP_STAT_OFFLINE_GRAY;
  325. break;
  326. }
  327. else if (pInfo->level == 1)
  328. {
  329. stAlarm = pInfo->time;
  330. stat = eZL_MP_STAT::MP_STAT_ALARM_RED;
  331. break;
  332. }
  333. else if (pInfo->level == 0)
  334. {
  335. stAlarm = pInfo->time;
  336. stat = eZL_MP_STAT::MP_STAT_WARN_ORANGE;
  337. break;
  338. }
  339. }
  340. }
  341. return stat;
  342. }
  343. uint32_t CResistAlarmMng::GeneralAlarmSet(uint8_t** pack, int* len)
  344. {
  345. auto doc = yyjson_mut_doc_new(nullptr);
  346. auto root = yyjson_mut_arr(doc);
  347. yyjson_mut_doc_set_root(doc, root);
  348. for (const auto& it : m_alarm_set)
  349. {
  350. auto obj = yyjson_mut_obj(doc);
  351. yyjson_mut_arr_add_val(root, obj);
  352. auto ss = it.second;
  353. auto kk = it.first;
  354. int nPos = kk.find('.');
  355. int nPos2 = kk.find('.', nPos + 1);
  356. string mo = kk.substr(0, nPos);
  357. string mp = kk.substr(nPos + 1, nPos2 - nPos - 1);
  358. string momp = kk.substr(0, nPos2);
  359. string mmomp_name, station;
  360. CMonitorObjectMng::Instance()->GetStationNameByMomP(momp, station, mmomp_name);
  361. yyjson_mut_obj_add_strcpy(doc, obj, "mo", mo.c_str());
  362. yyjson_mut_obj_add_strcpy(doc, obj, "mp", mp.c_str());
  363. yyjson_mut_obj_add_strcpy(doc, obj, "station", station.c_str());
  364. yyjson_mut_obj_add_int(doc, obj, "no", ss->no);
  365. yyjson_mut_obj_add_int(doc, obj, "type", uint8_t(ss->type));
  366. switch (ss->type)
  367. {
  368. case eZL_ALARMTYPE::MAX_OVER_LIMIT:
  369. {
  370. auto conf = yyjson_mut_arr(doc);
  371. yyjson_mut_obj_add_val(doc, obj, "conf", conf);
  372. {
  373. auto object = yyjson_mut_obj(doc);
  374. yyjson_mut_arr_add_val(conf, object);
  375. yyjson_mut_obj_add_str(doc, object, "name", "enable");
  376. yyjson_mut_obj_add_str(doc, object, "val", ss->enable ? "true" : "false");
  377. }
  378. auto pInfo = (MAX_OVER_LIMIT_INFO*)ss;
  379. {
  380. auto object = yyjson_mut_obj(doc);
  381. yyjson_mut_arr_add_val(conf, object);
  382. if (ss->no == 2)
  383. yyjson_mut_obj_add_str(doc, object, "name", "d_alarm_high_limit");
  384. else
  385. yyjson_mut_obj_add_str(doc, object, "name", "lock_alarm_high_limit");
  386. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->alarm_high_limit).c_str());
  387. }
  388. {
  389. auto object = yyjson_mut_obj(doc);
  390. yyjson_mut_arr_add_val(conf, object);
  391. if (ss->no == 2)
  392. yyjson_mut_obj_add_str(doc, object, "name", "d_warn_high_limit");
  393. else
  394. yyjson_mut_obj_add_str(doc, object, "name", "lock_warn_high_limit");
  395. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->warn_high_limit).c_str());
  396. }
  397. {
  398. auto object = yyjson_mut_obj(doc);
  399. yyjson_mut_arr_add_val(conf, object);
  400. if (ss->no == 2)
  401. yyjson_mut_obj_add_str(doc, object, "name", "f_alarm_high_limit");
  402. else
  403. yyjson_mut_obj_add_str(doc, object, "name", "keep_alarm_high_limit");
  404. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->f_alarm_high_limit).c_str());
  405. }
  406. {
  407. auto object = yyjson_mut_obj(doc);
  408. yyjson_mut_arr_add_val(conf, object);
  409. if (ss->no == 2)
  410. yyjson_mut_obj_add_str(doc, object, "name", "keep_warn_high_limit");
  411. else
  412. yyjson_mut_obj_add_str(doc, object, "name", "f_warn_high_limit");
  413. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->f_warn_high_limit).c_str());
  414. }
  415. }
  416. break;
  417. case eZL_ALARMTYPE::FRICTION_OVER_LIMIT:
  418. {
  419. auto conf = yyjson_mut_arr(doc);
  420. yyjson_mut_obj_add_val(doc, obj, "conf", conf);
  421. {
  422. auto object = yyjson_mut_obj(doc);
  423. yyjson_mut_arr_add_val(conf, object);
  424. yyjson_mut_obj_add_str(doc, object, "name", "enable");
  425. yyjson_mut_obj_add_str(doc, object, "val", ss->enable ? "true" : "false");
  426. }
  427. auto pInfo = (FRICTION_OVER_LIMIT_INFO*)ss;
  428. {
  429. auto object = yyjson_mut_obj(doc);
  430. yyjson_mut_arr_add_val(conf, object);
  431. yyjson_mut_obj_add_str(doc, object, "name", "up_alarm_low_limit");
  432. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->up_alarm_low_limit).c_str());
  433. }
  434. {
  435. auto object = yyjson_mut_obj(doc);
  436. yyjson_mut_arr_add_val(conf, object);
  437. yyjson_mut_obj_add_str(doc, object, "name", "up_warn_low_limit");
  438. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->up_warn_low_limit).c_str());
  439. }
  440. {
  441. auto object = yyjson_mut_obj(doc);
  442. yyjson_mut_arr_add_val(conf, object);
  443. yyjson_mut_obj_add_str(doc, object, "name", "dw_alarm_high_limit");
  444. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->dw_alarm_high_limit).c_str());
  445. }
  446. {
  447. auto object = yyjson_mut_obj(doc);
  448. yyjson_mut_arr_add_val(conf, object);
  449. yyjson_mut_obj_add_str(doc, object, "name", "dw_warn_high_limit");
  450. yyjson_mut_obj_add_strcpy(doc, object, "val", to_string(pInfo->dw_warn_high_limit).c_str());
  451. }
  452. }
  453. break;
  454. case eZL_ALARMTYPE::SUOBI_LOCK_LIMIT:
  455. {
  456. auto conf = yyjson_mut_obj(doc);
  457. yyjson_mut_obj_add_val(doc, obj, "conf", conf);
  458. auto pInfo = (SUOBI_OVER_LIMIT_INFO*)ss;
  459. yyjson_mut_obj_add_bool(doc, conf, "enable", pInfo->enable);
  460. yyjson_mut_obj_add_int(doc, conf, "alarm_low_limit", pInfo->alarm_low_limit);
  461. yyjson_mut_obj_add_int(doc, conf, "warn_low_limit", pInfo->warn_low_limit);
  462. yyjson_mut_obj_add_int(doc, conf, "alarm_high_limit", pInfo->alarm_high_limit);
  463. yyjson_mut_obj_add_int(doc, conf, "warn_high_limit", pInfo->warn_high_limit);
  464. }
  465. break;
  466. case eZL_ALARMTYPE::CONVERT_LIMIT:
  467. {
  468. auto conf = yyjson_mut_obj(doc);
  469. yyjson_mut_obj_add_val(doc, obj, "conf", conf);
  470. auto pInfo = (CONVERT_RESIST_OVER_LIMIT*)ss;
  471. yyjson_mut_obj_add_bool(doc, conf, "enable", pInfo->enable);
  472. yyjson_mut_obj_add_int(doc, conf, "dw_alarm_low_limit", pInfo->dw_alarm_low_limit);
  473. yyjson_mut_obj_add_int(doc, conf, "dw_warn_low_limit", pInfo->dw_warn_low_limit);
  474. yyjson_mut_obj_add_int(doc, conf, "up_alarm_high_limit", pInfo->up_alarm_high_limit);
  475. yyjson_mut_obj_add_int(doc, conf, "up_warn_high_limit", pInfo->up_warn_high_limit);
  476. }
  477. break;
  478. default:
  479. ASSERT(0);
  480. break;
  481. }
  482. }
  483. size_t json_len;
  484. auto json = yyjson_mut_write(doc, 0, &json_len);
  485. yyjson_mut_doc_free(doc);
  486. *len = json_len + sizeof(HJDATAHEAD2) + 2;
  487. *pack = new uint8_t[*len];
  488. auto no = CAppService::Instance()->GetPackNo();
  489. CHjDataConver::conver_sendpack(*pack, (LPBYTE)json, json_len, 0, no, E_ZL_PROTOCAL::ZL_ALARM_SET,
  490. OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_SYNC));
  491. free(json);
  492. return no;
  493. }
  494. uint32_t CResistAlarmMng::GeneralUnAck(uint8_t** pack, int* len)
  495. {
  496. auto doc = yyjson_mut_doc_new(nullptr);
  497. auto root = yyjson_mut_arr(doc);
  498. yyjson_mut_doc_set_root(doc, root);
  499. {
  500. //查询数据库近一天的数据
  501. lock_guard<mutex> lock(m_mtxAlarm);
  502. auto it = m_lstUnConfirmAlarm.begin();
  503. for (it; it != m_lstUnConfirmAlarm.end(); ++it)
  504. {
  505. auto& pInfo = *it;
  506. auto obj = yyjson_mut_obj(doc);
  507. yyjson_mut_arr_add_val(root, obj);
  508. yyjson_mut_obj_add_uint(doc, obj, "id", pInfo->id);
  509. yyjson_mut_obj_add_strcpy(doc, obj, "mo", pInfo->mo.c_str());
  510. yyjson_mut_obj_add_strcpy(doc, obj, "mp", pInfo->mp.c_str());
  511. yyjson_mut_obj_add_int(doc, obj, "no", pInfo->no);
  512. yyjson_mut_obj_add_int(doc, obj, "type", int(pInfo->type));
  513. yyjson_mut_obj_add_int(doc, obj, "level", pInfo->level);
  514. yyjson_mut_obj_add_strcpy(doc, obj, "time", fmt::format("{}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}",
  515. pInfo->time.wYear, pInfo->time.wMonth, pInfo->time.wDay, pInfo->time.wHour, pInfo->time.wMinute, pInfo->time.wSecond).c_str());
  516. yyjson_mut_obj_add_strcpy(doc, obj, "desc", pInfo->desc.c_str());
  517. yyjson_mut_obj_add_strcpy(doc, obj, "desc", pInfo->suggest.c_str());
  518. yyjson_mut_obj_add_int(doc, obj, "val", pInfo->val);
  519. yyjson_mut_obj_add_int(doc, obj, "ack_result", pInfo->ack_result);
  520. yyjson_mut_obj_add_strcpy(doc, obj, "ack_name", pInfo->ack_name.c_str());
  521. yyjson_mut_obj_add_int(doc, obj, "ack_time", pInfo->ack_time.GetTime());
  522. yyjson_mut_obj_add_strcpy(doc, obj, "event_id", pInfo->event_id.c_str());
  523. yyjson_mut_obj_add_strcpy(doc, obj, "rel_id", pInfo->rel_id.c_str());
  524. yyjson_mut_obj_add_strcpy(doc, obj, "recoverytime", fmt::format("{}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}",
  525. pInfo->recoveryTime.wYear, pInfo->recoveryTime.wMonth, pInfo->recoveryTime.wDay, pInfo->recoveryTime.wHour, pInfo->recoveryTime.wMinute, pInfo->recoveryTime.wSecond).c_str());
  526. }
  527. }
  528. size_t json_len;
  529. auto json = yyjson_mut_write(doc, 0, &json_len);
  530. yyjson_mut_doc_free(doc);
  531. *len = json_len + sizeof(HJDATAHEAD2) + 2;
  532. *pack = new uint8_t[*len];
  533. auto no = CAppService::Instance()->GetPackNo();
  534. CHjDataConver::conver_sendpack(*pack, (LPBYTE)json, json_len, 0, no, E_ZL_PROTOCAL::ZL_ALARM_UNACK,
  535. OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_SYNC));
  536. free(json);
  537. return no;
  538. }
  539. uint32_t CResistAlarmMng::GeneralNewAlarmData(ALARM_INFO* pInfo, uint8_t** pack, int* len)
  540. {
  541. auto doc = yyjson_mut_doc_new(nullptr);
  542. auto root = yyjson_mut_arr(doc);
  543. yyjson_mut_doc_set_root(doc, root);
  544. auto obj = yyjson_mut_obj(doc);
  545. yyjson_mut_arr_add_val(root, obj);
  546. yyjson_mut_obj_add_uint(doc, obj, "id", pInfo->id);
  547. yyjson_mut_obj_add_strcpy(doc, obj, "mo", pInfo->mo.c_str());
  548. yyjson_mut_obj_add_strcpy(doc, obj, "mp", pInfo->mp.c_str());
  549. yyjson_mut_obj_add_int(doc, obj, "no", pInfo->no);
  550. yyjson_mut_obj_add_int(doc, obj, "type", int(pInfo->type));
  551. yyjson_mut_obj_add_int(doc, obj, "level", pInfo->level);
  552. yyjson_mut_obj_add_strcpy(doc, obj, "time", fmt::format("{}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}",
  553. pInfo->time.wYear, pInfo->time.wMonth, pInfo->time.wDay, pInfo->time.wHour, pInfo->time.wMinute, pInfo->time.wSecond).c_str());
  554. yyjson_mut_obj_add_strcpy(doc, obj, "desc", pInfo->desc.c_str());
  555. yyjson_mut_obj_add_strcpy(doc, obj, "suggest", pInfo->suggest.c_str());
  556. yyjson_mut_obj_add_int(doc, obj, "val", pInfo->val);
  557. yyjson_mut_obj_add_int(doc, obj, "ack_result", pInfo->ack_result);
  558. yyjson_mut_obj_add_strcpy(doc, obj, "ack_name", pInfo->ack_name.c_str());
  559. yyjson_mut_obj_add_int(doc, obj, "ack_time", pInfo->ack_time.GetTime());
  560. yyjson_mut_obj_add_strcpy(doc, obj, "event_id", pInfo->event_id.c_str());
  561. yyjson_mut_obj_add_strcpy(doc, obj, "rel_id", pInfo->rel_id.c_str());
  562. yyjson_mut_obj_add_strcpy(doc, obj, "recoverytime", fmt::format("{}-{:0>2}-{:0>2} {:0>2}:{:0>2}:{:0>2}",
  563. pInfo->recoveryTime.wYear, pInfo->recoveryTime.wMonth, pInfo->recoveryTime.wDay, pInfo->recoveryTime.wHour, pInfo->recoveryTime.wMinute, pInfo->recoveryTime.wSecond).c_str());
  564. size_t json_len;
  565. auto json = yyjson_mut_write(doc, 0, &json_len);
  566. yyjson_mut_doc_free(doc);
  567. *len = json_len + sizeof(HJDATAHEAD2) + 2;
  568. *pack = new uint8_t[*len];
  569. auto no = CAppService::Instance()->GetPackNo();
  570. CHjDataConver::conver_sendpack(*pack, (LPBYTE)json, json_len, 0, no, E_ZL_PROTOCAL::ZL_ALARM_DATA,
  571. OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_ADD));
  572. free(json);
  573. return no;
  574. }
  575. bool CResistAlarmMng::AlarmInfo2Pack(const ALARM_INFO* pAlarmInfo, rapidjson::StringBuffer& buffer, bool bGb2312 /*= false*/)
  576. {
  577. char szInfo[200];
  578. char szUtf[200];
  579. using namespace rapidjson;
  580. Writer<StringBuffer> writer(buffer);
  581. writer.StartObject();
  582. writer.Key("cmd");
  583. writer.String("new_alarm");
  584. writer.Key("type");
  585. //if (pAlarmInfo->type == MAX_OVER_LIMIT) writer.String("monitor.alarm.max_over_limit");
  586. //else if (pAlarmInfo->type == EQUIP_OFFLINE) writer.String("monitor.alarm.equip_offline");
  587. //else if (pAlarmInfo->type == SENSOR_ABNORMAL) writer.String("monitor.alarm.sensor_abnormal");
  588. //else writer.String("");
  589. writer.Int((int)pAlarmInfo->type);
  590. writer.Key("tag");
  591. writer.String((pAlarmInfo->mo + '.' + pAlarmInfo->mp + '.' + to_string(pAlarmInfo->no + 1) + "#").c_str());
  592. string up, momp_name, up_name;
  593. string momp = pAlarmInfo->mo + "." + pAlarmInfo->mp;
  594. CMonitorObjectMng::Instance()->GetStationNameByMomP(momp, up, momp_name);
  595. auto pObject = CMonitorObjectMng::Instance()->GetTreeByID(up);
  596. if (pObject) up_name = pObject->name;
  597. string name1, name2, name3;
  598. CMonitorObjectMng::Instance()->GetNameByMoMp(momp, name1, name2, name3);
  599. if (pAlarmInfo->no == 0 && name1.length() == 0) name1 = "1号测力点";
  600. else if (pAlarmInfo->no == 1 && name2.length() == 0) name2 = "2号测力点";
  601. else if (pAlarmInfo->no == 2 && name3.length() == 0) name3 = "3号测力点";
  602. sprintf_s(szInfo, 200, "%s.%s.%s.%s%s", up_name.c_str(), momp_name.c_str(),
  603. pAlarmInfo->no == 0 ? name1.c_str() : pAlarmInfo->no == 1 ? name2.c_str() : pAlarmInfo->no == 2 ? name3.c_str() : "",
  604. GetAlarmName(pAlarmInfo->type).c_str(),
  605. pAlarmInfo->level == 1 ? "报警" : "预警"
  606. );
  607. gbk2utf8(szUtf, sizeof(szUtf), szInfo);
  608. writer.Key("title");
  609. writer.String(szUtf);
  610. gbk2utf8(szUtf, sizeof(szUtf), up.c_str());
  611. writer.Key("up");
  612. writer.String(szUtf);
  613. gbk2utf8(szUtf, sizeof(szUtf), up_name.c_str());
  614. writer.Key("up_name");
  615. writer.String(szUtf);
  616. writer.Key("alarm_id");
  617. writer.Uint(pAlarmInfo->id);
  618. writer.Key("event_id");
  619. writer.String(pAlarmInfo->event_id.c_str());
  620. writer.Key("rel_id");
  621. writer.String(pAlarmInfo->rel_id.c_str());
  622. writer.Key("sunroof");
  623. writer.Int(pAlarmInfo->sunroof);
  624. writer.Key("level");
  625. if (pAlarmInfo->level)
  626. writer.String("alarm");
  627. else
  628. writer.String("warn");
  629. writer.Key("occur_time");
  630. sprintf_s(szInfo, sizeof(szInfo), "%04d-%02d-%02d %02d:%02d:%02d.%03d",
  631. pAlarmInfo->time.wYear, pAlarmInfo->time.wMonth, pAlarmInfo->time.wDay,
  632. pAlarmInfo->time.wHour, pAlarmInfo->time.wMinute, pAlarmInfo->time.wSecond, pAlarmInfo->time.wMilliseconds);
  633. writer.String(szInfo);
  634. writer.Key("val");
  635. if (pAlarmInfo->type == eZL_ALARMTYPE::EQUIP_OFFLINE || pAlarmInfo->type == eZL_ALARMTYPE::SENSOR_ABNORMAL)
  636. writer.String("-");
  637. else
  638. writer.String(to_string(pAlarmInfo->val).c_str());
  639. writer.String("desc");
  640. gbk2utf8(szInfo, sizeof(szInfo), pAlarmInfo->desc.c_str());
  641. writer.String(szInfo);
  642. writer.Key("suggest");
  643. writer.String("");
  644. if (pAlarmInfo->ack_result)
  645. {
  646. //已受理的报警
  647. writer.String("ack_result");
  648. writer.Int(pAlarmInfo->ack_result);
  649. writer.Key("ack_name");
  650. writer.String(ANSItoUTF8(pAlarmInfo->ack_name).c_str());
  651. writer.Key("ack_time");
  652. writer.String(pAlarmInfo->ack_time.Format("%Y-%m-%d %H:%M:%S"));
  653. }
  654. if (pAlarmInfo->recoveryTime.wYear != 1900)
  655. {
  656. writer.Key("recoverytime");
  657. sprintf_s(szInfo, sizeof(szInfo), "%04d-%02d-%02d %02d:%02d:%02d.%03d",
  658. pAlarmInfo->recoveryTime.wYear, pAlarmInfo->recoveryTime.wMonth, pAlarmInfo->recoveryTime.wDay,
  659. pAlarmInfo->recoveryTime.wHour, pAlarmInfo->recoveryTime.wMinute, pAlarmInfo->recoveryTime.wSecond, pAlarmInfo->recoveryTime.wMilliseconds);
  660. writer.String(szInfo);
  661. }
  662. writer.EndObject();
  663. return true;
  664. }
  665. CResistAlarmMng CResistAlarmMng::obj;
  666. void CResistAlarmMng::InsertToDBByMove(string mo, string mp, time_t show_time, time_t start_time, time_t end_time,
  667. int curr_val, int show_val, uint8_t idx, uint8_t posi, uint8_t type,
  668. string mark)
  669. {
  670. if (show_time == 0) return;
  671. CTime ctShowTime(show_time / 1000);
  672. uint8_t sunroof = CSkylightMng::GetInstance()->IsSkylight(&ctShowTime); //2024年6月9日 添加天窗
  673. string table_name = fmt::format("rm_move_{:0>4}{:0>2}", ctShowTime.GetYear(), ctShowTime.GetMonth());
  674. auto strShowTime = (LPCSTR)ctShowTime.Format("%Y-%m-%d %H:%M:%S.") + to_string(show_time % 1000);
  675. auto strStartTime = start_time ? CTime(start_time).Format("%Y-%m-%d %H:%M:%S") : "";
  676. auto strEndTime = end_time ? CTime(end_time).Format("%Y-%m-%d %H:%M:%S") : "";
  677. CString sql = fmt::format("INSERT INTO {} (mo,mp,show_time,start_time,end_time,curr_val,show_val,idx,posi,[type],sunroof,mark) values ('{}', '{}', '{}', '{}','{}', {}, {}, {}, {}, {}, {}, '{}');",
  678. table_name, mo, mp, strShowTime, strStartTime, strEndTime, curr_val, show_val, idx, posi, type, sunroof, mark).c_str();
  679. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  680. CSimpleLog::Error(sql);
  681. //上送
  682. //组装数据
  683. auto doc = yyjson_mut_doc_new(nullptr);
  684. auto root = yyjson_mut_arr(doc);
  685. yyjson_mut_doc_set_root(doc, root);
  686. //添加数据
  687. auto obj = yyjson_mut_obj(doc);
  688. yyjson_mut_arr_add_val(root, obj);
  689. yyjson_mut_obj_add_strcpy(doc, obj, "mo", mo.c_str());
  690. yyjson_mut_obj_add_strcpy(doc, obj, "mp", mp.c_str());
  691. yyjson_mut_obj_add_strcpy(doc, obj, "show_time", strShowTime.c_str());
  692. yyjson_mut_obj_add_strcpy(doc, obj, "start_time", strStartTime);
  693. yyjson_mut_obj_add_strcpy(doc, obj, "end_time", strEndTime);
  694. yyjson_mut_obj_add_strcpy(doc, obj, "mark", mark.c_str());
  695. yyjson_mut_obj_add_int(doc, obj, "curr_val", curr_val);
  696. yyjson_mut_obj_add_int(doc, obj, "show_val", show_val);
  697. yyjson_mut_obj_add_int(doc, obj, "idx", idx);
  698. yyjson_mut_obj_add_int(doc, obj, "posi", posi);
  699. yyjson_mut_obj_add_int(doc, obj, "sunroof", sunroof);
  700. size_t json_len;
  701. auto json = yyjson_mut_write(doc, 0, &json_len);
  702. auto send_len = json_len + sizeof(HJDATAHEAD2) + 2;
  703. auto pack = new uint8_t[send_len];
  704. auto packno = CAppService::Instance()->GetPackNo();
  705. CHjDataConver::conver_sendpack(pack, (LPBYTE)json, json_len, 0, packno, E_ZL_PROTOCAL::ZL_MOVE,
  706. OR_DATA_INFO(0, 0, 1, 2, OPT_TYPE::OPT_ADD));
  707. CSuperManager::Instance()->SendPack(pack, send_len, packno, E_ZL_PROTOCAL::ZL_MOVE, true);
  708. delete[] pack;
  709. pack = nullptr;
  710. free(json);
  711. yyjson_mut_doc_free(doc);
  712. }
  713. //判断锁闭力是否超限
  714. void CResistAlarmMng::JudgeAlarm(CResistAlarmMng* pService, SUOBI_OVER_LIMIT_INFO* pSuobiOverInfo,
  715. time_t show_time, int show_val, eSuoBiPosi posi, string mo, string mp, uint32_t zzjno)
  716. {
  717. if (pSuobiOverInfo == nullptr || pSuobiOverInfo->enable == false) return;
  718. bool bNew = false;
  719. ALARM_INFO* pAlarmInfo = nullptr;
  720. bool bAlarm = false, bWarn = false; int alarm_value = 0, alarm_refer; time_t alarm_time = show_time;
  721. CTime ctAlarmTime = CTime(alarm_time / 1000);
  722. eLowHigh loworhigh = eLowHigh::LH_UNKNOWN; //0:最小值 1:最大值
  723. {
  724. if (show_val < pSuobiOverInfo->alarm_low_limit)
  725. {
  726. bAlarm = true;
  727. alarm_refer = pSuobiOverInfo->alarm_low_limit;
  728. alarm_value = show_val;
  729. loworhigh = eLowHigh::LH_LOW;
  730. }
  731. else if (show_val < pSuobiOverInfo->warn_low_limit)
  732. {
  733. bWarn = true;
  734. alarm_refer = pSuobiOverInfo->warn_low_limit;
  735. alarm_value = show_val;
  736. loworhigh = eLowHigh::LH_LOW;
  737. }
  738. if (show_val > pSuobiOverInfo->alarm_high_limit)
  739. {
  740. bAlarm = true;
  741. alarm_refer = pSuobiOverInfo->alarm_high_limit;
  742. alarm_value = show_val;
  743. loworhigh = eLowHigh::LH_HIGH;
  744. }
  745. else if (show_val > pSuobiOverInfo->warn_high_limit)
  746. {
  747. bWarn = true;
  748. alarm_refer = pSuobiOverInfo->warn_high_limit;
  749. alarm_value = show_val;
  750. loworhigh = eLowHigh::LH_HIGH;
  751. }
  752. if (bAlarm == false && bWarn == false)
  753. {
  754. {
  755. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  756. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  757. {
  758. auto pAlarm = *it;
  759. if (pAlarm->no == (uint8_t)posi && pAlarm->type == eZL_ALARMTYPE::SUOBI_LOCK_LIMIT && pAlarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  760. && pAlarm->mo.compare(mo) == 0 && pAlarm->mp.compare(mp) == 0)
  761. {
  762. pAlarmInfo = pAlarm;
  763. if (pAlarmInfo)
  764. {
  765. //恢复
  766. //1. 更新数据库
  767. //2. 更新内存数据
  768. //3. 上送恢复消息
  769. if (ctAlarmTime.GetTime() == 0) ctAlarmTime = CTime::GetCurrentTime();
  770. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  771. CString sql;
  772. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  773. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  774. if (pAlarmInfo->ack_result == 1)
  775. {
  776. it = pService->m_lstUnConfirmAlarm.erase(it);
  777. continue;
  778. }
  779. else
  780. {
  781. ctAlarmTime.GetAsSystemTime(pAlarm->recoveryTime);
  782. }
  783. g_p315ClientManager->GetTcpClient()->SendAlarmData(zzjno, 2, CTime(pAlarmInfo->time).GetTime(), alarm_time / 1000,
  784. pAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pAlarmInfo->level, pAlarm->loworhigh,
  785. TIEDA_ACQ_VALUE(pAlarm->val, TIEDA_VAL_STATE::TVS_MOVEING), TIEDA_ACQ_VALUE(pAlarm->refer_val, TIEDA_VAL_STATE::TVS_MOVEING), 0);
  786. }
  787. }
  788. it++;
  789. }
  790. }
  791. return;
  792. }
  793. //查找是否原有的报警已存在
  794. uint8_t level = 0;
  795. if (bAlarm) level = 1;
  796. {
  797. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  798. for (const auto& alarm : pService->m_lstUnConfirmAlarm)
  799. {
  800. if (alarm->no == (uint8_t)posi && alarm->type == eZL_ALARMTYPE::SUOBI_LOCK_LIMIT && level == alarm->level && alarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  801. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  802. {
  803. pAlarmInfo = alarm;
  804. break;
  805. }
  806. }
  807. }
  808. ALARM_INFO *pNewAlarmInfo = nullptr;
  809. {
  810. bNew = true;
  811. pNewAlarmInfo = new ALARM_INFO;
  812. pNewAlarmInfo->event_id = hjfunc_GetGUID();
  813. pNewAlarmInfo->rel_id = pAlarmInfo ? pAlarmInfo->event_id : "";
  814. pNewAlarmInfo->id = ++pService->m_nAlarmID;
  815. pNewAlarmInfo->level = (bAlarm == 1);
  816. pNewAlarmInfo->mo = mo;
  817. pNewAlarmInfo->mp = mp;
  818. pNewAlarmInfo->no = (uint8_t)posi;
  819. ctAlarmTime.GetAsSystemTime(pNewAlarmInfo->time);
  820. pNewAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  821. pNewAlarmInfo->type = eZL_ALARMTYPE::SUOBI_LOCK_LIMIT;
  822. pNewAlarmInfo->val = alarm_value;
  823. pNewAlarmInfo->refer_val = alarm_refer;
  824. pNewAlarmInfo->posi = (posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT);
  825. pNewAlarmInfo->loworhigh = loworhigh;
  826. pNewAlarmInfo->zzjno = zzjno;
  827. pNewAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pNewAlarmInfo->time);
  828. char szInfo[200] = { 0 };
  829. sprintf_s(szInfo, sizeof(szInfo), "锁闭力超限,位置:%s, 报警值为%dN, 参考值为%dN",
  830. posi == eSuoBiPosi::SB_FIX ? "定位" : "反位", alarm_value, alarm_refer);
  831. pNewAlarmInfo->desc = szInfo;
  832. lock_guard<mutex> lock(pService->m_mtxAlarm);
  833. pService->m_lstUnConfirmAlarm.push_back(pNewAlarmInfo);
  834. }
  835. //send
  836. //if (bNew) //不再推送
  837. {
  838. rapidjson::StringBuffer buffer;
  839. auto ret = AlarmInfo2Pack(pNewAlarmInfo, buffer);
  840. const char* output = buffer.GetString();
  841. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  842. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  843. }
  844. //主动推送315
  845. //if (bNew)
  846. {
  847. g_p315ClientManager->GetTcpClient()->SendAlarmData(pNewAlarmInfo->zzjno, 1, CTime(pNewAlarmInfo->time).GetTime(), 0xFFFFFFFF,
  848. pNewAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pNewAlarmInfo->level, loworhigh,
  849. TIEDA_ACQ_VALUE(alarm_value, TIEDA_VAL_STATE::TVS_MOVEING), TIEDA_ACQ_VALUE(alarm_refer, TIEDA_VAL_STATE::TVS_MOVEING), 0);
  850. }
  851. //save
  852. //if (bNew)
  853. {
  854. CString sql;
  855. sql.Format("INSERT INTO [rm_alarm]([ID],[mo],[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id],posi,loworhigh,referval,[sunroof]) "\
  856. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  857. pNewAlarmInfo->id, pNewAlarmInfo->mo.c_str(), pNewAlarmInfo->mp.c_str(), pNewAlarmInfo->no, pNewAlarmInfo->type,
  858. ctAlarmTime.Format("%Y-%m-%d %H:%M:%S"), pNewAlarmInfo->level, pNewAlarmInfo->desc.c_str(), pNewAlarmInfo->suggest.c_str(), pNewAlarmInfo->val,
  859. pNewAlarmInfo->event_id.c_str(), pNewAlarmInfo->rel_id.c_str(), uint8_t(pNewAlarmInfo->posi), uint8_t(pNewAlarmInfo->loworhigh), pNewAlarmInfo->refer_val,
  860. pNewAlarmInfo->sunroof);
  861. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  862. CSimpleLog::Error("执行语句失败" + sql);
  863. else
  864. {
  865. uint8_t* pack = nullptr; int len = 0;
  866. auto packno = CResistAlarmMng::GeneralNewAlarmData(pNewAlarmInfo, &pack, &len);
  867. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  868. //g_p315ClientManager->GetTcpClient()->SendAlarmData(pNewAlarmInfo->no, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, (WORD)pNewAlarmInfo->type, pNewAlarmInfo->val, 0, 0);
  869. delete[] pack;
  870. pack = nullptr;
  871. }
  872. }
  873. }
  874. }
  875. //判断转换阻力是否超限
  876. void CResistAlarmMng::JudgeAlarm(CResistAlarmMng* pService, CONVERT_RESIST_OVER_LIMIT* pConvertOverLimit,
  877. time_t show_time, int show_val, eDaoChaPosi posi, eUpOrDownInfo eUpOrDown, string mo, string mp, uint32_t zzjno)
  878. {
  879. if (pConvertOverLimit == nullptr || pConvertOverLimit->enable == false) return;
  880. bool bNew = false;
  881. ALARM_INFO* pAlarmInfo = nullptr;
  882. bool bAlarm = false, bWarn = false; int alarm_value = show_val, alarm_refer; time_t alarm_time = show_time;
  883. CTime ctAlarmTime = CTime(alarm_time / 1000);
  884. {
  885. if (eUpOrDown == eUpOrDownInfo::UOD_UP)//缩进
  886. {
  887. if (show_val > pConvertOverLimit->up_alarm_high_limit)
  888. {
  889. bAlarm = true;
  890. alarm_refer = pConvertOverLimit->up_alarm_high_limit;
  891. }
  892. else if (show_val > pConvertOverLimit->up_warn_high_limit)
  893. {
  894. bWarn = true;
  895. alarm_refer = pConvertOverLimit->up_warn_high_limit;
  896. }
  897. }
  898. else if (eUpOrDown == eUpOrDownInfo::UOD_DOWN)
  899. {
  900. if (show_val > pConvertOverLimit->dw_alarm_low_limit)
  901. {
  902. bAlarm = true;
  903. alarm_refer = pConvertOverLimit->dw_alarm_low_limit;
  904. }
  905. else if (show_val > pConvertOverLimit->dw_warn_low_limit)
  906. {
  907. bWarn = true;
  908. alarm_refer = pConvertOverLimit->dw_warn_low_limit;
  909. }
  910. }
  911. if (bAlarm == false && bWarn == false)
  912. {
  913. {
  914. string strPosi= "方向:";
  915. strPosi += (posi == eDaoChaPosi::DCP_FIX2INVERT ? "定扳反" : "反扳定");
  916. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  917. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  918. {
  919. auto alarm = *it;
  920. if (alarm->no == 2 && alarm->type == eZL_ALARMTYPE::CONVERT_LIMIT //预警和报警单独计算 //
  921. && alarm->posi == posi && alarm->recoveryTime.wYear < 2000
  922. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0) //跟上次报警时间超过1小时,则算新报警
  923. {
  924. pAlarmInfo = alarm;
  925. if (pAlarmInfo)
  926. {
  927. //恢复
  928. //1. 更新数据库
  929. //2. 更新内存数据
  930. //3. 上送恢复消息
  931. if (ctAlarmTime.GetTime() == 0) ctAlarmTime = CTime::GetCurrentTime();
  932. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  933. CString sql;
  934. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  935. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  936. if (pAlarmInfo->ack_result == 1)
  937. {
  938. it = pService->m_lstUnConfirmAlarm.erase(it);
  939. continue;
  940. }
  941. else
  942. {
  943. ctAlarmTime.GetAsSystemTime(alarm->recoveryTime);
  944. }
  945. g_p315ClientManager->GetTcpClient()->SendAlarmData(alarm->zzjno, 2, CTime(pAlarmInfo->time).GetTime(), alarm_time / 1000,
  946. pAlarmInfo->type, posi, pAlarmInfo->level, eLowHigh::LH_HIGH,
  947. TIEDA_ACQ_VALUE(alarm->val, TIEDA_VAL_STATE::TVS_MOVEING), TIEDA_ACQ_VALUE(alarm->refer_val, TIEDA_VAL_STATE::TVS_MOVEING), 0);
  948. }
  949. }
  950. ++it;
  951. }
  952. }
  953. return;
  954. }
  955. //查找是否原有的报警已存在
  956. uint8_t level = 0;
  957. if (bAlarm) level = 1;
  958. {
  959. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  960. for (const auto& alarm : pService->m_lstUnConfirmAlarm)
  961. {
  962. if (alarm->no == 2 && alarm->type == eZL_ALARMTYPE::CONVERT_LIMIT && level == alarm->level && alarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  963. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  964. {
  965. pAlarmInfo = alarm;
  966. break;
  967. }
  968. }
  969. }
  970. ALARM_INFO* pNewAlarmInfo = nullptr;
  971. {
  972. bNew = true;
  973. pNewAlarmInfo = new ALARM_INFO;
  974. pNewAlarmInfo->event_id = hjfunc_GetGUID();
  975. pNewAlarmInfo->rel_id = pAlarmInfo ? pAlarmInfo->event_id : "";
  976. pNewAlarmInfo->id = ++pService->m_nAlarmID;
  977. pNewAlarmInfo->level = (bAlarm == 1);
  978. pNewAlarmInfo->mo = mo;
  979. pNewAlarmInfo->mp = mp;
  980. pNewAlarmInfo->no = 2;
  981. ctAlarmTime.GetAsSystemTime(pNewAlarmInfo->time);
  982. pNewAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  983. pNewAlarmInfo->type = eZL_ALARMTYPE::CONVERT_LIMIT;
  984. pNewAlarmInfo->val = alarm_value;
  985. pNewAlarmInfo->refer_val = alarm_refer;
  986. pNewAlarmInfo->posi = posi;
  987. pNewAlarmInfo->zzjno = zzjno;
  988. pNewAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pNewAlarmInfo->time);
  989. char szInfo[200] = { 0 };
  990. sprintf_s(szInfo, sizeof(szInfo), "转换阻力超限,方向:%s, 报警值为%dN, 参考值为%dN",
  991. posi == eDaoChaPosi::DCP_FIX2INVERT ? "定扳反" : "反扳定", alarm_value, alarm_refer);
  992. pNewAlarmInfo->desc = szInfo;
  993. lock_guard<mutex> lock(pService->m_mtxAlarm);
  994. pService->m_lstUnConfirmAlarm.push_back(pNewAlarmInfo);
  995. }
  996. //send
  997. //if (bNew) //不再推送
  998. {
  999. rapidjson::StringBuffer buffer;
  1000. auto ret = AlarmInfo2Pack(pNewAlarmInfo, buffer);
  1001. const char* output = buffer.GetString();
  1002. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  1003. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  1004. }
  1005. //主动推送315
  1006. //if (bNew)
  1007. {
  1008. g_p315ClientManager->GetTcpClient()->SendAlarmData(pNewAlarmInfo->zzjno, 1, CTime(pNewAlarmInfo->time).GetTime(), 0xFFFFFFFF,
  1009. pNewAlarmInfo->type, posi, pNewAlarmInfo->level, eLowHigh::LH_HIGH,
  1010. TIEDA_ACQ_VALUE(alarm_value, TIEDA_VAL_STATE::TVS_MOVEING), TIEDA_ACQ_VALUE(alarm_refer, TIEDA_VAL_STATE::TVS_MOVEING), 0);
  1011. }
  1012. //save
  1013. {
  1014. CString sql;
  1015. sql.Format("INSERT INTO [rm_alarm]([ID],[mo],[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id],posi,loworhigh,referval,[sunroof]) "\
  1016. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  1017. pNewAlarmInfo->id, pNewAlarmInfo->mo.c_str(), pNewAlarmInfo->mp.c_str(), pNewAlarmInfo->no, pNewAlarmInfo->type,
  1018. ctAlarmTime.Format("%Y-%m-%d %H:%M:%S"), pNewAlarmInfo->level, pNewAlarmInfo->desc.c_str(), pNewAlarmInfo->suggest.c_str(), pNewAlarmInfo->val,
  1019. pNewAlarmInfo->event_id.c_str(), pNewAlarmInfo->rel_id.c_str(), uint8_t(pNewAlarmInfo->posi), uint8_t(pNewAlarmInfo->loworhigh), pNewAlarmInfo->refer_val,
  1020. pNewAlarmInfo->sunroof);
  1021. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  1022. CSimpleLog::Error("执行语句失败" + sql);
  1023. else
  1024. {
  1025. uint8_t* pack = nullptr; int len = 0;
  1026. auto packno = CResistAlarmMng::GeneralNewAlarmData(pNewAlarmInfo, &pack, &len);
  1027. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  1028. //g_p315ClientManager->GetTcpClient()->SendAlarmData(pNewAlarmInfo->no, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, (WORD)pNewAlarmInfo->type, pNewAlarmInfo->val, 0, 0);
  1029. delete[] pack;
  1030. pack = nullptr;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. void CResistAlarmMng::JudgeAlarm(CResistAlarmMng* pService, RETENSION_FORCE_DROP* pRetensionForceDropInfo, time_t show_time, int show_val, eSuoBiPosi posi, string mo, string mp, uint32_t zzjno)
  1036. {
  1037. if (pRetensionForceDropInfo == nullptr || pRetensionForceDropInfo->enable == false) return;
  1038. bool bNew = false;
  1039. ALARM_INFO* pAlarmInfo = nullptr;
  1040. bool bAlarm = false, bWarn = false; int alarm_value = 0, alarm_refer; time_t alarm_time = show_time;
  1041. CTime ctAlarmTime = CTime(alarm_time / 1000);
  1042. eLowHigh loworhigh = eLowHigh::LH_HIGH; //0:最小值 1:最大值
  1043. {
  1044. if (show_val < pRetensionForceDropInfo->dw_alarm_low_drop)
  1045. {
  1046. bWarn = true;
  1047. alarm_refer = pRetensionForceDropInfo->dw_alarm_low_drop;
  1048. alarm_value = show_val;
  1049. //loworhigh = eLowHigh::LH_LOW;
  1050. }
  1051. if (bAlarm == false && bWarn == false)
  1052. {
  1053. if (g_strMoMp.compare(mo + "." + mp) == 0)
  1054. SPDLOG_INFO("[保持力下降]{}.{} 未检测到下降. {} ", mo, mp, show_val);
  1055. {
  1056. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1057. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  1058. {
  1059. auto pAlarm = *it;
  1060. if (pAlarm->no == (uint8_t)posi && pAlarm->type == eZL_ALARMTYPE::RETENSION_FORCE && pAlarm->recoveryTime.wYear < 2000 //预警和报警统一恢复
  1061. && pAlarm->mo.compare(mo) == 0 && pAlarm->mp.compare(mp) == 0)
  1062. {
  1063. pAlarmInfo = pAlarm;
  1064. if (pAlarmInfo)
  1065. {
  1066. //恢复
  1067. //1. 更新数据库
  1068. //2. 更新内存数据
  1069. //3. 上送恢复消息
  1070. if (ctAlarmTime.GetTime() == 0) ctAlarmTime = CTime::GetCurrentTime();
  1071. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  1072. CString sql;
  1073. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  1074. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1075. if (pAlarmInfo->ack_result == 1)
  1076. {
  1077. it = pService->m_lstUnConfirmAlarm.erase(it);
  1078. continue;
  1079. }
  1080. else
  1081. {
  1082. ctAlarmTime.GetAsSystemTime(pAlarm->recoveryTime);
  1083. }
  1084. g_p315ClientManager->GetTcpClient()->SendAlarmData(zzjno, 2, CTime(pAlarmInfo->time).GetTime(), alarm_time / 1000,
  1085. pAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pAlarmInfo->level, pAlarm->loworhigh,
  1086. TIEDA_ACQ_VALUE(pAlarm->val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), TIEDA_ACQ_VALUE(pAlarm->refer_val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), 0);
  1087. }
  1088. }
  1089. it++;
  1090. }
  1091. }
  1092. return;
  1093. }
  1094. SPDLOG_INFO("[保持力下降]{}.{} 检测到下降. {} ", mo, mp, show_val);
  1095. //查找是否原有的报警已存在
  1096. uint8_t level = 0;
  1097. if (bAlarm) level = 1;
  1098. {
  1099. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1100. for (const auto& alarm : pService->m_lstUnConfirmAlarm)
  1101. {
  1102. if (alarm->no == (uint8_t)posi && alarm->type == eZL_ALARMTYPE::RETENSION_FORCE && level == alarm->level && alarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  1103. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  1104. {
  1105. pAlarmInfo = alarm;
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. if (pAlarmInfo == nullptr)
  1111. {
  1112. bNew = true;
  1113. pAlarmInfo = new ALARM_INFO;
  1114. pAlarmInfo->event_id = hjfunc_GetGUID();
  1115. pAlarmInfo->id = ++pService->m_nAlarmID;
  1116. pAlarmInfo->level = (bAlarm == 1);
  1117. pAlarmInfo->mo = mo;
  1118. pAlarmInfo->mp = mp;
  1119. pAlarmInfo->no = (uint8_t)posi;
  1120. ctAlarmTime.GetAsSystemTime(pAlarmInfo->time);
  1121. pAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  1122. pAlarmInfo->type = eZL_ALARMTYPE::RETENSION_FORCE;
  1123. pAlarmInfo->val = alarm_value;
  1124. pAlarmInfo->refer_val = alarm_refer;
  1125. pAlarmInfo->posi = (posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT);
  1126. pAlarmInfo->loworhigh = loworhigh;
  1127. pAlarmInfo->zzjno = zzjno;
  1128. pAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pAlarmInfo->time);
  1129. char szInfo[200] = { 0 };
  1130. sprintf_s(szInfo, sizeof(szInfo), "保持力下降,位置:%s, 报警值为%dN, 参考值为%dN",
  1131. posi == eSuoBiPosi::SB_FIX ? "定位" : "反位", alarm_value, alarm_refer);
  1132. pAlarmInfo->desc = szInfo;
  1133. lock_guard<mutex> lock(pService->m_mtxAlarm);
  1134. pService->m_lstUnConfirmAlarm.push_back(pAlarmInfo);
  1135. }
  1136. //send
  1137. if (bNew) //不再推送
  1138. {
  1139. rapidjson::StringBuffer buffer;
  1140. auto ret = AlarmInfo2Pack(pAlarmInfo, buffer);
  1141. const char* output = buffer.GetString();
  1142. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  1143. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  1144. }
  1145. //主动推送315
  1146. if (bNew)
  1147. {
  1148. g_p315ClientManager->GetTcpClient()->SendAlarmData(zzjno, 1, CTime(pAlarmInfo->time).GetTime(), 0xFFFFFFFF,
  1149. pAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pAlarmInfo->level, loworhigh,
  1150. TIEDA_ACQ_VALUE(alarm_value, TIEDA_VAL_STATE::TVS_AFTER_MOVE), TIEDA_ACQ_VALUE(alarm_refer, TIEDA_VAL_STATE::TVS_AFTER_MOVE), 0);
  1151. }
  1152. //save
  1153. if (bNew)
  1154. {
  1155. CString sql;
  1156. sql.Format("INSERT INTO [rm_alarm]([ID],[mo],[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id],posi,loworhigh,referval,[sunroof]) "\
  1157. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  1158. pAlarmInfo->id, pAlarmInfo->mo.c_str(), pAlarmInfo->mp.c_str(), pAlarmInfo->no, pAlarmInfo->type,
  1159. ctAlarmTime.Format("%Y-%m-%d %H:%M:%S"), pAlarmInfo->level, pAlarmInfo->desc.c_str(), pAlarmInfo->suggest.c_str(), pAlarmInfo->val,
  1160. pAlarmInfo->event_id.c_str(), pAlarmInfo->rel_id.c_str(), uint8_t(pAlarmInfo->posi), uint8_t(pAlarmInfo->loworhigh), pAlarmInfo->refer_val,
  1161. pAlarmInfo->sunroof);
  1162. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  1163. CSimpleLog::Error("执行语句失败" + sql);
  1164. else
  1165. {
  1166. uint8_t* pack = nullptr; int len = 0;
  1167. auto packno = CResistAlarmMng::GeneralNewAlarmData(pAlarmInfo, &pack, &len);
  1168. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  1169. delete[] pack;
  1170. pack = nullptr;
  1171. }
  1172. }
  1173. }
  1174. }
  1175. void CResistAlarmMng::JudgeAlarm1(CResistAlarmMng* pService, RETENSION_FORCE_DROP* pRetensionForceDropInfo, time_t show_time, int show_val,
  1176. eSuoBiPosi posi, string mo, string mp, uint32_t zzjno)
  1177. {
  1178. if (pRetensionForceDropInfo == nullptr || pRetensionForceDropInfo->enable == false) return;
  1179. bool bNew = false;
  1180. ALARM_INFO* pAlarmInfo = nullptr;
  1181. bool bAlarm = false, bWarn = false; int alarm_value = 0, alarm_refer; time_t alarm_time = show_time;
  1182. CTime ctAlarmTime = CTime(alarm_time / 1000);
  1183. eLowHigh loworhigh = eLowHigh::LH_HIGH; //0:最小值 1:最大值
  1184. {
  1185. if (show_val < pRetensionForceDropInfo->dw_alarm_low_drop)
  1186. {
  1187. bAlarm = true;
  1188. alarm_refer = pRetensionForceDropInfo->dw_alarm_low_drop;
  1189. alarm_value = show_val;
  1190. //loworhigh = eLowHigh::LH_LOW;
  1191. }
  1192. if (bAlarm == false && bWarn == false)
  1193. {
  1194. {
  1195. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1196. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  1197. {
  1198. auto pAlarm = *it;
  1199. if (pAlarm->no == (uint8_t)posi && pAlarm->type == eZL_ALARMTYPE::RETENSION_FORCE && pAlarm->recoveryTime.wYear < 2000 //预警和报警统一恢复
  1200. && pAlarm->mo.compare(mo) == 0 && pAlarm->mp.compare(mp) == 0)
  1201. {
  1202. pAlarmInfo = pAlarm;
  1203. if (pAlarmInfo)
  1204. {
  1205. //恢复
  1206. //1. 更新数据库
  1207. //2. 更新内存数据
  1208. //3. 上送恢复消息
  1209. if (ctAlarmTime.GetTime() == 0) ctAlarmTime = CTime::GetCurrentTime();
  1210. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  1211. CString sql;
  1212. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  1213. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1214. if (pAlarmInfo->ack_result == 1)
  1215. {
  1216. it = pService->m_lstUnConfirmAlarm.erase(it);
  1217. continue;
  1218. }
  1219. else
  1220. {
  1221. ctAlarmTime.GetAsSystemTime(pAlarm->recoveryTime);
  1222. }
  1223. g_p315ClientManager->GetTcpClient()->SendAlarmData(zzjno, 2, CTime(pAlarmInfo->time).GetTime(), alarm_time / 1000,
  1224. pAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pAlarmInfo->level, pAlarm->loworhigh,
  1225. TIEDA_ACQ_VALUE(pAlarm->val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), TIEDA_ACQ_VALUE(pAlarm->refer_val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), 0);
  1226. }
  1227. }
  1228. it++;
  1229. }
  1230. }
  1231. return;
  1232. }
  1233. //查找是否原有的报警已存在
  1234. uint8_t level = 0;
  1235. if (bAlarm) level = 1;
  1236. {
  1237. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1238. for (const auto& alarm : pService->m_lstUnConfirmAlarm)
  1239. {
  1240. if (alarm->no == (uint8_t)posi && alarm->type == eZL_ALARMTYPE::RETENSION_FORCE && level == alarm->level && alarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  1241. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  1242. {
  1243. pAlarmInfo = alarm;
  1244. break;
  1245. }
  1246. }
  1247. }
  1248. ALARM_INFO* pNewAlarmInfo = nullptr;
  1249. {
  1250. bNew = true;
  1251. pNewAlarmInfo = new ALARM_INFO;
  1252. pNewAlarmInfo->event_id = hjfunc_GetGUID();
  1253. pNewAlarmInfo->rel_id = pAlarmInfo ? pAlarmInfo->event_id : "";
  1254. pNewAlarmInfo->id = ++pService->m_nAlarmID;
  1255. pNewAlarmInfo->level = (bAlarm == 1);
  1256. pNewAlarmInfo->mo = mo;
  1257. pNewAlarmInfo->mp = mp;
  1258. pNewAlarmInfo->no = (uint8_t)posi;
  1259. ctAlarmTime.GetAsSystemTime(pNewAlarmInfo->time);
  1260. pNewAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  1261. pNewAlarmInfo->type = eZL_ALARMTYPE::RETENSION_FORCE;
  1262. pNewAlarmInfo->val = alarm_value;
  1263. pNewAlarmInfo->refer_val = alarm_refer;
  1264. pNewAlarmInfo->posi = (posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT);
  1265. pNewAlarmInfo->loworhigh = loworhigh;
  1266. pNewAlarmInfo->zzjno = zzjno;
  1267. pNewAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pNewAlarmInfo->time);
  1268. char szInfo[200] = { 0 };
  1269. sprintf_s(szInfo, sizeof(szInfo), "保持力下降,位置:%s, 报警值为%dN, 参考值为%dN",
  1270. posi == eSuoBiPosi::SB_FIX ? "定位" : "反位", alarm_value, alarm_refer);
  1271. pNewAlarmInfo->desc = szInfo;
  1272. lock_guard<mutex> lock(pService->m_mtxAlarm);
  1273. pService->m_lstUnConfirmAlarm.push_back(pNewAlarmInfo);
  1274. }
  1275. //send
  1276. //if (bNew) //不再推送
  1277. {
  1278. rapidjson::StringBuffer buffer;
  1279. auto ret = AlarmInfo2Pack(pNewAlarmInfo, buffer);
  1280. const char* output = buffer.GetString();
  1281. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  1282. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  1283. }
  1284. //主动推送315
  1285. //if (bNew)
  1286. {
  1287. g_p315ClientManager->GetTcpClient()->SendAlarmData(zzjno, 1, CTime(pNewAlarmInfo->time).GetTime(), 0xFFFFFFFF,
  1288. pNewAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pNewAlarmInfo->level, loworhigh,
  1289. TIEDA_ACQ_VALUE(alarm_value, TIEDA_VAL_STATE::TVS_AFTER_MOVE), TIEDA_ACQ_VALUE(alarm_refer, TIEDA_VAL_STATE::TVS_AFTER_MOVE), 0);
  1290. }
  1291. //save
  1292. //if (bNew)
  1293. {
  1294. CString sql;
  1295. sql.Format("INSERT INTO [rm_alarm]([ID],[mo],[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id],posi,loworhigh,referval,[sunroof]) "\
  1296. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  1297. pNewAlarmInfo->id, pNewAlarmInfo->mo.c_str(), pNewAlarmInfo->mp.c_str(), pNewAlarmInfo->no, pNewAlarmInfo->type,
  1298. ctAlarmTime.Format("%Y-%m-%d %H:%M:%S"), pNewAlarmInfo->level, pNewAlarmInfo->desc.c_str(), pNewAlarmInfo->suggest.c_str(), pNewAlarmInfo->val,
  1299. pNewAlarmInfo->event_id.c_str(), pNewAlarmInfo->rel_id.c_str(), uint8_t(pNewAlarmInfo->posi), uint8_t(pNewAlarmInfo->loworhigh), pNewAlarmInfo->refer_val,
  1300. pNewAlarmInfo->sunroof);
  1301. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  1302. CSimpleLog::Error("执行语句失败" + sql);
  1303. else
  1304. {
  1305. uint8_t* pack = nullptr; int len = 0;
  1306. auto packno = CResistAlarmMng::GeneralNewAlarmData(pNewAlarmInfo, &pack, &len);
  1307. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  1308. //g_p315ClientManager->GetTcpClient()->SendAlarmData(pNewAlarmInfo->no, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, (WORD)pNewAlarmInfo->type, pNewAlarmInfo->val, 0, 0);
  1309. delete[] pack;
  1310. pack = nullptr;
  1311. }
  1312. }
  1313. }
  1314. }
  1315. BOOL ConmpareValue(std::map<time_t, int>::const_iterator i, std::map<time_t, int>::const_iterator j, int refer_value, int& hight_num, int& low_num)
  1316. {
  1317. int start_value = 0;
  1318. for (; i != j; i++)
  1319. {
  1320. if (i->second > refer_value)
  1321. {
  1322. if (start_value == 1)
  1323. {
  1324. //原来大于,现在也是大于
  1325. continue;
  1326. }
  1327. else
  1328. {
  1329. //原来小于或者无效,现在大于
  1330. start_value = 1;
  1331. hight_num++;
  1332. }
  1333. }
  1334. else
  1335. {
  1336. if (start_value == -1)
  1337. {
  1338. //原来小于,现在小于等于
  1339. continue;
  1340. }
  1341. else
  1342. {
  1343. //原来大于或者无效,现在小于
  1344. start_value = -1;
  1345. low_num++;
  1346. }
  1347. }
  1348. }
  1349. return TRUE;
  1350. }
  1351. void CResistAlarmMng::ThreadProcAlarmSet(DWORD_PTR param)
  1352. {
  1353. auto pService = (CResistAlarmMng*)param;
  1354. Sleep(2000);
  1355. time_t tmNow;
  1356. bool bLock = false;
  1357. do
  1358. {
  1359. if (bLock)
  1360. {
  1361. g_lockSync.Unlock();
  1362. bLock = false;
  1363. }
  1364. Sleep(800);
  1365. time(&tmNow);
  1366. if (tmNow == pService->m_lastDetectTime)
  1367. continue;
  1368. pService->m_lastDetectTime = tmNow;
  1369. bLock = true;
  1370. g_lockSync.ReadLock();
  1371. //1秒判断一次
  1372. for (const auto& item : pService->m_alarm_set)
  1373. {
  1374. if (item.second == nullptr) continue;
  1375. if (item.second->enable == false) continue;
  1376. if (item.second->type != eZL_ALARMTYPE::MAX_OVER_LIMIT && item.second->type != eZL_ALARMTYPE::FRICTION_OVER_LIMIT) //这个逻辑里面只支持这两种
  1377. continue;
  1378. if (item.second->no < 0 || item.second->no > 2) continue;
  1379. int nPos = item.first.find('.');
  1380. if (nPos == -1) continue;
  1381. int nPos2 = item.first.find('.', nPos + 1);
  1382. if (nPos2 == -1) continue;
  1383. string mo = item.first.substr(0, nPos);
  1384. string mp = item.first.substr(nPos + 1, nPos2 - nPos - 1);
  1385. string momp = item.first.substr(0, nPos2);
  1386. bool bNew = false;
  1387. ALARM_INFO *pAlarmInfo = nullptr;
  1388. CTime ctAlarmTime;
  1389. string imei; int idx;
  1390. auto ret = CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei, idx);
  1391. if (ret == false) continue;
  1392. #ifdef _DEBUG
  1393. if (strcmp("860588048955283", imei.c_str()) != 0) continue;
  1394. #endif
  1395. auto pDevice = CDeviceMng::Instance()->Find(imei);
  1396. if (pDevice == nullptr)
  1397. {
  1398. CSimpleLog::Error(("找不到imei(" + imei + ")" + std::to_string(__LINE__)).c_str());
  1399. continue;
  1400. }
  1401. int desc_type = 0;
  1402. bool bAlarm = false, bWarn = false; int alarm_value = 0; time_t alarm_time = 0;
  1403. eDaoChaPosi posi = eDaoChaPosi::DCP_UNKNOWN;
  1404. int high_alarm_limit = INT_MAX, high_warn_limit = INT_MAX, low_alarm_limit = INT_MIN, low_warn_limit = INT_MIN, alarm_refer = INT_MAX;
  1405. time_t tmNow; time(&tmNow);
  1406. BASE_INFO* pAlarmSet = item.second;
  1407. do{
  1408. Sleep(0);
  1409. lock_guard<mutex> lock(pDevice->m_mtx);
  1410. std::map<time_t, int>* pData = pDevice->GetMapData(idx, item.second->no);
  1411. assert(pData);
  1412. if (pData == nullptr) continue;
  1413. if (pData->size() == 0) continue;
  1414. if (item.second == 0) continue;
  1415. if (item.second->type == eZL_ALARMTYPE::MAX_OVER_LIMIT)
  1416. {
  1417. auto pInfo = (MAX_OVER_LIMIT_INFO*)pAlarmSet;
  1418. #ifdef _DEBUG
  1419. if (pInfo->tmLastCheckTime > pData->rbegin()->first)
  1420. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1421. #endif
  1422. auto it = pData->cbegin();
  1423. if (pInfo->tmLastCheckTime == 0) pInfo->tmLastCheckTime = pData->rbegin()->first;
  1424. else if (pInfo->tmLastCheckTime != 0) it = pData->find(pInfo->tmLastCheckTime);
  1425. if (it == pData->cend())
  1426. {
  1427. it = pData->find(pInfo->tmLastCheckTime / 1000 * 1000);
  1428. if (it == pData->cend())
  1429. {
  1430. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1431. continue;
  1432. }
  1433. }
  1434. if (pInfo->tmLastCheckTime == pData->rbegin()->first) //没有新的数据
  1435. continue;
  1436. #ifdef _DEBUG
  1437. TRACE("%s.%s.%d.%d lastcheck:%s now:%s\r\n", mo.c_str(), mp.c_str(), pInfo->no, (int)eZL_ALARMTYPE::MAX_OVER_LIMIT,
  1438. CTime(pInfo->tmLastCheckTime / 1000).Format("%Y-%m-%d %H:%M:%S"), CTime(pData->rbegin()->first / 1000).Format("%Y-%m-%d %H:%M:%S"));
  1439. #endif // _DEBUG
  1440. time_t tmStartSteady = pInfo->tmLastCheckTime / 1000 - 3; //稳态报警检测开始, 从上次3秒前到当前秒
  1441. if (item.second->no == 2)
  1442. {
  1443. if (pInfo->alarm_high_limit > pInfo->f_alarm_high_limit)
  1444. {//定板反
  1445. high_alarm_limit = pInfo->alarm_high_limit;
  1446. high_warn_limit = pInfo->warn_high_limit;
  1447. low_alarm_limit = pInfo->f_alarm_high_limit;
  1448. low_warn_limit = pInfo->f_warn_high_limit;
  1449. posi = eDaoChaPosi::DCP_FIX2INVERT;
  1450. }
  1451. else
  1452. {//反板定
  1453. low_alarm_limit = pInfo->alarm_high_limit;
  1454. low_warn_limit = pInfo->warn_high_limit;
  1455. high_alarm_limit = pInfo->f_alarm_high_limit;
  1456. high_warn_limit = pInfo->f_warn_high_limit;
  1457. posi = eDaoChaPosi::DCP_FIX2INVERT;
  1458. }
  1459. }
  1460. for (++it; it != pData->cend(); ++it)
  1461. {
  1462. //CString str = CTime(it->first / 1000).Format("%Y-%m-%d %H:%M:%S");
  1463. if (it->second == INVLID_VAL)
  1464. {
  1465. pInfo->tmLastCheckTime = it->first;
  1466. continue;
  1467. }
  1468. if (item.second->no < 2)
  1469. {
  1470. //1,2 锁闭力报警
  1471. if (it->second > pInfo->alarm_high_limit)
  1472. {
  1473. bAlarm = true;
  1474. alarm_refer = pInfo->alarm_high_limit;
  1475. alarm_value = it->second;
  1476. alarm_time = it->first;
  1477. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1478. desc_type = 1;
  1479. break;
  1480. }
  1481. //else if (it->second > pInfo->warn_high_limit) //稳态报警单独判断
  1482. //{
  1483. // bWarn = true;
  1484. // alarm_refer = pInfo->warn_high_limit;
  1485. // alarm_value = it->second;
  1486. // alarm_time = it->first;
  1487. // pInfo->tmLastCheckTime = pData->rbegin()->first;
  1488. //}
  1489. }
  1490. else
  1491. {
  1492. assert(item.second->no == 2);
  1493. if (pInfo->alarm_high_limit == INT_MAX || pInfo->f_alarm_high_limit == INT_MAX || pInfo->alarm_high_limit == pInfo->f_alarm_high_limit)
  1494. break;
  1495. desc_type = 3;
  1496. if (it->second > high_alarm_limit)
  1497. {
  1498. bAlarm = true;
  1499. alarm_refer = high_alarm_limit;
  1500. alarm_value = it->second;
  1501. alarm_time = it->first;
  1502. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1503. break;
  1504. }
  1505. else if (it->second < low_alarm_limit)
  1506. {
  1507. bAlarm = true;
  1508. alarm_refer = low_alarm_limit;
  1509. alarm_value = it->second;
  1510. alarm_time = it->first;
  1511. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1512. break;
  1513. }
  1514. else if (low_warn_limit == INT_MIN || high_warn_limit == INT_MIN)
  1515. {
  1516. //未设置预警值
  1517. }
  1518. else if (it->second > high_warn_limit)
  1519. {
  1520. bWarn = true;
  1521. alarm_refer = high_warn_limit;
  1522. alarm_value = it->second;
  1523. alarm_time = it->first;
  1524. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1525. }
  1526. else if (it->second < low_warn_limit)
  1527. {
  1528. bWarn = true;
  1529. alarm_refer = low_warn_limit;
  1530. alarm_value = it->second;
  1531. alarm_time = it->first;
  1532. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1533. }
  1534. }
  1535. pInfo->tmLastCheckTime = it->first;
  1536. }
  1537. //稳态报警判断
  1538. if (item.second->no < 2 && bAlarm == false)
  1539. {
  1540. time_t tmEnd = pInfo->tmLastCheckTime / 1000;
  1541. int dif = tmEnd - tmStartSteady;
  1542. for (time_t i = tmStartSteady; i < tmEnd - 3; i++)
  1543. {
  1544. #ifdef _DEBUG
  1545. COleDateTime t(i);
  1546. TRACE("%s\r\n", t.Format("%Y-%m-%d %H:%M:%S"));
  1547. #endif // _DEBUG
  1548. auto j = pData->find(i * 1000);
  1549. if (j == pData->end()) continue;
  1550. time_t t2 = (i + 1) * 1000;
  1551. auto k = pData->find(t2);
  1552. if (k == pData->end()) continue;
  1553. t2 = (i + 2) * 1000;
  1554. k = pData->find(t2);
  1555. if (k == pData->end()) continue;
  1556. //连续3秒在线,继续找下一个结束点
  1557. t2 = (i + 3) * 1000;
  1558. auto g = pData->find(t2);
  1559. if (g == pData->end())
  1560. {
  1561. for (++k; k != pData->end(); k++)
  1562. {
  1563. if (k->first >= t2)
  1564. {
  1565. g = k;
  1566. break;
  1567. }
  1568. }
  1569. }
  1570. if (g == pData->end())
  1571. continue;
  1572. int high_num = 0, low_num = 0;
  1573. ConmpareValue(j, g, pInfo->warn_high_limit, high_num, low_num);
  1574. if (high_num > 0 && high_num <= 2)
  1575. {
  1576. bWarn = true;
  1577. desc_type = 2;
  1578. alarm_refer = pInfo->warn_high_limit;
  1579. alarm_value = high_num;
  1580. alarm_time = i * 1000; //报警时间
  1581. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1582. break;
  1583. }
  1584. }
  1585. }
  1586. }
  1587. else if (item.second->type == eZL_ALARMTYPE::FRICTION_OVER_LIMIT)
  1588. {
  1589. auto pInfo = (FRICTION_OVER_LIMIT_INFO*)item.second;
  1590. #ifdef _DEBUG
  1591. if (pInfo->tmLastCheckTime > pData->rbegin()->first)
  1592. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1593. #endif
  1594. auto it = pData->cbegin();
  1595. if (pInfo->tmLastCheckTime == 0) pInfo->tmLastCheckTime = pData->rbegin()->first;
  1596. else if (pInfo->tmLastCheckTime != 0) it = pData->find(pInfo->tmLastCheckTime);
  1597. if (it == pData->cend())
  1598. {
  1599. it = pData->find(pInfo->tmLastCheckTime / 1000 * 1000);
  1600. if (it == pData->cend())
  1601. {
  1602. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1603. continue;
  1604. }
  1605. }
  1606. if (pInfo->tmLastCheckTime == pData->rbegin()->first) //没有新的数据
  1607. continue;
  1608. if (tmNow * 1000 - pInfo->tmLastCheckTime < 5000) //5秒检测一次
  1609. continue;
  1610. #ifdef _DEBUG
  1611. TRACE("%s.%s.%d.%d lastcheck:%s now:%s\r\n", mo.c_str(), mp.c_str(), pInfo->no, (int)eZL_ALARMTYPE::FRICTION_OVER_LIMIT,
  1612. CTime(pInfo->tmLastCheckTime / 1000).Format("%Y-%m-%d %H:%M:%S"), CTime(pData->rbegin()->first / 1000).Format("%Y-%m-%d %H:%M:%S"));
  1613. #endif // _DEBUG
  1614. if (item.second->no != 2) continue;
  1615. //从检测时间往前取30秒的数据
  1616. static const int dif_resist = 200; //稳定期判定差值
  1617. int up_max_resist = INT_MIN; //上最大值
  1618. time_t up_max_time = 0;
  1619. int up_avg_resist = INT_MIN; //上稳定期平均值
  1620. uint64_t up_stable_sum = 0; //上稳定期的和
  1621. int up_stable_time = 0; //上稳定期的数值个数
  1622. int up_start_resist_stabel = INT_MIN; //上稳定期开始值
  1623. time_t up_start_stable_time = 0; //规定是最大值之后的2秒
  1624. time_t up_end_stable_time = 0;//稳定结束的时间
  1625. int up_dif_time_stable = 0; //上稳定期的时长
  1626. int down_max_resist = INT_MAX; //上最大值
  1627. time_t down_max_time = 0;
  1628. int down_avg_resist = INT_MAX; //上稳定期平均值
  1629. uint64_t down_stable_sum = 0; //上稳定期的和
  1630. int down_stable_time = 0; //上稳定期的数值个数
  1631. int down_start_resist_stabel = INT_MAX; //上稳定期开始值
  1632. time_t down_start_stable_time = 0; //规定是最大值之后的2秒
  1633. int down_dif_time_stable = 0; //上稳定期的时长
  1634. time_t tmEnd = pData->rbegin()->first / 1000 + 1; //结束时间
  1635. time_t tmStart = tmEnd - 36;//开始时间是从上次35秒到当前秒
  1636. std::map<time_t, int>::const_iterator it_start, it_up_max, it_down_min, it_up_stable_start, it_down_stable_start, it_up_stable_end, it_down_stable_end;
  1637. //寻找起始时间
  1638. for (time_t i = tmStart; i < tmEnd; i++)
  1639. {
  1640. #ifdef _DEBUG
  1641. COleDateTime t(i);
  1642. TRACE("%s\r\n", t.Format("%Y-%m-%d %H:%M:%S"));
  1643. #endif // _DEBUG
  1644. it_start = pData->find(i * 1000);
  1645. if (it_start != pData->cend()) break;
  1646. }
  1647. pInfo->tmLastCheckTime = pData->rbegin()->first; //赋值最后的时间
  1648. if (it_start == pData->cend()) break;
  1649. bool bUp = true, bDown = true;
  1650. //先找最大值
  1651. for (auto i = it_start; i != pData->cend(); i++)
  1652. {
  1653. //寻找最大的点
  1654. if (i->second > up_max_resist)
  1655. {
  1656. up_max_resist = i->second;
  1657. it_up_max = i;
  1658. }
  1659. //寻找最小的点
  1660. if (i->second < down_max_resist)
  1661. {
  1662. down_max_resist = i->second;
  1663. it_down_min = i;
  1664. }
  1665. }
  1666. if (pInfo->tmLastCheckTime - it_up_max->first < 17000) bUp = false; //小于17秒
  1667. if (pInfo->tmLastCheckTime - it_down_min->first < 1700) bDown = false; //小于17秒
  1668. if(bUp == false && bDown == false) break;
  1669. //再找稳定开始
  1670. if (bUp)
  1671. {
  1672. it_up_stable_start = it_up_max;
  1673. for (auto i = it_up_max; i != pData->cend(); i++)
  1674. {
  1675. if (i->first - it_up_max->first < 2000) continue;
  1676. else
  1677. {
  1678. it_up_stable_start = i;
  1679. break;
  1680. }
  1681. }
  1682. if (it_up_stable_start == it_up_max) bUp = false;
  1683. }
  1684. if (bDown)
  1685. {
  1686. it_down_stable_start = it_down_min;
  1687. for (auto i = it_down_min; i != pData->cend(); i++)
  1688. {
  1689. if (i->first - it_down_min->first < 2000) continue;
  1690. else
  1691. {
  1692. it_down_stable_start = i;
  1693. break;
  1694. }
  1695. }
  1696. if (it_down_stable_start == it_down_min) bDown = false;
  1697. }
  1698. if (bUp == false && bDown == false) break;
  1699. if (bUp)
  1700. {
  1701. up_start_resist_stabel = it_up_stable_start->second;//稳定期开始的值
  1702. it_up_stable_end = it_up_stable_start;
  1703. for (auto i = it_up_stable_start; i != pData->cend(); i++)
  1704. {
  1705. if (abs(up_start_resist_stabel - i->second) < dif_resist)
  1706. {
  1707. up_stable_sum += i->second;
  1708. up_start_stable_time++;
  1709. it_up_stable_end = i;
  1710. continue;
  1711. }
  1712. else
  1713. {
  1714. break;
  1715. }
  1716. }
  1717. //时间条件限定在 15s-30s 之间
  1718. auto dif = it_up_stable_end->first - it_up_stable_start->first;
  1719. if (dif > 15000 && dif < 30000)
  1720. {
  1721. //平均值
  1722. up_avg_resist = up_stable_sum / up_start_stable_time;
  1723. if (up_avg_resist < pInfo->up_alarm_low_limit)
  1724. {
  1725. bAlarm = true;
  1726. alarm_value = up_avg_resist;
  1727. alarm_time = it_up_stable_start->first;
  1728. alarm_refer = pInfo->up_alarm_low_limit;
  1729. desc_type = 4;
  1730. }
  1731. else if (up_avg_resist < pInfo->up_warn_low_limit)
  1732. {
  1733. bWarn = true;
  1734. alarm_value = up_avg_resist;
  1735. alarm_time = it_up_stable_start->first;
  1736. alarm_refer = pInfo->up_warn_low_limit;
  1737. desc_type = 4;
  1738. }
  1739. }
  1740. }
  1741. if (bDown)
  1742. {
  1743. down_start_resist_stabel = it_down_stable_start->second;//稳定期开始的值
  1744. it_down_stable_end = it_down_stable_start;
  1745. for (auto i = it_down_stable_start; i != pData->cend(); i++)
  1746. {
  1747. if (abs(down_start_resist_stabel - i->second) < dif_resist)
  1748. {
  1749. down_stable_sum += i->second;
  1750. down_start_stable_time++;
  1751. it_down_stable_end = i;
  1752. continue;
  1753. }
  1754. else
  1755. {
  1756. break;
  1757. }
  1758. }
  1759. //时间条件限定在 15s-30s 之间
  1760. auto dif = it_down_stable_end->first - it_down_stable_start->first;
  1761. if (dif > 15000 && dif < 30000)
  1762. {
  1763. //平均值
  1764. down_avg_resist = down_stable_sum / down_start_stable_time;
  1765. if (down_avg_resist > pInfo->dw_alarm_high_limit)
  1766. {
  1767. bAlarm = true;
  1768. alarm_value = down_avg_resist;
  1769. alarm_time = it_down_stable_start->first;
  1770. alarm_refer = pInfo->dw_alarm_high_limit;
  1771. desc_type = 5;
  1772. }
  1773. else if (down_avg_resist > pInfo->dw_warn_high_limit)
  1774. {
  1775. bWarn = true;
  1776. alarm_value = down_avg_resist;
  1777. alarm_time = it_down_stable_start->first;
  1778. alarm_refer = pInfo->dw_warn_high_limit;
  1779. desc_type = 5;
  1780. }
  1781. }
  1782. }
  1783. }
  1784. else
  1785. {
  1786. //其他未实现
  1787. //TODO
  1788. assert(0);
  1789. continue;
  1790. }
  1791. }while (false);
  1792. //update
  1793. CString sql;
  1794. sql.Format("update rm_alarm_set SET time = %I64u WHERE mo = '%s' and mp = '%s' and no = %d and type = %d",
  1795. pAlarmSet->tmLastCheckTime, mo.c_str(), mp.c_str(), pAlarmSet->no, pAlarmSet->type);
  1796. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  1797. {
  1798. assert(0);
  1799. CSimpleLog::Error("语句执行失败" + sql);
  1800. }
  1801. string name1, name2, name3, out_name, in_name;
  1802. CMonitorObjectMng::Instance()->GetNameByMoMp(momp, name1, name2, name3, out_name, in_name);
  1803. auto pMoMpInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(momp);
  1804. if (item.second->no == 0 || item.second->no == 1)
  1805. {
  1806. if (name1.find("定位") != -1)
  1807. {
  1808. posi = item.second->no == 0 ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT;
  1809. }
  1810. else if (name1.find("反位") != -1)
  1811. {
  1812. posi = item.second->no == 0 ? eDaoChaPosi::DCP_INVERT : eDaoChaPosi::DCP_FIX;
  1813. }
  1814. else if (pMoMpInfo)
  1815. SPDLOG_WARN("{} 未配置定位位关系 {}, {}, {}, {}", momp, name1, name2, pMoMpInfo->name1, pMoMpInfo->name2);
  1816. }
  1817. else if (item.second->no == 2)
  1818. {
  1819. //TODO
  1820. if (posi == eDaoChaPosi::DCP_UNKNOWN)
  1821. posi = eDaoChaPosi::DCP_FIX2INVERT;
  1822. }
  1823. else
  1824. ASSERT(FALSE);
  1825. if (bAlarm == false && bWarn == false) //没有报警
  1826. {
  1827. //恢复报警
  1828. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1829. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  1830. {
  1831. auto& alarm = *it;
  1832. if (alarm->no == item.second->no && alarm->type == pAlarmSet->type && alarm->recoveryTime.wYear < 2000
  1833. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  1834. {
  1835. auto pAlarmInfo = alarm;
  1836. //1. 更新数据库
  1837. //2. 更新内存数据
  1838. //3. 上送恢复消息
  1839. if (ctAlarmTime.GetTime() == 0)
  1840. ctAlarmTime = CTime::GetCurrentTime();
  1841. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  1842. CString sql;
  1843. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  1844. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1845. if (pAlarmInfo->ack_result == 1)
  1846. {
  1847. it = pService->m_lstUnConfirmAlarm.erase(it);
  1848. continue;
  1849. }
  1850. else
  1851. {
  1852. ctAlarmTime.GetAsSystemTime(alarm->recoveryTime);
  1853. }
  1854. g_p315ClientManager->GetTcpClient()->SendAlarmData(CMonitorObjectMng::Instance()->GetZZJNO(mo + '.' + mp), 2, CTime(pAlarmInfo->time).GetTime(), ctAlarmTime.GetTime(),
  1855. pAlarmInfo->type, posi, pAlarmInfo->level, eLowHigh::LH_HIGH,
  1856. TIEDA_ACQ_VALUE(alarm->val, TIEDA_VAL_STATE::TVS_MOVEING), TIEDA_ACQ_VALUE(alarm->refer_val, TIEDA_VAL_STATE::TVS_MOVEING), 0);
  1857. }
  1858. ++it;
  1859. }
  1860. continue; //误报警继续下一个循环
  1861. }
  1862. //查找是否原有的报警已存在
  1863. uint8_t level = 0;
  1864. if (bAlarm) level = 1;
  1865. ctAlarmTime = CTime(alarm_time / 1000);
  1866. {
  1867. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1868. for (const auto& alarm : pService->m_lstUnConfirmAlarm)
  1869. {
  1870. if (alarm->no == item.second->no && alarm->type == pAlarmSet->type && level == alarm->level && alarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  1871. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  1872. {
  1873. pAlarmInfo = alarm;
  1874. break;
  1875. }
  1876. }
  1877. }
  1878. if (pAlarmInfo == nullptr)
  1879. {
  1880. bNew = true;
  1881. pAlarmInfo = new ALARM_INFO;
  1882. pAlarmInfo->event_id = hjfunc_GetGUID();
  1883. pAlarmInfo->id = ++pService->m_nAlarmID;
  1884. pAlarmInfo->level = (bAlarm == 1);
  1885. pAlarmInfo->mo = mo;
  1886. pAlarmInfo->mp = mp;
  1887. pAlarmInfo->no = item.second->no;
  1888. ctAlarmTime.GetAsSystemTime(pAlarmInfo->time);
  1889. pAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  1890. pAlarmInfo->type = item.second->type;
  1891. pAlarmInfo->val = alarm_value;
  1892. pAlarmInfo->refer_val = alarm_refer;
  1893. pAlarmInfo->posi = posi;
  1894. pAlarmInfo->zzjno = CMonitorObjectMng::Instance()->GetZZJNO(momp);
  1895. pAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pAlarmInfo->time);
  1896. char szInfo[200];
  1897. if (desc_type == 1)
  1898. {
  1899. sprintf_s(szInfo, sizeof(szInfo), "瞬时冲击力超限报警,报警值为%dN, 参考值为%dN", alarm_value, alarm_refer);//101 102
  1900. if (pAlarmInfo->type == eZL_ALARMTYPE::MAX_OVER_LIMIT)
  1901. {
  1902. g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->zzjno, 1, CTime(pAlarmInfo->time).GetTime(), 0xFFFFFFFF,
  1903. pAlarmInfo->type, pAlarmInfo->posi, pAlarmInfo->level, eLowHigh::LH_HIGH,
  1904. TIEDA_ACQ_VALUE(alarm_value, TIEDA_VAL_STATE::TVS_POLL), TIEDA_ACQ_VALUE(alarm_refer, TIEDA_VAL_STATE::TVS_POLL), 0);
  1905. }
  1906. }
  1907. else if (desc_type == 2)
  1908. sprintf_s(szInfo, sizeof(szInfo), "稳态值超限预警, 超限次数为%d次, 参考值为%dN", alarm_value, alarm_refer);
  1909. else if (desc_type == 3)
  1910. sprintf_s(szInfo, sizeof(szInfo), "转换阻力值超限报警, 报警值为%dN, 参考值为%dN", alarm_value, alarm_refer);
  1911. else if (desc_type == 4)
  1912. {
  1913. sprintf_s(szInfo, sizeof(szInfo), "摩擦力超限%s, 缩进位报警值为%dN, 参考值为:%dN", bAlarm == 1 ? "告警" : "预警", alarm_value, alarm_refer);
  1914. string direct1, direct2;
  1915. CMonitorObjectMng::Instance()->GetDirectByMoMp(mo + '.' + mp, direct1, direct2);
  1916. }
  1917. else if (desc_type == 5)
  1918. {
  1919. sprintf_s(szInfo, sizeof(szInfo), "摩擦力超限%s, 伸出位报警值为%dN, 参考值为:%dN", bAlarm == 1 ? "告警" : "预警", alarm_value, alarm_refer);
  1920. string direct1, direct2;
  1921. CMonitorObjectMng::Instance()->GetDirectByMoMp(mo + '.' + mp, direct1, direct2);
  1922. }
  1923. else
  1924. assert(0);
  1925. pAlarmInfo->desc = szInfo;
  1926. lock_guard<mutex> lock(pService->m_mtxAlarm);
  1927. pService->m_lstUnConfirmAlarm.push_back(pAlarmInfo);
  1928. }
  1929. //send
  1930. if (bNew) //不再推送
  1931. {
  1932. rapidjson::StringBuffer buffer;
  1933. auto ret = AlarmInfo2Pack(pAlarmInfo, buffer);
  1934. const char* output = buffer.GetString();
  1935. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  1936. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  1937. }
  1938. //save
  1939. if (bNew)
  1940. {
  1941. CString sql;
  1942. sql.Format("INSERT INTO [rm_alarm]([ID],[mo],[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id],posi,loworhigh,referval,[sunroof]) "\
  1943. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  1944. pAlarmInfo->id, pAlarmInfo->mo.c_str(), pAlarmInfo->mp.c_str(), pAlarmInfo->no, pAlarmInfo->type,
  1945. ctAlarmTime.Format("%Y-%m-%d %H:%M:%S"), pAlarmInfo->level, pAlarmInfo->desc.c_str(), pAlarmInfo->suggest.c_str(), pAlarmInfo->val,
  1946. pAlarmInfo->event_id.c_str(), pAlarmInfo->rel_id.c_str(), uint8_t(pAlarmInfo->posi), uint8_t(pAlarmInfo->loworhigh), pAlarmInfo->refer_val, pAlarmInfo->sunroof);
  1947. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  1948. CSimpleLog::Error("执行语句失败" + sql);
  1949. else
  1950. {
  1951. uint8_t* pack = nullptr; int len = 0;
  1952. auto packno = CResistAlarmMng::GeneralNewAlarmData(pAlarmInfo, &pack, &len);
  1953. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  1954. //g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->no, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, (WORD)pAlarmInfo->type, pAlarmInfo->val, 0, 0);
  1955. delete[] pack;
  1956. pack = nullptr;
  1957. }
  1958. }
  1959. }
  1960. //保持力下降检测
  1961. if (tmNow - pService->m_lastDetectDrop >= 30)
  1962. {
  1963. pService->m_lastDetectDrop = tmNow;
  1964. time_t tt;
  1965. time(&tt);
  1966. for (const auto& item : pService->m_alarm_set)
  1967. {
  1968. if (item.second == nullptr) continue;
  1969. if (item.second->enable == false) continue;
  1970. if (item.second->type != eZL_ALARMTYPE::RETENSION_FORCE) //这个逻辑里面只支持保持力
  1971. continue;
  1972. if ((eSuoBiPosi)item.second->no != eSuoBiPosi::SB_FIX && (eSuoBiPosi)item.second->no != eSuoBiPosi::SB_INVERT)
  1973. continue;
  1974. int nPos = item.first.find('.');
  1975. if (nPos == -1) continue;
  1976. int nPos2 = item.first.find('.', nPos + 1);
  1977. if (nPos2 == -1) continue;
  1978. string mo = item.first.substr(0, nPos);
  1979. string mp = item.first.substr(nPos + 1, nPos2 - nPos - 1);
  1980. string momp = item.first.substr(0, nPos2);
  1981. auto pInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(momp);
  1982. //SPDLOG_INFO("[保持力下降]{} {}", momp, (bool)pInfo);
  1983. //ASSERT(pInfo && pInfo->pDeivce);
  1984. if (pInfo == nullptr) continue;
  1985. if (pInfo->pDeivce == nullptr) continue;
  1986. auto pAlarmSet = (RETENSION_FORCE_DROP*)item.second;
  1987. if (pAlarmSet->tmLastCheckTime == pInfo->pDeivce->m_ctUpdateTime.GetTime()) continue;//不重复检测
  1988. pAlarmSet->tmLastCheckTime = pInfo->pDeivce->m_ctUpdateTime.GetTime();
  1989. auto pMap0 = pInfo->pDeivce->GetStatInfo(pInfo->idx, 0);
  1990. auto pMap1 = pInfo->pDeivce->GetStatInfo(pInfo->idx, 1);
  1991. //auto pMap2 = pInfo->pDeivce->GetStatInfo(pInfo->idx, 0);
  1992. std::map<time_t, tagSecondStatInfo>* pMap = nullptr;
  1993. if (((eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_FIX && pInfo->name1.find("定位") != -1) ||
  1994. ((eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_INVERT && pInfo->name1.find("反位") != -1))
  1995. {
  1996. if (pMap0 == nullptr || pMap0->size() < 3) {
  1997. //SPDLOG_INFO("[保持力下降]{}.{} {} 未满足检测条件. {} ", mo, mp,
  1998. // (eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_FIX ? "定位" : "反位", pMap0->size());
  1999. continue;
  2000. }
  2001. pMap = pMap0;
  2002. }
  2003. else if(((eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_FIX && pInfo->name2.find("定位") != -1) ||
  2004. ((eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_INVERT && pInfo->name2.find("反位") != -1))
  2005. {
  2006. if (pMap1 == nullptr || pMap1->size() < 3) {
  2007. //SPDLOG_INFO("[保持力下降]{}.{} {} 未满足检测条件. {} ", mo, mp,
  2008. // (eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_FIX ? "定位" : "反位", pMap1->size());
  2009. continue;
  2010. }
  2011. pMap = pMap1;
  2012. }
  2013. else
  2014. continue;
  2015. int nMinVal = INT_MAX;
  2016. time_t tMinTime = 0;
  2017. {
  2018. lock_guard<mutex> lock(pInfo->pDeivce->m_mtx);
  2019. auto it = pMap->end();
  2020. auto it0 = --it;
  2021. auto it1 = --it;
  2022. auto it2 = --it;
  2023. if (momp.compare(g_strMoMp) == 0)
  2024. SPDLOG_INFO("[保持力下降]{}.{} 波动值. {} {} {}", mo, mp, it2->second.dif_val, it1->second.dif_val, it0->second.dif_val);
  2025. if (it0->second.dif_val < 100 && it1->second.dif_val < 100 && it2->second.dif_val < 100)
  2026. {
  2027. nMinVal = it0->second.max_val;
  2028. tMinTime = it0->second.max_time;
  2029. if (it1->second.max_val > nMinVal)
  2030. {
  2031. nMinVal = it1->second.max_val;
  2032. tMinTime = it1->second.max_time;
  2033. }
  2034. else if (it2->second.max_val < nMinVal)
  2035. {
  2036. nMinVal = it2->second.max_val;
  2037. tMinTime = it2->second.max_time;
  2038. }
  2039. }
  2040. }
  2041. if (nMinVal != INT_MAX)
  2042. JudgeAlarm(pService, pAlarmSet, tMinTime, nMinVal, (eSuoBiPosi)pAlarmSet->no, pInfo->mo, pInfo->mp, pInfo->zzjno);
  2043. }
  2044. }
  2045. } while (pService->m_bWork);
  2046. if (bLock)
  2047. {
  2048. g_lockSync.Unlock();
  2049. bLock = false;
  2050. }
  2051. }
  2052. typedef struct tagMoMp
  2053. {
  2054. string mo;
  2055. string mp;
  2056. uint8_t no; //传感器的序号
  2057. tagMoMp(string a, string b)
  2058. {
  2059. mo = a;
  2060. mp = b;
  2061. }
  2062. tagMoMp(uint8_t a)
  2063. {
  2064. no = a;
  2065. }
  2066. };
  2067. typedef struct offline_info
  2068. {
  2069. string imei;
  2070. CTime time;
  2071. uint8_t idx = 0;
  2072. std::vector<tagMoMp> vct;
  2073. offline_info(string a, CTime b)
  2074. {
  2075. imei = a;
  2076. time = b;
  2077. }
  2078. offline_info(string a, CTime b, uint8_t c, uint8_t d)
  2079. {
  2080. imei = a;
  2081. time = b;
  2082. idx = c;
  2083. vct.push_back(tagMoMp(d));
  2084. }
  2085. };
  2086. void CResistAlarmMng::ThreadProcDevice(DWORD_PTR param)
  2087. {
  2088. auto pService = (CResistAlarmMng*)param;
  2089. this_thread::sleep_for(chrono::seconds(2));
  2090. static CTimeSpan offline_time(0, 0, 3, 0); //设备离线时间暂定5分钟
  2091. static CTimeSpan offline_limit(30, 0, 0, 0);
  2092. bool bLock = false;
  2093. do
  2094. {
  2095. if (bLock)
  2096. {
  2097. g_lockSync.Unlock();
  2098. bLock = false;
  2099. }
  2100. for (int i = 0; i < 30; i++)
  2101. {
  2102. if (false == pService->m_bWork) break;
  2103. Sleep(1000);
  2104. }
  2105. g_lockSync.ReadLock();
  2106. bLock = true;
  2107. CTime ctNow = CTime::GetCurrentTime();
  2108. //离线设备
  2109. {
  2110. auto& pMap = CMonitorObjectMng::Instance()->m_mapMoMpInfo;
  2111. for (auto& it : pMap)
  2112. {
  2113. auto pInfo = it.second;
  2114. if (pInfo == nullptr) continue;
  2115. bool bAlarm = false;
  2116. if (!pInfo->pDeivce) continue;
  2117. const auto pDevice = pInfo->pDeivce;
  2118. if (ctNow - pDevice->m_ctUpdateTime > offline_time)
  2119. bAlarm = true;
  2120. else
  2121. int ii = 0;
  2122. ALARM_INFO* pAlarmInfo = nullptr;
  2123. bool bNew = false;
  2124. auto ctAlarmTime = ctNow;
  2125. {
  2126. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  2127. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  2128. {
  2129. auto pAlarm = *it;
  2130. if (pAlarm->type == eZL_ALARMTYPE::EQUIP_OFFLINE && pAlarm->recoveryTime.wYear < 2000 &&
  2131. pAlarm->mo.compare(pInfo->mo) == 0 && pAlarm->mp.compare(pInfo->mp) == 0)
  2132. {
  2133. pAlarmInfo = pAlarm;
  2134. if (bAlarm) //连续报警
  2135. {
  2136. break;
  2137. }
  2138. else
  2139. {
  2140. //恢复报警
  2141. //更新数据库
  2142. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  2143. CString sql;
  2144. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  2145. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  2146. //315上送
  2147. g_p315ClientManager->GetTcpClient()->SendAlarmData(pInfo->zzjno, 2, CTime(pAlarm->time).GetTime(), ctAlarmTime.GetTime(), pAlarm->type,
  2148. eDaoChaPosi::DCP_UNKNOWN, pAlarm->level, eLowHigh::LH_UNKNOWN, TIEDA_ACQ_VALUE(INT_MIN), TIEDA_ACQ_VALUE(INT_MIN), 0);
  2149. if (pAlarm->ack_result == 1)
  2150. {
  2151. it = pService->m_lstUnConfirmAlarm.erase(it);
  2152. continue;
  2153. }
  2154. else
  2155. {
  2156. ctAlarmTime.GetAsSystemTime(pAlarm->recoveryTime);
  2157. }
  2158. }
  2159. }
  2160. ++it;
  2161. }
  2162. }
  2163. if (bAlarm == false) continue;
  2164. if (pAlarmInfo == nullptr)
  2165. {
  2166. bNew = true;
  2167. pAlarmInfo = new ALARM_INFO;
  2168. pAlarmInfo->event_id = hjfunc_GetGUID();
  2169. pAlarmInfo->id = ++pService->m_nAlarmID;
  2170. pAlarmInfo->level = 1;
  2171. pAlarmInfo->mo = pInfo->mo;
  2172. pAlarmInfo->mp = pInfo->mp;
  2173. pAlarmInfo->no = -1;
  2174. ctAlarmTime.GetAsSystemTime(pAlarmInfo->time);
  2175. //pAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  2176. pAlarmInfo->type = eZL_ALARMTYPE::EQUIP_OFFLINE;
  2177. pAlarmInfo->val = 0;
  2178. pAlarmInfo->zzjno = pInfo->zzjno;
  2179. char szInfo[200];
  2180. sprintf_s(szInfo, sizeof(szInfo), "设备离线时间:%s", (LPCSTR)(pDevice->m_ctUpdateTime.Format("%Y-%m-%d %H:%M:%S")));
  2181. pAlarmInfo->desc = szInfo;
  2182. pAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pAlarmInfo->time);
  2183. lock_guard<mutex> lock(pService->m_mtxAlarm);
  2184. pService->m_lstUnConfirmAlarm.push_back(pAlarmInfo);
  2185. }
  2186. //send
  2187. if (bNew) //不再推送
  2188. {
  2189. rapidjson::StringBuffer buffer;
  2190. auto ret = AlarmInfo2Pack(pAlarmInfo, buffer);
  2191. const char* output = buffer.GetString();
  2192. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  2193. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  2194. }
  2195. //save
  2196. if (bNew)
  2197. {
  2198. CString sql;
  2199. sql.Format("INSERT INTO [rm_alarm]([ID],[mo],[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id],posi,loworhigh,referval,[sunroof]) "\
  2200. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  2201. pAlarmInfo->id, pAlarmInfo->mo.c_str(), pAlarmInfo->mp.c_str(), pAlarmInfo->no, pAlarmInfo->type,
  2202. (LPCSTR)(ctAlarmTime.Format("%Y-%m-%d %H:%M:%S")), pAlarmInfo->level, pAlarmInfo->desc.c_str(), pAlarmInfo->suggest.c_str(), pAlarmInfo->val,
  2203. pAlarmInfo->event_id.c_str(), pAlarmInfo->rel_id.c_str(), uint8_t(pAlarmInfo->posi), uint8_t(pAlarmInfo->loworhigh), pAlarmInfo->refer_val, pAlarmInfo->sunroof);
  2204. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  2205. CSimpleLog::Error("执行语句失败" + sql);
  2206. else
  2207. {
  2208. uint8_t* pack = nullptr; int len = 0;
  2209. auto packno = CResistAlarmMng::GeneralNewAlarmData(pAlarmInfo, &pack, &len);
  2210. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  2211. g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->zzjno, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, pAlarmInfo->type, eDaoChaPosi::DCP_UNKNOWN, 1, eLowHigh::LH_UNKNOWN,
  2212. TIEDA_ACQ_VALUE(INT_MIN), TIEDA_ACQ_VALUE(INT_MIN), 0);
  2213. delete[] pack;
  2214. pack = nullptr;
  2215. }
  2216. }
  2217. }
  2218. }
  2219. } while (pService->m_bWork);
  2220. if (bLock)
  2221. {
  2222. g_lockSync.Unlock();
  2223. bLock = false;
  2224. }
  2225. }
  2226. void CResistAlarmMng::ThreadProcMove(DWORD_PTR param)
  2227. {
  2228. auto pService = (CResistAlarmMng*)param;
  2229. Sleep(2000);
  2230. time_t tmNow;
  2231. auto strIniPath = CSimpleLog::GetAppDir() + "DataServices.ini";
  2232. bool bLock = false;
  2233. do
  2234. {
  2235. if (bLock)
  2236. {
  2237. g_lockSync.Unlock();
  2238. bLock = false;
  2239. }
  2240. Sleep(800);
  2241. time(&tmNow);
  2242. if (pService->m_lastDetectMove > tmNow)
  2243. {
  2244. SPDLOG_INFO("时间有跳变");
  2245. pService->m_lastDetectMove = tmNow;
  2246. }
  2247. //10秒归档一次 扳动判断
  2248. if (tmNow - pService->m_lastDetectMove < 10)
  2249. continue;
  2250. g_lockSync.ReadLock();
  2251. bLock = true;
  2252. pService->m_lastDetectMove = tmNow;
  2253. char szTemp[100] = { 0 };
  2254. ::GetPrivateProfileString("SET", "momp", "", szTemp, sizeof(szTemp), strIniPath);
  2255. g_strMoMp = szTemp;
  2256. //SPDLOG_INFO("扳动判断:{}", g_strMoMp);
  2257. auto& mapDevice = CDeviceMng::Instance()->m_map_devices;
  2258. for (auto& it : mapDevice)
  2259. {
  2260. auto pDevice = it.second;
  2261. for (auto i = 0; i < 3; i++)
  2262. {
  2263. string mo, mp;
  2264. if (FALSE == CMonitorObjectMng::Instance()->IMEI2MOMP(pDevice->imei, i, mo, mp))
  2265. {
  2266. //CSimpleLog::Error(fmt::format("IMEI2MOMP fail. imei:{}, idx:{}, {}:{}", pDevice->imei, i, __FILE__, __LINE__).c_str());
  2267. continue; //没有绑定返回
  2268. }
  2269. std::map<time_t, tagSecondStatInfo> map0, map1, map2;
  2270. std::map<time_t, int>* mapData[3] = { 0 };
  2271. if (i == 0)
  2272. {
  2273. if (tmNow - pDevice->GetUpdateTime(0) < 3) continue; //3秒钟 无数据上送 开始判断
  2274. else if (tmNow - pDevice->m_tmMoveDetectTime0 < 10) continue; //距离上次应大于10秒
  2275. else if (pDevice->m_mapSecondStatInfo00.size() == 0) continue;
  2276. const auto tCheck = pDevice->m_tmMoveDetectTime0;
  2277. lock_guard<mutex> lock(pDevice->m_mtx);
  2278. for (auto it = pDevice->m_mapSecondStatInfo00.rbegin(); it != pDevice->m_mapSecondStatInfo00.rend(); it++)
  2279. {
  2280. if (it->first < tCheck) break;
  2281. map0.insert(*it);
  2282. }
  2283. for (auto it = pDevice->m_mapSecondStatInfo01.rbegin(); it != pDevice->m_mapSecondStatInfo01.rend(); it++)
  2284. {
  2285. if (it->first < tCheck) break;
  2286. map1.insert(*it);
  2287. }
  2288. for (auto it = pDevice->m_mapSecondStatInfo02.rbegin(); it != pDevice->m_mapSecondStatInfo02.rend(); it++)
  2289. {
  2290. if (it->first < tCheck) break;
  2291. map2.insert(*it);
  2292. }
  2293. if (map2.size() > 2)
  2294. { //拷贝原始数据
  2295. time_t tStart = 0;
  2296. std::map<time_t, int>::iterator itStart2;
  2297. if (tCheck)
  2298. {
  2299. for (auto ii = tCheck; ii < tmNow; ii++)
  2300. {
  2301. itStart2 = pDevice->map_resist_idx02.find(ii*1000);
  2302. if (itStart2 != pDevice->map_resist_idx02.end())
  2303. {
  2304. tStart = ii*1000;
  2305. break;
  2306. }
  2307. }
  2308. }
  2309. else
  2310. {
  2311. itStart2 = pDevice->map_resist_idx02.begin();
  2312. tStart = itStart2->first;
  2313. }
  2314. if (tStart)
  2315. {
  2316. mapData[0] = new std::map<time_t, int>(pDevice->map_resist_idx00.find(tStart), pDevice->map_resist_idx00.end());
  2317. mapData[1] = new std::map<time_t, int>(pDevice->map_resist_idx01.find(tStart), pDevice->map_resist_idx01.end());
  2318. mapData[2] = new std::map<time_t, int>(itStart2, pDevice->map_resist_idx02.end());
  2319. }
  2320. else
  2321. ASSERT(0);
  2322. }
  2323. pDevice->m_tmMoveDetectTime0 = tmNow;
  2324. }
  2325. else if (i == 1)
  2326. {
  2327. if (tmNow - pDevice->GetUpdateTime(1) < 3) continue; //3秒钟 无数据上送 开始判断
  2328. else if (tmNow - pDevice->m_tmMoveDetectTime1 < 10) continue; //距离上次应大于10秒
  2329. else if (pDevice->m_mapSecondStatInfo10.size() == 0) continue;
  2330. const auto tCheck = pDevice->m_tmMoveDetectTime1;
  2331. lock_guard<mutex> lock(pDevice->m_mtx);
  2332. for (auto it = pDevice->m_mapSecondStatInfo10.rbegin(); it != pDevice->m_mapSecondStatInfo10.rend(); it++)
  2333. {
  2334. if (it->first < tCheck) break;
  2335. map0.insert(*it);
  2336. }
  2337. for (auto it = pDevice->m_mapSecondStatInfo11.rbegin(); it != pDevice->m_mapSecondStatInfo11.rend(); it++)
  2338. {
  2339. if (it->first < tCheck) break;
  2340. map1.insert(*it);
  2341. }
  2342. for (auto it = pDevice->m_mapSecondStatInfo12.rbegin(); it != pDevice->m_mapSecondStatInfo12.rend(); it++)
  2343. {
  2344. if (it->first < tCheck) break;
  2345. map2.insert(*it);
  2346. }
  2347. if (map2.size() > 2)
  2348. { //拷贝原始数据
  2349. time_t tStart = 0;
  2350. std::map<time_t, int>::iterator itStart2;
  2351. if (tCheck)
  2352. {
  2353. for (auto ii = tCheck; ii < tmNow; ii++)
  2354. {
  2355. itStart2 = pDevice->map_resist_idx12.find(ii*1000);
  2356. if (itStart2 != pDevice->map_resist_idx12.end())
  2357. {
  2358. tStart = ii*1000;
  2359. break;
  2360. }
  2361. }
  2362. }
  2363. else
  2364. {
  2365. itStart2 = pDevice->map_resist_idx12.begin();
  2366. tStart = itStart2->first;
  2367. }
  2368. if (tStart)
  2369. {
  2370. mapData[0] = new std::map<time_t, int>(pDevice->map_resist_idx10.find(tStart), pDevice->map_resist_idx10.end());
  2371. mapData[1] = new std::map<time_t, int>(pDevice->map_resist_idx11.find(tStart), pDevice->map_resist_idx11.end());
  2372. mapData[2] = new std::map<time_t, int>(itStart2, pDevice->map_resist_idx12.end());
  2373. }
  2374. else
  2375. ASSERT(0);
  2376. }
  2377. pDevice->m_tmMoveDetectTime1 = tmNow;
  2378. }
  2379. else
  2380. {
  2381. if (tmNow - pDevice->GetUpdateTime(2) < 3) continue; //3秒钟 无数据上送 开始判断
  2382. else if (tmNow - pDevice->m_tmMoveDetectTime2 < 10) continue; //距离上次应大于10秒
  2383. else if (pDevice->m_mapSecondStatInfo20.size() == 0) continue;
  2384. //else if (pDevice->m_mapSecondStatInfo20.rbegin()->second.dif_val > 100) continue; //最后值需要稳定 2024年5月31日移除
  2385. //else if (pDevice->m_mapSecondStatInfo21.rbegin()->second.dif_val > 100) continue;
  2386. //else if (pDevice->m_mapSecondStatInfo22.rbegin()->second.dif_val > 100) continue;
  2387. const auto tCheck = pDevice->m_tmMoveDetectTime2;
  2388. lock_guard<mutex> lock(pDevice->m_mtx);
  2389. for (auto it = pDevice->m_mapSecondStatInfo20.rbegin(); it != pDevice->m_mapSecondStatInfo20.rend(); it++)
  2390. {
  2391. //RACE("20: first:%s check:%s\r\n", CTime(it->first).Format("%Y-%m-%d %H:%M:%S"), CTime(tCheck).Format("%Y-%m-%d %H:%M:%S"));
  2392. if (it->first < tCheck) break;
  2393. map0.insert(*it);
  2394. }
  2395. for (auto it = pDevice->m_mapSecondStatInfo21.rbegin(); it != pDevice->m_mapSecondStatInfo21.rend(); it++)
  2396. {
  2397. //TRACE("21: first:%s check:%s\r\n", CTime(it->first).Format("%Y-%m-%d %H:%M:%S"), CTime(tCheck).Format("%Y-%m-%d %H:%M:%S"));
  2398. if (it->first < tCheck) break;
  2399. map1.insert(*it);
  2400. }
  2401. for (auto it = pDevice->m_mapSecondStatInfo22.rbegin(); it != pDevice->m_mapSecondStatInfo22.rend(); it++)
  2402. {
  2403. //TRACE("22: first:%s check:%s\r\n", CTime(it->first).Format("%Y-%m-%d %H:%M:%S"), CTime(tCheck).Format("%Y-%m-%d %H:%M:%S"));
  2404. if (it->first < tCheck) break;
  2405. map2.insert(*it);
  2406. }
  2407. if (map2.size() > 2)
  2408. { //拷贝原始数据
  2409. time_t tStart = 0;
  2410. std::map<time_t, int>::iterator itStart2;
  2411. if (tCheck)
  2412. {
  2413. for (auto ii = tCheck; ii < tmNow; ii++)
  2414. {
  2415. itStart2 = pDevice->map_resist_idx22.find(ii*1000);
  2416. if (itStart2 != pDevice->map_resist_idx22.end())
  2417. {
  2418. tStart = ii*1000;
  2419. break;
  2420. }
  2421. }
  2422. }
  2423. else
  2424. {
  2425. itStart2 = pDevice->map_resist_idx22.begin();
  2426. tStart = itStart2->first;
  2427. }
  2428. if (tStart)
  2429. {
  2430. mapData[0] = new std::map<time_t, int>(pDevice->map_resist_idx20.find(tStart), pDevice->map_resist_idx20.end());
  2431. mapData[1] = new std::map<time_t, int>(pDevice->map_resist_idx21.find(tStart), pDevice->map_resist_idx21.end());
  2432. mapData[2] = new std::map<time_t, int>(itStart2, pDevice->map_resist_idx22.end());
  2433. }
  2434. else
  2435. assert(0);
  2436. }
  2437. TRACE("update: m_tmMoveDetectTime2:%s tmNow:%s size:%d\r\n", CTime(pDevice->m_tmMoveDetectTime2).Format("%Y-%m-%d %H:%M:%S"), CTime(tmNow).Format("%Y-%m-%d %H:%M:%S"), map2.size());
  2438. pDevice->m_tmMoveDetectTime2 = tmNow;
  2439. }
  2440. if (map2.size() <= 2) continue;
  2441. if (!mapData[0] && !mapData[1] && !mapData[2]) continue;
  2442. if (mapData[0]->size() != mapData[1]->size() || mapData[1]->size() != mapData[2]->size()) continue;
  2443. SPDLOG_INFO("扳动判断数据{}.{} map:{} 0:{} 1:{} 2:{}", mo, mp, map2.size(), mapData[0]->size(), mapData[1]->size(), mapData[2]->size());
  2444. list<CONVERT_RESIST> lstResit2; //转换力数据
  2445. list<PASS_RESIST> lstPass; //过车数据
  2446. std::map<time_t, int64_t> maxlock0, maxlock1;//锁闭力 前面时间戳, 后面 高4位显示值, 低4位报警值
  2447. std::map<time_t, int64_t> retentionforce0, retentionforce1; //保持力 时间戳, 高4位显示值, 低4位报警值
  2448. mg_per_session_data::GetMaxResistNew(map2, lstResit2, mo, mp);
  2449. if (g_bDataCompression)
  2450. {
  2451. SPDLOG_INFO("[过车判断]{}.{} 开始过车判断 size:{}", mo, mp, map0.size());
  2452. mg_per_session_data::GetPassNew(map0, mapData[0], map1, mapData[1], mo, mp, &lstPass);
  2453. SPDLOG_INFO("[过车判断]{}.{} 结束过车判断 size:{} pass:{}", mo, mp, map0.size(), lstPass.size());
  2454. //过车过滤
  2455. for (auto& item : lstPass)
  2456. {
  2457. for (auto it = lstResit2.begin(); it != lstResit2.end();)
  2458. {
  2459. if (it->tmStart * 1000 <= item.show_time && item.show_time <= (it->tmEnd + 1) * 1000)
  2460. {
  2461. it = lstResit2.erase(it);
  2462. break;
  2463. }
  2464. ++it;
  2465. }
  2466. }
  2467. }
  2468. if (lstResit2.size())
  2469. {
  2470. mg_per_session_data::GetMaxLockNew(map0, lstResit2, maxlock0, retentionforce0);
  2471. mg_per_session_data::GetMaxLockNew(map1, lstResit2, maxlock1, retentionforce1);
  2472. SPDLOG_INFO("[转换阻力][{}:{}] lock0:{} lock1:{}, resist2:{}", mo, mp, maxlock0.size(), maxlock1.size(), lstResit2.size());
  2473. }
  2474. auto pMompInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(mo + "." + mp);
  2475. ASSERT(pMompInfo);
  2476. //扳动处理逻辑
  2477. if (lstResit2.size())
  2478. {
  2479. //获取报警设置
  2480. //转换阻力报警设置项
  2481. auto pConvertResistOverLimitInfo =
  2482. (CONVERT_RESIST_OVER_LIMIT*)pService->Find(mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT);
  2483. //定位锁闭力报警设置项
  2484. auto pFixSuobiOverInfo = (SUOBI_OVER_LIMIT_INFO*)pService->Find(mo, mp, (uint8_t)eSuoBiPosi::SB_FIX, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT);
  2485. //反位
  2486. auto pInvertSuobiOverInfo = (SUOBI_OVER_LIMIT_INFO*)pService->Find(mo, mp, (uint8_t)eSuoBiPosi::SB_INVERT, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT);
  2487. //获取报警设置 end
  2488. eDaoChaPosi posiLock0;
  2489. if (pMompInfo->name1.find("定位") != -1)
  2490. {
  2491. posiLock0 = eDaoChaPosi::DCP_FIX;
  2492. for (auto& it : maxlock0)
  2493. {
  2494. int show_val = (it.second >> 32);
  2495. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0,
  2496. (int)it.second, show_val, i, (uint8_t)eDaoChaPosi::DCP_FIX, (uint8_t)ePowerName::PN_RESIST, fmt::format("锁闭力:{}", show_val));
  2497. JudgeAlarm(pService, pFixSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, pMompInfo->zzjno);
  2498. }
  2499. for (auto& it : retentionforce0)
  2500. {
  2501. int show_val = (int)it.second;
  2502. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0,
  2503. (int)it.second, show_val, i, (uint8_t)eDaoChaPosi::DCP_FIX, (uint8_t)ePowerName::PN_RETENTION, fmt::format("保持力:{}", show_val));
  2504. //JudgeAlarm(pService, pFixSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, zzjno);
  2505. }
  2506. for (auto& it : maxlock1)
  2507. {
  2508. int show_val = (it.second >> 32);
  2509. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0,
  2510. (int)it.second, show_val, i, (uint8_t)eDaoChaPosi::DCP_INVERT, (uint8_t)ePowerName::PN_RESIST, fmt::format("锁闭力:{}", show_val));
  2511. JudgeAlarm(pService, pInvertSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_INVERT, mo, mp, pMompInfo->zzjno);
  2512. }
  2513. for (auto& it : retentionforce1)
  2514. {
  2515. int show_val = (int)it.second;
  2516. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0,
  2517. (int)it.second, show_val, i, (uint8_t)eDaoChaPosi::DCP_INVERT, (uint8_t)ePowerName::PN_RETENTION, fmt::format("保持力:{}", show_val));
  2518. //JudgeAlarm(pService, pFixSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, zzjno);
  2519. }
  2520. }
  2521. else
  2522. {
  2523. posiLock0 = eDaoChaPosi::DCP_INVERT;
  2524. for (auto& it : maxlock0)
  2525. {
  2526. int show_val = (it.second >> 32);
  2527. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0, (int)it.second, show_val, i,
  2528. (uint8_t)eDaoChaPosi::DCP_INVERT, (uint8_t)ePowerName::PN_RESIST, fmt::format("锁闭力:{}", show_val));
  2529. JudgeAlarm(pService, pInvertSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_INVERT, mo, mp, pMompInfo->zzjno);
  2530. }
  2531. for (auto& it : retentionforce0)
  2532. {
  2533. int show_val = (int)it.second;
  2534. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0, (int)it.second, show_val, i,
  2535. (uint8_t)eDaoChaPosi::DCP_INVERT, (uint8_t)ePowerName::PN_RETENTION, fmt::format("保持力:{}", show_val));
  2536. //JudgeAlarm(pService, pInvertSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_INVERT, mo, mp, zzjno);
  2537. }
  2538. for (auto& it : maxlock1)
  2539. {
  2540. int show_val = (it.second >> 32);
  2541. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0, (int)it.second, show_val, i,
  2542. (uint8_t)eDaoChaPosi::DCP_FIX, (uint8_t)ePowerName::PN_RESIST, fmt::format("锁闭力:{}", show_val));
  2543. JudgeAlarm(pService, pFixSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, pMompInfo->zzjno);
  2544. }
  2545. for (auto& it : retentionforce1)
  2546. {
  2547. int show_val = (int)it.second;
  2548. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0, (int)it.second, show_val, i,
  2549. (uint8_t)eDaoChaPosi::DCP_FIX, (uint8_t)ePowerName::PN_RETENTION, fmt::format("保持力:{}", show_val));
  2550. //JudgeAlarm(pService, pFixSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, zzjno);
  2551. }
  2552. }
  2553. auto mg_315_server = g_p315ClientManager->GetTcpClient();
  2554. //SPDLOG_INFO("扳动判断数据{}.{} 转换力", mo, mp);
  2555. auto eqpno = CMonitorObjectMng::Instance()->GetZZJNO(mo + "." + mp);
  2556. for (auto& it : lstResit2)
  2557. {
  2558. //auto it_start0 = mapData[0]->find(it.tmStart * 1000);
  2559. //auto it_end0 = mapData[0]->find(it.tmEnd * 1000);
  2560. //auto it_start1 = mapData[1]->find(it.tmStart * 1000);
  2561. //auto it_end1 = mapData[1]->find(it.tmEnd * 1000);
  2562. //auto it_start2 = mapData[2]->find(it.tmStart * 1000);
  2563. //auto it_end2 = mapData[2]->find(it.tmEnd * 1000);
  2564. eDaoChaPosi posi = eDaoChaPosi::DCP_UNKNOWN;
  2565. if (it.bUpOrDown == eUpOrDownInfo::UOD_UP)
  2566. {
  2567. if (pMompInfo->in_name.find("定扳反") != -1)
  2568. {
  2569. CResistAlarmMng::InsertToDBByMove(mo, mp, it.time, it.tmStart, it.tmEnd, it.val, it.fluctuation_val, i,
  2570. (uint8_t)eDaoChaPosi::DCP_FIX2INVERT, (uint8_t)ePowerName::PN_MOVE, fmt::format("{}:{}", pMompInfo->in_name, it.fluctuation_val));
  2571. posi = eDaoChaPosi::DCP_FIX2INVERT;
  2572. CMonitorObjectMng::Instance()->UpdateZZJEPOS(mo + "." + mp, DAOCHA_POSITION::MP_INVERT);
  2573. JudgeAlarm(pService, pConvertResistOverLimitInfo, it.time, it.fluctuation_val, posi, it.bUpOrDown, mo, mp, pMompInfo->zzjno);
  2574. }
  2575. else
  2576. {
  2577. CResistAlarmMng::InsertToDBByMove(mo, mp, it.time, it.tmStart, it.tmEnd, it.val, it.fluctuation_val, i,
  2578. (uint8_t)eDaoChaPosi::DCP_INVERT2FIX, (uint8_t)ePowerName::PN_MOVE, fmt::format("{}:{}", pMompInfo->in_name, it.fluctuation_val));
  2579. posi = eDaoChaPosi::DCP_INVERT2FIX;
  2580. CMonitorObjectMng::Instance()->UpdateZZJEPOS(mo + "." + mp, DAOCHA_POSITION::MP_FIX);
  2581. JudgeAlarm(pService, pConvertResistOverLimitInfo, it.time, it.fluctuation_val, posi, it.bUpOrDown, mo, mp, pMompInfo->zzjno);
  2582. }
  2583. }
  2584. else
  2585. {
  2586. if (pMompInfo->out_name.find("定扳反") != -1)
  2587. {
  2588. CResistAlarmMng::InsertToDBByMove(mo, mp, it.time, it.tmStart, it.tmEnd, it.val, it.fluctuation_val, i,
  2589. (uint8_t)eDaoChaPosi::DCP_FIX2INVERT, (uint8_t)ePowerName::PN_MOVE, fmt::format("{}:{}", pMompInfo->out_name, it.fluctuation_val));
  2590. posi = eDaoChaPosi::DCP_FIX2INVERT;
  2591. CMonitorObjectMng::Instance()->UpdateZZJEPOS(mo + "." + mp, DAOCHA_POSITION::MP_INVERT);
  2592. JudgeAlarm(pService, pConvertResistOverLimitInfo, it.time, it.fluctuation_val, posi, it.bUpOrDown, mo, mp, pMompInfo->zzjno);
  2593. }
  2594. else
  2595. {
  2596. CResistAlarmMng::InsertToDBByMove(mo, mp, it.time, it.tmStart, it.tmEnd, it.val, it.fluctuation_val, i,
  2597. (uint8_t)eDaoChaPosi::DCP_INVERT2FIX, (uint8_t)ePowerName::PN_MOVE, fmt::format("{}:{}", pMompInfo->out_name, it.fluctuation_val));
  2598. posi = eDaoChaPosi::DCP_INVERT2FIX;
  2599. CMonitorObjectMng::Instance()->UpdateZZJEPOS(mo + "." + mp, DAOCHA_POSITION::MP_FIX);
  2600. JudgeAlarm(pService, pConvertResistOverLimitInfo, it.time, it.fluctuation_val, posi, it.bUpOrDown, mo, mp, pMompInfo->zzjno);
  2601. }
  2602. }
  2603. SPDLOG_INFO("扳动判断数据{}.{} 315 上送", mo, mp);
  2604. if(mapData[0])
  2605. mg_315_server->SendCurveData(pMompInfo->zzjno, posi, 1, true, mapData, it.tmStart, it.tmEnd);
  2606. SPDLOG_INFO("扳动判断数据结束{}.{} 315 上送", mo, mp);
  2607. }
  2608. }
  2609. //过车处理逻辑
  2610. if (lstPass.size())
  2611. {
  2612. //TODO
  2613. //SPDLOG_INFO("扳动判断数据{}.{} 315 上送", mo, mp);
  2614. //mg_315_server->SendCurveData(pMompInfo->zzjno, posi, 1, true, mapData, it.tmStart, it.tmEnd);
  2615. //SPDLOG_INFO("扳动判断数据结束{}.{} 315 上送", mo, mp);
  2616. }
  2617. delete mapData[0];
  2618. delete mapData[1];
  2619. delete mapData[2];
  2620. }
  2621. }
  2622. } while (pService->m_bWork);
  2623. if (bLock)
  2624. {
  2625. g_lockSync.Unlock();
  2626. bLock = false;
  2627. }
  2628. }
  2629. BOOL CResistAlarmMng::LoadAlarmSet()
  2630. {
  2631. CString sql = "SELECT [mo],[mp],[no],[type],[conf],[time] FROM [rm_alarm_set]";
  2632. COdbcStatement stmt;
  2633. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  2634. {
  2635. CSimpleLog::Error("执行语句失败" + sql);
  2636. return FALSE;
  2637. }
  2638. for (auto& it : m_alarm_set)
  2639. {
  2640. //delete it.second; //暂时不释放
  2641. it.second = nullptr;
  2642. }
  2643. m_alarm_set.clear();
  2644. char mo[51], mp[51], conf[501];
  2645. uint8_t no, type;
  2646. __time64_t time;
  2647. int nCol = 1;
  2648. stmt.BindCharCol(nCol++, mo, sizeof(mo));
  2649. stmt.BindCharCol(nCol++, mp, sizeof(mp));
  2650. stmt.BindTinyIntCol(nCol++, &no);
  2651. stmt.BindTinyIntCol(nCol++, &type);
  2652. stmt.BindCharCol(nCol++, conf, sizeof(conf));
  2653. stmt.BindBigIntCol(nCol++, &time);
  2654. do
  2655. {
  2656. if (stmt.FetchNext() != 0) break;
  2657. using namespace rapidjson;
  2658. int len = strlen(conf);
  2659. if (len == 0) continue;
  2660. Document doc;
  2661. if (doc.Parse(conf, len).HasParseError())
  2662. {
  2663. CSimpleLog::Error(CString("解析数据出错") + conf);
  2664. continue;
  2665. }
  2666. const auto eType = (eZL_ALARMTYPE)type;
  2667. if (eType == eZL_ALARMTYPE::MAX_OVER_LIMIT)
  2668. {
  2669. if (doc.IsArray() == false)
  2670. {
  2671. CSimpleLog::Error(CString("conf是非数组") + conf);
  2672. continue;
  2673. }
  2674. SizeType n = doc.Size();
  2675. bool enable = false;
  2676. int alarm_high_limit = INT_MAX;
  2677. int warn_high_limit = INT_MAX;
  2678. int f_alarm_high_limit = INT_MAX;
  2679. int f_warn_high_limit = INT_MAX;
  2680. for (SizeType i = 0; i < n; i++)
  2681. {
  2682. if (doc[i].HasMember("name") == false || doc[i].HasMember("val") == false || doc[i]["name"].IsString() == false || doc[i]["val"].IsString() == false)
  2683. {
  2684. assert(false);
  2685. continue;
  2686. }
  2687. string key = doc[i]["name"].GetString();
  2688. string value = doc[i]["val"].GetString();
  2689. if (key.compare("enable") == 0)
  2690. {
  2691. enable = (value.compare("true") == 0);
  2692. }
  2693. else if (key.compare("lock_alarm_high_limit") == 0)
  2694. {
  2695. alarm_high_limit = atoi(value.c_str());
  2696. }
  2697. else if (key.compare("lock_warn_high_limit") == 0)
  2698. {
  2699. warn_high_limit = atoi(value.c_str());
  2700. }
  2701. else if (key.compare("d_alarm_high_limit") == 0)
  2702. {
  2703. alarm_high_limit = atoi(value.c_str());
  2704. }
  2705. else if (key.compare("d_warn_high_limit") == 0)
  2706. {
  2707. warn_high_limit = atoi(value.c_str());
  2708. }
  2709. else if (key.compare("f_alarm_high_limit") == 0)
  2710. {
  2711. f_alarm_high_limit = atoi(value.c_str());
  2712. }
  2713. else if (key.compare("f_warn_high_limit") == 0)
  2714. {
  2715. f_warn_high_limit = atoi(value.c_str());
  2716. }
  2717. else if (key.compare("keep_alarm_high_limit") == 0)
  2718. {
  2719. f_alarm_high_limit = atoi(value.c_str());
  2720. }
  2721. else if (key.compare("keep_warn_high_limit") == 0)
  2722. {
  2723. f_warn_high_limit = atoi(value.c_str());
  2724. }
  2725. else
  2726. {
  2727. assert(0);
  2728. }
  2729. }
  2730. if (alarm_high_limit == INT_MAX || warn_high_limit == INT_MAX) continue;
  2731. auto pInfo = new MAX_OVER_LIMIT_INFO;
  2732. pInfo->enable = enable;
  2733. pInfo->no = no;
  2734. pInfo->type = eType;
  2735. pInfo->alarm_high_limit = alarm_high_limit;
  2736. pInfo->warn_high_limit = warn_high_limit;
  2737. pInfo->f_alarm_high_limit = f_alarm_high_limit;
  2738. pInfo->f_warn_high_limit = f_warn_high_limit;
  2739. pInfo->tmLastCheckTime = time;
  2740. stringstream ss;
  2741. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  2742. m_alarm_set[ss.str()] = pInfo;
  2743. }
  2744. else if (eType == eZL_ALARMTYPE::FRICTION_OVER_LIMIT)
  2745. {
  2746. if (doc.IsArray() == false)
  2747. {
  2748. CSimpleLog::Error(CString("conf是非数组") + conf);
  2749. continue;
  2750. }
  2751. SizeType n = doc.Size();
  2752. bool enable = false;
  2753. int up_alarm_low_limit = INT_MAX;
  2754. int up_warn_low_limit = INT_MAX;
  2755. int dw_alarm_high_limit = INT_MIN;
  2756. int dw_warn_high_limit = INT_MIN;
  2757. for (SizeType i = 0; i < n; i++)
  2758. {
  2759. if (doc[i].HasMember("name") == false || doc[i].HasMember("val") == false || doc[i]["name"].IsString() == false || doc[i]["val"].IsString() == false)
  2760. {
  2761. assert(false);
  2762. continue;
  2763. }
  2764. string key = doc[i]["name"].GetString();
  2765. string value = doc[i]["val"].GetString();
  2766. if (key.compare("enable") == 0)
  2767. {
  2768. enable = (value.compare("true") == 0);
  2769. }
  2770. else if (key.compare("up_alarm_low_limit") == 0)
  2771. {
  2772. up_alarm_low_limit = atoi(value.c_str());
  2773. }
  2774. else if (key.compare("up_warn_low_limit") == 0)
  2775. {
  2776. up_warn_low_limit = atoi(value.c_str());
  2777. }
  2778. else if (key.compare("dw_alarm_high_limit") == 0)
  2779. {
  2780. dw_alarm_high_limit = atoi(value.c_str());
  2781. }
  2782. else if (key.compare("dw_warn_high_limit") == 0)
  2783. {
  2784. dw_warn_high_limit = atoi(value.c_str());
  2785. }
  2786. else
  2787. {
  2788. assert(0);
  2789. }
  2790. }
  2791. auto pInfo = new FRICTION_OVER_LIMIT_INFO;
  2792. pInfo->enable = enable;
  2793. pInfo->no = no;
  2794. pInfo->type = eType;
  2795. pInfo->up_alarm_low_limit = up_alarm_low_limit;
  2796. pInfo->up_warn_low_limit = up_warn_low_limit;
  2797. pInfo->dw_alarm_high_limit = dw_alarm_high_limit;
  2798. pInfo->dw_warn_high_limit = dw_warn_high_limit;
  2799. pInfo->tmLastCheckTime = time;
  2800. stringstream ss;
  2801. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  2802. m_alarm_set[ss.str()] = pInfo;
  2803. }
  2804. else if (eType == eZL_ALARMTYPE::SUOBI_LOCK_LIMIT)
  2805. {
  2806. auto doc = yyjson_read(conf, len, 0);
  2807. if (doc == nullptr) continue;
  2808. auto root = yyjson_doc_get_root(doc);
  2809. auto pInfo = new SUOBI_OVER_LIMIT_INFO;
  2810. pInfo->type = eZL_ALARMTYPE::SUOBI_LOCK_LIMIT;
  2811. pInfo->no = no;
  2812. pInfo->enable = yyjson_get_bool(yyjson_obj_get(root, "enable"));
  2813. pInfo->alarm_low_limit = yyjson_get_int(yyjson_obj_get(root, "alarm_low_limit"));
  2814. pInfo->warn_low_limit = yyjson_get_int(yyjson_obj_get(root, "warn_low_limit"));
  2815. pInfo->alarm_high_limit = yyjson_get_int(yyjson_obj_get(root, "alarm_high_limit"));
  2816. pInfo->warn_high_limit = yyjson_get_int(yyjson_obj_get(root, "warn_high_limit"));
  2817. stringstream ss;
  2818. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  2819. m_alarm_set[ss.str()] = pInfo;
  2820. yyjson_doc_free(doc);
  2821. }
  2822. else if (eType == eZL_ALARMTYPE::CONVERT_LIMIT)
  2823. {
  2824. auto doc = yyjson_read(conf, len, 0);
  2825. if (doc == nullptr) continue;
  2826. auto root = yyjson_doc_get_root(doc);
  2827. auto pInfo = new CONVERT_RESIST_OVER_LIMIT;
  2828. pInfo->type = eZL_ALARMTYPE::CONVERT_LIMIT;
  2829. pInfo->no = 2;
  2830. pInfo->enable = yyjson_get_bool(yyjson_obj_get(root, "enable"));
  2831. pInfo->dw_alarm_low_limit = yyjson_get_int(yyjson_obj_get(root, "dw_alarm_low_limit"));
  2832. pInfo->dw_warn_low_limit = yyjson_get_int(yyjson_obj_get(root, "dw_warn_low_limit"));
  2833. pInfo->up_alarm_high_limit = yyjson_get_int(yyjson_obj_get(root, "up_alarm_high_limit"));
  2834. pInfo->up_warn_high_limit = yyjson_get_int(yyjson_obj_get(root, "up_warn_high_limit"));
  2835. stringstream ss;
  2836. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  2837. m_alarm_set[ss.str()] = pInfo;
  2838. yyjson_doc_free(doc);
  2839. }
  2840. else if (eType == eZL_ALARMTYPE::RETENSION_FORCE)
  2841. {
  2842. auto doc = yyjson_read(conf, len, 0);
  2843. if (doc == nullptr) continue;
  2844. auto root = yyjson_doc_get_root(doc);
  2845. auto pInfo = new RETENSION_FORCE_DROP;
  2846. pInfo->type = eZL_ALARMTYPE::RETENSION_FORCE;
  2847. pInfo->no = no;
  2848. pInfo->enable = yyjson_get_bool(yyjson_obj_get(root, "enable"));
  2849. pInfo->dw_alarm_low_drop = yyjson_get_int(yyjson_obj_get(root, "dw_alarm_low_drop"));
  2850. stringstream ss;
  2851. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  2852. m_alarm_set[ss.str()] = pInfo;
  2853. yyjson_doc_free(doc);
  2854. }
  2855. else
  2856. {
  2857. ASSERT(0);
  2858. }
  2859. } while (true);
  2860. CSimpleLog::Info(fmt::format("一共加载到{}条报警规则:{}", m_alarm_set.size(), sql).c_str());
  2861. return TRUE;
  2862. }
  2863. BOOL CResistAlarmMng::LoadAlarmID()
  2864. {
  2865. CString sql = "SELECT TOP 1 ID FROM RM_ALARM ORDER BY ID DESC";
  2866. COdbcStatement stmt;
  2867. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  2868. {
  2869. CSimpleLog::Error("执行语句失败" + sql);
  2870. return FALSE;
  2871. }
  2872. int alarm_id;
  2873. int nCol = 1;
  2874. stmt.BindIntCol(nCol++, &alarm_id);
  2875. do
  2876. {
  2877. if (stmt.FetchNext() != 0) break;
  2878. m_nAlarmID = alarm_id;
  2879. } while (false);
  2880. stmt.Close();
  2881. return TRUE;
  2882. }
  2883. //加载未受理和未恢复的报警
  2884. BOOL CResistAlarmMng::LoadUnAck()
  2885. {
  2886. //只加载31天内的报警
  2887. CString sql = " SELECT [ID],a.[mo],a.[mp],[no],[type],[occur_time],[level],[desc],[suggest],[val],[event_id],[rel_id],[posi],[loworhigh],[referval],[recovery_time],[sunroof],zzjno FROM [rm_alarm] as A \
  2888. LEFT JOIN(SELECT mo, mp, zzjno FROM rm_map) AS B ON a.mo = b.mo and a.mp = b.mp \
  2889. WHERE DATEADD(dd, -31, getdate()) < occur_time and (ack_result = 0 OR [recovery_time] < '2000') \
  2890. ORDER BY ID; ";
  2891. COdbcStatement stmt;
  2892. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  2893. {
  2894. ASSERT(FALSE);
  2895. CSimpleLog::Error("执行语句失败" + sql);
  2896. return FALSE;
  2897. }
  2898. char mo[51], mp[51], desc[200], suggest[200], event_id[37] = { 0 }, rel_id[37] = { 0 };
  2899. uint8_t no, type, level, posi, loworhigh, sunroof;
  2900. int val;
  2901. int id;
  2902. int zzjno;
  2903. TIMESTAMP_STRUCT ts,tsRecovery;
  2904. int referval;
  2905. int nCol = 1;
  2906. stmt.BindIntCol(nCol++, &id);
  2907. stmt.BindCharCol(nCol++, mo, sizeof(mo));
  2908. stmt.BindCharCol(nCol++, mp, sizeof(mp));
  2909. stmt.BindTinyIntCol(nCol++, &no);
  2910. stmt.BindTinyIntCol(nCol++, &type);
  2911. stmt.BindTimeStampCol(nCol++, &ts);
  2912. stmt.BindTinyIntCol(nCol++, &level);
  2913. stmt.BindCharCol(nCol++, desc, sizeof(desc));
  2914. stmt.BindCharCol(nCol++, suggest, sizeof(suggest));
  2915. stmt.BindIntCol(nCol++, &val);
  2916. stmt.BindCharCol(nCol++, event_id, 36);
  2917. stmt.BindCharCol(nCol++, rel_id, 36);
  2918. stmt.BindTinyIntCol(nCol++, &posi);
  2919. stmt.BindTinyIntCol(nCol++, &loworhigh);
  2920. stmt.BindIntCol(nCol++, &referval);
  2921. stmt.BindTimeStampCol(nCol++, &tsRecovery);
  2922. stmt.BindTinyIntCol(nCol++, &sunroof);
  2923. stmt.BindIntCol(nCol++, &zzjno);
  2924. do
  2925. {
  2926. if (stmt.FetchNext() != 0) break;
  2927. ALARM_INFO *pAlarm = new ALARM_INFO;
  2928. pAlarm->event_id = event_id;
  2929. if (rel_id[0] != 0x20)
  2930. pAlarm->rel_id = rel_id;
  2931. pAlarm->id = id;
  2932. pAlarm->mo = mo;
  2933. pAlarm->mp = mp;
  2934. pAlarm->no = no;
  2935. pAlarm->type = (eZL_ALARMTYPE)type;
  2936. ConvertData(ts, pAlarm->time);
  2937. pAlarm->val = val;
  2938. pAlarm->level = level;
  2939. pAlarm->desc = desc;
  2940. pAlarm->suggest = suggest;
  2941. pAlarm->posi = (eDaoChaPosi)posi;
  2942. pAlarm->loworhigh = (eLowHigh)loworhigh;
  2943. pAlarm->refer_val = referval;
  2944. ConvertData(tsRecovery, pAlarm->recoveryTime);
  2945. pAlarm->sunroof = sunroof;
  2946. pAlarm->zzjno = zzjno;
  2947. m_lstUnConfirmAlarm.push_back(pAlarm);
  2948. } while (true);
  2949. stmt.Close();
  2950. CSimpleLog::Info(("一共加载(" + to_string(m_lstUnConfirmAlarm.size()) + ")条未受理和未恢复的报警").c_str());
  2951. return TRUE;
  2952. }