Browse Source

loc_tool中添加基础消息接收接口

zzj 6 years ago
parent
commit
f8b7372550
7 changed files with 92 additions and 47 deletions
  1. 4 4
      Makefile.am
  2. 29 3
      ant.cpp
  3. 10 2
      ant.h
  4. 41 28
      card.cpp
  5. 4 6
      card.h
  6. 1 1
      main.cpp
  7. 3 3
      worker.cpp

+ 4 - 4
Makefile.am

@@ -1,3 +1,5 @@
+bin_PROGRAMS=yals 
+noinst_PROGRAMS=client async test
 
 AM_SOURCES= ant.cpp ant.h base64.cpp base64.h card.cpp card.h clock.h \
 io_buf.h line.h log.h message.cpp message.h point.cpp \
@@ -7,11 +9,9 @@ zloop.h znet.cpp znet.h zstream.h net-service.h net-service.cpp
 AM_CPPFLAGS=-Wall -pthread -m64 -std=c++11 -fPIC
 AM_LDFLAGS= -Wall -pthread -m64 -std=c++11 -fPIC
 
-bin_PROGRAMS=yals client async test
-
 yals_SOURCES=${AM_SOURCES} main.cpp 
-yals_CPPFLAGS=${AM_CPPFLAGS}
-yals_LDFLAGS=${AM_LDFLAGS}  -L. -lzlog -lrt
+yals_CPPFLAGS=${AM_CPPFLAGS} -I${prefix}/include
+yals_LDFLAGS=${AM_LDFLAGS}  -L${prefix}/lib -lzlog -lrt
 
 async_SOURCES=async.cpp
 async_CPPFLAGS=${AM_CPPFLAGS}

+ 29 - 3
ant.cpp

@@ -2,7 +2,9 @@
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
-#include "ant.h"
+#include <message.h>
+#include <ant.h>
+#include <log.h>
 
 int ant::index()const
 {
@@ -78,6 +80,11 @@ int loc_tool_tof_2_base::index()
 	return 2;
 }
 
+void loc_tool_tof_1_base::on_loc_message(ant*,const message_locinfo&m)
+{
+	log_info("tof1-message:site=%d,ant=%d,card=%d,ct=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d",
+			m.m_site_id,m.m_ant_id,m.m_card_id,m.m_card_ct,m.m_tof,m.m_rav,m.m_acc,m.m_rssi);
+}
 
 std::vector<point> loc_tool_tof_1_base::calc_location(const std::vector<loc_message>&locm)
 {
@@ -104,6 +111,14 @@ loc_tool_main::~loc_tool_main()
 		delete tool;
 }
 
+loc_tool* loc_tool_main::get_tool(int index)
+{
+	if((uint32_t)index >= sizeof(g_tool)/sizeof(loc_tool*))
+		return nullptr;
+	
+	return g_tool[index];
+}
+
 void loc_tool_main::set_tool(loc_tool*tool)
 {
 	int index=tool->index();
@@ -115,6 +130,19 @@ void loc_tool_main::set_tool(loc_tool*tool)
 	g_tool[index]=tool;
 }
 
