Browse Source

fix person-device anti collision

zhuyf 3 years ago
parent
commit
264f41316d

+ 2 - 2
ant.cpp

@@ -252,12 +252,12 @@ bool visit_site_status::visit(std::shared_ptr<site> s)
 	event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_ERROR, s->m_id, READER_TIMEOUT, diff, diff>READER_TIMEOUT);
 
 	// 定位基站长时间无定位告警
-	if(s->m_loc_time > 0 && s->m_device_type_id == DEVICE_TYPE::LOCATE_SITE)
+	/*if(s->m_loc_time > 0 && s->m_device_type_id == DEVICE_TYPE::LOCATE_SITE)
 	{
 		int diff = now - s->m_loc_time;
 		log_info("site_no_position: site_id=%d, diff=%d, loc_time=%ld", s->m_id, diff, s->m_loc_time);
 		event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_LONG_TIME_NO_POSITION, s->m_id, CYaSetting::m_sys_setting.site_no_position_time, diff, diff>CYaSetting::m_sys_setting.site_no_position_time);
-	}
+	}*/
 
 	// 定位基站天线poa异常告警
 	if(s->m_device_type_id == DEVICE_TYPE::LOCATE_SITE){

+ 62 - 2
card_base.cpp

@@ -267,9 +267,12 @@ void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& lo
         snprintf(sql, 1024, "insert into his_distance_reader(card_type, ident, reader_id, dist, dir, cur_time) values(%d, %d, %d, %.2f, %d, '%s')", loc.m_card_type, loc.m_card_id, loc.m_site_id, loc.m_tof*15.65*2.996*1E-4, (tool_other::get_pdoa(loc.m_poa, site_ptr->m_pdoa_offset)>=0?1:-1), tool_time::to_str(time(NULL)).c_str());
         db_tool::PushAsync(sql);
         log_info("[sql] %s", sql);
-
+		
+		do_site_locate_event(loc);
+		do_card_locate_event(loc);
+ 
         m_message_handle->on_message(loop, loc, is_history);
-    }
+   }
 }
 
 //前端推送位置函数.
@@ -468,6 +471,14 @@ std::shared_ptr<card_location_base> card_location_base::make_car(const std::stri
 	return std::make_shared<car>(type, cardid, needdisplay, t, deptid, categoryid, type_id, level_id, cid);
 }
 
+/*
+ * @brief	基站覆盖距离检测
+ * @param	message_pdoa_locinfo& loc		卡数据
+ * @return	无
+ * @note
+ * @bug
+ * @warning
+ * */
 void card_location_base::do_site_coverage(message_pdoa_locinfo& loc)
 {
 	auto site_ptr = sit_list::instance()->get(loc.m_site_id);
@@ -504,3 +515,52 @@ void card_location_base::do_site_coverage(message_pdoa_locinfo& loc)
         logn_info(2, "[sql] %s", sql);
 	}
 }
+
+/*
+ * @brief	卡定位异常检测
+ * @param	message_pdoa_locinfo& loc		卡数据
+ * @return	无
+ * @note
+ * @bug
+ * @warning
+ * */
+void card_location_base::do_card_locate_event(const message_pdoa_locinfo& loc)
+{
+	bool status = false;
+
+	status = ((loc.m_tof == 0)?true:false);
+
+	uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
+    event_tool::instance()->handle_event(OT_CARD, ET_CARD_LOCATE_ERROR, id, 0, 0, status);	
+	//log_info("[card-locate-event] card_id=%d, ct=%d, tof=%lld", m_id, m_ct, loc.m_tof);
+}
+
+/*
+ * @brief	基站定位异常检测
+ * @param	message_pdoa_locinfo& loc		卡数据
+ * @return	无
+ * @note
+ * @bug
+ * @warning
+ * */
+void card_location_base::do_site_locate_event(const message_pdoa_locinfo& loc)
+{		
+	//log_info("[site-locate-event] cur_ct=%d, ct=%d, m_last_ct=%d, m_last_site_id=%d, loc.m_site_id=%d", loc.m_card_ct, ct, m_last_ct, m_last_site_id, loc.m_site_id);	
+	if(m_last_ct <= 0 || m_last_site_id == 0){
+		return;
+	}
+
+	if(loc.m_site_id != m_last_site_id){
+		return;
+	}
+
+	int dt = 0;
+	if(loc.m_card_ct > m_last_ct){
+		dt = loc.m_card_ct - m_last_ct;
+	}else{
+		dt = 65535 + loc.m_card_ct - m_last_ct;
+	}
+	
+	event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_LONG_TIME_NO_POSITION, loc.m_site_id, 3, dt, (dt>=3));
+	log_info("[site-locate-event] site_id=%d, card_id=%d, ct=%d, dt=%d", loc.m_site_id, m_id, loc.m_card_ct, dt);
+}	

