Browse Source

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

lixioayao 5 years ago
parent
commit
5d44486cb7

+ 2 - 0
card_base.cpp

@@ -99,6 +99,7 @@ void card_location_base::make_his_location(uint64_t t,const point & pt,bool bclo
 		site_id=site_ptr->m_id;
 	}
 
+    log_info("make_his_location: card_id: %d,map_id: %d,area_id: %d,site_id: %d",m_id,map_id,area_id,site_id);
 	m_his_location_card->push(t,pt,area_id,map_id,site_id,bclose);
 }
 
@@ -235,6 +236,7 @@ int  card_location_base::get_stat()
     log_info("card_lost:%03d%010d,[%lu-%lu]tlost:%lu---%s",m_type,m_id,now,m_time,tlost,tlost>CARD_LOST_TIME_OUT?"True":"False");
     if(tlost>CARD_LOST_TIME_OUT)
     {
+        // 人卡盲区: 当人卡丢失信号大于60s,判定人卡进入盲区
         return STATUS_LOST;
     }
     else if(auto ev_ptr = event_list::instance()->get_event_card(m_id, m_type, ET_CARD_HELP))

+ 3 - 2
db/db_api/CDBCommon.h

@@ -39,7 +39,7 @@ namespace YADB
 	//                                 其它定义
 	//----------------------------------------------------------------------------
 	const int MAX_ASYNC_EXEC_FAILED_COUNT   = 3;//最大异步执行失败次数