+void loc_tool_main::on_loc_message(ant*a, const message_locinfo&m)
+{
+	loc_tool*lt=get_tool(a->index());
+	if(lt==nullptr)
+	{
+		log_warn("无法找到对应的loctool-message:site=%d,ant=%d,card=%d,ct=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d",
+				m.m_site_id,m.m_ant_id,m.m_card_id,m.m_card_ct,m.m_tof,m.m_rav,m.m_acc,m.m_rssi);
+		return;
+	}
+
+	lt->on_loc_message(a, m);
+}
+
 std::vector<point> loc_tool_main::calc_location(const std::vector<loc_message>&locm)
 {
 	if(locm.empty()) return {};
@@ -158,8 +186,6 @@ std::vector<point> loc_tool_main::calc_location(const std::vector<loc_message>&l
 	return std::move(rc);
 }
 
-loc_tool* loc_tool_main::g_tool[6]={0,0,0,0,0,0};
-
 algo_config ant::g_config[]=
 {
 	{ "tof-1",  1, 2, 100, 1000 },

+ 10 - 2
ant.h

@@ -89,8 +89,11 @@ struct loc_message
 	int tool_index()const;
 };
 
+struct message_locinfo;
 struct loc_tool
 {
+	//将基础信息送入工具类,目前考虑各种算法输出不同的实时日志
+	virtual void on_loc_message(ant*a, const message_locinfo&m){};
 	virtual std::vector<point> calc_location(const std::vector<loc_message>&locm)=0;
 	virtual int index()
 	{
@@ -132,15 +135,20 @@ struct loc_tool_tof_2_base:loc_tool
 struct loc_tool_tof_1_base:loc_tool
 {
 	virtual std::vector<point> calc_location(const std::vector<loc_message>&locm);
+	virtual void on_loc_message(ant*a, const message_locinfo&m);
 	virtual int index();
 };
 
 struct loc_tool_main:loc_tool
 {
-	static loc_tool* g_tool[6];
+	loc_tool* g_tool[6];
+
 	loc_tool_main();
 	~loc_tool_main();
-	static void set_tool(loc_tool*tool);
+	void set_tool(loc_tool*tool);
+	loc_tool* get_tool(int index);
+
+	void on_loc_message(ant*a, const message_locinfo&m);
 	std::vector<point> calc_location(const std::vector<loc_message>&locm);
 };
 

+ 41 - 28
card.cpp

@@ -4,6 +4,7 @@
 #include <card.h>
 #include <zloop.h>
 #include <ev++.h>
+#include <message.h>
 
 struct card_location_base
 {
@@ -21,15 +22,10 @@ struct one_ct_message_handle
 	const algo_config*m_ac=nullptr;
 	int  m_ct;
 	bool m_min_timeout=false;
-	one_ct_message_handle(ev::dynamic_loop&loop,card_location_base*card)
+	one_ct_message_handle(card_location_base*card)
 	{
 		m_card=card;
 		m_ct=-1;
-
-		m_min_timer.set(loop);
-		m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
-		m_max_timer.set(loop);
-		m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
 	}
 
 	void reset()
@@ -56,12 +52,32 @@ struct one_ct_message_handle
 		calc_location();
 	}
 
-	void on_message(const message_locinfo&loc)
+	bool once_flag=false;
+	void once_init(ev::dynamic_loop&loop)
 	{
+		if(once_flag) 
+			return;
+
+		once_flag=true;
+		m_min_timer.set(loop);
+		m_min_timer.set<one_ct_message_handle,&one_ct_message_handle::on_min_timer>(this);
+		m_max_timer.set(loop);
+		m_max_timer.set<one_ct_message_handle,&one_ct_message_handle::on_max_timer>(this);
+	}
+
+	void on_message(ev::dynamic_loop&loop, const message_locinfo&loc)
+	{
+		once_init(loop);
+
 		if(!m_msg_list.empty()&& m_ct!=loc.m_card_ct)
+		{
 			m_msg_list.clear();
+		}
+
 
 		ant*a=ant_list::instance()->get(loc.m_site_id, loc.m_ant_id);
+
+		m_loc_tool.on_loc_message(a, loc);
 		if(m_msg_list.empty())
 		{
 			m_ct=loc.m_card_ct;
@@ -101,17 +117,17 @@ struct card_message_handle
 	std::vector<loc_message> m_msg_list;
 	std::array<one_ct_message_handle*,16> m_ct_list;
 
-	card_message_handle(zloop_base&loop,card_location_base*card)
+	card_message_handle(card_location_base*card)
 	{
 		m_card=card;
 
 		for(size_t i=0;i<m_ct_list.size();i++)
 		{
-			m_ct_list[i]=new one_ct_message_handle(loop,card);
+			m_ct_list[i]=new one_ct_message_handle(card);
 		}
 	}
 
-	void on_message(const message_locinfo&loc,bool is_history)
+	void on_message(ev::dynamic_loop&loop, const message_locinfo&loc,bool is_history)
 	{
 		if(is_history)
 		{
@@ -119,44 +135,44 @@ struct card_message_handle
 			return;
 		}
 
-		m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loc);
+		m_ct_list[loc.m_card_ct&(m_ct_list.size()-1)]->on_message(loop, loc);
 	}
 };
 
 struct card:card_location_base
 {
 	card_message_handle m_message_handle;
-	card(zloop_base&loop)
-		:m_message_handle(loop,this)
+
+	card()
+		:m_message_handle(this)
 	{
 	}
 
-	void on_message(const message_locinfo&loc,bool is_history)
+	~card()
 	{
-		m_message_handle.on_message(loc,is_history);
 	}
 
-	void on_location(const std::vector<point>&points)
+	void on_message(ev::dynamic_loop&loop, const message_locinfo&loc,bool is_history)
 	{
+		m_message_handle.on_message(loop, loc, is_history);
 	}
 
-	~card(){}
+	void on_location(const std::vector<point>&points)
+	{
+	}
 };
 
 loc_tool_main one_ct_message_handle::m_loc_tool;
 struct card_list_impl:card_list
 {
-	zloop<task*>&m_loop;
 	std::vector<card*> m_list;
-	card_list_impl(zloop<task*>&loop)
-		:m_loop(loop)
+	card_list_impl()
 	{
 		m_list.reserve(1<<16);
 	}
 
 	void init_card_from_db()
 	{
-		card _(m_loop);
 	}
 
 	card*get(uint64_t card_id)const
@@ -169,7 +185,7 @@ struct card_list_impl:card_list
 		return m_list[cid];
 	}
 
-	void on_message(const message_locinfo&loc,bool is_history)
+	void on_message(ev::dynamic_loop&loop, const message_locinfo&loc,bool is_history)
 	{
 		card*c=get(loc.m_card_id);
 
@@ -179,16 +195,13 @@ struct card_list_impl:card_list
 			return;
 		}
 
-		log_info("card message:site=%d,ant=%d,card=%d,tof=%lld,rav=%02X,acc=%02X,rssi=%d",
-				loc.m_site_id,loc.m_ant_id,loc.m_card_id,loc.m_tof,loc.m_rav,loc.m_acc,loc.m_rssi);
-
-		c->on_message(loc,is_history);
+		c->on_message(loop, loc, is_history);
 	}
 };
 
-card_list *card_list::instance(zloop<task*>&loop)
+card_list *card_list::instance()
 {
-	static card_list_impl _impl(loop);
+	static card_list_impl _impl;
 	return &_impl;
 }
 

+ 4 - 6
card.h

@@ -1,17 +1,15 @@
 #ifndef _CARD_HPP_
 #define _CARD_HPP_
-#include <message.h>
-#include <ev++.h>
 
-struct task;
-template<typename T> struct zloop;
+namespace ev { struct dynamic_loop; }
+struct message_locinfo;
 
 struct card_list
 {
-	virtual void on_message(const message_locinfo&loc,bool is_history)=0;
+	virtual void on_message(ev::dynamic_loop&loop, const message_locinfo&loc, bool is_history)=0;
 	virtual ~card_list(){}
 
-	static card_list *instance(zloop<task*>&loop);
+	static card_list *instance();
 };
 
 #endif

+ 1 - 1
main.cpp

@@ -4,7 +4,7 @@
 
 int main()
 {
-	log_init("log.ini");
+	log_init("../etc/log.ini");
 
 	net_service mh;
 	service_handle::instance(&mh)->run(4000);

+ 3 - 3
worker.cpp

@@ -22,7 +22,7 @@ struct worker_thread: zloop<task*>
 	worker_thread ()
 	{
 		m_thread.reset(new std::thread(std::bind(&worker_thread::run,this)));
-		m_card_list=card_list::instance(*this);//*********
+		m_card_list=card_list::instance();//*********
 	}
 
 	void run()
@@ -47,13 +47,13 @@ struct worker_thread: zloop<task*>
 			case 0x843b://tof
 			case 0x863b://tdoa
 				log_info("card loc message%04X",t.m_cmd_code);
-				m_card_list->on_message(t.body<message_locinfo>(),false);
+				m_card_list->on_message(*this,t.body<message_locinfo>(),false);
 				//card_message::on_loc_message(this,t.m_param1);
 			break;
 			case 0x853b://tof his
 			case 0x873b://tdoa his
 				log_info("site history message%04X",t.m_cmd_code);
-				m_card_list->on_message(t.body<message_locinfo>(),true);
+				m_card_list->on_message(*this,t.body<message_locinfo>(),true);
 				//site_message::on_sync(this,t.m_param1);
 			break;