#include "uplink.h"
#include "device.h"
#include "../dev_mgr/gateway/gateway.h"
#include "../network_mgr/net_proc.h"
#include "../tax/tax.h"
#include "../../BSP/BSP.h"
#include "downlink.h"
#include "me3616.h"
#include "../network_mgr/sx1268/lora.h"
extern ME3616  air;
extern volatile uint32_t TickSec;
//uplink_tax_t uplink_tax;
/* 上报的业务数据类(0x03)  0x2001*/
int uplink_tax_comb(uint8_t *outdata,uint8_t *len, uint8_t node,uint8_t port,uint8_t gun,uint32_t seq_no)
{
    uplink_tax_t *p_msg = (uplink_tax_t *)outdata;

    p_msg->gateway_id = downlink_config.gateway_id;//GATEWAY_ID;
    p_msg->collect_id = downlink_config.collect_conf[node].collect_no; //nodeConf.taxconf[node].device_id;
    p_msg->tax_no = downlink_config.collect_conf[node].tax_conf[port].tax_id;//nodeConf.taxconf[node].port[port].tax_id;
    p_msg->gun_no = downlink_config.collect_conf[node].tax_conf[port].gunid[gun];//nodeConf.taxconf[node].port[port].gun[gun].gun_no;
    p_msg->seq_no = seq_no;//此号需要加BCD码 nodeConf.taxconf[node].port[port].gun[gun].rcv_seq;
    p_msg->unit_price = rcv_data_temp.unit_price;
    p_msg->oil_volume = rcv_data_temp.oil_volume;
    p_msg->price = rcv_data_temp.price;
    p_msg->total_oil_volume = rcv_data_temp.total_oil_volume;
    p_msg->total_price = rcv_data_temp.total_price;
    p_msg->time_day = rcv_data_temp.time_day;
    p_msg->time_hour = rcv_data_temp.time_hour;
    p_msg->time_minute = rcv_data_temp.time_minute;

    *len = sizeof(uplink_tax_t);
    return 0;
}
/* 上报的业务的错误数据   数据类(0x03)  0x2002*/
int uplink_tax_comb_2002(uint8_t *outdata,uint8_t *len, uint8_t node,uint8_t port,uint8_t gun,uint8_t errorcode)
{
    uplink_tax_error_t *p_msg = (uplink_tax_error_t *)outdata;

    p_msg->gateway_id = downlink_config.gateway_id;//GATEWAY_ID;
    p_msg->collect_id = downlink_config.collect_conf[node].collect_no; //nodeConf.taxconf[node].device_id;
    p_msg->tax_no = downlink_config.collect_conf[node].tax_conf[port].tax_id;//nodeConf.taxconf[node].port[port].tax_id;
    p_msg->gun_no = downlink_config.collect_conf[node].tax_conf[port].gunid[gun];//nodeConf.taxconf[node].port[port].gun[gun].gun_no;
    p_msg->errorcode = errorcode;
    p_msg->reserve = 0;
    *len = sizeof(uplink_tax_error_t);
    return 0;
}