-	const int MAX_ASYNC_QUEQUE_CAPACITY     = 32 * 1024;//异步执行队列最大容量
+	const int MAX_ASYNC_QUEQUE_CAPACITY     = 1<<20;//异步执行队列最大容量
 
 	/**
 	* @brief
@@ -93,7 +93,8 @@ namespace YADB
 	{
 		int FailedCount;//执行失败次数
 		std::string SQL; //SQ语句
-		_ASYNC_SQL_()
+		_ASYNC_SQL_(std::string&&sql)
+			:SQL(sql)
 		{
 			FailedCount = 0;
 		}

+ 78 - 57
db/db_api/CDBConnPool.cpp

@@ -4,10 +4,14 @@
 
 namespace YADB
 {
-	boost::lockfree::queue<_ASYNC_SQL_*, boost::lockfree::capacity<MAX_ASYNC_QUEQUE_CAPACITY>> __AsyncQueue;//异步执行无锁队列
+//	boost::lockfree::queue<_ASYNC_SQL_*, boost::lockfree::capacity<32768>> __AsyncQueue;//异步执行无锁队列
+
+	std::mutex g_mutex;
+	std::vector<_ASYNC_SQL_*> g_sql_list;
 
 	CDBConnPool::CDBConnPool()
 	{
+		g_sql_list.reserve(1<<15);
 		__pAsyncDBConn = 0;
 	}
 
@@ -201,57 +205,68 @@ namespace YADB
 		}
 	}
 
-	void CDBConnPool::_AsyncThreadFunc( CDBConnPool* pOwner )
+	void CDBConnPool::ExecAsyncSql(_ASYNC_SQL_*pData)
 	{
 		std::string Error;
-		while( pOwner->__Running )
-		{
-			_ASYNC_SQL_* pData = 0;
+		if ( !pData )
+			return;
 
-			while ( __AsyncQueue.pop( pData ) )
+		if ( __pAsyncDBConn )
+		{
+			my_ulonglong llRes = 0;
+			llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error ); 
+			if ( (my_ulonglong)-1 == llRes )
 			{
-				if ( pData )
+				//Execute failed, write log...
+				log_error( "Error,调用ExcuteRealSql失败,Err=%s,[%s]\n", Error.c_str(),pData->SQL.c_str());
+				//如果失败了看是不是数据库断开连接了,尝试重新连接一次
+				if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
 				{
-					if ( __pAsyncDBConn )
+					_DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
+					__pAsyncDBConn->Close();
+					int nRet = __pAsyncDBConn->Connect( ConnSetting, Error );
+					if ( nRet < 0 )
 					{
-						my_ulonglong llRes = 0;
-						llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error ); 
-						if ( (my_ulonglong)-1 == llRes )
-						{
-							//Execute failed, write log...
-							log_error( "Error,调用ExcuteRealSql失败,Err=%s,[%s]\n", Error.c_str(),pData->SQL.c_str());
-							//如果失败了看是不是数据库断开连接了,尝试重新连接一次
-							if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
-							{
-								_DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
-								__pAsyncDBConn->Close();
-								int nRet = __pAsyncDBConn->Connect( ConnSetting, Error );
-								if ( nRet < 0 )
-								{
-									Error = "Error,failed connect to database!";
-									//Connect failed, write log...
-									log_error( "Error,failed connect to database,Err=%s\n", Error.c_str() );
-									//如果连接失败了休息一下
-									boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
-								}
-							}
-
-							//如果执行失败,失败次数加一,失败次数小于最大失败次数放到队尾下次再执行
-							pData->FailedCount++;
-							if ( pData->FailedCount < MAX_ASYNC_EXEC_FAILED_COUNT )
-							{
-								_ASYNC_SQL_* pNewData = new _ASYNC_SQL_();
-								pNewData->FailedCount = pData->FailedCount;
-								pNewData->SQL         = pData->SQL;
-								__AsyncQueue.push( pNewData );
-							}
-						}
+						Error = "Error,failed connect to database!";
+						//Connect failed, write log...
+						log_error( "Error,failed connect to database,Err=%s\n", Error.c_str() );
+						//如果连接失败了休息一下
+						boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
 					}
+				}
 
-					delete pData;
-					pData = 0;
+				//如果执行失败,失败次数加一,失败次数小于最大失败次数放到队尾下次再执行
+				pData->FailedCount++;
+				if ( pData->FailedCount < MAX_ASYNC_EXEC_FAILED_COUNT )
+				{
+					std::unique_lock<std::mutex> lock(g_mutex);
+					g_sql_list.push_back(pData);
+					return;
 				}
 			}
+		}
+
+		delete pData;
+		pData = 0;
+
+	}
+
+	void CDBConnPool::_AsyncThreadFunc( CDBConnPool* pOwner )
+	{
+		while( pOwner->__Running )
+		{
+			std::vector<_ASYNC_SQL_*> sql_list;
+			sql_list.reserve(1<<15);
+
+			{
+				std::unique_lock<std::mutex> lock(g_mutex);
+				sql_list.swap(g_sql_list);
+			}
+
+			for(auto pData:sql_list)
+			{
+				ExecAsyncSql(pData);
+			}
 
 			boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
 		}
@@ -276,14 +291,26 @@ namespace YADB
 		}
 
 		//把异步执行无锁队列中每个元素释放
-		_ASYNC_SQL_* pData = 0;
-		while ( __AsyncQueue.pop( pData ) )
 		{
-			if (pData)
+			std::unique_lock<std::mutex> lock( g_mutex );
+			std::for_each(g_sql_list.begin(),g_sql_list.end(),[](_ASYNC_SQL_*psql){
+				delete psql;
+			});
+
+			g_sql_list.clear();
+
+
+			#if 0
+			_ASYNC_SQL_* pData = 0;
+			while ( __AsyncQueue.pop( pData ) )
 			{
-				delete pData;
-				pData = 0;
+				if (pData)
+				{
+					delete pData;
+					pData = 0;
+				}
 			}
+			#endif
 		}
 	}
 
@@ -322,17 +349,11 @@ namespace YADB
 		}
 	}
 
-	bool CDBConnPool::PushAsync( const std::string& strSQL )
+	bool CDBConnPool::PushAsync( std::string&& strSQL )
 	{
-		_ASYNC_SQL_* pData = new _ASYNC_SQL_;
-		if ( !pData )
-		{
-            logn_error(2,"PushAsync new pData失败");
-			return false;
-		}
-
-		pData->SQL = strSQL;
-		return __AsyncQueue.push( pData );
+		std::unique_lock<std::mutex> lock(g_mutex);
+		g_sql_list.push_back(new _ASYNC_SQL_(std::move(strSQL)));
+		return true;
 	}
 
 	bool CDBConnPool::Query( const char *szSql, CDBResultSet& DBRes,std::string& Error )

+ 2 - 1
db/db_api/CDBConnPool.h

@@ -147,6 +147,7 @@ namespace YADB
 
 		*/
 		void _AsyncThreadFunc( CDBConnPool* pOwner );//线程函数
