#include "uplink.h"
#include "device.h"
#include "../dev_mgr/gateway/gateway.h"
#include "../network_mgr/net_proc.h"
#include "../../BSP/BSP.h"
#include "downlink.h"
#include "me3616.h"

extern ME3616  air;
extern volatile uint32_t TickCounter;
//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;
//  
    *len = sizeof(uplink_tax_t);
	
	return 0;
}
/* 上报液位仪的数据 数据类（0x03) 0x2021 */
int uplink_oiltank_datacmd_0x2021(uint8_t *outdata, uint16_t *len)
{

	return 0;
}

/*上报的系统类(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;
}

/*上报的状态类(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: // 采集器的
       
        break;
    case DEVICE_OILL: // 油罐车的
        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_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;
    g_runData.rssi_4G = air.State.dBm+1000;
    g_firmwareMsg.gatewayMsg.gate_runTime = TickCounter/1000;
    pmsg->reserver = 0;
    pmsg->loraRcvNum = 0;
    pmsg->loraSendNum = 0;
    pmsg->loraPaIndex = 0;
    switch (type)
    {
    case DEVICE_GATEWAY: // 网关的
        pmsg->sn = downlink_config.gateway_id; //
        pmsg->rssi_lora = 0;
        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; // 温度
        break;
    case DEVICE_COLLECT: // 采集器的
       
        break;
    case DEVICE_OILL: // 油罐车的
        pmsg->sn = downlink_config.gateway_id; //
        pmsg->rssi_lora = 0;
        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; // 温度
        break;
    case DEVICE_LED: // 显示屏的

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