/* 上报密文的原始数据 0x2071 */
void uplink_data_cmd_0x2071(uint8_t *outdata, uint8_t *len, uint32_t serialNo, uint8_t cIndex, uint8_t portId, uint8_t gunId,rcv_data_0x8C_t * msg)
{
    dataCmd0x2071 *p_msg = (dataCmd0x2071 *)outdata;

    p_msg->gatewaySn = downlink_config.gateway_id;
    p_msg->collSn = downlink_config.collect_conf[cIndex].collect_no;
    p_msg->taxId = portId+1;
    p_msg->gunId = gunId+1;
    p_msg->serialNo = serialNo;
    p_msg->dir = 0;
    p_msg->result = 0;
    p_msg->shakedown1[0] = 0;
    p_msg->shakedown1[1] = 0;
    p_msg->shakedown1[2] = 0;
    p_msg->reserve = 0;
    p_msg->da11len = msg->data11len;
    p_msg->da14len = msg->data14len;

    memcpy(p_msg->serialNum,msg->monitor_serino,10);
    memcpy(p_msg->data,msg->data14,msg->data14len);
    memcpy(&p_msg->data[msg->data14len],msg->data11,msg->data11len);

    *len = sizeof(dataCmd0x2071) - 128 + msg->data11len + msg->data14len;
}
int uplink_tax_comb_error(uint8_t *outdata,uint8_t *len, uint8_t node,uint8_t port,uint8_t gun,uint32_t id,uint8_t flag)
{
    uplink_tax_t *p_msg = (uplink_tax_t *)outdata;

    p_msg->gateway_id = downlink_config.gateway_id;//GATEWAY_ID;
//    if(flag == 0xA0) {
//        p_msg->collect_id = downlink_config.collect_conf[node].collect_no; //nodeConf.taxconf[node].device_id;
//        p_msg->tax_no = downlink_config.collect_conf[node].tax_conf[port].tax_id|flag;//nodeConf.taxconf[node].port[port].tax_id;
//        p_msg->gun_no = downlink_config.collect_conf[node].tax_conf[port].gunid[gun];//nodeConf.taxconf[node].port[port].gun[gun].gun_no;
//    }
//    else {
        p_msg->collect_id = id;
        p_msg->tax_no = port|flag;
        p_msg->gun_no = gun;
//    }
    p_msg->seq_no = 0;//seq_no;//此号需要加BCD码 nodeConf.taxconf[node].port[port].gun[gun].rcv_seq;
    p_msg->unit_price = 0;//rcv_data_temp.unit_price;
    p_msg->oil_volume = 0;//rcv_data_temp.oil_volume;
    p_msg->price = 0;//rcv_data_temp.price;
    p_msg->total_oil_volume = 0;//rcv_data_temp.total_oil_volume;
    p_msg->total_price = 0;//rcv_data_temp.total_price;
    p_msg->time_day = 0;//rcv_data_temp.time_day;
    p_msg->time_hour = 0;//rcv_data_temp.time_hour;
    p_msg->time_minute = 0;//rcv_data_temp.time_minute;

    *len = sizeof(uplink_tax_t);
    return 0;
}
/* 上报编码器的数据（0x03)  0x2051 */
void uplink_encoder_data0x2051(uint8_t *outdata,uint8_t *len,uint8_t node,uint8_t port, uint8_t gun,uint16_t palen, uint8_t *da)
{
    updataencoder0x2051 *p_msg = (updataencoder0x2051*)outdata;

    p_msg->gateway_id = downlink_config.gateway_id;//GATEWAY_ID;
    p_msg->collect_id = downlink_config.collect_conf[node].collect_no;
    p_msg->taxno = port;
    p_msg->gunno = gun;
    p_msg->reserve = 0;
    p_msg->paralen = palen;
    memcpy(p_msg->para,da,palen);

   // *len = sizeof(updataencoder0x2051)-32+20;
    *len = 15+palen;
}

