Prechádzať zdrojové kódy

add device status json

zhuyf 3 rokov pred
rodič
commit
efb341a933

+ 17 - 4
ant.cpp

@@ -7,6 +7,7 @@
 #include "tool_time.h"
 #include "area.h"
 #include "card_path.h"
+#include "websocket/wsTimerThread.h"
 
 int site::index()const
 {
@@ -20,7 +21,7 @@ site::site(int id)
 	,m_id(id)
 	,m_path_empty(true)
 {
-	m_time=time(0);
+	//m_time = time(0);
 	for(uint32_t i=0;i<m_timeoff_count.size();i++)
 		m_timeoff_count[i]=0;
 }
@@ -216,6 +217,7 @@ std::vector<point> ant::getsol(const double &dist) const
 		{
 			continue;
 		}
+
 		v.push_back(pt);
 	}
 	return std::move(v);
@@ -224,8 +226,15 @@ std::vector<point> ant::getsol(const double &dist) const
 bool visit_site_status::visit(std::shared_ptr<site> s)
 {
 	time_t now = time(0);
-	int diff = now-s->m_time;
+	int diff = now - s->m_time;
 	event_tool::instance()->handle_event(OT_DEVICE_READER, ET_READER_ERROR, s->m_id, READER_TIMEOUT, diff, diff>READER_TIMEOUT);
+
+	// 更新设备状态
+	int state = (diff > READER_TIMEOUT ? 1 : 0);
+	//log_info("[device_state] rid=%d, diff=%d, timeout=%d, state=%d, now=%lld, rtime=%lld", s->m_id, diff, READER_TIMEOUT, state, now, s->m_time);
+	sys::device_state ds(s->m_id, s->m_device_type_id, state, now);
+	swsTimerThrd.upt_device_state(ds);
+
 	return true;
 }
 
