
#include <thread>
#include <mutex>
#include <atomic>
#include <iostream>
#include <memory>
#include <string.h>
#include <math.h>

#include <log.h>
#include <znet.h>
#include <zstream.h>
#include <sys/time.h>
#include "worker.h"
#include "tdoa_sync.h"
#include "net-service.h"
#include "ant.h"
#include "card.h"
#include "mine_business.h"
net_service::net_service()
{
	m_loc_worker=worker::instance();
	m_sync_worker=tdoa_sync::instance();
}

net_service::~net_service()
{
	if(m_loc_worker)
		m_loc_worker->stop();
}

void net_service::on_timer()
{
	static int version = -1;
	visit_site_status vss;
	sit_list::instance()->accept(vss);
	int v = card_list::instance()->version();
	if(v != version)
	{
		version=v;
		mine_business::inst()->clear_vehicle();
		card_list_visit clv;
		card_list::instance()->accept(clv);
	}
	mine_business::inst()->run_business();
}

void net_service::on_message(std::shared_ptr<client> clt,const char*data,size_t len)
{
	logn_bin(1,clt->name().c_str(),data,len);//输出二进制日志

	zistream is(data,len-2);
	uint16_t cmd;
	is>>skip(2)>>cmd;
	switch(cmd)
	{
		case CHAR_LOCATEDATA_TOF_EXTEND://tof-扩展
			{
				uint32_t site_id;
				uint8_t  power;
				is>>site_id>>skip(11)>>power;
                auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
                if(!site_ptr)
                {
                    log_error("在全局分站列表中找不到分站：分站id=%d", site_id);
                    break;
                }
                site_ptr->set_client(clt);
                site_ptr->on_power_status((power&1)==0);

				struct timeval tv;
				gettimeofday(&tv,NULL);
				uint64_t t = tv.tv_sec*1000 + tv.tv_usec/1000;
				int index=0;
				while(!is.eof())
				{
					uint64_t tstamp = t -1000 + 50 + index* 45;
					index++;
					task*t=task::alloc<message_locinfo>();
					message_locinfo&m=t->body<message_locinfo>();
					m.load(is,false);
					m.m_time_stamp=tstamp;
					m.m_site_id=site_id;
					//t_site->m_site_data = 0;

					t->m_cmd_code=cmd;
					t->m_hash_id=m.m_card_id;
					m_loc_worker->request(t);
					if(m.m_card_type==5)
						mine_business::inst()->make_reverse_condition(m.m_card_type,m.m_card_id,m.m_ant_id,m.m_card_ct,m.m_tof,m.m_site_id);
				}
			}
			break;
		case CHAR_LOCATEDATA_TDOA_EXTEND://tdoa
			{
				uint32_t site_id;
				is>>site_id>>skip(12);
				while(!is.eof())
				{
					task*t=task::alloc<message_locinfo>();
					message_locinfo&m=t->body<message_locinfo>();
					m.load(is,true);
					m.m_site_id=site_id;
					t->m_cmd_code=cmd;
					t->m_hash_id=m.m_card_id;
					m_sync_worker->translate(m);
					m_loc_worker->request(t);
				}
			}
			break;
		case CHAR_TDOA_READER_SYNC_TIME://time sync
			{
				message_tdoasync m;
				m.load(is);
				m_sync_worker->on_message(m);
			}
			//site_message::on_sync(this,t.m_param1);
			break;
		case CHAR_LOCATEDATAHIS_TOF_EXTEND://tof his
		case CHAR_LOCATEDATAHIS_TDOA_EXTEND://tdoa his
			break;
		case CHAR_CTRL_READER_CMD://ctrl site message
			break;
		case CHAR_ADHOC://自组网数据
			break;
	}
}