/*上报的系统类(0x01)的 税控序列号 以及枪的个数 0x2011*/
void uplink_tax_systemcmd_0x2011(uint8_t *outdata,uint8_t *len, uint8_t node,uint8_t port,uint8_t gun)
{
    syscmd0x2011 *pmsg = (syscmd0x2011 *)outdata;

    pmsg->gateway_sn = downlink_config.gateway_id; // 网关sn号
    pmsg->coll_sn = downlink_config.collect_conf[node].collect_no;; // 采集器SN
    pmsg->tax_no = downlink_config.collect_conf[node].tax_conf[port].tax_id; // 报税口号
    pmsg->encrypt = downlink_config.collect_conf[node].tax_type; //明文  密文
    pmsg->tax_factory = downlink_config.collect_conf[node].tax_conf[port].factory; // 厂家
    pmsg->gun_num = downlink_config.collect_conf[node].tax_conf[port].gun_num;
    memcpy(pmsg->minor_num,downlink_config.collect_conf[node].tax_conf[port].monitor_serino+5,10);

    *len = sizeof(syscmd0x2011);
	return ;
}
/* 上报的系统类(0x01)的 升级信息状态 0x2001 */
void uplink_update_systemcmd_0x2001(uint8_t *outdata,uint8_t *len,uint16_t type,uint32_t sn,uint16_t taskNo,uint8_t status)
{
    syscmd0x2001 *pmsg = (syscmd0x2001*)outdata;

    pmsg->targetType = type;
    pmsg->deviceSn = sn;
    pmsg->taskNo = taskNo;
    pmsg->status = status;
    *len = sizeof(syscmd0x2001);
	
	return;
}
/* 上报的系统类(0x01)的 lora参数 0x2017 */
void uplink_update_systemcmd_0x2017(uint8_t *outdata,uint8_t *len,uint32_t coll_sn,uint8_t result,uint8_t index)
{
    syscmd0x2017 *pmsg = (syscmd0x2017*)outdata;

    pmsg->gateway_sn = downlink_config.gateway_id;
    pmsg->coll_sn = coll_sn;
    pmsg->result = result;
    pmsg->loraIndex = index;
    pmsg->freq = 0;
    pmsg->power = 0;
    pmsg->bandwidth = 0;
    pmsg->spreadingfactor = 0;
    pmsg->codingrate = 0;
    pmsg->preambleLen = 0;

    *len = sizeof(syscmd0x2017);

    return;
}
/* 上报的系统类(0x01)的 拓盛密文实时读取 0x2051 */
void uplink_update_systemcmd_0x2051(uint8_t *outdata,uint8_t *len,uint32_t coll_sn,uint8_t taxno,uint8_t benable)
{
    syscmd0x2051 *pmsg = (syscmd0x2051*)outdata;

    pmsg->gateway_sn = downlink_config.gateway_id;
    pmsg->coll_sn = coll_sn;
    pmsg->taxNo = taxno;
    pmsg->benable = benable;
    pmsg->reserve = 0;

    *len = sizeof(syscmd0x2051);

    return;
}
/*上报的状态类(0x02)的 固件信息 0x2001*/
void uplink_tax_statusmcmd_0x2001(uint8_t *outdata,uint8_t *len,uint16_t type, uint8_t index)
{
    statusCmd0x2001 *pmsg = (statusCmd0x2001 *)outdata;

    memset(pmsg->Uuid,0,12);//UUID
    memset(pmsg->Imei,0,15); //IMEI
    memset(pmsg->Iccid1,0,20);//ICCID1
    memset(pmsg->Iccid2,0,20);//ICCID2
    memset(pmsg->Iccid3,0,20);//ICCID3
    pmsg->gateway_Sn        = g_firmwareMsg.gatewayMsg.hardwareMsg.gateway_sn; //网关编号
    pmsg->target_Type       = type;//设备类型（0x0101：网关；0x0201；采集器：0x0301:液位仪采集器；0x0401：屏采集器）

    switch (type){
    case DEVICE_GATEWAY: // 网关的
        pmsg->sn                = g_firmwareMsg.gatewayMsg.hardwareMsg.gateway_sn;//设备SN
        pmsg->bootloader_Version= g_firmwareMsg.gatewayMsg.verMsg.gate_bootloaderVr; //bootloader版本
        pmsg->app_Version       = g_firmwareMsg.gatewayMsg.verMsg.gate_appVr;//APP版本
        pmsg->reset_Times       = g_firmwareMsg.gatewayMsg.gate_resetnum;//复位次数
        pmsg->last_Reset_Type   = g_firmwareMsg.gatewayMsg.gate_resetType;//最后一次复位类型
       
        memcpy(pmsg->Uuid  ,g_firmwareMsg.gatewayMsg.Uuid,12);//UUID
        memcpy(pmsg->Imei  ,g_firmwareMsg.gatewayMsg.Imei,15); //IMEI
        memcpy(pmsg->Iccid1,g_firmwareMsg.gatewayMsg.Iccid1,20);//ICCID1
        memcpy(pmsg->Iccid2,g_firmwareMsg.gatewayMsg.Iccid2,20);//ICCID2
        memcpy(pmsg->Iccid3,g_firmwareMsg.gatewayMsg.Iccid3,20);//ICCID3
        break;
    case DEVICE_COLLECT: case 0x0202:// 采集器的
        pmsg->sn                = g_firmwareMsg.collMsg[index].device_sn;//设备SN
        pmsg->bootloader_Version= g_firmwareMsg.collMsg[index].bootloaderVr; //bootloader版本
        pmsg->app_Version       = g_firmwareMsg.collMsg[index].appVr;//APP版本
        pmsg->reset_Times       = g_firmwareMsg.collMsg[index].resetNum;//复位次数
        pmsg->last_Reset_Type   = g_firmwareMsg.collMsg[index].resetType;//最后一次复位类型
        
        memcpy(pmsg->Uuid,g_firmwareMsg.collMsg[index].uuid,12);// UUID
        break;
    case DEVICE_OILL: // 油罐车的

        break;
    case DEVICE_LED: // 显示屏的

        break;
    default:
        break;
    }

    *len = sizeof(statusCmd0x2001);
    return;
}
/*上报的状态类(0x02)的 状态信息 0x2002*/
void uplink_tax_statusmcmd_0x2002(uint8_t *outdata, uint8_t *len, uint16_t type, uint8_t index)
{
    statusCmd0x2002 *pmsg = (statusCmd0x2002 *)outdata;

    pmsg->gateway_sn = downlink_config.gateway_id; // 网关sn号
    pmsg->target_Type = type;
    pmsg->loraPaIndex = g_runData.curloraIndex; // lora信号的通道号
    g_runData.rssi_4G = air.State.dBm +1000;
    g_firmwareMsg.gatewayMsg.gate_runTime = TickCounter/1000 + TickSec*4294967;
    switch (type)
    {
    case DEVICE_GATEWAY: // 网关的
        pmsg->sn = downlink_config.gateway_id; //
        pmsg->rssi_lora = 0;//g_runData.rssi_lora;
        pmsg->rssi_4G = g_runData.rssi_4G;
        pmsg->status = 1;
        pmsg->taxstatus[0] = 0;
        pmsg->taxstatus[1] = 0;
        pmsg->run_Time          = g_firmwareMsg.gatewayMsg.gate_runTime;//运行时常
        pmsg->voltage           = g_firmwareMsg.gatewayMsg.fVol.voltage; // 电压
        pmsg->tempperature      = g_firmwareMsg.gatewayMsg.fTemper.temperature; // 温度
        //pmsg->loraPaIndex = g_runData.curloraIndex;
        pmsg->loraRcvNum = g_loraCollMsg.lora_rcvCount;
        pmsg->loraSendNum = g_loraCollMsg.lora_sendCount;
        pmsg->data = g_firmwareMsg.gatewayMsg.configmsg.bInitMsg.bInit.benable_qei&0xFF;
        pmsg->collerr = g_runData.collerr;
        pmsg->reserve = 0x00;
        break;
    case DEVICE_COLLECT: case 0x0202:// 采集器的
        pmsg->sn =  downlink_config.collect_conf[index].collect_no;//g_firmwareMsg.collMsg[index].device_sn;
        pmsg->rssi_lora = g_runData.collrunMsg[index].lorastatus;
        pmsg->rssi_4G =  0;//g_runData.rssi_4G;
        pmsg->status = downlink_config.collect_conf[index].collect_status;// 采集器的状态 1：在线  0：离线
        pmsg->taxstatus[0] = downlink_config.collect_conf[index].tax_conf[0].status; // 报税口的状态 1： 在线 0：离线
        pmsg->taxstatus[1] = downlink_config.collect_conf[index].tax_conf[1].status; // 报税口的状态 1：在线 0：离线
        pmsg->run_Time          = g_firmwareMsg.collMsg[index].runTime;//运行时常
        pmsg->voltage           = g_firmwareMsg.collMsg[index].voltage; // 电压
        pmsg->tempperature      = g_firmwareMsg.collMsg[index].temperature; // 温度
        pmsg->loraRcvNum = g_loraCollMsg.info[index].rcvNum;
        pmsg->loraSendNum = g_loraCollMsg.info[index].sendNum;
        pmsg->data = downlink_config.collect_conf[index].tax_type;
        pmsg->collerr = g_runData.collerr;
        pmsg->reserve = 0x00;
        break;
    case DEVICE_OILL: // 油罐车的

        break;
    case DEVICE_LED: // 显示屏的

        break;
    default:
        break;
    }
    *len = sizeof(statusCmd0x2002);
    return;
}