+		void ExecAsyncSql(_ASYNC_SQL_*pData);
 	public:
 		CDBConnPool();
 		~CDBConnPool();
@@ -236,7 +237,7 @@ namespace YADB
 		* @bug
 
 		*/
-		bool PushAsync( const std::string& strSQL );
+		bool PushAsync( std::string&& strSQL );
 		/**
 		* @brief
 		执行SQL语句返回结果集函数。

+ 1 - 0
db/db_tool.cpp

@@ -27,6 +27,7 @@ namespace db_tool
 		return std::string(sql);
 	}
 
+    // 保存考勤开始或结束数据到数据库
     void save_attendance(const std::shared_ptr<card_location_base>& card_ptr,
                          const std::shared_ptr<area_hover>& area_hover_ptr)
     {

+ 1 - 0
his_location.cpp

@@ -212,6 +212,7 @@ void location_card::push(uint64_t timestamp,const point & p,int32_t areaid,int32
         update(p,timestamp,iflag);
         //set_invalid();
         if(iflag>1){
+            m_siteid = siteid;
             set(p,timestamp);
             insert();
         }

+ 2 - 0
module_service/module_web.cpp

@@ -59,6 +59,8 @@ void module_web::accept( int ID, std::string const& name,
         else if(JSON_CMD_VALUE_CALL_CARD_CANCEL_REQUEST == cmd)//取消呼叫
         {
             module_call::instance()->accept_cancel(data_value);
+        }else if(JSON_CMD_REQ_ALL_PERSON_ON_CAR == cmd) // 所有在车上的人卡信息
+        {
         }
     }
 }

+ 1 - 0
module_service/module_web.h

@@ -50,6 +50,7 @@ public:
         MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_CALL_CARD_CANCEL_REQUEST, &module_web::accept ) );
 
         MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_CLEAR_CARD, &module_web::accept ) );//手动升井
+        MsgFuncList.insert( std::make_pair( JSON_CMD_REQ_ALL_PERSON_ON_CAR,&module_web::accept)); // 接收web端请求的在车上的人卡信息
     }
 
     /// web前端有用户登录时,反馈给web所有信息

+ 16 - 16
websocket/ws_common.h

@@ -50,22 +50,22 @@ namespace YA
 	*/
 	struct _BASE_CARD_
 	{
-		int Type;//卡类型
-		int ID;//卡ID
-		double x;//x坐标
-		double y;//y坐标
-		double z;//z坐标
-		double down_time;//入井时间戳
-		double enter_area_time;//进入区域时间戳
-		double rec_time;//最后接收时间戳
-		double work_time;//工作时长
-		int map_id;//地图编号
-		int area_id;//区域ID
-		int dept_id;//部门编号
-		int stat;//状态
-		int running_stat;//运行状态
-		int biz_stat;//业务状态
-		double speed;//速度
+		int Type;               //卡类型
+		int ID;                 //卡ID
+		double x;               //x坐标
+		double y;               //y坐标
+		double z;               //z坐标
+		double down_time;       //入井时间戳
+		double enter_area_time; //进入区域时间戳
+		double rec_time;        //最后接收时间戳
+		double work_time;       //工作时长
+		int map_id;             //地图编号
+		int area_id;            //区域ID
+		int dept_id;            //部门编号
+		int stat;               //状态
+		int running_stat;       //运行状态: 包括上猴车状态
+		int biz_stat;           //业务状态
+		double speed;           //速度
 		_BASE_CARD_()
 		{
 			Type            = 0;