@@ -401,8 +410,8 @@ void sit_list::read_ant_path(int id)
 void sit_list::init_site(const std::string &ids /*=""*/)
 {
      std::string sql = "SELECT reader_id, reader_type_id, dat_reader.map_id, \
-             area_id, device_type_id, dimension, dat_map.scale,need_power_alarm\
-			 ,x,y, pdoa_offset, pdoa_direction, isSpecial\
+             area_id, device_type_id, dimension, dat_map.scale,need_power_alarm \
+			 ,x,y, pdoa_offset, pdoa_direction, isSpecial, down_stream_idx \
              FROM dat_reader, dat_map where \
              dat_reader.map_id=dat_map.map_id and state=0";
 
@@ -465,6 +474,9 @@ void sit_list::init_site(const std::string &ids /*=""*/)
          int power_alarm  = 0;
          DBRes.GetField( "need_power_alarm",power_alarm, Error );
 
+		 int down_stream_idx = 0;
+		 DBRes.GetField("down_stream_idx", down_stream_idx, Error);
+
          site_ptr->m_reader_type_id = reader_type_id;
          site_ptr->m_map_id = map_id;
          site_ptr->m_area_id = area_id;
@@ -472,6 +484,7 @@ void sit_list::init_site(const std::string &ids /*=""*/)
          site_ptr->m_device_type_id = device_type_id;
          site_ptr->m_num_dims = dimension;
          site_ptr->m_scale = scale;
+		 site_ptr->m_down_stream_idx = down_stream_idx;
          site_ptr->create_area();
 
          double x = 0, y = 0;

+ 6 - 3
ant.h

@@ -101,12 +101,14 @@ struct site:point,std::enable_shared_from_this<site>
     mutable double m_ant_dist=0;
     mutable double m_ant_dist_sum_new=0;
     mutable int m_ant_dist_cnt_new=0;
-    ///分站位置 READER_TYPE_ID
+    ///分站位置 READER_TYPE_ID, 井上分站,井下分站
     int m_reader_type_id = 0;
     int m_map_id = 0;
     int m_area_id = 0;
-    /// 设备类型,分站、通信分站、交通灯等
+    // 设备类型,分站、通信分站、交通灯等
     int m_device_type_id=0;
+	// 是否为网络设备,0为can设备,1为网络设备
+	int m_net_device_status;
     
     //pdoa
     double m_pdoa_offset = 0.0;     // pdoa分站天线相位偏移量,90度的偏移值
@@ -131,6 +133,7 @@ struct site:point,std::enable_shared_from_this<site>
 	std::array<int,32>	m_timeoff_count;
 	int  m_package_count=0;
     int  m_special = 0;
+	int  m_down_stream_idx = 0;
 
     site(int id=-1);
 
@@ -155,7 +158,7 @@ struct site:point,std::enable_shared_from_this<site>
     {
 		if(m_clt != clt)
 			m_clt = clt;
-		m_time=time(0);
+		m_time = time(0);
     }
 
     std::shared_ptr<client> get_client()

+ 9 - 1
card_base.cpp

@@ -33,11 +33,16 @@ card_location_base::card_location_base(const std::string&type,uint32_t id,uint16
     m_cb_pdoa.set_capacity(5);
     m_cb_tof.set_capacity(5);
     m_cb_point.set_capacity(5);
+
     for(int index = 0; index < 5; ++index){
         m_cb_pdoa.push_back(100);
         m_cb_tof.push_back(0);
         m_cb_point.push_back(point(0,0));
     }
+
+	m_cb_stream_idx.set_capacity(2);
+	m_cb_stream_idx.push_back(0);
+	m_cb_stream_idx.push_back(0);
 }
 
 void card_location_base::do_status(int st)
@@ -227,7 +232,7 @@ void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& lo
 
     // 此条件用于解决井下有的基站不能送出电量值时,会将卡的电量值按0输出,这种情况,默认卡的电量为初始值或之前值
     //if(loc.m_batty_status > 0){
-        m_battery_value = loc.m_batty_status;       // 电量值
+    m_battery_value = loc.m_batty_status;       // 电量值
     //}
 
     handle_message(loc.m_card_ct, loc.m_batty_status);
@@ -235,6 +240,9 @@ void card_location_base::on_message(zloop<task*>* loop, message_pdoa_locinfo& lo
         log_info("card=%d被井上分站[site=%d]收到", m_id, site_ptr->id());
         area_tool->on_point(shared_from_this(), point(1,1));
         this->site_hover(loc.m_site_id);
+		m_last_site_id = site_ptr->id();
+		//m_last_down_stream_idx = site_ptr->m_down_stream_idx;
+		m_cb_stream_idx.push_back(site_ptr->m_down_stream_idx);
     }
     else{
         char sql[1024] = {0};

+ 7 - 2
card_base.h

@@ -92,8 +92,8 @@ struct card_location_base:card,std::enable_shared_from_this<card_location_base>
 	std::unique_ptr<location_card> m_his_location_card;
 	std::atomic<int> m_upmine_flag{0};
 
-	time_t m_help_last_time=0;                  // 呼救持续时间
-	int    m_help_bit=0;
+	time_t m_help_last_time = 0;                  // 呼救持续时间
+	int    m_help_bit = 0;
 
     //pdoa
     int16_t m_last_ct           = -1;
@@ -110,6 +110,11 @@ struct card_location_base:card,std::enable_shared_from_this<card_location_base>
     boost::circular_buffer<float> m_cb_tof;     // the list of tof that is pdoa history value
     boost::circular_buffer<point> m_cb_point;
     point m_last_point;
+	int m_cur_site_counts = 0;				// 当前基站缓存数据总数
+	std::vector<float> m_vtp_dist;			// 相同分站存储距离数据
+	bool m_over_site = false;				// 过分站状态
+	//int m_last_down_stream_idx = 0;
+	boost::circular_buffer<int> m_cb_stream_idx;	// 分站下行方向的索引号
 
     bool m_enable_anti_collision = false;
     int m_call_level = 0;

+ 5 - 3
module_service/module_call_help.cpp

@@ -15,9 +15,11 @@
  */
 void module_call_help::rev_from_card_help(std::shared_ptr<card_location_base> card_ptr)
 {
-    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_HELP, id, 0, 0, true);
-    if(card_ptr)card_ptr->set_event_flag(ET_CARD_HELP);
+	if(card_ptr->m_type == CT_PERSON){
+		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_HELP, id, 0, 0, true);
+		if(card_ptr)card_ptr->set_event_flag(ET_CARD_HELP);
+	}
 }
 
 /**

+ 1 - 1
module_service/module_traffic_light_manager.cpp

@@ -451,7 +451,7 @@ bool traffic_light_manager::on_path(const std::vector<pos_data>& vtp, const poin
 
     bool exist = false;
     if(it != m_map_path.end()){
-        for(int i = 0;i<it->second.size();++i)
+        for(size_t i = 0;i<it->second.size();++i)
         {
             if(it->second[i].contain(p, 20)){
                 exist = true;

+ 0 - 1
module_service/module_web.cpp

@@ -208,4 +208,3 @@ void module_web::_delete_end(std::vector<std::shared_ptr<ya_event>>& arr)
     if(!todelete.empty())
         event_list::instance()->remove(todelete);
 }
-

+ 1 - 1
module_service/module_web.h

@@ -63,7 +63,7 @@ private:
     ///获取所有的告警事件
     void _get_all_events(std::vector<std::shared_ptr<ya_event>>& arr,bool f=false);
 
-    ///在全局列表中删除已经处理或结束了的告警
+	///在全局列表中删除已经处理或结束了的告警
     void _delete_end(std::vector<std::shared_ptr<ya_event>>& arr);
 };
 

+ 11 - 5
net-service.cpp

@@ -239,7 +239,8 @@ void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,
                         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);
-                        logn_info(3, "[pdoa] site_id=%d, card_id=%d, ct=%d, dist=%.3f, rav=%d, poa1=%.4f, poa2=%.4f, poa3=%.4f, pdoa=%.4f, pdoa_offset=%.4f", site_id, m.m_card_id, m.m_card_ct, dist, m.m_rav, m.m_poa[0], m.m_poa[1], m.m_poa[2], pdoa, site_ptr->m_pdoa_offset);
+                        
+						logn_info(3, "[pdoa] site_id=%d, card_id=%d, ct=%d, dist=%.3f, rav=%d, poa1=%.4f, poa2=%.4f, poa3=%.4f, pdoa=%.4f, pdoa_offset=%.4f", site_id, m.m_card_id, m.m_card_ct, dist, m.m_rav, m.m_poa[0], m.m_poa[1], m.m_poa[2], pdoa, site_ptr->m_pdoa_offset);
                      
                         if(m.m_poa[1] == 10.0 || dist < 0.0001 ){
                             continue;
@@ -318,7 +319,9 @@ void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,
                         break;
                     }
 
-					site_ptr->m_device_type_id = 0x08;
+					site_ptr->m_time = time(0);
+					//site_ptr->m_device_type_id = 0x08;
+					site_ptr->m_net_device_status = 1;
                 }
                 break;
             case 0x793B: //虚拟推送的心跳测试
@@ -474,17 +477,20 @@ int32_t net_service::parse_data_anchor_opt(const std::shared_ptr<client>& clt, z
         return -1;
     }*/
 