void uplink_tax_log_da(uint8_t *outdata,uint8_t *len, uint8_t type,uint8_t node,uint8_t port,uint8_t gun,uint32_t id,uint8_t cmd,uint8_t err)
{
    char temp[64] = {0},i=0;
    uint16_t tempda = 0;
    uint16_t alsn = 0;
    switch(type){
    case LOG_BASIC_MSG:
        sprintf((char*)outdata,"DT:%04x|G:%010u|V:%04x|N:%01x",\
                0x0101,\
                downlink_config.gateway_id,\
                g_firmwareMsg.gatewayMsg.verMsg.gate_appVr&0xFFFF,\
                downlink_config.collect_num);
        for(i=node;i<node+err;i++){
            memset(temp,0,32);
            tempda = downlink_config.collect_conf[i].tax_type;
            alsn = (downlink_config.collect_conf[i].tax_conf[0].alrd_sn<<2) |downlink_config.collect_conf[i].tax_conf[1].alrd_sn;
            alsn = (alsn<<8);
            sprintf(temp,"|%010u,%03x,%04x",\
                    downlink_config.collect_conf[i].collect_no,\
                    alsn|(downlink_config.collect_conf[i].tax_conf[0].gun_num<<4)|downlink_config.collect_conf[i].tax_conf[1].gun_num,\
                    (tempda<<8)|(downlink_config.collect_conf[i].tax_conf[0].factory<<4)|downlink_config.collect_conf[i].tax_conf[1].factory);
            strcat((char*)outdata,temp);
        }
        break;
    case LOG_TAX_DA:
        switch(err){

        case LOG_00: strcpy(temp,(char*)"执行错误"); break;
        case LOG_01: strcpy(temp,(char*)"执行成功"); break;
        case LOG_02: strcpy(temp,(char*)"数据模块忙，稍后再试"); break;
        case LOG_03: strcpy(temp,(char*)"监控处理器忙"); break;
        case LOG_04: strcpy(temp,(char*)"监控处理器通讯超时"); break;
        case LOG_05: strcpy(temp,(char*)"查询记录不存在"); break;
        case LOG_10: strcpy(temp,(char*)"监控处理器序列号不存在"); break;
        case LOG_11: strcpy(temp,(char*)"密文数据解密错误"); break;


        case LOG_CRC: strcpy(temp,(char*)"校验错误"); break;
        case LOG_XSP: strcpy(temp,(char*)"显示屏"); break;
        case LOG_NOTAXID: strcpy(temp,(char*)"报税口不存在"); break;
        case LOG_NOGUNID: strcpy(temp,(char*)"枪号不存在"); break;
        case LOG_COLL_TIMOUT: strcpy(temp,(char*)"采-2超时"); break;
        case LOG_COLL_ENCPYST: strcpy(temp,(char*)"采-3"); break;
        case LOG_COLL_BUSY: strcpy(temp,(char*)"采-6"); break;

        default : strcpy(temp,(char*)"其它"); break;
        }

        sprintf((char*)outdata,"DT:%04x|GW:%010u|ET:%01d|C:%010u|T:%01d|SC:%02x|G:%02x|CM:%02x|ER:%2x%s",\
               0x0101,downlink_config.gateway_id,0x01,id,port,\
               (downlink_config.collect_conf[node].tax_type<<4)|downlink_config.collect_conf[node].tax_conf[port-1].factory,\
               ((downlink_config.collect_conf[node].tax_conf[port-1].gun_num<<4)|gun),cmd,err,temp);

        break;
    case LOG_TAX_COLLSN: // 不是本网关下的采集器的SN
        sprintf((char*)outdata,"SN:%010u,不是本网关下的采集器\r\n",id);
         break;
    case LOG_LORA_ERROR:
        case LOG_CRC1: strcpy(temp,(char*)"lora接收检验错误"); break;
        case LOG_LORA_HEADERROR: strcpy(temp,(char*)"lora接收数据头不是fefe"); break;

    default : break;
    }
    *len = strlen((char*)outdata);
    return;
}



