ResistAlarm.cpp 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507
  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::JudgeAlarm2(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(), ctAlarmTime.GetTime(),
  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::JudgeAlarm(CResistAlarmMng* pService, RETENSION_FORCE_DROP* pConstRetensionForceWaveInfo,
  1176. time_t show_time, int show_val, eSuoBiPosi posi, string mo, string mp, void* pInfo)
  1177. {
  1178. if (pConstRetensionForceWaveInfo == nullptr) return;
  1179. ST_MOMP_INFO* pMoMpInfo = (ST_MOMP_INFO*)pInfo;
  1180. CTime ctShowTime(show_time / 1000);
  1181. bool bSkylight = CSkylightMng::GetInstance()->IsSkylight(&ctShowTime);
  1182. int nOldVal = INT_MIN;
  1183. if (bSkylight && show_val == 0)
  1184. {
  1185. if (posi == eSuoBiPosi::SB_FIX)
  1186. {
  1187. pMoMpInfo->fix_const_retension_force = INT_MIN;
  1188. }
  1189. else
  1190. {
  1191. pMoMpInfo->invert_const_retension_force = INT_MIN;
  1192. }
  1193. }
  1194. else
  1195. {
  1196. if (posi == eSuoBiPosi::SB_FIX)
  1197. {
  1198. nOldVal = pMoMpInfo->fix_const_retension_force;
  1199. if (nOldVal == INT_MIN)
  1200. {
  1201. pMoMpInfo->fix_const_retension_force = show_val;
  1202. SPDLOG_INFO("[常态保持力]标定{}.{} {} {}N", mo, mp, "定位", show_val);
  1203. }
  1204. }
  1205. else
  1206. {
  1207. nOldVal = pMoMpInfo->invert_const_retension_force;
  1208. if (nOldVal == INT_MIN)
  1209. {
  1210. pMoMpInfo->invert_const_retension_force = show_val;
  1211. SPDLOG_INFO("[常态保持力]标定{}.{} {} {}N", mo, mp, "反位", show_val);
  1212. }
  1213. }
  1214. }
  1215. if (pConstRetensionForceWaveInfo->enable == false) return;
  1216. if (nOldVal == INT_MIN)
  1217. return;
  1218. eLowHigh loworhigh = eLowHigh::LH_UNKNOWN;
  1219. int alarm_refer = 0, alarm_value = 0;
  1220. bool bAlarm = false, bWarn = false;
  1221. if (show_val < pConstRetensionForceWaveInfo->dw_alarm_low_drop) //当前值小于预警值
  1222. {
  1223. bWarn = true;
  1224. alarm_refer = pConstRetensionForceWaveInfo->dw_alarm_low_drop;
  1225. alarm_value = show_val;
  1226. loworhigh = eLowHigh::LH_LOW;
  1227. }
  1228. if (loworhigh == eLowHigh::LH_UNKNOWN && nOldVal != 0)
  1229. {
  1230. int nRatio = show_val * 100 / nOldVal;
  1231. //下降20%或上升40% 进行预警
  1232. if (pConstRetensionForceWaveInfo->alarm_low_percent > 0 &&
  1233. nRatio <= (100 - pConstRetensionForceWaveInfo->alarm_low_percent))
  1234. {
  1235. bWarn = true;
  1236. loworhigh = eLowHigh::LH_LOW;
  1237. alarm_value = show_val;
  1238. alarm_refer = nOldVal * (100 - pConstRetensionForceWaveInfo->alarm_low_percent) / 100;
  1239. }
  1240. else if (pConstRetensionForceWaveInfo->alarm_high_percent > 0 &&
  1241. nRatio >= (100 + pConstRetensionForceWaveInfo->alarm_high_percent))
  1242. {
  1243. bWarn = true;
  1244. loworhigh = eLowHigh::LH_HIGH;
  1245. alarm_value = show_val;
  1246. alarm_refer = nOldVal * (100 + pConstRetensionForceWaveInfo->alarm_high_percent) / 100;
  1247. }
  1248. }
  1249. ALARM_INFO* pAlarmInfo = nullptr;
  1250. CTime ctAlarmTime(show_time / 1000);
  1251. bool bNew = false;
  1252. //产生预警
  1253. if (loworhigh != eLowHigh::LH_UNKNOWN)
  1254. {
  1255. //查找是否原有的报警已存在
  1256. int level = bAlarm ? 1 : 0;// 预警
  1257. {
  1258. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1259. for (const auto& alarm : pService->m_lstUnConfirmAlarm)
  1260. {
  1261. if (alarm->no == (uint8_t)posi && alarm->type == eZL_ALARMTYPE::RETENSION_FORCE
  1262. && level == alarm->level && alarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  1263. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  1264. {
  1265. pAlarmInfo = alarm;
  1266. break;
  1267. }
  1268. }
  1269. }
  1270. if (pAlarmInfo == nullptr)
  1271. {
  1272. bNew = true;
  1273. pAlarmInfo = new ALARM_INFO;
  1274. pAlarmInfo->event_id = hjfunc_GetGUID();
  1275. pAlarmInfo->id = ++pService->m_nAlarmID;
  1276. pAlarmInfo->level = level;// 预警
  1277. pAlarmInfo->mo = mo;
  1278. pAlarmInfo->mp = mp;
  1279. pAlarmInfo->no = (uint8_t)posi;
  1280. ctAlarmTime.GetAsSystemTime(pAlarmInfo->time);
  1281. pAlarmInfo->time.wMilliseconds = show_time % 1000;
  1282. pAlarmInfo->type = eZL_ALARMTYPE::RETENSION_FORCE;
  1283. pAlarmInfo->val = alarm_value;
  1284. pAlarmInfo->refer_val = alarm_refer;
  1285. pAlarmInfo->posi = (posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT);
  1286. pAlarmInfo->loworhigh = loworhigh;
  1287. pAlarmInfo->zzjno = pMoMpInfo->zzjno;
  1288. pAlarmInfo->sunroof = bSkylight;
  1289. char szInfo[200] = { 0 };
  1290. sprintf_s(szInfo, sizeof(szInfo), "保持力异常预警,位置:%s, 报警值为%dN, 上一次值:%dN, 参考值为%dN",
  1291. posi == eSuoBiPosi::SB_FIX ? "定位" : "反位", show_val, nOldVal, alarm_refer);
  1292. pAlarmInfo->desc = szInfo;
  1293. lock_guard<mutex> lock(pService->m_mtxAlarm);
  1294. pService->m_lstUnConfirmAlarm.push_back(pAlarmInfo);
  1295. }
  1296. //主动推送315
  1297. if (bNew)
  1298. {
  1299. g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->zzjno, 1, CTime(pAlarmInfo->time).GetTime(), 0xFFFFFFFF,
  1300. pAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pAlarmInfo->level, loworhigh,
  1301. TIEDA_ACQ_VALUE(pAlarmInfo->val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), TIEDA_ACQ_VALUE(pAlarmInfo->refer_val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), 0);
  1302. }
  1303. if (bNew)
  1304. {
  1305. CString sql;
  1306. 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]) "\
  1307. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  1308. pAlarmInfo->id, pAlarmInfo->mo.c_str(), pAlarmInfo->mp.c_str(), pAlarmInfo->no, pAlarmInfo->type,
  1309. ctAlarmTime.Format("%Y-%m-%d %H:%M:%S"), pAlarmInfo->level, pAlarmInfo->desc.c_str(), pAlarmInfo->suggest.c_str(), pAlarmInfo->val,
  1310. pAlarmInfo->event_id.c_str(), pAlarmInfo->rel_id.c_str(), uint8_t(pAlarmInfo->posi), uint8_t(pAlarmInfo->loworhigh), pAlarmInfo->refer_val,
  1311. pAlarmInfo->sunroof);
  1312. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  1313. CSimpleLog::Error("执行语句失败" + sql);
  1314. else
  1315. {
  1316. uint8_t* pack = nullptr; int len = 0;
  1317. auto packno = CResistAlarmMng::GeneralNewAlarmData(pAlarmInfo, &pack, &len);
  1318. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  1319. delete[] pack;
  1320. pack = nullptr;
  1321. }
  1322. }
  1323. }
  1324. else
  1325. {
  1326. //恢复
  1327. {
  1328. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1329. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  1330. {
  1331. auto pAlarm = *it;
  1332. if (pAlarm->no == (uint8_t)posi && pAlarm->type == eZL_ALARMTYPE::RETENSION_FORCE && pAlarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  1333. && pAlarm->mo.compare(mo) == 0 && pAlarm->mp.compare(mp) == 0)
  1334. {
  1335. pAlarmInfo = pAlarm;
  1336. if (pAlarmInfo)
  1337. {
  1338. //恢复
  1339. //1. 更新数据库
  1340. //2. 更新内存数据
  1341. //3. 上送恢复消息
  1342. if (ctAlarmTime.GetTime() == 0) ctAlarmTime = CTime::GetCurrentTime();
  1343. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  1344. CString sql;
  1345. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  1346. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1347. if (pAlarmInfo->ack_result == 1)
  1348. {
  1349. it = pService->m_lstUnConfirmAlarm.erase(it);
  1350. continue;
  1351. }
  1352. else
  1353. {
  1354. ctAlarmTime.GetAsSystemTime(pAlarm->recoveryTime);
  1355. }
  1356. g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->zzjno, 2, CTime(pAlarmInfo->time).GetTime(), ctAlarmTime.GetTime(),
  1357. pAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pAlarmInfo->level, pAlarm->loworhigh,
  1358. TIEDA_ACQ_VALUE(pAlarm->val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), TIEDA_ACQ_VALUE(pAlarm->refer_val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), 0);
  1359. }
  1360. }
  1361. it++;
  1362. }
  1363. }
  1364. return;
  1365. }
  1366. }
  1367. void CResistAlarmMng::JudgeAlarm3(CResistAlarmMng* pService, RETENSION_FORCE_DROP* pRetensionForceDropInfo, time_t show_time, int show_val,
  1368. eSuoBiPosi posi, string mo, string mp, uint32_t zzjno)
  1369. {
  1370. if (pRetensionForceDropInfo == nullptr || pRetensionForceDropInfo->enable == false) return;
  1371. bool bNew = false;
  1372. ALARM_INFO* pAlarmInfo = nullptr;
  1373. bool bAlarm = false, bWarn = false; int alarm_value = 0, alarm_refer; time_t alarm_time = show_time;
  1374. CTime ctAlarmTime = CTime(alarm_time / 1000);
  1375. eLowHigh loworhigh = eLowHigh::LH_HIGH; //0:最小值 1:最大值
  1376. {
  1377. if (show_val < pRetensionForceDropInfo->dw_alarm_low_drop)
  1378. {
  1379. bAlarm = true;
  1380. alarm_refer = pRetensionForceDropInfo->dw_alarm_low_drop;
  1381. alarm_value = show_val;
  1382. //loworhigh = eLowHigh::LH_LOW;
  1383. }
  1384. if (bAlarm == false && bWarn == false)
  1385. {
  1386. {
  1387. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1388. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  1389. {
  1390. auto pAlarm = *it;
  1391. if (pAlarm->no == (uint8_t)posi && pAlarm->type == eZL_ALARMTYPE::RETENSION_FORCE && pAlarm->recoveryTime.wYear < 2000 //预警和报警统一恢复
  1392. && pAlarm->mo.compare(mo) == 0 && pAlarm->mp.compare(mp) == 0)
  1393. {
  1394. pAlarmInfo = pAlarm;
  1395. if (pAlarmInfo)
  1396. {
  1397. //恢复
  1398. //1. 更新数据库
  1399. //2. 更新内存数据
  1400. //3. 上送恢复消息
  1401. if (ctAlarmTime.GetTime() == 0) ctAlarmTime = CTime::GetCurrentTime();
  1402. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  1403. CString sql;
  1404. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  1405. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  1406. if (pAlarmInfo->ack_result == 1)
  1407. {
  1408. it = pService->m_lstUnConfirmAlarm.erase(it);
  1409. continue;
  1410. }
  1411. else
  1412. {
  1413. ctAlarmTime.GetAsSystemTime(pAlarm->recoveryTime);
  1414. }
  1415. g_p315ClientManager->GetTcpClient()->SendAlarmData(zzjno, 2, CTime(pAlarmInfo->time).GetTime(), alarm_time / 1000,
  1416. pAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pAlarmInfo->level, pAlarm->loworhigh,
  1417. TIEDA_ACQ_VALUE(pAlarm->val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), TIEDA_ACQ_VALUE(pAlarm->refer_val, TIEDA_VAL_STATE::TVS_AFTER_MOVE), 0);
  1418. }
  1419. }
  1420. it++;
  1421. }
  1422. }
  1423. return;
  1424. }
  1425. //查找是否原有的报警已存在
  1426. uint8_t level = 0;
  1427. if (bAlarm) level = 1;
  1428. {
  1429. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  1430. for (const auto& alarm : pService->m_lstUnConfirmAlarm)
  1431. {
  1432. if (alarm->no == (uint8_t)posi && alarm->type == eZL_ALARMTYPE::RETENSION_FORCE && level == alarm->level && alarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  1433. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  1434. {
  1435. pAlarmInfo = alarm;
  1436. break;
  1437. }
  1438. }
  1439. }
  1440. ALARM_INFO* pNewAlarmInfo = nullptr;
  1441. {
  1442. bNew = true;
  1443. pNewAlarmInfo = new ALARM_INFO;
  1444. pNewAlarmInfo->event_id = hjfunc_GetGUID();
  1445. pNewAlarmInfo->rel_id = pAlarmInfo ? pAlarmInfo->event_id : "";
  1446. pNewAlarmInfo->id = ++pService->m_nAlarmID;
  1447. pNewAlarmInfo->level = (bAlarm == 1);
  1448. pNewAlarmInfo->mo = mo;
  1449. pNewAlarmInfo->mp = mp;
  1450. pNewAlarmInfo->no = (uint8_t)posi;
  1451. ctAlarmTime.GetAsSystemTime(pNewAlarmInfo->time);
  1452. pNewAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  1453. pNewAlarmInfo->type = eZL_ALARMTYPE::RETENSION_FORCE;
  1454. pNewAlarmInfo->val = alarm_value;
  1455. pNewAlarmInfo->refer_val = alarm_refer;
  1456. pNewAlarmInfo->posi = (posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT);
  1457. pNewAlarmInfo->loworhigh = loworhigh;
  1458. pNewAlarmInfo->zzjno = zzjno;
  1459. pNewAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pNewAlarmInfo->time);
  1460. char szInfo[200] = { 0 };
  1461. sprintf_s(szInfo, sizeof(szInfo), "保持力下降,位置:%s, 报警值为%dN, 参考值为%dN",
  1462. posi == eSuoBiPosi::SB_FIX ? "定位" : "反位", alarm_value, alarm_refer);
  1463. pNewAlarmInfo->desc = szInfo;
  1464. lock_guard<mutex> lock(pService->m_mtxAlarm);
  1465. pService->m_lstUnConfirmAlarm.push_back(pNewAlarmInfo);
  1466. }
  1467. //send
  1468. //if (bNew) //不再推送
  1469. {
  1470. rapidjson::StringBuffer buffer;
  1471. auto ret = AlarmInfo2Pack(pNewAlarmInfo, buffer);
  1472. const char* output = buffer.GetString();
  1473. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  1474. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  1475. }
  1476. //主动推送315
  1477. //if (bNew)
  1478. {
  1479. g_p315ClientManager->GetTcpClient()->SendAlarmData(zzjno, 1, CTime(pNewAlarmInfo->time).GetTime(), 0xFFFFFFFF,
  1480. pNewAlarmInfo->type, posi == eSuoBiPosi::SB_FIX ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT, pNewAlarmInfo->level, loworhigh,
  1481. TIEDA_ACQ_VALUE(alarm_value, TIEDA_VAL_STATE::TVS_AFTER_MOVE), TIEDA_ACQ_VALUE(alarm_refer, TIEDA_VAL_STATE::TVS_AFTER_MOVE), 0);
  1482. }
  1483. //save
  1484. //if (bNew)
  1485. {
  1486. CString sql;
  1487. 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]) "\
  1488. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  1489. pNewAlarmInfo->id, pNewAlarmInfo->mo.c_str(), pNewAlarmInfo->mp.c_str(), pNewAlarmInfo->no, pNewAlarmInfo->type,
  1490. ctAlarmTime.Format("%Y-%m-%d %H:%M:%S"), pNewAlarmInfo->level, pNewAlarmInfo->desc.c_str(), pNewAlarmInfo->suggest.c_str(), pNewAlarmInfo->val,
  1491. pNewAlarmInfo->event_id.c_str(), pNewAlarmInfo->rel_id.c_str(), uint8_t(pNewAlarmInfo->posi), uint8_t(pNewAlarmInfo->loworhigh), pNewAlarmInfo->refer_val,
  1492. pNewAlarmInfo->sunroof);
  1493. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  1494. CSimpleLog::Error("执行语句失败" + sql);
  1495. else
  1496. {
  1497. uint8_t* pack = nullptr; int len = 0;
  1498. auto packno = CResistAlarmMng::GeneralNewAlarmData(pNewAlarmInfo, &pack, &len);
  1499. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  1500. //g_p315ClientManager->GetTcpClient()->SendAlarmData(pNewAlarmInfo->no, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, (WORD)pNewAlarmInfo->type, pNewAlarmInfo->val, 0, 0);
  1501. delete[] pack;
  1502. pack = nullptr;
  1503. }
  1504. }
  1505. }
  1506. }
  1507. 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)
  1508. {
  1509. int start_value = 0;
  1510. for (; i != j; i++)
  1511. {
  1512. if (i->second > refer_value)
  1513. {
  1514. if (start_value == 1)
  1515. {
  1516. //原来大于,现在也是大于
  1517. continue;
  1518. }
  1519. else
  1520. {
  1521. //原来小于或者无效,现在大于
  1522. start_value = 1;
  1523. hight_num++;
  1524. }
  1525. }
  1526. else
  1527. {
  1528. if (start_value == -1)
  1529. {
  1530. //原来小于,现在小于等于
  1531. continue;
  1532. }
  1533. else
  1534. {
  1535. //原来大于或者无效,现在小于
  1536. start_value = -1;
  1537. low_num++;
  1538. }
  1539. }
  1540. }
  1541. return TRUE;
  1542. }
  1543. void CResistAlarmMng::ThreadProcAlarmSet(DWORD_PTR param)
  1544. {
  1545. auto pService = (CResistAlarmMng*)param;
  1546. Sleep(2000);
  1547. time_t tmNow;
  1548. bool bLock = false;
  1549. do
  1550. {
  1551. if (bLock)
  1552. {
  1553. g_lockSync.Unlock();
  1554. bLock = false;
  1555. }
  1556. Sleep(800);
  1557. time(&tmNow);
  1558. if (tmNow == pService->m_lastDetectTime)
  1559. continue;
  1560. pService->m_lastDetectTime = tmNow;
  1561. bLock = true;
  1562. g_lockSync.ReadLock();
  1563. //1秒判断一次
  1564. for (const auto& item : pService->m_alarm_set)
  1565. {
  1566. if (item.second == nullptr) continue;
  1567. if (item.second->enable == false) continue;
  1568. if (item.second->type != eZL_ALARMTYPE::MAX_OVER_LIMIT && item.second->type != eZL_ALARMTYPE::FRICTION_OVER_LIMIT) //这个逻辑里面只支持这两种
  1569. continue;
  1570. if (item.second->no < 0 || item.second->no > 2) continue;
  1571. int nPos = item.first.find('.');
  1572. if (nPos == -1) continue;
  1573. int nPos2 = item.first.find('.', nPos + 1);
  1574. if (nPos2 == -1) continue;
  1575. string mo = item.first.substr(0, nPos);
  1576. string mp = item.first.substr(nPos + 1, nPos2 - nPos - 1);
  1577. string momp = item.first.substr(0, nPos2);
  1578. bool bNew = false;
  1579. ALARM_INFO *pAlarmInfo = nullptr;
  1580. CTime ctAlarmTime;
  1581. string imei; int idx;
  1582. auto ret = CMonitorObjectMng::Instance()->MOMP2IMEI(momp, imei, idx);
  1583. if (ret == false) continue;
  1584. #ifdef _DEBUG
  1585. if (strcmp("860588048955283", imei.c_str()) != 0) continue;
  1586. #endif
  1587. auto pDevice = CDeviceMng::Instance()->Find(imei);
  1588. if (pDevice == nullptr)
  1589. {
  1590. CSimpleLog::Error(("找不到imei(" + imei + ")" + std::to_string(__LINE__)).c_str());
  1591. continue;
  1592. }
  1593. int desc_type = 0;
  1594. bool bAlarm = false, bWarn = false; int alarm_value = 0; time_t alarm_time = 0;
  1595. eDaoChaPosi posi = eDaoChaPosi::DCP_UNKNOWN;
  1596. 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;
  1597. time_t tmNow; time(&tmNow);
  1598. BASE_INFO* pAlarmSet = item.second;
  1599. do{
  1600. Sleep(0);
  1601. lock_guard<mutex> lock(pDevice->m_mtx);
  1602. std::map<time_t, int>* pData = pDevice->GetMapData(idx, item.second->no);
  1603. assert(pData);
  1604. if (pData == nullptr) continue;
  1605. if (pData->size() == 0) continue;
  1606. if (item.second == 0) continue;
  1607. if (item.second->type == eZL_ALARMTYPE::MAX_OVER_LIMIT)
  1608. {
  1609. auto pInfo = (MAX_OVER_LIMIT_INFO*)pAlarmSet;
  1610. #ifdef _DEBUG
  1611. if (pInfo->tmLastCheckTime > pData->rbegin()->first)
  1612. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1613. #endif
  1614. auto it = pData->cbegin();
  1615. if (pInfo->tmLastCheckTime == 0) pInfo->tmLastCheckTime = pData->rbegin()->first;
  1616. else if (pInfo->tmLastCheckTime != 0) it = pData->find(pInfo->tmLastCheckTime);
  1617. if (it == pData->cend())
  1618. {
  1619. it = pData->find(pInfo->tmLastCheckTime / 1000 * 1000);
  1620. if (it == pData->cend())
  1621. {
  1622. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1623. continue;
  1624. }
  1625. }
  1626. if (pInfo->tmLastCheckTime == pData->rbegin()->first) //没有新的数据
  1627. continue;
  1628. #ifdef _DEBUG
  1629. 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,
  1630. CTime(pInfo->tmLastCheckTime / 1000).Format("%Y-%m-%d %H:%M:%S"), CTime(pData->rbegin()->first / 1000).Format("%Y-%m-%d %H:%M:%S"));
  1631. #endif // _DEBUG
  1632. time_t tmStartSteady = pInfo->tmLastCheckTime / 1000 - 3; //稳态报警检测开始, 从上次3秒前到当前秒
  1633. if (item.second->no == 2)
  1634. {
  1635. if (pInfo->alarm_high_limit > pInfo->f_alarm_high_limit)
  1636. {//定板反
  1637. high_alarm_limit = pInfo->alarm_high_limit;
  1638. high_warn_limit = pInfo->warn_high_limit;
  1639. low_alarm_limit = pInfo->f_alarm_high_limit;
  1640. low_warn_limit = pInfo->f_warn_high_limit;
  1641. posi = eDaoChaPosi::DCP_FIX2INVERT;
  1642. }
  1643. else
  1644. {//反板定
  1645. low_alarm_limit = pInfo->alarm_high_limit;
  1646. low_warn_limit = pInfo->warn_high_limit;
  1647. high_alarm_limit = pInfo->f_alarm_high_limit;
  1648. high_warn_limit = pInfo->f_warn_high_limit;
  1649. posi = eDaoChaPosi::DCP_FIX2INVERT;
  1650. }
  1651. }
  1652. for (++it; it != pData->cend(); ++it)
  1653. {
  1654. //CString str = CTime(it->first / 1000).Format("%Y-%m-%d %H:%M:%S");
  1655. if (it->second == INVLID_VAL)
  1656. {
  1657. pInfo->tmLastCheckTime = it->first;
  1658. continue;
  1659. }
  1660. if (item.second->no < 2)
  1661. {
  1662. //1,2 锁闭力报警
  1663. if (it->second > pInfo->alarm_high_limit)
  1664. {
  1665. bAlarm = true;
  1666. alarm_refer = pInfo->alarm_high_limit;
  1667. alarm_value = it->second;
  1668. alarm_time = it->first;
  1669. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1670. desc_type = 1;
  1671. break;
  1672. }
  1673. //else if (it->second > pInfo->warn_high_limit) //稳态报警单独判断
  1674. //{
  1675. // bWarn = true;
  1676. // alarm_refer = pInfo->warn_high_limit;
  1677. // alarm_value = it->second;
  1678. // alarm_time = it->first;
  1679. // pInfo->tmLastCheckTime = pData->rbegin()->first;
  1680. //}
  1681. }
  1682. else
  1683. {
  1684. assert(item.second->no == 2);
  1685. if (pInfo->alarm_high_limit == INT_MAX || pInfo->f_alarm_high_limit == INT_MAX || pInfo->alarm_high_limit == pInfo->f_alarm_high_limit)
  1686. break;
  1687. desc_type = 3;
  1688. if (it->second > high_alarm_limit)
  1689. {
  1690. bAlarm = true;
  1691. alarm_refer = high_alarm_limit;
  1692. alarm_value = it->second;
  1693. alarm_time = it->first;
  1694. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1695. break;
  1696. }
  1697. else if (it->second < low_alarm_limit)
  1698. {
  1699. bAlarm = true;
  1700. alarm_refer = low_alarm_limit;
  1701. alarm_value = it->second;
  1702. alarm_time = it->first;
  1703. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1704. break;
  1705. }
  1706. else if (low_warn_limit == INT_MIN || high_warn_limit == INT_MIN)
  1707. {
  1708. //未设置预警值
  1709. }
  1710. else if (it->second > high_warn_limit)
  1711. {
  1712. bWarn = true;
  1713. alarm_refer = high_warn_limit;
  1714. alarm_value = it->second;
  1715. alarm_time = it->first;
  1716. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1717. }
  1718. else if (it->second < low_warn_limit)
  1719. {
  1720. bWarn = true;
  1721. alarm_refer = low_warn_limit;
  1722. alarm_value = it->second;
  1723. alarm_time = it->first;
  1724. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1725. }
  1726. }
  1727. pInfo->tmLastCheckTime = it->first;
  1728. }
  1729. //稳态报警判断
  1730. if (item.second->no < 2 && bAlarm == false)
  1731. {
  1732. time_t tmEnd = pInfo->tmLastCheckTime / 1000;
  1733. int dif = tmEnd - tmStartSteady;
  1734. for (time_t i = tmStartSteady; i < tmEnd - 3; i++)
  1735. {
  1736. #ifdef _DEBUG
  1737. COleDateTime t(i);
  1738. TRACE("%s\r\n", t.Format("%Y-%m-%d %H:%M:%S"));
  1739. #endif // _DEBUG
  1740. auto j = pData->find(i * 1000);
  1741. if (j == pData->end()) continue;
  1742. time_t t2 = (i + 1) * 1000;
  1743. auto k = pData->find(t2);
  1744. if (k == pData->end()) continue;
  1745. t2 = (i + 2) * 1000;
  1746. k = pData->find(t2);
  1747. if (k == pData->end()) continue;
  1748. //连续3秒在线,继续找下一个结束点
  1749. t2 = (i + 3) * 1000;
  1750. auto g = pData->find(t2);
  1751. if (g == pData->end())
  1752. {
  1753. for (++k; k != pData->end(); k++)
  1754. {
  1755. if (k->first >= t2)
  1756. {
  1757. g = k;
  1758. break;
  1759. }
  1760. }
  1761. }
  1762. if (g == pData->end())
  1763. continue;
  1764. int high_num = 0, low_num = 0;
  1765. ConmpareValue(j, g, pInfo->warn_high_limit, high_num, low_num);
  1766. if (high_num > 0 && high_num <= 2)
  1767. {
  1768. bWarn = true;
  1769. desc_type = 2;
  1770. alarm_refer = pInfo->warn_high_limit;
  1771. alarm_value = high_num;
  1772. alarm_time = i * 1000; //报警时间
  1773. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1774. break;
  1775. }
  1776. }
  1777. }
  1778. }
  1779. else if (item.second->type == eZL_ALARMTYPE::FRICTION_OVER_LIMIT)
  1780. {
  1781. auto pInfo = (FRICTION_OVER_LIMIT_INFO*)item.second;
  1782. #ifdef _DEBUG
  1783. if (pInfo->tmLastCheckTime > pData->rbegin()->first)
  1784. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1785. #endif
  1786. auto it = pData->cbegin();
  1787. if (pInfo->tmLastCheckTime == 0) pInfo->tmLastCheckTime = pData->rbegin()->first;
  1788. else if (pInfo->tmLastCheckTime != 0) it = pData->find(pInfo->tmLastCheckTime);
  1789. if (it == pData->cend())
  1790. {
  1791. it = pData->find(pInfo->tmLastCheckTime / 1000 * 1000);
  1792. if (it == pData->cend())
  1793. {
  1794. pInfo->tmLastCheckTime = pData->rbegin()->first;
  1795. continue;
  1796. }
  1797. }
  1798. if (pInfo->tmLastCheckTime == pData->rbegin()->first) //没有新的数据
  1799. continue;
  1800. if (tmNow * 1000 - pInfo->tmLastCheckTime < 5000) //5秒检测一次
  1801. continue;
  1802. #ifdef _DEBUG
  1803. 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,
  1804. CTime(pInfo->tmLastCheckTime / 1000).Format("%Y-%m-%d %H:%M:%S"), CTime(pData->rbegin()->first / 1000).Format("%Y-%m-%d %H:%M:%S"));
  1805. #endif // _DEBUG
  1806. if (item.second->no != 2) continue;
  1807. //从检测时间往前取30秒的数据
  1808. static const int dif_resist = 200; //稳定期判定差值
  1809. int up_max_resist = INT_MIN; //上最大值
  1810. time_t up_max_time = 0;
  1811. int up_avg_resist = INT_MIN; //上稳定期平均值
  1812. uint64_t up_stable_sum = 0; //上稳定期的和
  1813. int up_stable_time = 0; //上稳定期的数值个数
  1814. int up_start_resist_stabel = INT_MIN; //上稳定期开始值
  1815. time_t up_start_stable_time = 0; //规定是最大值之后的2秒
  1816. time_t up_end_stable_time = 0;//稳定结束的时间
  1817. int up_dif_time_stable = 0; //上稳定期的时长
  1818. int down_max_resist = INT_MAX; //上最大值
  1819. time_t down_max_time = 0;
  1820. int down_avg_resist = INT_MAX; //上稳定期平均值
  1821. uint64_t down_stable_sum = 0; //上稳定期的和
  1822. int down_stable_time = 0; //上稳定期的数值个数
  1823. int down_start_resist_stabel = INT_MAX; //上稳定期开始值
  1824. time_t down_start_stable_time = 0; //规定是最大值之后的2秒
  1825. int down_dif_time_stable = 0; //上稳定期的时长
  1826. time_t tmEnd = pData->rbegin()->first / 1000 + 1; //结束时间
  1827. time_t tmStart = tmEnd - 36;//开始时间是从上次35秒到当前秒
  1828. 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;
  1829. //寻找起始时间
  1830. for (time_t i = tmStart; i < tmEnd; i++)
  1831. {
  1832. #ifdef _DEBUG
  1833. COleDateTime t(i);
  1834. TRACE("%s\r\n", t.Format("%Y-%m-%d %H:%M:%S"));
  1835. #endif // _DEBUG
  1836. it_start = pData->find(i * 1000);
  1837. if (it_start != pData->cend()) break;
  1838. }
  1839. pInfo->tmLastCheckTime = pData->rbegin()->first; //赋值最后的时间
  1840. if (it_start == pData->cend()) break;
  1841. bool bUp = true, bDown = true;
  1842. //先找最大值
  1843. for (auto i = it_start; i != pData->cend(); i++)
  1844. {
  1845. //寻找最大的点
  1846. if (i->second > up_max_resist)
  1847. {
  1848. up_max_resist = i->second;
  1849. it_up_max = i;
  1850. }
  1851. //寻找最小的点
  1852. if (i->second < down_max_resist)
  1853. {
  1854. down_max_resist = i->second;
  1855. it_down_min = i;
  1856. }
  1857. }
  1858. if (pInfo->tmLastCheckTime - it_up_max->first < 17000) bUp = false; //小于17秒
  1859. if (pInfo->tmLastCheckTime - it_down_min->first < 1700) bDown = false; //小于17秒
  1860. if(bUp == false && bDown == false) break;
  1861. //再找稳定开始
  1862. if (bUp)
  1863. {
  1864. it_up_stable_start = it_up_max;
  1865. for (auto i = it_up_max; i != pData->cend(); i++)
  1866. {
  1867. if (i->first - it_up_max->first < 2000) continue;
  1868. else
  1869. {
  1870. it_up_stable_start = i;
  1871. break;
  1872. }
  1873. }
  1874. if (it_up_stable_start == it_up_max) bUp = false;
  1875. }
  1876. if (bDown)
  1877. {
  1878. it_down_stable_start = it_down_min;
  1879. for (auto i = it_down_min; i != pData->cend(); i++)
  1880. {
  1881. if (i->first - it_down_min->first < 2000) continue;
  1882. else
  1883. {
  1884. it_down_stable_start = i;
  1885. break;
  1886. }
  1887. }
  1888. if (it_down_stable_start == it_down_min) bDown = false;
  1889. }
  1890. if (bUp == false && bDown == false) break;
  1891. if (bUp)
  1892. {
  1893. up_start_resist_stabel = it_up_stable_start->second;//稳定期开始的值
  1894. it_up_stable_end = it_up_stable_start;
  1895. for (auto i = it_up_stable_start; i != pData->cend(); i++)
  1896. {
  1897. if (abs(up_start_resist_stabel - i->second) < dif_resist)
  1898. {
  1899. up_stable_sum += i->second;
  1900. up_start_stable_time++;
  1901. it_up_stable_end = i;
  1902. continue;
  1903. }
  1904. else
  1905. {
  1906. break;
  1907. }
  1908. }
  1909. //时间条件限定在 15s-30s 之间
  1910. auto dif = it_up_stable_end->first - it_up_stable_start->first;
  1911. if (dif > 15000 && dif < 30000)
  1912. {
  1913. //平均值
  1914. up_avg_resist = up_stable_sum / up_start_stable_time;
  1915. if (up_avg_resist < pInfo->up_alarm_low_limit)
  1916. {
  1917. bAlarm = true;
  1918. alarm_value = up_avg_resist;
  1919. alarm_time = it_up_stable_start->first;
  1920. alarm_refer = pInfo->up_alarm_low_limit;
  1921. desc_type = 4;
  1922. }
  1923. else if (up_avg_resist < pInfo->up_warn_low_limit)
  1924. {
  1925. bWarn = true;
  1926. alarm_value = up_avg_resist;
  1927. alarm_time = it_up_stable_start->first;
  1928. alarm_refer = pInfo->up_warn_low_limit;
  1929. desc_type = 4;
  1930. }
  1931. }
  1932. }
  1933. if (bDown)
  1934. {
  1935. down_start_resist_stabel = it_down_stable_start->second;//稳定期开始的值
  1936. it_down_stable_end = it_down_stable_start;
  1937. for (auto i = it_down_stable_start; i != pData->cend(); i++)
  1938. {
  1939. if (abs(down_start_resist_stabel - i->second) < dif_resist)
  1940. {
  1941. down_stable_sum += i->second;
  1942. down_start_stable_time++;
  1943. it_down_stable_end = i;
  1944. continue;
  1945. }
  1946. else
  1947. {
  1948. break;
  1949. }
  1950. }
  1951. //时间条件限定在 15s-30s 之间
  1952. auto dif = it_down_stable_end->first - it_down_stable_start->first;
  1953. if (dif > 15000 && dif < 30000)
  1954. {
  1955. //平均值
  1956. down_avg_resist = down_stable_sum / down_start_stable_time;
  1957. if (down_avg_resist > pInfo->dw_alarm_high_limit)
  1958. {
  1959. bAlarm = true;
  1960. alarm_value = down_avg_resist;
  1961. alarm_time = it_down_stable_start->first;
  1962. alarm_refer = pInfo->dw_alarm_high_limit;
  1963. desc_type = 5;
  1964. }
  1965. else if (down_avg_resist > pInfo->dw_warn_high_limit)
  1966. {
  1967. bWarn = true;
  1968. alarm_value = down_avg_resist;
  1969. alarm_time = it_down_stable_start->first;
  1970. alarm_refer = pInfo->dw_warn_high_limit;
  1971. desc_type = 5;
  1972. }
  1973. }
  1974. }
  1975. }
  1976. else
  1977. {
  1978. //其他未实现
  1979. //TODO
  1980. assert(0);
  1981. continue;
  1982. }
  1983. }while (false);
  1984. //update
  1985. CString sql;
  1986. sql.Format("update rm_alarm_set SET time = %I64u WHERE mo = '%s' and mp = '%s' and no = %d and type = %d",
  1987. pAlarmSet->tmLastCheckTime, mo.c_str(), mp.c_str(), pAlarmSet->no, pAlarmSet->type);
  1988. if (CDBConnectPool::Instance()->DBExecuteSQL(sql) == FALSE)
  1989. {
  1990. assert(0);
  1991. CSimpleLog::Error("语句执行失败" + sql);
  1992. }
  1993. string name1, name2, name3, out_name, in_name;
  1994. CMonitorObjectMng::Instance()->GetNameByMoMp(momp, name1, name2, name3, out_name, in_name);
  1995. auto pMoMpInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(momp);
  1996. if (item.second->no == 0 || item.second->no == 1)
  1997. {
  1998. if (name1.find("定位") != -1)
  1999. {
  2000. posi = item.second->no == 0 ? eDaoChaPosi::DCP_FIX : eDaoChaPosi::DCP_INVERT;
  2001. }
  2002. else if (name1.find("反位") != -1)
  2003. {
  2004. posi = item.second->no == 0 ? eDaoChaPosi::DCP_INVERT : eDaoChaPosi::DCP_FIX;
  2005. }
  2006. else if (pMoMpInfo)
  2007. SPDLOG_WARN("{} 未配置定位位关系 {}, {}, {}, {}", momp, name1, name2, pMoMpInfo->name1, pMoMpInfo->name2);
  2008. }
  2009. else if (item.second->no == 2)
  2010. {
  2011. //TODO
  2012. if (posi == eDaoChaPosi::DCP_UNKNOWN)
  2013. posi = eDaoChaPosi::DCP_FIX2INVERT;
  2014. }
  2015. else
  2016. ASSERT(FALSE);
  2017. if (bAlarm == false && bWarn == false) //没有报警
  2018. {
  2019. //恢复报警
  2020. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  2021. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  2022. {
  2023. auto& alarm = *it;
  2024. if (alarm->no == item.second->no && alarm->type == pAlarmSet->type && alarm->recoveryTime.wYear < 2000
  2025. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  2026. {
  2027. auto pAlarmInfo = alarm;
  2028. //1. 更新数据库
  2029. //2. 更新内存数据
  2030. //3. 上送恢复消息
  2031. if (ctAlarmTime.GetTime() == 0)
  2032. ctAlarmTime = CTime::GetCurrentTime();
  2033. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  2034. CString sql;
  2035. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  2036. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  2037. if (pAlarmInfo->ack_result == 1)
  2038. {
  2039. it = pService->m_lstUnConfirmAlarm.erase(it);
  2040. continue;
  2041. }
  2042. else
  2043. {
  2044. ctAlarmTime.GetAsSystemTime(alarm->recoveryTime);
  2045. }
  2046. g_p315ClientManager->GetTcpClient()->SendAlarmData(CMonitorObjectMng::Instance()->GetZZJNO(mo + '.' + mp), 2, CTime(pAlarmInfo->time).GetTime(), ctAlarmTime.GetTime(),
  2047. pAlarmInfo->type, posi, pAlarmInfo->level, eLowHigh::LH_HIGH,
  2048. TIEDA_ACQ_VALUE(alarm->val, TIEDA_VAL_STATE::TVS_MOVEING), TIEDA_ACQ_VALUE(alarm->refer_val, TIEDA_VAL_STATE::TVS_MOVEING), 0);
  2049. }
  2050. ++it;
  2051. }
  2052. continue; //误报警继续下一个循环
  2053. }
  2054. //查找是否原有的报警已存在
  2055. uint8_t level = 0;
  2056. if (bAlarm) level = 1;
  2057. ctAlarmTime = CTime(alarm_time / 1000);
  2058. {
  2059. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  2060. for (const auto& alarm : pService->m_lstUnConfirmAlarm)
  2061. {
  2062. if (alarm->no == item.second->no && alarm->type == pAlarmSet->type && level == alarm->level && alarm->recoveryTime.wYear < 2000 //预警和报警单独计算
  2063. && alarm->mo.compare(mo) == 0 && alarm->mp.compare(mp) == 0)
  2064. {
  2065. pAlarmInfo = alarm;
  2066. break;
  2067. }
  2068. }
  2069. }
  2070. if (pAlarmInfo == nullptr)
  2071. {
  2072. bNew = true;
  2073. pAlarmInfo = new ALARM_INFO;
  2074. pAlarmInfo->event_id = hjfunc_GetGUID();
  2075. pAlarmInfo->id = ++pService->m_nAlarmID;
  2076. pAlarmInfo->level = (bAlarm == 1);
  2077. pAlarmInfo->mo = mo;
  2078. pAlarmInfo->mp = mp;
  2079. pAlarmInfo->no = item.second->no;
  2080. ctAlarmTime.GetAsSystemTime(pAlarmInfo->time);
  2081. pAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  2082. pAlarmInfo->type = item.second->type;
  2083. pAlarmInfo->val = alarm_value;
  2084. pAlarmInfo->refer_val = alarm_refer;
  2085. pAlarmInfo->posi = posi;
  2086. pAlarmInfo->zzjno = CMonitorObjectMng::Instance()->GetZZJNO(momp);
  2087. pAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pAlarmInfo->time);
  2088. char szInfo[200];
  2089. if (desc_type == 1)
  2090. {
  2091. sprintf_s(szInfo, sizeof(szInfo), "瞬时冲击力超限报警,报警值为%dN, 参考值为%dN", alarm_value, alarm_refer);//101 102
  2092. if (pAlarmInfo->type == eZL_ALARMTYPE::MAX_OVER_LIMIT)
  2093. {
  2094. g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->zzjno, 1, CTime(pAlarmInfo->time).GetTime(), 0xFFFFFFFF,
  2095. pAlarmInfo->type, pAlarmInfo->posi, pAlarmInfo->level, eLowHigh::LH_HIGH,
  2096. TIEDA_ACQ_VALUE(alarm_value, TIEDA_VAL_STATE::TVS_POLL), TIEDA_ACQ_VALUE(alarm_refer, TIEDA_VAL_STATE::TVS_POLL), 0);
  2097. }
  2098. }
  2099. else if (desc_type == 2)
  2100. sprintf_s(szInfo, sizeof(szInfo), "稳态值超限预警, 超限次数为%d次, 参考值为%dN", alarm_value, alarm_refer);
  2101. else if (desc_type == 3)
  2102. sprintf_s(szInfo, sizeof(szInfo), "转换阻力值超限报警, 报警值为%dN, 参考值为%dN", alarm_value, alarm_refer);
  2103. else if (desc_type == 4)
  2104. {
  2105. sprintf_s(szInfo, sizeof(szInfo), "摩擦力超限%s, 缩进位报警值为%dN, 参考值为:%dN", bAlarm == 1 ? "告警" : "预警", alarm_value, alarm_refer);
  2106. string direct1, direct2;
  2107. CMonitorObjectMng::Instance()->GetDirectByMoMp(mo + '.' + mp, direct1, direct2);
  2108. }
  2109. else if (desc_type == 5)
  2110. {
  2111. sprintf_s(szInfo, sizeof(szInfo), "摩擦力超限%s, 伸出位报警值为%dN, 参考值为:%dN", bAlarm == 1 ? "告警" : "预警", alarm_value, alarm_refer);
  2112. string direct1, direct2;
  2113. CMonitorObjectMng::Instance()->GetDirectByMoMp(mo + '.' + mp, direct1, direct2);
  2114. }
  2115. else
  2116. assert(0);
  2117. pAlarmInfo->desc = szInfo;
  2118. lock_guard<mutex> lock(pService->m_mtxAlarm);
  2119. pService->m_lstUnConfirmAlarm.push_back(pAlarmInfo);
  2120. }
  2121. //send
  2122. if (bNew) //不再推送
  2123. {
  2124. rapidjson::StringBuffer buffer;
  2125. auto ret = AlarmInfo2Pack(pAlarmInfo, buffer);
  2126. const char* output = buffer.GetString();
  2127. //CAppService::Instance()->GetLwsServer()->SendPackToALLClient_with_noEncode((uint8_t*)output, buffer.GetLength());
  2128. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  2129. }
  2130. //save
  2131. if (bNew)
  2132. {
  2133. CString sql;
  2134. 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]) "\
  2135. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  2136. pAlarmInfo->id, pAlarmInfo->mo.c_str(), pAlarmInfo->mp.c_str(), pAlarmInfo->no, pAlarmInfo->type,
  2137. ctAlarmTime.Format("%Y-%m-%d %H:%M:%S"), pAlarmInfo->level, pAlarmInfo->desc.c_str(), pAlarmInfo->suggest.c_str(), pAlarmInfo->val,
  2138. pAlarmInfo->event_id.c_str(), pAlarmInfo->rel_id.c_str(), uint8_t(pAlarmInfo->posi), uint8_t(pAlarmInfo->loworhigh), pAlarmInfo->refer_val, pAlarmInfo->sunroof);
  2139. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  2140. CSimpleLog::Error("执行语句失败" + sql);
  2141. else
  2142. {
  2143. uint8_t* pack = nullptr; int len = 0;
  2144. auto packno = CResistAlarmMng::GeneralNewAlarmData(pAlarmInfo, &pack, &len);
  2145. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  2146. //g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->no, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, (WORD)pAlarmInfo->type, pAlarmInfo->val, 0, 0);
  2147. delete[] pack;
  2148. pack = nullptr;
  2149. }
  2150. }
  2151. }
  2152. //保持力下降检测
  2153. if (tmNow - pService->m_lastDetectDrop >= 30)
  2154. {
  2155. pService->m_lastDetectDrop = tmNow;
  2156. time_t tt;
  2157. time(&tt);
  2158. for (const auto& item : pService->m_alarm_set)
  2159. {
  2160. if (item.second == nullptr) continue;
  2161. if (item.second->enable == false) continue;
  2162. if (item.second->type != eZL_ALARMTYPE::RETENSION_FORCE) //这个逻辑里面只支持保持力
  2163. continue;
  2164. if ((eSuoBiPosi)item.second->no != eSuoBiPosi::SB_FIX && (eSuoBiPosi)item.second->no != eSuoBiPosi::SB_INVERT)
  2165. continue;
  2166. string momp = item.second->momp;
  2167. if (momp.empty())
  2168. {
  2169. int nPos = item.first.find('.');
  2170. if (nPos == -1) continue;
  2171. int nPos2 = item.first.find('.', nPos + 1);
  2172. if (nPos2 == -1) continue;
  2173. string mo = item.first.substr(0, nPos);
  2174. string mp = item.first.substr(nPos + 1, nPos2 - nPos - 1);
  2175. momp = item.first.substr(0, nPos2);
  2176. }
  2177. auto pInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(momp);
  2178. if (pInfo == nullptr) continue;
  2179. if (pInfo->pDeivce == nullptr) continue;
  2180. auto pAlarmSet = (RETENSION_FORCE_DROP*)item.second;
  2181. auto pMap0 = pInfo->pDeivce->GetStatInfo(pInfo->idx, 0);
  2182. auto pMap1 = pInfo->pDeivce->GetStatInfo(pInfo->idx, 1);
  2183. if (pMap0->size() < 2) continue;
  2184. if (pAlarmSet->tmLastCheckTime >= pMap0->rbegin()->first) continue;//不重复检测
  2185. pAlarmSet->tmLastCheckTime = pMap0->rbegin()->first;
  2186. std::map<time_t, tagSecondStatInfo>* pMap = nullptr;
  2187. if (((eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_FIX && pInfo->name1.find("定位") != -1) ||
  2188. ((eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_INVERT && pInfo->name1.find("反位") != -1))
  2189. {
  2190. pMap = pMap0;
  2191. }
  2192. else if(((eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_FIX && pInfo->name2.find("定位") != -1) ||
  2193. ((eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_INVERT && pInfo->name2.find("反位") != -1))
  2194. {
  2195. pMap = pMap1;
  2196. }
  2197. else
  2198. continue;
  2199. //zzj位置是否变化
  2200. bool bEPOSChanged = CMonitorObjectMng::Instance()->IsZZJEPOSChanged(momp,
  2201. (eSuoBiPosi)pAlarmSet->no == eSuoBiPosi::SB_FIX ? DAOCHA_POSITION::MP_FIX : DAOCHA_POSITION::MP_INVERT, tt);
  2202. //位置变化后不能使用
  2203. if (bEPOSChanged) continue;
  2204. int nMinVal = INT_MAX;
  2205. time_t tMinTime = 0;
  2206. {
  2207. lock_guard<mutex> lock(pInfo->pDeivce->m_mtx);
  2208. auto it = pMap->end();
  2209. auto it0 = --it;
  2210. auto it1 = --it;
  2211. if (momp.compare(g_strMoMp) == 0)
  2212. SPDLOG_INFO("[保持力下降]{} 波动值. {} {}", momp, it1->second.dif_val, it0->second.dif_val);
  2213. if (it0->second.dif_val < 200 && it1->second.dif_val < 200 /*&& (it0->first - it1->first) > 2*/)
  2214. {
  2215. nMinVal = it0->second.min_val;
  2216. tMinTime = it0->second.min_time;
  2217. if (it1->second.max_val > nMinVal)
  2218. {
  2219. nMinVal = it1->second.min_val;
  2220. tMinTime = it1->second.min_time;
  2221. }
  2222. //else if (it2->second.max_val < nMinVal)
  2223. //{
  2224. // nMinVal = it2->second.min_val;
  2225. // tMinTime = it2->second.min_time;
  2226. //}
  2227. }
  2228. }
  2229. if (nMinVal != INT_MAX)
  2230. JudgeAlarm(pService, pAlarmSet, tMinTime, nMinVal, (eSuoBiPosi)pAlarmSet->no, pInfo->mo, pInfo->mp, pInfo);
  2231. }
  2232. //清理无效位置
  2233. CMonitorObjectMng::Instance()->ClearZZJHistroyEPOS(tt);
  2234. }
  2235. } while (pService->m_bWork);
  2236. if (bLock)
  2237. {
  2238. g_lockSync.Unlock();
  2239. bLock = false;
  2240. }
  2241. }
  2242. typedef struct tagMoMp
  2243. {
  2244. string mo;
  2245. string mp;
  2246. uint8_t no; //传感器的序号
  2247. tagMoMp(string a, string b)
  2248. {
  2249. mo = a;
  2250. mp = b;
  2251. }
  2252. tagMoMp(uint8_t a)
  2253. {
  2254. no = a;
  2255. }
  2256. };
  2257. typedef struct offline_info
  2258. {
  2259. string imei;
  2260. CTime time;
  2261. uint8_t idx = 0;
  2262. std::vector<tagMoMp> vct;
  2263. offline_info(string a, CTime b)
  2264. {
  2265. imei = a;
  2266. time = b;
  2267. }
  2268. offline_info(string a, CTime b, uint8_t c, uint8_t d)
  2269. {
  2270. imei = a;
  2271. time = b;
  2272. idx = c;
  2273. vct.push_back(tagMoMp(d));
  2274. }
  2275. };
  2276. void CResistAlarmMng::ThreadProcDevice(DWORD_PTR param)
  2277. {
  2278. auto pService = (CResistAlarmMng*)param;
  2279. this_thread::sleep_for(chrono::seconds(2));
  2280. static CTimeSpan offline_time(0, 0, 5, 0); //设备离线时间暂定5分钟
  2281. static CTimeSpan offline_limit(30, 0, 0, 0);
  2282. bool bLock = false;
  2283. do
  2284. {
  2285. if (bLock)
  2286. {
  2287. g_lockSync.Unlock();
  2288. bLock = false;
  2289. }
  2290. for (int i = 0; i < 30; i++)
  2291. {
  2292. if (false == pService->m_bWork) break;
  2293. Sleep(1000);
  2294. }
  2295. g_lockSync.ReadLock();
  2296. bLock = true;
  2297. CTime ctNow = CTime::GetCurrentTime();
  2298. //离线设备
  2299. {
  2300. auto& pMap = CMonitorObjectMng::Instance()->m_mapMoMpInfo;
  2301. for (auto& it : pMap)
  2302. {
  2303. auto& pInfo = it.second;
  2304. if (pInfo == nullptr) continue;
  2305. bool bAlarm = false;
  2306. if (!pInfo->pDeivce) continue;
  2307. const auto pDevice = pInfo->pDeivce;
  2308. if (pDevice->m_ctUpdateTime.GetTime() == 0)
  2309. continue;
  2310. if (ctNow - pDevice->m_ctUpdateTime > offline_time)
  2311. bAlarm = true;
  2312. else if (pInfo->bFirstOnline)
  2313. {//第一次上线发送一次315离线报警恢复 因铁大未作报警同步
  2314. pInfo->bFirstOnline = false;
  2315. g_p315ClientManager->GetTcpClient()->SendAlarmData(pInfo->zzjno, 2, pDevice->m_ctUpdateTime.GetTime(), pDevice->m_ctUpdateTime.GetTime(), eZL_ALARMTYPE::EQUIP_OFFLINE,
  2316. eDaoChaPosi::DCP_UNKNOWN, 1, eLowHigh::LH_UNKNOWN, TIEDA_ACQ_VALUE(INT_MIN), TIEDA_ACQ_VALUE(INT_MIN), 0);
  2317. SPDLOG_INFO("{}第一次上线发送一次315离线报警恢复", it.first);
  2318. }
  2319. ALARM_INFO* pAlarmInfo = nullptr;
  2320. bool bNew = false;
  2321. auto ctAlarmTime = ctNow;
  2322. {
  2323. std::lock_guard<mutex> lock(pService->m_mtxAlarm);
  2324. for (auto it = pService->m_lstUnConfirmAlarm.begin(); it != pService->m_lstUnConfirmAlarm.end();)
  2325. {
  2326. auto pAlarm = *it;
  2327. if (pAlarm->type == eZL_ALARMTYPE::EQUIP_OFFLINE && pAlarm->recoveryTime.wYear < 2000 &&
  2328. pAlarm->mo.compare(pInfo->mo) == 0 && pAlarm->mp.compare(pInfo->mp) == 0)
  2329. {
  2330. pAlarmInfo = pAlarm;
  2331. if (bAlarm) //连续报警
  2332. {
  2333. break;
  2334. }
  2335. else
  2336. {
  2337. //恢复报警
  2338. //更新数据库
  2339. if (ctAlarmTime.GetTime() == 0) ctAlarmTime = CTime::GetCurrentTime();
  2340. string recovery_time = ctAlarmTime.Format("%Y-%m-%d %H:%M:%S");
  2341. CString sql;
  2342. sql.Format("UPDATE [rm_alarm] SET [recovery_time]='%s' WHERE ID = %d;", recovery_time.c_str(), pAlarmInfo->id);
  2343. CDBConnectPool::Instance()->DBExecuteSQL(sql);
  2344. //315上送
  2345. g_p315ClientManager->GetTcpClient()->SendAlarmData(pInfo->zzjno, 2, CTime(pAlarm->time).GetTime(), ctAlarmTime.GetTime(), pAlarm->type,
  2346. eDaoChaPosi::DCP_UNKNOWN, pAlarm->level, eLowHigh::LH_UNKNOWN, TIEDA_ACQ_VALUE(INT_MIN), TIEDA_ACQ_VALUE(INT_MIN), 0);
  2347. if (pAlarm->ack_result == 1)
  2348. {
  2349. it = pService->m_lstUnConfirmAlarm.erase(it);
  2350. continue;
  2351. }
  2352. else
  2353. {
  2354. ctAlarmTime.GetAsSystemTime(pAlarm->recoveryTime);
  2355. }
  2356. }
  2357. }
  2358. ++it;
  2359. }
  2360. }
  2361. if (bAlarm == false) continue;
  2362. if (pAlarmInfo == nullptr)
  2363. {
  2364. bNew = true;
  2365. pAlarmInfo = new ALARM_INFO;
  2366. pAlarmInfo->event_id = hjfunc_GetGUID();
  2367. pAlarmInfo->id = ++pService->m_nAlarmID;
  2368. pAlarmInfo->level = 1;
  2369. pAlarmInfo->mo = pInfo->mo;
  2370. pAlarmInfo->mp = pInfo->mp;
  2371. pAlarmInfo->no = -1;
  2372. ctAlarmTime.GetAsSystemTime(pAlarmInfo->time);
  2373. //pAlarmInfo->time.wMilliseconds = alarm_time % 1000;
  2374. pAlarmInfo->type = eZL_ALARMTYPE::EQUIP_OFFLINE;
  2375. pAlarmInfo->val = 0;
  2376. pAlarmInfo->zzjno = pInfo->zzjno;
  2377. char szInfo[200];
  2378. sprintf_s(szInfo, sizeof(szInfo), "设备离线时间:%s", (LPCSTR)(pDevice->m_ctUpdateTime.Format("%Y-%m-%d %H:%M:%S")));
  2379. pAlarmInfo->desc = szInfo;
  2380. pAlarmInfo->sunroof = CSkylightMng::GetInstance()->IsSkylight(&pAlarmInfo->time);
  2381. lock_guard<mutex> lock(pService->m_mtxAlarm);
  2382. pService->m_lstUnConfirmAlarm.push_back(pAlarmInfo);
  2383. }
  2384. //send
  2385. if (bNew) //不再推送
  2386. {
  2387. rapidjson::StringBuffer buffer;
  2388. auto ret = AlarmInfo2Pack(pAlarmInfo, buffer);
  2389. const char* output = buffer.GetString();
  2390. CAppService::Instance()->GetMgServer()->SendToAllClient(output, buffer.GetLength());
  2391. }
  2392. //save
  2393. if (bNew)
  2394. {
  2395. CString sql;
  2396. 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]) "\
  2397. "VALUES(%d, '%s', '%s', %d, %d, '%s', %d, '%s', '%s', %d, '%s', '%s',%d,%d,%d,%d);",
  2398. pAlarmInfo->id, pAlarmInfo->mo.c_str(), pAlarmInfo->mp.c_str(), pAlarmInfo->no, pAlarmInfo->type,
  2399. (LPCSTR)(ctAlarmTime.Format("%Y-%m-%d %H:%M:%S")), pAlarmInfo->level, pAlarmInfo->desc.c_str(), pAlarmInfo->suggest.c_str(), pAlarmInfo->val,
  2400. pAlarmInfo->event_id.c_str(), pAlarmInfo->rel_id.c_str(), uint8_t(pAlarmInfo->posi), uint8_t(pAlarmInfo->loworhigh), pAlarmInfo->refer_val, pAlarmInfo->sunroof);
  2401. if (false == CDBConnectPool::Instance()->DBExecuteSQL(sql))
  2402. CSimpleLog::Error("执行语句失败" + sql);
  2403. else
  2404. {
  2405. uint8_t* pack = nullptr; int len = 0;
  2406. auto packno = CResistAlarmMng::GeneralNewAlarmData(pAlarmInfo, &pack, &len);
  2407. if (packno) CSuperManager::Instance()->SendPack(pack, len, packno, E_ZL_PROTOCAL::ZL_ALARM_DATA, true);
  2408. g_p315ClientManager->GetTcpClient()->SendAlarmData(pAlarmInfo->zzjno, 1, ctAlarmTime.GetTime(), 0xFFFFFFFF, pAlarmInfo->type, eDaoChaPosi::DCP_UNKNOWN, 1, eLowHigh::LH_UNKNOWN,
  2409. TIEDA_ACQ_VALUE(INT_MIN), TIEDA_ACQ_VALUE(INT_MIN), 0);
  2410. delete[] pack;
  2411. pack = nullptr;
  2412. }
  2413. }
  2414. }
  2415. }
  2416. } while (pService->m_bWork);
  2417. if (bLock)
  2418. {
  2419. g_lockSync.Unlock();
  2420. bLock = false;
  2421. }
  2422. }
  2423. void CResistAlarmMng::ThreadProcMove(DWORD_PTR param)
  2424. {
  2425. auto pService = (CResistAlarmMng*)param;
  2426. Sleep(2000);
  2427. time_t tmNow;
  2428. auto strIniPath = CSimpleLog::GetAppDir() + "DataServices.ini";
  2429. bool bLock = false;
  2430. do
  2431. {
  2432. if (bLock)
  2433. {
  2434. g_lockSync.Unlock();
  2435. bLock = false;
  2436. }
  2437. Sleep(800);
  2438. time(&tmNow);
  2439. if (pService->m_lastDetectMove > tmNow)
  2440. {
  2441. SPDLOG_INFO("时间有跳变");
  2442. pService->m_lastDetectMove = tmNow;
  2443. }
  2444. //10秒归档一次 扳动判断
  2445. if (tmNow - pService->m_lastDetectMove < 10)
  2446. continue;
  2447. g_lockSync.ReadLock();
  2448. bLock = true;
  2449. pService->m_lastDetectMove = tmNow;
  2450. char szTemp[100] = { 0 };
  2451. ::GetPrivateProfileString("SET", "momp", "", szTemp, sizeof(szTemp), strIniPath);
  2452. g_strMoMp = szTemp;
  2453. //SPDLOG_INFO("扳动判断:{}", g_strMoMp);
  2454. auto& mapDevice = CDeviceMng::Instance()->m_map_devices;
  2455. for (auto& it : mapDevice)
  2456. {
  2457. auto pDevice = it.second;
  2458. for (auto i = 0; i < 3; i++)
  2459. {
  2460. string mo, mp;
  2461. if (FALSE == CMonitorObjectMng::Instance()->IMEI2MOMP(pDevice->imei, i, mo, mp))
  2462. {
  2463. //CSimpleLog::Error(fmt::format("IMEI2MOMP fail. imei:{}, idx:{}, {}:{}", pDevice->imei, i, __FILE__, __LINE__).c_str());
  2464. continue; //没有绑定返回
  2465. }
  2466. std::map<time_t, tagSecondStatInfo> map0, map1, map2;
  2467. std::map<time_t, int>* mapData[3] = { 0 };
  2468. if (i == 0)
  2469. {
  2470. if (tmNow - pDevice->GetUpdateTime(0) < 3) continue; //3秒钟 无数据上送 开始判断
  2471. else if (tmNow - pDevice->m_tmMoveDetectTime0 < 10) continue; //距离上次应大于10秒
  2472. else if (pDevice->m_mapSecondStatInfo00.size() == 0) continue;
  2473. const auto tCheck = pDevice->m_tmMoveDetectTime0;
  2474. lock_guard<mutex> lock(pDevice->m_mtx);
  2475. for (auto it = pDevice->m_mapSecondStatInfo00.rbegin(); it != pDevice->m_mapSecondStatInfo00.rend(); it++)
  2476. {
  2477. if (it->first < tCheck) break;
  2478. map0.insert(*it);
  2479. }
  2480. for (auto it = pDevice->m_mapSecondStatInfo01.rbegin(); it != pDevice->m_mapSecondStatInfo01.rend(); it++)
  2481. {
  2482. if (it->first < tCheck) break;
  2483. map1.insert(*it);
  2484. }
  2485. for (auto it = pDevice->m_mapSecondStatInfo02.rbegin(); it != pDevice->m_mapSecondStatInfo02.rend(); it++)
  2486. {
  2487. if (it->first < tCheck) break;
  2488. map2.insert(*it);
  2489. }
  2490. if (map2.size() > 2)
  2491. { //拷贝原始数据
  2492. time_t tStart = 0;
  2493. std::map<time_t, int>::iterator itStart2;
  2494. if (tCheck)
  2495. {
  2496. for (auto ii = tCheck; ii < tmNow; ii++)
  2497. {
  2498. itStart2 = pDevice->map_resist_idx02.find(ii*1000);
  2499. if (itStart2 != pDevice->map_resist_idx02.end())
  2500. {
  2501. tStart = ii*1000;
  2502. break;
  2503. }
  2504. }
  2505. }
  2506. else
  2507. {
  2508. itStart2 = pDevice->map_resist_idx02.begin();
  2509. tStart = itStart2->first;
  2510. }
  2511. if (tStart)
  2512. {
  2513. mapData[0] = new std::map<time_t, int>(pDevice->map_resist_idx00.find(tStart), pDevice->map_resist_idx00.end());
  2514. mapData[1] = new std::map<time_t, int>(pDevice->map_resist_idx01.find(tStart), pDevice->map_resist_idx01.end());
  2515. mapData[2] = new std::map<time_t, int>(itStart2, pDevice->map_resist_idx02.end());
  2516. }
  2517. else
  2518. ASSERT(0);
  2519. }
  2520. pDevice->m_tmMoveDetectTime0 = tmNow;
  2521. }
  2522. else if (i == 1)
  2523. {
  2524. if (tmNow - pDevice->GetUpdateTime(1) < 3) continue; //3秒钟 无数据上送 开始判断
  2525. else if (tmNow - pDevice->m_tmMoveDetectTime1 < 10) continue; //距离上次应大于10秒
  2526. else if (pDevice->m_mapSecondStatInfo10.size() == 0) continue;
  2527. const auto tCheck = pDevice->m_tmMoveDetectTime1;
  2528. lock_guard<mutex> lock(pDevice->m_mtx);
  2529. for (auto it = pDevice->m_mapSecondStatInfo10.rbegin(); it != pDevice->m_mapSecondStatInfo10.rend(); it++)
  2530. {
  2531. if (it->first < tCheck) break;
  2532. map0.insert(*it);
  2533. }
  2534. for (auto it = pDevice->m_mapSecondStatInfo11.rbegin(); it != pDevice->m_mapSecondStatInfo11.rend(); it++)
  2535. {
  2536. if (it->first < tCheck) break;
  2537. map1.insert(*it);
  2538. }
  2539. for (auto it = pDevice->m_mapSecondStatInfo12.rbegin(); it != pDevice->m_mapSecondStatInfo12.rend(); it++)
  2540. {
  2541. if (it->first < tCheck) break;
  2542. map2.insert(*it);
  2543. }
  2544. if (map2.size() > 2)
  2545. { //拷贝原始数据
  2546. time_t tStart = 0;
  2547. std::map<time_t, int>::iterator itStart2;
  2548. if (tCheck)
  2549. {
  2550. for (auto ii = tCheck; ii < tmNow; ii++)
  2551. {
  2552. itStart2 = pDevice->map_resist_idx12.find(ii*1000);
  2553. if (itStart2 != pDevice->map_resist_idx12.end())
  2554. {
  2555. tStart = ii*1000;
  2556. break;
  2557. }
  2558. }
  2559. }
  2560. else
  2561. {
  2562. itStart2 = pDevice->map_resist_idx12.begin();
  2563. tStart = itStart2->first;
  2564. }
  2565. if (tStart)
  2566. {
  2567. mapData[0] = new std::map<time_t, int>(pDevice->map_resist_idx10.find(tStart), pDevice->map_resist_idx10.end());
  2568. mapData[1] = new std::map<time_t, int>(pDevice->map_resist_idx11.find(tStart), pDevice->map_resist_idx11.end());
  2569. mapData[2] = new std::map<time_t, int>(itStart2, pDevice->map_resist_idx12.end());
  2570. }
  2571. else
  2572. ASSERT(0);
  2573. }
  2574. pDevice->m_tmMoveDetectTime1 = tmNow;
  2575. }
  2576. else
  2577. {
  2578. if (tmNow - pDevice->GetUpdateTime(2) < 3) continue; //3秒钟 无数据上送 开始判断
  2579. else if (tmNow - pDevice->m_tmMoveDetectTime2 < 10) continue; //距离上次应大于10秒
  2580. else if (pDevice->m_mapSecondStatInfo20.size() == 0) continue;
  2581. //else if (pDevice->m_mapSecondStatInfo20.rbegin()->second.dif_val > 100) continue; //最后值需要稳定 2024年5月31日移除
  2582. //else if (pDevice->m_mapSecondStatInfo21.rbegin()->second.dif_val > 100) continue;
  2583. //else if (pDevice->m_mapSecondStatInfo22.rbegin()->second.dif_val > 100) continue;
  2584. const auto tCheck = pDevice->m_tmMoveDetectTime2;
  2585. lock_guard<mutex> lock(pDevice->m_mtx);
  2586. for (auto it = pDevice->m_mapSecondStatInfo20.rbegin(); it != pDevice->m_mapSecondStatInfo20.rend(); it++)
  2587. {
  2588. //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"));
  2589. if (it->first < tCheck) break;
  2590. map0.insert(*it);
  2591. }
  2592. for (auto it = pDevice->m_mapSecondStatInfo21.rbegin(); it != pDevice->m_mapSecondStatInfo21.rend(); it++)
  2593. {
  2594. //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"));
  2595. if (it->first < tCheck) break;
  2596. map1.insert(*it);
  2597. }
  2598. for (auto it = pDevice->m_mapSecondStatInfo22.rbegin(); it != pDevice->m_mapSecondStatInfo22.rend(); it++)
  2599. {
  2600. //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"));
  2601. if (it->first < tCheck) break;
  2602. map2.insert(*it);
  2603. }
  2604. if (map2.size() > 2)
  2605. { //拷贝原始数据
  2606. time_t tStart = 0;
  2607. std::map<time_t, int>::iterator itStart2;
  2608. if (tCheck)
  2609. {
  2610. for (auto ii = tCheck; ii < tmNow; ii++)
  2611. {
  2612. itStart2 = pDevice->map_resist_idx22.find(ii*1000);
  2613. if (itStart2 != pDevice->map_resist_idx22.end())
  2614. {
  2615. tStart = ii*1000;
  2616. break;
  2617. }
  2618. }
  2619. }
  2620. else
  2621. {
  2622. itStart2 = pDevice->map_resist_idx22.begin();
  2623. tStart = itStart2->first;
  2624. }
  2625. if (tStart)
  2626. {
  2627. mapData[0] = new std::map<time_t, int>(pDevice->map_resist_idx20.find(tStart), pDevice->map_resist_idx20.end());
  2628. mapData[1] = new std::map<time_t, int>(pDevice->map_resist_idx21.find(tStart), pDevice->map_resist_idx21.end());
  2629. mapData[2] = new std::map<time_t, int>(itStart2, pDevice->map_resist_idx22.end());
  2630. }
  2631. else
  2632. assert(0);
  2633. }
  2634. 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());
  2635. pDevice->m_tmMoveDetectTime2 = tmNow;
  2636. }
  2637. if (map2.size() <= 2) continue;
  2638. if (!mapData[0] && !mapData[1] && !mapData[2]) continue;
  2639. if (mapData[0]->size() != mapData[1]->size() || mapData[1]->size() != mapData[2]->size()) continue;
  2640. SPDLOG_INFO("扳动判断数据{}.{} map:{} 0:{} 1:{} 2:{}", mo, mp, map2.size(), mapData[0]->size(), mapData[1]->size(), mapData[2]->size());
  2641. list<CONVERT_RESIST> lstResit2; //转换力数据
  2642. list<PASS_RESIST> lstPass; //过车数据
  2643. std::map<time_t, int64_t> maxlock0, maxlock1;//锁闭力 前面时间戳, 后面 高4位显示值, 低4位报警值
  2644. std::map<time_t, int64_t> retentionforce0, retentionforce1; //保持力 时间戳, 高4位显示值, 低4位报警值
  2645. mg_per_session_data::GetMaxResistNew(map2, lstResit2, mo, mp);
  2646. if (g_bDataCompression)
  2647. {
  2648. SPDLOG_INFO("[过车判断]{}.{} 开始过车判断 size:{}", mo, mp, map0.size());
  2649. mg_per_session_data::GetPassNew(map0, mapData[0], map1, mapData[1], mo, mp, &lstPass);
  2650. SPDLOG_INFO("[过车判断]{}.{} 结束过车判断 size:{} pass:{}", mo, mp, map0.size(), lstPass.size());
  2651. //过车过滤
  2652. for (auto& item : lstPass)
  2653. {
  2654. for (auto it = lstResit2.begin(); it != lstResit2.end();)
  2655. {
  2656. if (it->tmStart * 1000 <= item.show_time && item.show_time <= (it->tmEnd + 1) * 1000)
  2657. {
  2658. it = lstResit2.erase(it);
  2659. break;
  2660. }
  2661. ++it;
  2662. }
  2663. }
  2664. }
  2665. if (lstResit2.size())
  2666. {
  2667. mg_per_session_data::GetMaxLockNew(map0, lstResit2, maxlock0, retentionforce0);
  2668. mg_per_session_data::GetMaxLockNew(map1, lstResit2, maxlock1, retentionforce1);
  2669. SPDLOG_INFO("[转换阻力][{}:{}] lock0:{} lock1:{}, resist2:{}", mo, mp, maxlock0.size(), maxlock1.size(), lstResit2.size());
  2670. }
  2671. auto pMompInfo = CMonitorObjectMng::Instance()->GetMoMpInfo(mo + "." + mp);
  2672. ASSERT(pMompInfo);
  2673. //扳动处理逻辑
  2674. if (lstResit2.size())
  2675. {
  2676. //获取报警设置
  2677. //转换阻力报警设置项
  2678. auto pConvertResistOverLimitInfo =
  2679. (CONVERT_RESIST_OVER_LIMIT*)pService->Find(mo, mp, 2, eZL_ALARMTYPE::CONVERT_LIMIT);
  2680. //定位锁闭力报警设置项
  2681. auto pFixSuobiOverInfo = (SUOBI_OVER_LIMIT_INFO*)pService->Find(mo, mp, (uint8_t)eSuoBiPosi::SB_FIX, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT);
  2682. //反位
  2683. auto pInvertSuobiOverInfo = (SUOBI_OVER_LIMIT_INFO*)pService->Find(mo, mp, (uint8_t)eSuoBiPosi::SB_INVERT, eZL_ALARMTYPE::SUOBI_LOCK_LIMIT);
  2684. //定位保持力报警设置项
  2685. auto pFixConstRetensionForceWaveInfo = (RETENSION_FORCE_DROP*)pService->Find(mo, mp, (uint8_t)eSuoBiPosi::SB_FIX, eZL_ALARMTYPE::RETENSION_FORCE);
  2686. if (pFixConstRetensionForceWaveInfo == nullptr)//默认开启
  2687. {
  2688. ASSERT(FALSE);
  2689. //pFixConstRetensionForceWaveInfo = new RETENSION_FORCE_DROP;
  2690. ///pService->Insert(mo, mp, (uint8_t)eSuoBiPosi::SB_FIX, (uint8_t)eZL_ALARMTYPE::RETENSION_FORCE, pFixConstRetensionForceWaveInfo);
  2691. }
  2692. //反位
  2693. auto pInvertConstRetensionForceWaveInfo = (RETENSION_FORCE_DROP*)pService->Find(mo, mp, (uint8_t)eSuoBiPosi::SB_INVERT, eZL_ALARMTYPE::RETENSION_FORCE);
  2694. if (pInvertConstRetensionForceWaveInfo == nullptr)//默认开启
  2695. {
  2696. ASSERT(FALSE);
  2697. //pInvertConstRetensionForceWaveInfo = new RETENSION_FORCE_DROP;
  2698. //pService->Insert(mo, mp, (uint8_t)eSuoBiPosi::SB_INVERT, (uint8_t)eZL_ALARMTYPE::RETENSION_FORCE, pInvertConstRetensionForceWaveInfo);
  2699. }
  2700. //获取报警设置 end
  2701. eDaoChaPosi posiLock0;
  2702. if (pMompInfo->name1.find("定位") != -1)
  2703. {
  2704. posiLock0 = eDaoChaPosi::DCP_FIX;
  2705. for (auto& it : maxlock0)
  2706. {
  2707. int show_val = (it.second >> 32);
  2708. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0,
  2709. (int)it.second, show_val, i, (uint8_t)eDaoChaPosi::DCP_FIX, (uint8_t)ePowerName::PN_RESIST, fmt::format("锁闭力:{}", show_val));
  2710. JudgeAlarm(pService, pFixSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, pMompInfo->zzjno);
  2711. }
  2712. for (auto& it : retentionforce0)
  2713. {
  2714. int show_val = (int)it.second;
  2715. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0,
  2716. (int)it.second, show_val, i, (uint8_t)eDaoChaPosi::DCP_FIX, (uint8_t)ePowerName::PN_RETENTION, fmt::format("保持力:{}", show_val));
  2717. JudgeAlarm(pService, pFixConstRetensionForceWaveInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, pMompInfo);
  2718. }
  2719. for (auto& it : maxlock1)
  2720. {
  2721. int show_val = (it.second >> 32);
  2722. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0,
  2723. (int)it.second, show_val, i, (uint8_t)eDaoChaPosi::DCP_INVERT, (uint8_t)ePowerName::PN_RESIST, fmt::format("锁闭力:{}", show_val));
  2724. JudgeAlarm(pService, pInvertSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_INVERT, mo, mp, pMompInfo->zzjno);
  2725. }
  2726. for (auto& it : retentionforce1)
  2727. {
  2728. int show_val = (int)it.second;
  2729. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0,
  2730. (int)it.second, show_val, i, (uint8_t)eDaoChaPosi::DCP_INVERT, (uint8_t)ePowerName::PN_RETENTION, fmt::format("保持力:{}", show_val));
  2731. JudgeAlarm(pService, pInvertConstRetensionForceWaveInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, pMompInfo);
  2732. }
  2733. }
  2734. else
  2735. {
  2736. posiLock0 = eDaoChaPosi::DCP_INVERT;
  2737. for (auto& it : maxlock0)
  2738. {
  2739. int show_val = (it.second >> 32);
  2740. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0, (int)it.second, show_val, i,
  2741. (uint8_t)eDaoChaPosi::DCP_INVERT, (uint8_t)ePowerName::PN_RESIST, fmt::format("锁闭力:{}", show_val));
  2742. JudgeAlarm(pService, pInvertSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_INVERT, mo, mp, pMompInfo->zzjno);
  2743. }
  2744. for (auto& it : retentionforce0)
  2745. {
  2746. int show_val = (int)it.second;
  2747. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0, (int)it.second, show_val, i,
  2748. (uint8_t)eDaoChaPosi::DCP_INVERT, (uint8_t)ePowerName::PN_RETENTION, fmt::format("保持力:{}", show_val));
  2749. JudgeAlarm(pService, pInvertConstRetensionForceWaveInfo, it.first, show_val, eSuoBiPosi::SB_INVERT, mo, mp, pMompInfo);
  2750. }
  2751. for (auto& it : maxlock1)
  2752. {
  2753. int show_val = (it.second >> 32);
  2754. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0, (int)it.second, show_val, i,
  2755. (uint8_t)eDaoChaPosi::DCP_FIX, (uint8_t)ePowerName::PN_RESIST, fmt::format("锁闭力:{}", show_val));
  2756. JudgeAlarm(pService, pFixSuobiOverInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, pMompInfo->zzjno);
  2757. }
  2758. for (auto& it : retentionforce1)
  2759. {
  2760. int show_val = (int)it.second;
  2761. CResistAlarmMng::InsertToDBByMove(mo, mp, it.first, 0, 0, (int)it.second, show_val, i,
  2762. (uint8_t)eDaoChaPosi::DCP_FIX, (uint8_t)ePowerName::PN_RETENTION, fmt::format("保持力:{}", show_val));
  2763. JudgeAlarm(pService, pFixConstRetensionForceWaveInfo, it.first, show_val, eSuoBiPosi::SB_FIX, mo, mp, pMompInfo);
  2764. }
  2765. }
  2766. auto mg_315_server = g_p315ClientManager->GetTcpClient();
  2767. //SPDLOG_INFO("扳动判断数据{}.{} 转换力", mo, mp);
  2768. auto eqpno = CMonitorObjectMng::Instance()->GetZZJNO(mo + "." + mp);
  2769. for (auto& it : lstResit2)
  2770. {
  2771. //auto it_start0 = mapData[0]->find(it.tmStart * 1000);
  2772. //auto it_end0 = mapData[0]->find(it.tmEnd * 1000);
  2773. //auto it_start1 = mapData[1]->find(it.tmStart * 1000);
  2774. //auto it_end1 = mapData[1]->find(it.tmEnd * 1000);
  2775. //auto it_start2 = mapData[2]->find(it.tmStart * 1000);
  2776. //auto it_end2 = mapData[2]->find(it.tmEnd * 1000);
  2777. eDaoChaPosi posi = eDaoChaPosi::DCP_UNKNOWN;
  2778. if (it.bUpOrDown == eUpOrDownInfo::UOD_UP)
  2779. {
  2780. if (pMompInfo->in_name.find("定扳反") != -1)
  2781. {
  2782. CResistAlarmMng::InsertToDBByMove(mo, mp, it.time, it.tmStart, it.tmEnd, it.val, it.fluctuation_val, i,
  2783. (uint8_t)eDaoChaPosi::DCP_FIX2INVERT, (uint8_t)ePowerName::PN_MOVE, fmt::format("{}:{}", pMompInfo->in_name, it.fluctuation_val));
  2784. posi = eDaoChaPosi::DCP_FIX2INVERT;
  2785. CMonitorObjectMng::Instance()->UpdateZZJEPOS(mo + "." + mp, DAOCHA_POSITION::MP_INVERT);
  2786. JudgeAlarm(pService, pConvertResistOverLimitInfo, it.time, it.fluctuation_val, posi, it.bUpOrDown, mo, mp, pMompInfo->zzjno);
  2787. }
  2788. else
  2789. {
  2790. CResistAlarmMng::InsertToDBByMove(mo, mp, it.time, it.tmStart, it.tmEnd, it.val, it.fluctuation_val, i,
  2791. (uint8_t)eDaoChaPosi::DCP_INVERT2FIX, (uint8_t)ePowerName::PN_MOVE, fmt::format("{}:{}", pMompInfo->in_name, it.fluctuation_val));
  2792. posi = eDaoChaPosi::DCP_INVERT2FIX;
  2793. CMonitorObjectMng::Instance()->UpdateZZJEPOS(mo + "." + mp, DAOCHA_POSITION::MP_FIX);
  2794. JudgeAlarm(pService, pConvertResistOverLimitInfo, it.time, it.fluctuation_val, posi, it.bUpOrDown, mo, mp, pMompInfo->zzjno);
  2795. }
  2796. }
  2797. else
  2798. {
  2799. if (pMompInfo->out_name.find("定扳反") != -1)
  2800. {
  2801. CResistAlarmMng::InsertToDBByMove(mo, mp, it.time, it.tmStart, it.tmEnd, it.val, it.fluctuation_val, i,
  2802. (uint8_t)eDaoChaPosi::DCP_FIX2INVERT, (uint8_t)ePowerName::PN_MOVE, fmt::format("{}:{}", pMompInfo->out_name, it.fluctuation_val));
  2803. posi = eDaoChaPosi::DCP_FIX2INVERT;
  2804. CMonitorObjectMng::Instance()->UpdateZZJEPOS(mo + "." + mp, DAOCHA_POSITION::MP_INVERT);
  2805. JudgeAlarm(pService, pConvertResistOverLimitInfo, it.time, it.fluctuation_val, posi, it.bUpOrDown, mo, mp, pMompInfo->zzjno);
  2806. }
  2807. else
  2808. {
  2809. CResistAlarmMng::InsertToDBByMove(mo, mp, it.time, it.tmStart, it.tmEnd, it.val, it.fluctuation_val, i,
  2810. (uint8_t)eDaoChaPosi::DCP_INVERT2FIX, (uint8_t)ePowerName::PN_MOVE, fmt::format("{}:{}", pMompInfo->out_name, it.fluctuation_val));
  2811. posi = eDaoChaPosi::DCP_INVERT2FIX;
  2812. CMonitorObjectMng::Instance()->UpdateZZJEPOS(mo + "." + mp, DAOCHA_POSITION::MP_FIX);
  2813. JudgeAlarm(pService, pConvertResistOverLimitInfo, it.time, it.fluctuation_val, posi, it.bUpOrDown, mo, mp, pMompInfo->zzjno);
  2814. }
  2815. }
  2816. SPDLOG_INFO("扳动判断数据{}.{} 315 上送", mo, mp);
  2817. if(mapData[0])
  2818. mg_315_server->SendCurveData(pMompInfo->zzjno, posi, 1, true, mapData, it.tmStart, it.tmEnd);
  2819. SPDLOG_INFO("扳动判断数据结束{}.{} 315 上送", mo, mp);
  2820. }
  2821. }
  2822. //过车处理逻辑
  2823. if (lstPass.size())
  2824. {
  2825. //TODO
  2826. //SPDLOG_INFO("扳动判断数据{}.{} 315 上送", mo, mp);
  2827. //mg_315_server->SendCurveData(pMompInfo->zzjno, posi, 1, true, mapData, it.tmStart, it.tmEnd);
  2828. //SPDLOG_INFO("扳动判断数据结束{}.{} 315 上送", mo, mp);
  2829. }
  2830. delete mapData[0];
  2831. delete mapData[1];
  2832. delete mapData[2];
  2833. }
  2834. }
  2835. } while (pService->m_bWork);
  2836. if (bLock)
  2837. {
  2838. g_lockSync.Unlock();
  2839. bLock = false;
  2840. }
  2841. }
  2842. BOOL CResistAlarmMng::LoadAlarmSet()
  2843. {
  2844. CString sql = "SELECT [mo],[mp],[no],[type],[conf],[time] FROM [rm_alarm_set]";
  2845. COdbcStatement stmt;
  2846. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  2847. {
  2848. CSimpleLog::Error("执行语句失败" + sql);
  2849. return FALSE;
  2850. }
  2851. for (auto& it : m_alarm_set)
  2852. {
  2853. //delete it.second; //暂时不释放
  2854. it.second = nullptr;
  2855. }
  2856. m_alarm_set.clear();
  2857. char mo[51], mp[51], conf[501];
  2858. uint8_t no, type;
  2859. __time64_t time;
  2860. int nCol = 1;
  2861. stmt.BindCharCol(nCol++, mo, sizeof(mo));
  2862. stmt.BindCharCol(nCol++, mp, sizeof(mp));
  2863. stmt.BindTinyIntCol(nCol++, &no);
  2864. stmt.BindTinyIntCol(nCol++, &type);
  2865. stmt.BindCharCol(nCol++, conf, sizeof(conf));
  2866. stmt.BindBigIntCol(nCol++, &time);
  2867. do
  2868. {
  2869. if (stmt.FetchNext() != 0) break;
  2870. using namespace rapidjson;
  2871. int len = strlen(conf);
  2872. if (len == 0) continue;
  2873. Document doc;
  2874. if (doc.Parse(conf, len).HasParseError())
  2875. {
  2876. CSimpleLog::Error(CString("解析数据出错") + conf);
  2877. continue;
  2878. }
  2879. const auto eType = (eZL_ALARMTYPE)type;
  2880. if (eType == eZL_ALARMTYPE::MAX_OVER_LIMIT)
  2881. {
  2882. if (doc.IsArray() == false)
  2883. {
  2884. CSimpleLog::Error(CString("conf是非数组") + conf);
  2885. continue;
  2886. }
  2887. SizeType n = doc.Size();
  2888. bool enable = false;
  2889. int alarm_high_limit = INT_MAX;
  2890. int warn_high_limit = INT_MAX;
  2891. int f_alarm_high_limit = INT_MAX;
  2892. int f_warn_high_limit = INT_MAX;
  2893. for (SizeType i = 0; i < n; i++)
  2894. {
  2895. if (doc[i].HasMember("name") == false || doc[i].HasMember("val") == false || doc[i]["name"].IsString() == false || doc[i]["val"].IsString() == false)
  2896. {
  2897. assert(false);
  2898. continue;
  2899. }
  2900. string key = doc[i]["name"].GetString();
  2901. string value = doc[i]["val"].GetString();
  2902. if (key.compare("enable") == 0)
  2903. {
  2904. enable = (value.compare("true") == 0);
  2905. }
  2906. else if (key.compare("lock_alarm_high_limit") == 0)
  2907. {
  2908. alarm_high_limit = atoi(value.c_str());
  2909. }
  2910. else if (key.compare("lock_warn_high_limit") == 0)
  2911. {
  2912. warn_high_limit = atoi(value.c_str());
  2913. }
  2914. else if (key.compare("d_alarm_high_limit") == 0)
  2915. {
  2916. alarm_high_limit = atoi(value.c_str());
  2917. }
  2918. else if (key.compare("d_warn_high_limit") == 0)
  2919. {
  2920. warn_high_limit = atoi(value.c_str());
  2921. }
  2922. else if (key.compare("f_alarm_high_limit") == 0)
  2923. {
  2924. f_alarm_high_limit = atoi(value.c_str());
  2925. }
  2926. else if (key.compare("f_warn_high_limit") == 0)
  2927. {
  2928. f_warn_high_limit = atoi(value.c_str());
  2929. }
  2930. else if (key.compare("keep_alarm_high_limit") == 0)
  2931. {
  2932. f_alarm_high_limit = atoi(value.c_str());
  2933. }
  2934. else if (key.compare("keep_warn_high_limit") == 0)
  2935. {
  2936. f_warn_high_limit = atoi(value.c_str());
  2937. }
  2938. else
  2939. {
  2940. assert(0);
  2941. }
  2942. }
  2943. if (alarm_high_limit == INT_MAX || warn_high_limit == INT_MAX) continue;
  2944. auto pInfo = new MAX_OVER_LIMIT_INFO;
  2945. pInfo->enable = enable;
  2946. pInfo->no = no;
  2947. pInfo->type = eType;
  2948. pInfo->alarm_high_limit = alarm_high_limit;
  2949. pInfo->warn_high_limit = warn_high_limit;
  2950. pInfo->f_alarm_high_limit = f_alarm_high_limit;
  2951. pInfo->f_warn_high_limit = f_warn_high_limit;
  2952. pInfo->tmLastCheckTime = time;
  2953. sprintf_s(pInfo->momp, 20, "%s.%s", mo, mp);
  2954. stringstream ss;
  2955. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  2956. m_alarm_set[ss.str()] = pInfo;
  2957. }
  2958. else if (eType == eZL_ALARMTYPE::FRICTION_OVER_LIMIT)
  2959. {
  2960. if (doc.IsArray() == false)
  2961. {
  2962. CSimpleLog::Error(CString("conf是非数组") + conf);
  2963. continue;
  2964. }
  2965. SizeType n = doc.Size();
  2966. bool enable = false;
  2967. int up_alarm_low_limit = INT_MAX;
  2968. int up_warn_low_limit = INT_MAX;
  2969. int dw_alarm_high_limit = INT_MIN;
  2970. int dw_warn_high_limit = INT_MIN;
  2971. for (SizeType i = 0; i < n; i++)
  2972. {
  2973. if (doc[i].HasMember("name") == false || doc[i].HasMember("val") == false || doc[i]["name"].IsString() == false || doc[i]["val"].IsString() == false)
  2974. {
  2975. assert(false);
  2976. continue;
  2977. }
  2978. string key = doc[i]["name"].GetString();
  2979. string value = doc[i]["val"].GetString();
  2980. if (key.compare("enable") == 0)
  2981. {
  2982. enable = (value.compare("true") == 0);
  2983. }
  2984. else if (key.compare("up_alarm_low_limit") == 0)
  2985. {
  2986. up_alarm_low_limit = atoi(value.c_str());
  2987. }
  2988. else if (key.compare("up_warn_low_limit") == 0)
  2989. {
  2990. up_warn_low_limit = atoi(value.c_str());
  2991. }
  2992. else if (key.compare("dw_alarm_high_limit") == 0)
  2993. {
  2994. dw_alarm_high_limit = atoi(value.c_str());
  2995. }
  2996. else if (key.compare("dw_warn_high_limit") == 0)
  2997. {
  2998. dw_warn_high_limit = atoi(value.c_str());
  2999. }
  3000. else
  3001. {
  3002. assert(0);
  3003. }
  3004. }
  3005. auto pInfo = new FRICTION_OVER_LIMIT_INFO;
  3006. pInfo->enable = enable;
  3007. pInfo->no = no;
  3008. pInfo->type = eType;
  3009. pInfo->up_alarm_low_limit = up_alarm_low_limit;
  3010. pInfo->up_warn_low_limit = up_warn_low_limit;
  3011. pInfo->dw_alarm_high_limit = dw_alarm_high_limit;
  3012. pInfo->dw_warn_high_limit = dw_warn_high_limit;
  3013. pInfo->tmLastCheckTime = time;
  3014. sprintf_s(pInfo->momp, 20, "%s.%s", mo, mp);
  3015. stringstream ss;
  3016. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  3017. m_alarm_set[ss.str()] = pInfo;
  3018. }
  3019. else if (eType == eZL_ALARMTYPE::SUOBI_LOCK_LIMIT)
  3020. {
  3021. auto doc = yyjson_read(conf, len, 0);
  3022. if (doc == nullptr) continue;
  3023. auto root = yyjson_doc_get_root(doc);
  3024. auto pInfo = new SUOBI_OVER_LIMIT_INFO;
  3025. pInfo->type = eZL_ALARMTYPE::SUOBI_LOCK_LIMIT;
  3026. pInfo->no = no;
  3027. pInfo->enable = yyjson_get_bool(yyjson_obj_get(root, "enable"));
  3028. pInfo->alarm_low_limit = yyjson_get_int(yyjson_obj_get(root, "alarm_low_limit"));
  3029. pInfo->warn_low_limit = yyjson_get_int(yyjson_obj_get(root, "warn_low_limit"));
  3030. pInfo->alarm_high_limit = yyjson_get_int(yyjson_obj_get(root, "alarm_high_limit"));
  3031. pInfo->warn_high_limit = yyjson_get_int(yyjson_obj_get(root, "warn_high_limit"));
  3032. sprintf_s(pInfo->momp, 20, "%s.%s", mo, mp);
  3033. stringstream ss;
  3034. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  3035. m_alarm_set[ss.str()] = pInfo;
  3036. yyjson_doc_free(doc);
  3037. }
  3038. else if (eType == eZL_ALARMTYPE::CONVERT_LIMIT)
  3039. {
  3040. auto doc = yyjson_read(conf, len, 0);
  3041. if (doc == nullptr) continue;
  3042. auto root = yyjson_doc_get_root(doc);
  3043. auto pInfo = new CONVERT_RESIST_OVER_LIMIT;
  3044. pInfo->type = eZL_ALARMTYPE::CONVERT_LIMIT;
  3045. pInfo->no = 2;
  3046. pInfo->enable = yyjson_get_bool(yyjson_obj_get(root, "enable"));
  3047. pInfo->dw_alarm_low_limit = yyjson_get_int(yyjson_obj_get(root, "dw_alarm_low_limit"));
  3048. pInfo->dw_warn_low_limit = yyjson_get_int(yyjson_obj_get(root, "dw_warn_low_limit"));
  3049. pInfo->up_alarm_high_limit = yyjson_get_int(yyjson_obj_get(root, "up_alarm_high_limit"));
  3050. pInfo->up_warn_high_limit = yyjson_get_int(yyjson_obj_get(root, "up_warn_high_limit"));
  3051. sprintf_s(pInfo->momp, 20, "%s.%s", mo, mp);
  3052. stringstream ss;
  3053. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  3054. m_alarm_set[ss.str()] = pInfo;
  3055. yyjson_doc_free(doc);
  3056. }
  3057. else if (eType == eZL_ALARMTYPE::RETENSION_FORCE)
  3058. {
  3059. auto doc = yyjson_read(conf, len, 0);
  3060. if (doc == nullptr) continue;
  3061. auto root = yyjson_doc_get_root(doc);
  3062. auto pInfo = new RETENSION_FORCE_DROP;
  3063. pInfo->type = eZL_ALARMTYPE::RETENSION_FORCE;
  3064. pInfo->no = no;
  3065. pInfo->enable = yyjson_get_bool(yyjson_obj_get(root, "enable"));
  3066. pInfo->dw_alarm_low_drop = yyjson_get_int(yyjson_obj_get(root, "dw_alarm_low_drop"));
  3067. pInfo->alarm_high_percent = yyjson_get_int(yyjson_obj_get(root, "alarm_high_percent"));
  3068. pInfo->alarm_low_percent = yyjson_get_int(yyjson_obj_get(root, "alarm_low_percent"));
  3069. sprintf_s(pInfo->momp, 20, "%s.%s", mo, mp);
  3070. stringstream ss;
  3071. ss << mo << '.' << mp << '.' << to_string(no) << '.' << to_string(type);
  3072. m_alarm_set[ss.str()] = pInfo;
  3073. yyjson_doc_free(doc);
  3074. }
  3075. else
  3076. {
  3077. ASSERT(0);
  3078. }
  3079. } while (true);
  3080. //2024年8月23日 开启默认开启最低3000N报警
  3081. const auto& mapInfo = CMonitorObjectMng::Instance()->m_mapMoMpInfo;
  3082. for (auto& it : mapInfo)
  3083. {
  3084. if (Find(it.second->mo, it.second->mp, (uint8_t)eSuoBiPosi::SB_FIX, eZL_ALARMTYPE::RETENSION_FORCE))
  3085. {
  3086. }
  3087. else
  3088. {
  3089. auto pInfo = new RETENSION_FORCE_DROP;
  3090. pInfo->no = (uint8_t)eSuoBiPosi::SB_FIX;
  3091. pInfo->enable = true;
  3092. pInfo->dw_alarm_low_drop = 3000;
  3093. sprintf_s(pInfo->momp, 20, "%s.%s", it.second->mo.c_str(), it.second->mp.c_str());
  3094. Insert(it.second->mo, it.second->mp, pInfo->no, (uint8_t)eZL_ALARMTYPE::RETENSION_FORCE, pInfo);
  3095. }
  3096. if (Find(it.second->mo, it.second->mp, (uint8_t)eSuoBiPosi::SB_INVERT, eZL_ALARMTYPE::RETENSION_FORCE))
  3097. {
  3098. }
  3099. else
  3100. {
  3101. auto pInfo = new RETENSION_FORCE_DROP;
  3102. pInfo->no = (uint8_t)eSuoBiPosi::SB_INVERT;
  3103. pInfo->enable = true;
  3104. pInfo->dw_alarm_low_drop = 3000;
  3105. sprintf_s(pInfo->momp, 20, "%s.%s", it.second->mo.c_str(), it.second->mp.c_str());
  3106. Insert(it.second->mo, it.second->mp, pInfo->no, (uint8_t)eZL_ALARMTYPE::RETENSION_FORCE, pInfo);
  3107. }
  3108. }
  3109. CSimpleLog::Info(fmt::format("一共加载到{}条报警规则:{}", m_alarm_set.size(), sql).c_str());
  3110. return TRUE;
  3111. }
  3112. BOOL CResistAlarmMng::LoadAlarmID()
  3113. {
  3114. CString sql = "SELECT TOP 1 ID FROM RM_ALARM ORDER BY ID DESC";
  3115. COdbcStatement stmt;
  3116. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  3117. {
  3118. CSimpleLog::Error("执行语句失败" + sql);
  3119. return FALSE;
  3120. }
  3121. int alarm_id;
  3122. int nCol = 1;
  3123. stmt.BindIntCol(nCol++, &alarm_id);
  3124. do
  3125. {
  3126. if (stmt.FetchNext() != 0) break;
  3127. m_nAlarmID = alarm_id;
  3128. } while (false);
  3129. stmt.Close();
  3130. return TRUE;
  3131. }
  3132. //加载未受理和未恢复的报警
  3133. BOOL CResistAlarmMng::LoadUnAck()
  3134. {
  3135. //只加载31天内的报警
  3136. 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 \
  3137. LEFT JOIN(SELECT mo, mp, zzjno FROM rm_map) AS B ON a.mo = b.mo and a.mp = b.mp \
  3138. WHERE DATEADD(dd, -31, getdate()) < occur_time and (ack_result = 0 OR [recovery_time] < '2000') \
  3139. ORDER BY ID; ";
  3140. COdbcStatement stmt;
  3141. if (!CDBConnectPool::Instance()->DBQuery(stmt, sql))
  3142. {
  3143. ASSERT(FALSE);
  3144. CSimpleLog::Error("执行语句失败" + sql);
  3145. return FALSE;
  3146. }
  3147. char mo[51], mp[51], desc[200], suggest[200], event_id[37] = { 0 }, rel_id[37] = { 0 };
  3148. uint8_t no, type, level, posi, loworhigh, sunroof;
  3149. int val;
  3150. int id;
  3151. int zzjno;
  3152. TIMESTAMP_STRUCT ts,tsRecovery;
  3153. int referval;
  3154. int nCol = 1;
  3155. stmt.BindIntCol(nCol++, &id);
  3156. stmt.BindCharCol(nCol++, mo, sizeof(mo));
  3157. stmt.BindCharCol(nCol++, mp, sizeof(mp));
  3158. stmt.BindTinyIntCol(nCol++, &no);
  3159. stmt.BindTinyIntCol(nCol++, &type);
  3160. stmt.BindTimeStampCol(nCol++, &ts);
  3161. stmt.BindTinyIntCol(nCol++, &level);
  3162. stmt.BindCharCol(nCol++, desc, sizeof(desc));
  3163. stmt.BindCharCol(nCol++, suggest, sizeof(suggest));
  3164. stmt.BindIntCol(nCol++, &val);
  3165. stmt.BindCharCol(nCol++, event_id, 36);
  3166. stmt.BindCharCol(nCol++, rel_id, 36);
  3167. stmt.BindTinyIntCol(nCol++, &posi);
  3168. stmt.BindTinyIntCol(nCol++, &loworhigh);
  3169. stmt.BindIntCol(nCol++, &referval);
  3170. stmt.BindTimeStampCol(nCol++, &tsRecovery);
  3171. stmt.BindTinyIntCol(nCol++, &sunroof);
  3172. stmt.BindIntCol(nCol++, &zzjno);
  3173. do
  3174. {
  3175. if (stmt.FetchNext() != 0) break;
  3176. ALARM_INFO *pAlarm = new ALARM_INFO;
  3177. pAlarm->event_id = event_id;
  3178. if (rel_id[0] != 0x20)
  3179. pAlarm->rel_id = rel_id;
  3180. pAlarm->id = id;
  3181. pAlarm->mo = mo;
  3182. pAlarm->mp = mp;
  3183. pAlarm->no = no;
  3184. pAlarm->type = (eZL_ALARMTYPE)type;
  3185. ConvertData(ts, pAlarm->time);
  3186. pAlarm->val = val;
  3187. pAlarm->level = level;
  3188. pAlarm->desc = desc;
  3189. pAlarm->suggest = suggest;
  3190. pAlarm->posi = (eDaoChaPosi)posi;
  3191. pAlarm->loworhigh = (eLowHigh)loworhigh;
  3192. pAlarm->refer_val = referval;
  3193. ConvertData(tsRecovery, pAlarm->recoveryTime);
  3194. pAlarm->sunroof = sunroof;
  3195. pAlarm->zzjno = zzjno;
  3196. m_lstUnConfirmAlarm.push_back(pAlarm);
  3197. } while (true);
  3198. stmt.Close();
  3199. CSimpleLog::Info(("一共加载(" + to_string(m_lstUnConfirmAlarm.size()) + ")条未受理和未恢复的报警").c_str());
  3200. return TRUE;
  3201. }