Browse Source

web重连与静止告警

daiyueteng 6 years ago
parent
commit
9ceda2f324

+ 8 - 94
main.cpp

@@ -20,7 +20,7 @@
 #include "mine_business.h"
 #include "main_test.h"
 #include "ya_setting.h"
-#include<atomic>
+#include "websocket/web_connect.h"
 
 config_file config;
 void handlereader(uint32_t readerid,bool duration,uint32_t t)
@@ -39,94 +39,6 @@ void Handle_ThreeRates_Event_Callback(const int evType, const int evId, uint64_t
     event_tool::instance()->handle_event((OBJECT_TYPE)evType,(EVENT_TYPE)evId,id,limitVal,curVal,bFalg);
 }
 
-#define BEATHEART_COUNT_NUM 1000
-std::atomic<int> beatheart_count;
-void web_beatheart( int ID, std::string const& name,
-                    sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
-{
-    beatheart_count=BEATHEART_COUNT_NUM;
-}
-
-void web_init(const std::vector<std::string>&uri_list,int32_t st)
-{
-    std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
-
-    module_mgr::init(config, MsgFuncList);
-
-    MsgFuncList.insert( std::make_pair( "beatheart", web_beatheart ) );
-
-    swsClientMgr.Build( uri_list, MsgFuncList );
-
-    //init thread...
-    YA::_THREAD_CONFIG_ Config;
-    Config.SendInterval = st;
-    swsTimerThrd.Init( Config );
-}
-
-bool web_connect()
-{
-    //连接服务器
-    if ( swsClientMgr.connect() != 0 )
-    {
-        log_error("web socket 连接失败....");
-        std_error("websocket 连接失败....");
-        return false;
-    }
-    //登录
-    swsClientMgr.login();
-
-    swsTimerThrd.Start();
-
-    return true;
-}
-
-void web_close()
-{
-    swsTimerThrd.Stop();
-    swsClientMgr.close();
-}
-
-void web_beatheart_thread()
-{
-    while (true)
-    {
-        std::this_thread::sleep_for(std::chrono::seconds(3));
-        if(beatheart_count==0)//一次都没收到web的心跳信号
-        {
-            log_info("没有收到web心跳信号");
-            continue;
-        }
-
-        if(beatheart_count==BEATHEART_COUNT_NUM)
-        {
-            beatheart_count++;
-            log_info("接收到web心跳信号");
-            continue;
-        }
-
-        beatheart_count=0;
-
-        int count=0;
-        while (true)
-        {
-            count++;
-            if(count>=10000)
-            {
-                count=10000;
-            }
-
-            web_close();
-            log_error("web连接异常中断, 开始重连web(第%d次)", count);
-            std::this_thread::sleep_for(std::chrono::seconds(2));
-            if(web_connect())
-            {
-                log_info("重连web ok");
-                break;
-            }
-        }
-    }
-}
-
 struct Init_Setting
 {
     void init()
@@ -203,9 +115,12 @@ struct Init_Setting
 
     bool wsClientMgr_init(const std::vector<std::string>&uri_list,int32_t st)
     {
-        web_init(uri_list, st);
+        std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
+        module_mgr::init(config, MsgFuncList);
+
+        web_connect::init(uri_list, st, MsgFuncList);
 
-        return web_connect();
+        return web_connect::connect();
     }
 };
 
@@ -213,7 +128,7 @@ struct Init_Setting
 void cleanup()
 {
     module_mgr::stop();
-    swsTimerThrd.Stop();
+    web_connect::close();
 }
 
 void usage(char ** argv)
@@ -257,8 +172,7 @@ int main(int argc ,char * argv[])
 	test_find_path(point(4600,-75),point(4727,-90));
 
     module_mgr::start();
-    auto th = std::thread(web_beatheart_thread);
-    th.detach();
+    web_connect::start_beatheart_monitor();
     atexit(&cleanup);
 
     net_service mh;

+ 17 - 8
module_service/area_business_motionless_persion.cpp

@@ -1,5 +1,6 @@
 #include"area_business_motionless_persion.h"
 
+#include<chrono>
 
 #include"common_tool.h"
 #include"log.h"
@@ -10,21 +11,19 @@
 #include"card.h"
 #include"config_file.h"
 
-
 struct motionless_data:business_data
 {
     motionless_data()
-        :m_acc_0count(0)
+        :m_acc_start_time(0)
+        ,m_acc_0count(0)
         ,m_is_warning(false)
     {
-        //        m_acc_0count=0;
-        //        m_acc_start_time=0;
     }
 
     ///;检测到多少次之后定位完全静止
     //   std::atomic<int> m_acc_0count;
-    //    ///检测到多久之后告警
-    //    time_t m_acc_start_time;
+    ///检测到多久之后告警
+    time_t m_acc_start_time;
 
     int m_acc_0count; //连续静止数量
     bool m_is_warning;  //
@@ -34,7 +33,7 @@ void area_business_motionless_persion::init(config_file& config)
 {
     //       sleep_ms = std::stoi(config.get("service.motionless_thread_sleep_ms","5000"));
     _acc_0count_limit = std::stoi(config.get("service.motionless_acc_0count_limit","40"));
-    //        _acc_seconds_limit = std::stoi(config.get("service.motionless_acc_seconds_limit","120"));
+    _acc_seconds_limit = std::stoi(config.get("service.motionless_acc_seconds_limit","120"));
 }
 
 void area_business_motionless_persion::on_load_his(const std::shared_ptr<area_hover>&area_hover_ptr,
@@ -86,7 +85,12 @@ void area_business_motionless_persion::on_hover(const std::shared_ptr<area_hover
     if(ptr_temp->m_acc_0count >= _acc_0count_limit)
     {
         ptr_temp->m_acc_0count = _acc_0count_limit;
-        if(!ptr_temp->m_is_warning)
+        if(ptr_temp->m_acc_start_time == 0)
+        {
+            time(&ptr_temp->m_acc_start_time);
+        }
+        if(!ptr_temp->m_is_warning
+                && tool_time::elapse_seconds(ptr_temp->m_acc_start_time) >= _acc_seconds_limit)
         {
             ptr_temp->m_is_warning = true;
             uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
@@ -98,6 +102,9 @@ void area_business_motionless_persion::on_hover(const std::shared_ptr<area_hover
         if(ptr_temp->m_is_warning)
         {
             ptr_temp->m_is_warning = false;
+
+            ptr_temp->m_acc_start_time = 0;
+
             uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
             event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, false);
         }
@@ -127,6 +134,8 @@ void area_business_motionless_persion::on_leave(const std::shared_ptr<area_hover
 }
 
 int area_business_motionless_persion::_acc_0count_limit=20;
+int area_business_motionless_persion::_acc_seconds_limit=120;
+
 
 //void module_motionless_persion::deal_alarm(std::shared_ptr<card_location_base>& card_ptr)
 //{

+ 1 - 0
module_service/area_business_motionless_persion.h

@@ -39,6 +39,7 @@ class area_business_motionless_persion : public area_business// : public i_threa
     //    void deal_alarm(std::shared_ptr<card_location_base>& card_ptr);
 private:
     static int _acc_0count_limit;
+    static int _acc_seconds_limit;
 
 public:
     static void init(config_file& config);

+ 104 - 0
websocket/web_connect.cpp

@@ -0,0 +1,104 @@
+#include "web_connect.h"
+
+#include<thread>
+#include<chrono>
+
+#include"log.h"
+#include"wsClientMgr.h"
+#include"wsTimerThread.h"
+
+#define BEATHEART_COUNT_NUM 1000
+
+web_connect::web_connect()
+{
+}
+
+std::atomic<int> web_connect::_beatheart_count;
+
+void web_connect::init(const std::vector<std::string>&uri_list,int32_t st, std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE>& MsgFuncList)
+{
+    MsgFuncList.insert( std::make_pair( "beatheart", &web_connect::_beatheart_callback ) );
+
+    swsClientMgr.Build( uri_list, MsgFuncList );
+
+    //init thread...
+    YA::_THREAD_CONFIG_ Config;
+    Config.SendInterval = st;
+    swsTimerThrd.Init( Config );
+}
+
+void web_connect::start_beatheart_monitor()
+{
+    auto th = std::thread(&web_connect::_beatheart_thread);
+    th.detach();
+}
+
+void web_connect::close()
+{
+    swsTimerThrd.Stop();
+    swsClientMgr.close();
+}
+
+bool web_connect::connect()
+{
+    //连接服务器
+    if ( swsClientMgr.connect() != 0 )
+    {
+        log_error("web socket 连接失败....");
+        std_error("websocket 连接失败....");
+        return false;
+    }
+    //登录
+    swsClientMgr.login();
+
+    swsTimerThrd.Start();
+
+    return true;
+}
+
+void web_connect::_beatheart_callback( int ID, std::string const& name,
+                    sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
+{
+    _beatheart_count=BEATHEART_COUNT_NUM;
+}
+
+void web_connect::_beatheart_thread()
+{
+    while (true)
+    {
+        std::this_thread::sleep_for(std::chrono::seconds(3));
+        if(_beatheart_count==0)//一次都没收到web的心跳信号
+        {
+            log_info("没有收到web心跳信号");
+            continue;
+        }
+
+        if(_beatheart_count==BEATHEART_COUNT_NUM)
+        {
+            _beatheart_count++;
+            log_info("接收到web心跳信号");
+            continue;
+        }
+
+        _beatheart_count=0;
+
+        int count=0;
+        while (true)
+        {
+            count++;
+            if(count>=10000)
+            {
+                count=10000;
+            }
+
+            close();
+            log_error("web连接异常中断, 开始重连web(第%d次)", count);
+            std::this_thread::sleep_for(std::chrono::seconds(2));
+            if(connect())
+            {
+                log_info("重连web ok");
+                break;
+            }
+        }
+    }
+}

+ 34 - 0
websocket/web_connect.h

@@ -0,0 +1,34 @@
+#ifndef WEB_CONNECT_H
+#define WEB_CONNECT_H
+
+#include<atomic>
+#include<vector>
+#include<string>
+#include<memory>
+
+#include"sio_message.h"
+#include"wsClient.h"
+
+class web_connect
+{
+public:
+    web_connect();
+
+    static void init(const std::vector<std::string>&uri_list,int32_t st, std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE>& MsgFuncList);
+
+    static void start_beatheart_monitor();
+
+    static bool connect();
+
+    static void close();
+
+private:
+    static void _beatheart_callback( int ID, std::string const& name,
+                           sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp );
+
+    static void _beatheart_thread();
+
+    static std::atomic<int> _beatheart_count;
+};
+
+#endif // WEB_CONNECT_H