浏览代码

Merge branch 'master' of http://local.beijingyongan.com:3000/linux-dev/ya-serv

lixioayao 6 年之前
父节点
当前提交
06445d7fce

+ 8 - 0
.gitignore

@@ -26,5 +26,13 @@ CMakeFiles/
 .git/
 .vscode/
 Makefile
+Debug/*
+CMakeFile*
+*.txt*
+/etc
+config_file.h
+log.h
+struct_def.h
+three_rates.h
 
 

+ 8 - 5
area_business.cpp

@@ -20,13 +20,16 @@ struct area_business_factory
 {
 	void regist(int type,area_business* ab)
 	{
+		type-=1;
+
+		ab->m_business_type=type;
+
 		if(type>=(int)m_check_list.size())
 		{
 			m_check_list.resize(type+1);
 		}
 
 		assert(!m_check_list[type]);
-
 		m_check_list[type].reset(ab);
 	}
 
@@ -75,11 +78,11 @@ struct area_business_factory
 		regist(6,new area_business_car_attendance);
 		regist(7,new area_business_forbid);
 		//regist(8,new area_business_monkey_area);
-		regist(9,new area_business_card_enter_or_leave);
+		regist(8,new area_business_card_enter_or_leave);
 
-		regist(10, new area_business_motionless_persion);
-		regist(11, new area_business_geofault);
-		regist(12, new area_business_work_rate);
+		regist(9, new area_business_motionless_persion);
+		regist(10, new area_business_geofault);
+		regist(11, new area_business_work_rate);
 	}
 
 };

+ 5 - 1
area_business.h

@@ -13,7 +13,11 @@ struct area_hover;
 struct card_location_base;
 struct area_business
 {
-	virtual int area_business_type()=0;
+	int m_business_type=0;
+	int area_business_type()
+	{
+		return m_business_type;
+	}
 
 	virtual void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr){}
 	virtual void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr){}

+ 8 - 25
main.cpp

@@ -20,15 +20,17 @@
 #include "mine_business.h"
 #include "main_test.h"
 #include "ya_setting.h"
+#include "websocket/web_connect.h"
 
 config_file config;
 void handlereader(uint32_t readerid,bool duration,uint32_t t)
 {
 
 }
+
 //三率模块中告警回调
 void Handle_ThreeRates_Event_Callback(const int evType, const int evId, uint64_t id
-        , double limitVal, double curVal, bool bFalg)
+                                      , double limitVal, double curVal, bool bFalg)
 {
     if (evId >= EVENT_TYPE::CARD_EVENT_COUNT_MAX  || evType > OBJECT_TYPE::OT_CARD)
     {
@@ -113,33 +115,12 @@ struct Init_Setting
 
     bool wsClientMgr_init(const std::vector<std::string>&uri_list,int32_t st)
     {
-		bool flag = true;
         std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
-
-        //MsgFuncList.insert( std::make_pair( "req_all_person_on_car", On_req_all_person_on_car ) );
-
         module_mgr::init(config, MsgFuncList);
 
-        swsClientMgr.Build( uri_list, MsgFuncList );
-
-		do{
-			//连接服务器
-			 if ( swsClientMgr.connect() != 0 )
-       		 {
-       		     log_error("web socket init failed.");
-       		     std_error("websocket 初始化失败....");
-	   		 	 flag =false;
-				 break;
-       		 }
-       		 //登录
-       		 swsClientMgr.login();
-       		 //init thread...
-       		 YA::_THREAD_CONFIG_ Config;
-       		 Config.SendInterval = st;
-       		 swsTimerThrd.Init( Config );
-       		 swsTimerThrd.Start();
-		}while(false);
-		return flag;
+        web_connect::init(uri_list, st, MsgFuncList);
+
+        return web_connect::connect();
     }
 };
 
@@ -147,6 +128,7 @@ struct Init_Setting
 void cleanup()
 {
     module_mgr::stop();
+    web_connect::close();
 }
 
 void usage(char ** argv)
@@ -190,6 +172,7 @@ int main(int argc ,char * argv[])
 	test_find_path(point(4600,-75),point(4727,-90));
 
     module_mgr::start();
+    web_connect::start_beatheart_monitor();
     atexit(&cleanup);
 
     net_service mh;

+ 0 - 5
module_service/area_business_car_attendance.h

@@ -11,11 +11,6 @@ class area_business_car_attendance:public area_business
 public:
     area_business_car_attendance();
 
-    virtual int area_business_type()
-    {
-        return 6;
-    }
-
     void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
 
     //记录进入时间等信息,结束考勤,根据离开的时间和距离,判断是否记录一条新的考勤记录

+ 0 - 5
module_service/area_business_card_enter_or_leave.h

@@ -5,11 +5,6 @@
 
 struct area_business_card_enter_or_leave:area_business
 {
-	int area_business_type()
-	{
-		return 9;
-	}
-
     void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
 
 	//进入区域则入库操作

+ 0 - 6
module_service/area_business_count_checker.h

@@ -10,12 +10,6 @@
 */
 struct area_business_count_checker:area_business
 {
-
-	int area_business_type()
-	{
-		return 3;
-	
-	}
 	//服务器重启加载数据
 	void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
 	//增加计数,并进行判断

+ 0 - 4
module_service/area_business_forbid.h

@@ -6,10 +6,6 @@
 class area_business_forbid : public area_business
 {
 public:
-    virtual int area_business_type()
-    {
-        return 7;
-    }
     void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
     //记录进入时间等信息,生成告警
     void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);

+ 0 - 5
module_service/area_business_geofault.h

@@ -21,11 +21,6 @@ struct area;
 class area_business_geofault:public area_business
 {
 public:
-    int area_business_type()
-    {
-        return 11;
-    }
-
     void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
 
     void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);

+ 18 - 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,
@@ -81,12 +80,18 @@ void area_business_motionless_persion::on_hover(const std::shared_ptr<area_hover
     else
     {
         ptr_temp->m_acc_0count=0;
+        ptr_temp->m_acc_start_time = 0;
     }
 
     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 +103,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 +135,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 - 4
module_service/area_business_motionless_persion.h

@@ -39,12 +39,9 @@ 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:
-    int area_business_type()
-    {
-        return 10;
-    }
     static void init(config_file& config);
 
     void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);

+ 0 - 6
module_service/area_business_person_attendance.h

@@ -9,12 +9,6 @@
 
 struct area_business_person_attendance:area_business
 {
-    int area_business_type()
-    {
-        return 5;
-    }
-
-
     //记录进入时间等信息,开始考勤
     void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
 

+ 0 - 4
module_service/area_business_person_dwell_checker.h

@@ -10,10 +10,6 @@
 */
 struct area_business_person_dwell_checker:area_business
 {
-	int area_business_type()
-	{
-		return 2;
-	}
 	//服务器重启加载数据
 	virtual void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
 	//进入区域,记录进入时间

+ 0 - 4
module_service/area_business_post_area.h

@@ -8,10 +8,6 @@
 */
 struct area_business_post_area:area_business
 {
-	int area_business_type()
-	{
-		return 1;
-	}
 	virtual void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
 	//将推送区域信息加入人员数据
 	void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);

+ 0 - 5
module_service/area_business_speed_checker.h

@@ -7,11 +7,6 @@
 class area_business_speed_checker : public area_business
 {
 public:
-    virtual int area_business_type()
-    {
-        return 4;
-    }
-
     void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
     void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
     void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);

+ 0 - 5
module_service/area_business_work_rate.h

@@ -9,11 +9,6 @@
 class area_business_work_rate:public area_business
 {
 public:
-    int area_business_type()
-    {
-        return 12;
-    }
-
     void on_load_his(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
 
     void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);

+ 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

+ 8 - 1
websocket/wsTimerThread.cpp

@@ -26,11 +26,17 @@ namespace YA
 	{
 		__Running = true;
 		__Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) );
-		__Thread->detach();
+        //__Thread->detach();
 	}
 
 	void wsTimerThread::Stop()
 	{
+        if(__Thread==nullptr)
+        {
+            return;
+        }
+        __Thread->interrupt();
+        __Running = false;
 		std::cout << "::Stop() begin" << std::endl;
 
 		__Enable = false;
@@ -42,6 +48,7 @@ namespace YA
 		}
 
 		__Reset();
+        __Thread=nullptr;
 
 		std::cout << "::Stop() end" << std::endl;
 	}