#include "stdafx.h"
#include <math.h>
#include "zstream.h"
#include "message.h"

NAMESPACE_POINT_BEGIN(NAMESPACE_POINT)
package::package(const void*d,uint64_t recv_time)
	:m_is(d,4)
	,m_recv_time(recv_time)
{
	m_is>>m_len>>m_cmd_id ;
	m_is.set_size(m_len+2);
	m_crc=m_is.load_uint16(m_len);
}

loc_info::loc_info(uint32_t cmd_id, uint64_t loc_time,const void*d)
	:m_loc_time(loc_time)
	,m_cmd_id(cmd_id)
	,m_rav(0)
	,m_acc(0)
	,m_card_sp(0)
{
	
	uint8_t rav,acc;
	uint16_t ct;
	zistream is(d,20);
	is>>m_card_type>>m_card_id>>ct
	  >>m_bat_level>>rav>>acc>>skip(1)>>m_tof>>m_ant_id ;
	
	m_card_ct=ct;
	m_bat_level&=0xF;
	--m_ant_id;

	m_rav=((rav&0x80)?-1.:1.)*(rav&0x7f)*3;
	m_acc=((acc&0x80)?-1.:1.)*(acc&0x7f)*98;

	m_card_sp=0.;
	uint16_t sp1=0,sp2=0;
	if(m_cmd_id==0x843b)
	{
		is>>sp1>>sp2;
		m_card_sp=10*log10(1.*sp1*(1<<17)/pow(sp2-64.,2))-121.74;
	}

}

/*
loc_info::loc_info*init(uint64_t loc_time,const void*d,loc_info*b)
{
}
*/

loc_package::loc_package(uint64_t recv_time,const void*data)
	:package(data,recv_time)
	,m_card_count(0)
{
	if(m_cmd_id!=0x753b && m_cmd_id!=0x843b)
		return;

	char time[8];
	m_is>>m_sit_id>>m_sit_ct>>npc(7)>>time
		>>m_sit_temp>>m_sit_status;

	m_card_data=m_is.cur_data();
	m_card_count=(m_len+2-(m_card_data-m_is.buff())) /bytes_per_card();
}

loc_info loc_package::get(int index)const
{
	return loc_info(m_cmd_id,m_recv_time-1000+50+index*45,m_card_data+index*bytes_per_card());
}
NAMESPACE_POINT_END(NAMESPACE_POINT)

