#include <memory.h>
#include "loc_point.h"
#include "ant.h"
#include "log.h"
#include "loc_message.h"

loc_point::loc_point()
    :m_time(0)
    ,m_ct(-1)
	,m_cid(-1)
    ,m_type(-1)
    ,m_sid(-1)
    ,m_acc(0)
    ,m_rav(0)
    ,m_cred_level(0)
    ,m_dist(0)
    ,m_speed(0)
	,m_angle(0) 
    ,m_stat(0)
    ,m_useless(false)
    ,m_dist1(0)
    ,m_dist2(0)
    ,m_smooth_x(0)
    ,m_smooth_y(0)
{
    memset(&m_tof,0,sizeof(m_tof));
    memset(&m_rsp,0,sizeof(m_rsp));
}

loc_point& loc_point::reset()
{
    memset(this,0,sizeof(*this));
    return *this;
}

inline const char* now(char*date_str,uint64_t time)
{
    time_t ntime=time/1000;
    struct tm buff;
    const struct tm*t=localtime_r(&ntime, &buff);

    sprintf(date_str,"%d-%02d-%02d %02d:%02d:%02d.%03d" ,
            t->tm_year+1900,t->tm_mon+1,t->tm_mday,
            t->tm_hour,t->tm_min,t->tm_sec,(int)(time%1000));

    return date_str;
}

void loc_point::debug_out(const char *str)const
{
    char time_buff[128];
#if 1
 	logn_info(3,"t=%s,sit=%d,card=%d,ct=%d,cred=%d,type=%d,tof1=%d,tof2=%d,pt=(%.2lf,%.2lf),"
 		        "rsp=%d,acc=%.2f,dist=%.2lf,dist1=%.2lf,dist2=%.2lf,rav=%.2f,speed=%lf",
             now(time_buff,m_time), m_sid, m_cid, m_ct, m_cred_level,m_type, m_tof[0], m_tof[1],  x, y ,
 			(m_rsp[0]+m_rsp[1])>>1,m_acc, m_dist1,m_dist,m_dist2, m_rav,m_speed
           );
#else
    printf("[%s]t=%s,sit=%d,ct=%d,cred=%d,"
            "tof1=%d,tof2=%d,pt=(%.2lf,"
            "%.2lf),rsp=,acc=%.2f,dist=%.2lf,dist1=%.2lf,dist2=%.2lf,rav=%.2f,speed=%.2f\n",
            str,now(time_buff,m_time), m_sid, m_ct, m_cred_level,
            m_tof[0], m_tof[1],  m_smooth_x,
            m_smooth_y ,m_acc,
            m_dist1,m_dist,m_dist2, m_rav, m_speed
          );
#endif
}
int loc_point::inc_cl(int cred_level)
{
    return m_cred_level += cred_level;
}

int loc_point::set_cl(int cred_level)
{
    return m_cred_level = cred_level;
}

int loc_point::cl()const
{
    return m_cred_level;
}

void loc_point::set_source(const loc_message&li,const loc_message&li2)
{
    m_sid = li.m_sit->m_id;
	m_cid = li.m_card_id;
    m_type=li.m_card_type;
    m_time=std::min(li.m_loc_time,li2.m_loc_time);
    m_ct=li.m_card_ct;
    m_acc=li.m_acc *10;// 1270.;
    m_rav=li.m_rav;
    m_tof[li.m_ant_id]=li.m_num_ticks;
    m_tof[li2.m_ant_id]=li2.m_num_ticks;
    m_rsp[li.m_ant_id]=li.m_rssi;
    m_rsp[li2.m_ant_id]=li2.m_rssi;
    m_step=0;
}

void loc_point::set_source(const loc_message&li)
{
    m_sid   = li.m_sit->m_id;
	m_cid   = li.m_card_id;
    m_type  = li.m_card_type;
    m_time  = li.m_loc_time;
    m_ct    = li.m_card_ct;
    m_acc   = li.m_acc * 10;// 1270.;
    m_rav   = li.m_rav;
    m_tof[li.m_ant_id]      = li.m_num_ticks;
    m_tof[li.m_ant_id?0:1]  = 0;
    m_rsp[li.m_ant_id]      = li.m_rssi;
    m_rsp[li.m_ant_id?0:1]  = 0;
    m_step  = 0;
}