-   	site_ptr->m_device_type_id = (power>>3)&0x07;
+   	//site_ptr->m_device_type_id = (power>>3)&0x07;
+	site_ptr->m_net_device_status = (((power>>3)&0x07) == 0 ? 0: 1);
 
     if(!check_message_time(clt, site_ptr, &site_tm, site_ct, power)){
 		// 分站时间异常,直接校时
-		switch(site_ptr->m_device_type_id)
+		//switch(site_ptr->m_device_type_id)
+		switch(site_ptr->m_net_device_status)
 		{
 		case 0:
 			can_cali_time(clt, site_id);
 			break;
 		case 1:
 			net_cali_time(clt);
+			break;
 		default:
 			net_cali_time(clt);
 			break;
@@ -500,7 +506,7 @@ int32_t net_service::parse_data_anchor_opt(const std::shared_ptr<client>& clt, z
 
 	uint64_t tt = site_time_stamp;
     tt *= 1000;
-    logn_info(1,"分站数据:site_id: %d, site_ct: %d, time_t: %llu, time: %s, power: %d, device_type: %d", site_id, site_ct, tt, tool_time::to_str_ex(tt).c_str(), power, site_ptr->m_device_type_id);
+    logn_info(1,"分站数据:site_id=%d, site_ct=%d, time_t=%llu, time=%s, power=%d, device_type=%d, is_net=%d", site_id, site_ct, tt, tool_time::to_str_ex(tt).c_str(), power, site_ptr->m_device_type_id, site_ptr->m_net_device_status);
 
     return site_id;
 }

+ 52 - 0
websocket/jsonBuilder.cpp

@@ -824,7 +824,59 @@ namespace sys
         root.Accept(writer);
         return sb.GetString();
     }
+	
+	/*
+	 * 构建设备状态json
+	 *
+	 * 参数
+	 *	const std::vector<device_state> vtd		设备状态列表
+	 *
+	 * 返回值
+	 *	设备状态json
+	 *
+	 * */
+	std::string jsonBuilder::build_device_state(const std::vector<device_state> vtd)
+    {
+ 		if(vtd.size() == 0){
+            return "";
+        }
 
+        rapidjson::StringBuffer sb;
+		rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
+		rapidjson::Document doc;
+		rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
+
+		rapidjson::Value root(rapidjson::kObjectType );
+
+        __SetCmd(JSON_CMD_VALUE_DEV_STATE, root, allocator);
+
+        rapidjson::Value data(rapidjson::kArrayType);
+        for(auto it : vtd){
+            rapidjson::Value elem(rapidjson::kArrayType);
+            rapidjson::Value tmp_object(rapidjson::kObjectType);
+
+            tmp_object.SetInt(it.m_id);
+            elem.PushBack(tmp_object, allocator);
+            
+            tmp_object.SetInt(it.m_type);
+            elem.PushBack(tmp_object, allocator);
+
+            tmp_object.SetInt(it.m_state);
+            elem.PushBack(tmp_object, allocator);
+
+            tmp_object.SetDouble(it.m_cur_time);
+            elem.PushBack(tmp_object, allocator);
+            
+            data.PushBack(elem, allocator);
+        }
+
+        root.AddMember("data", data, allocator);
+
+        __AddVersion(root, allocator);
+
+        root.Accept(writer);
+        return sb.GetString();
+    }
 }
 
 