+ 3 - 1
card_base.h

@@ -96,7 +96,7 @@ struct card_location_base:card,std::enable_shared_from_this<card_location_base>
 	int    m_help_bit = 0;
 
     //pdoa
-    int16_t m_last_ct           = -1;
+    int m_last_ct           = -1;
     float m_last_dist           = 0.0;
     uint16_t m_last_site_id     = 0;        // 卡上一次定位的分站id
     uint64_t m_last_recv_time   = 0;        // 卡上一次接收时间
@@ -155,6 +155,8 @@ struct card_location_base:card,std::enable_shared_from_this<card_location_base>
     void set_event_flag(EVENT_TYPE et,std::uint8_t f=1){m_event[et]=f;}
     bool get_event_flag(EVENT_TYPE et){return m_event[et];}
 	void do_site_coverage(message_pdoa_locinfo& loc);
+	void do_card_locate_event(const message_pdoa_locinfo& loc);
+	void do_site_locate_event(const message_pdoa_locinfo& loc);
 
     bool is_person() const;
     bool is_vehicle() const;

+ 6 - 0
card_car.cpp

@@ -70,9 +70,15 @@ void car::handle_anti_coll(const point& pt, const int& sid)
         //log_info("[anti_coll] key=%d, value=%.2f", k.first, k.second);
         for(auto& c : tmp_cards)
         {
+			// 车卡不参与车车之间防碰撞
             if(c.second->m_type == CT_VEHICLE){
                 continue;
             }
+			// 司机卡不参与人车之间的防碰撞
+			if(c.second->m_id>=9000&&c.second->m_id<=9999){
+				continue;
+			}
+
             bool s = false;
             int d = (c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) / 1000.0;
             s = ((c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) /1000.0 <= 30);

+ 14 - 0
event.cpp

@@ -95,6 +95,20 @@ std::shared_ptr<ya_event> Event::create_event(const std::string&obj_id,EVENT_TYP
     return ev_ptr;
 }
 
+/*
+ * @breif		构建开始或者取消告警事件
+ * @param		EVENT_TYPE et			事件设备类型
+ * @param		uint64_t id
+ * @param		double limit_value		阈值
+ * @param		double cur_value		当前值
+ * @param		bool f					告警标志,true表示开始告警,false表示取消告警
+ * @param		EVENT_DIS_TYPE edt
+ * @param		const std::string& desc 描述
+ * @return		无
+ * @note
+ * @bug
+ * @warning
+ * */
 void Event::handle_alarm_event(EVENT_TYPE et,uint64_t id,double limit_value,double cur_value,bool f,EVENT_DIS_TYPE edt,const std::string &desc)
 {
     std::shared_ptr<ya_event> ev_ptr = nullptr;

+ 95 - 0
module_service/module_device_net.cpp

@@ -0,0 +1,95 @@
+#include "module_device_net.h"
+#include <algorithm>
+#include "log.h"
+#include "db/db_tool.h"
+#include "tool_byte.h"
+#include "tool_time.h"
+
+void module_device_net::do_business(const std::string& ip, const int& id, const int& type)
+{
+	// 如果IP设备不存在,则创建设备列表
+	if(!exist(ip)){
+		std::shared_ptr<net_device_info> ptr_data = std::make_shared<net_device_info>(ip);
+		m_socket_device_list.insert(std::make_pair(ip, ptr_data));
+	}
+
+	auto it = m_socket_device_list.find(ip);
+	if(it == m_socket_device_list.end()){
+		return;
+	}
+
+	std::string did = tool_byte::to_cardid(type, id);
+	if(it->second->exist(did)){
+		return;
+	}
+
+	// 不存在设备,则创建设备网络连接信息
+	device_info data(did, id, type, time(0));
+	it->second->m_device_list.insert(std::make_pair(did, data));
+
+	char sql[500] = {0};
+	snprintf(sql, 500, "INSERT INTO his_device_net_log(device_id, device_type, connect_time) VALUES(%d, %d,'%s');", id, type, tool_time::to_str(data.m_time).c_str());
+	//logn_info(2, "[device-net] %s", sql);
+	db_tool::PushAsync(sql);	
+}
+
+bool module_device_net::exist(const std::string& ip)
+{
+	std::lock_guard<std::mutex> lg(m_mtx);
+	auto it = m_socket_device_list.find(ip);
+	if(it != m_socket_device_list.end()){
+		return true;
+	}
+
+	return false;
+}
+
+void module_device_net::clear()
+{
+	std::lock_guard<std::mutex> lg(m_mtx);
+	for(auto it : m_socket_device_list)
+	{
+		clear(it.first);
+	}
+}
+
+void module_device_net::load_db()
+{
+
+}
+
+void module_device_net::clear(const std::string& ip)
+{
+	std::lock_guard<std::mutex> lg(m_mtx);
+	log_info("[device-net] clear ip=%s", ip.c_str());
+	auto it = std::find_if(m_socket_device_list.begin(), m_socket_device_list.end(), [&](std::pair<std::string, std::shared_ptr<net_device_info>> item){
+			log_info("[device-net] exist ip=%s", item.second->m_ip.c_str());
+			if(item.second->m_ip == ip){
+				return true;
+			}else{
+				return false;
+			}
+		});
+
+	if(it == m_socket_device_list.end()){
+		return;
+	}
+
+	if(it->second->m_device_list.size() <= 0){
+		log_info("[device-net] there is no device which the ip is %s", ip.c_str());
+		return;
+	}
+
+	// 构造sql插入数据库
+	for(auto itd : it->second->m_device_list){
+		int dt = 0;
+		dt = time(0) - itd.second.m_time;
+
+		char sql[500] = {0};
+		snprintf(sql, 500, "UPDATE his_device_net_log SET disconnect_time = '%s' , duration=%d WHERE device_id=%d AND device_type=%d AND connect_time='%s';", tool_time::to_str(time(0)).c_str(), dt , itd.second.m_id, itd.second.m_type, tool_time::to_str(itd.second.m_time).c_str());
+		db_tool::PushAsync(sql);		
+		//logn_info(2, "[device-net] %s", sql);
+	}	
+
+	m_socket_device_list.erase(it);
+}

+ 102 - 0
module_service/module_device_net.h

@@ -0,0 +1,102 @@
+/*
+ * @file
+ *		module_device_net.h
+ *
+ * @brief
+ *		设备网络连接明细业务
+ * @version
+ *		V1.0
+ * @author
+ *		zhuyf
+ * @date
+ *		2022/01/11
+ * @note
+ *
+ * @warning
+ * @bug
+ * @copyright
+ * */
+#ifndef module_device_net_hpp
+#define module_device_net_hpp
+
+#include <map>
+#include <string>
+#include <memory>
+#include <time.h>
+#include "module_singleton_base.h"
+
+// 设备包括定位基站、通信基站、红绿灯等
+struct device_info{
+	std::string m_did;		// 设备类型+ID号,形如0030000000004
+	int m_id;				// 设备ID号
+	int m_type;				// 设备类型
+	time_t m_time;			// 设备网络建立时间
+
+	device_info()
+		: m_did("")
+		, m_id(0)
+		, m_type(0)
+		, m_time(0)
+	{}
+
+	device_info(const std::string& did, const int& id, const int& type, const time_t& t)
+		: m_did(did)
+		, m_id(id)
+		, m_type(type)
+		, m_time(t)
+	{}
+};
+
+struct net_device_info{
+	std::string m_ip;	// 网络的IP
+	std::map<std::string, device_info> m_device_list;	// key  value
+
+	net_device_info()
+		: m_ip("")
+	{}
+
+	net_device_info(const std::string& ip)
+		: m_ip(ip)
+	{}
+
+	~net_device_info()
+	{
+		clear();
+	}
+
+	void clear()
+	{
+		m_device_list.clear();
+	}
+
+	bool exist(const std::string& device_id)
+	{
+		auto it = m_device_list.find(device_id);
+
+		if(it != m_device_list.end()){
+			return true;
+		}
+
+		return false;
+	}
+};
+
+
+class module_device_net: public singleton_base<module_device_net>
+{
+	private:
+		friend class singleton_base<module_device_net>;
+		module_device_net(){}
+		
+	public:
+		void load_db();
+		void do_business(const std::string& ip, const int& id, const int & type);
+		bool exist(const std::string& ip);
+		void clear();
+		void clear(const std::string& ip);
+	private:
+		std::mutex m_mtx;
+		std::map<std::string, std::shared_ptr<net_device_info>> m_socket_device_list;
+};
+
+#endif

+ 0 - 1
module_service/module_other_alarm.cpp

@@ -30,4 +30,3 @@ void module_other_alarm::power_normal(std::shared_ptr<card_location_base> card_p
     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_LOW_POWER_SERIOUS, id, 0, 0, false);
 }
-

+ 1 - 1
net-service.cpp

@@ -239,7 +239,7 @@ void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,
                         m.m_site_id = site_id;  
                         m.m_time_stamp = tstamp;
                         m.m_loc_type = LDT_PDOA;
-                        t->m_cmd_code = cmd;
+						t->m_cmd_code = cmd;
                         t->m_hash_id = m.m_card_id;
                         pdoa = tool_other::get_pdoa(m.m_poa, site_ptr->m_pdoa_offset);