ResistAlarm.cpp 139 KB

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