+ 1 - 0
websocket/jsonBuilder.h

@@ -245,6 +245,7 @@ namespace sys
         std::string build_ios_card_pos(const _CARD_POS_& card);
         std::string build_tmp_card_pos(const std::map<uint64_t, _CARD_POS_>& cards);
         std::string build_traffic_light(const std::vector<light_state> lights);
+		std::string build_device_state(const std::vector<device_state> vtd);
 		/**
 		* @brief
 		生成车辆进入特殊区域jason函数。

+ 19 - 1
websocket/wsTimerThread.cpp

@@ -136,6 +136,18 @@ namespace sys
         light_state_list.erase(light_state_list.begin(), light_state_list.end());
     }
 
+	void wsTimerThread::send_device_state()
+	{
+		if(device_state_list.empty()){
+			return;
+		}
+
+		std::vector<device_state> ds = device_state_list;
+		std::string json_device = __jsBuilder.build_device_state(ds);
+		swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_device);
+		device_state_list.erase(device_state_list.begin(), device_state_list.end());
+	}
+
     /*
      * 定时器线程发送定位数据
      *
@@ -154,7 +166,8 @@ namespace sys
                 //service_position send
                 //pOwner->send_card_pos();
                 //pOwner->temp_send_card_pos();
-                pOwner->send_light_state();
+                //pOwner->send_light_state();
+				pOwner->send_device_state();
 				__LastSendTime = t;
 			}
           	boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
@@ -202,4 +215,9 @@ namespace sys
     {
         light_state_list.push_back(light);
     }
+
+	void wsTimerThread::upt_device_state(const device_state& ds)
+	{
+		device_state_list.push_back(ds);
+	}
 }

+ 5 - 1
websocket/wsTimerThread.h

@@ -46,7 +46,8 @@ namespace sys
 		thread_safe_map<uint64_t, _CARD_POS_> __CardPosList;//卡位置列表
         std::time_t  __LastSendTime{0};//上一次发送的时间
 		jsonBuilder __jsBuilder;//json构造器类
-        std::vector<light_state> light_state_list;
+        std::vector<light_state> light_state_list;		// 红绿灯状态列表
+		std::vector<device_state> device_state_list;	// 设备状态列表
 	private:
 		/**
 		* @brief
@@ -99,6 +100,8 @@ namespace sys
         void send_card_pos();
         void temp_send_card_pos();
         void send_light_state();
+		void send_device_state();
+
 	protected:
 		/**
 		* @brief
@@ -202,6 +205,7 @@ namespace sys
 		void del_card_pos( const _CARD_POS_& pos );
 
         void upt_light_state(const light_state& light);
+		void upt_device_state(const device_state& ds);
 	};
 }
 

+ 13 - 9
websocket/ws_common.h

@@ -171,21 +171,25 @@ namespace sys
         {}
     };
 
-    // 设备状态信息
-    struct device_state{
-        uint8_t  m_type;        // 设备类型
+    // 设备状态信息,基站类设备
+    struct device_state{ 
         uint32_t m_id;          // 设备ID
+	   	uint8_t  m_type;        // 设备类型
+		uint8_t  m_state;		// 设备状态
         uint64_t m_cur_time;    // 接收时间
 
-        device_state(uint8_t type, uint32_t id, uint64_t ctime)
-            : m_type(type), 
-            m_id(id),
-            m_cur_time(ctime)
+        device_state(uint32_t id, uint8_t type, uint8_t state, uint64_t ctime)
+            : m_id(id)
+			, m_type(type) 
+            , m_state(state)
+            , m_cur_time(ctime)
         {}
 
         device_state()
-            : m_id(0),
-            m_cur_time(0)
+            : m_id(0)
+			, m_type(0)
+			, m_state(0)
+            , m_cur_time(0)
         {}
     };
 }

+ 0 - 1
worker.cpp

@@ -207,7 +207,6 @@ struct worker_thread: loop_thread ,visitor<std::shared_ptr<card_location_base>>
 				t.destroy();
                 break;
             case CHAR_LOCATEDATA_PDOA:      // pdoa实时定位数据
-                //log_info("card loc message: 0x%04X", t.m_cmd_code);
                 card_list::instance()->on_message(this, t.body<message_pdoa_locinfo>(), false);
                 t.destroy();
                 break;