#ifndef __TAX_H__
#define __TAX_H__

#include "stm32f10x.h"
#include "../globalDef.h"
extern volatile uint32_t TickCounter;

extern volatile uint32_t TickSecond;

#define tax_send_cmd_83    0x83
#define tax_send_cmd_86    0x86
#define tax_send_cmd_89    0x89

#define NODE_DEVICE_NUM_MAX         16 //网关连接最大采集器数

#define TAX_UART_GET_REV_OK 				1	//串口完成数据接收
#define TAX_UART_GET_REV_IDLE 			0 //串口未完成数据接收

#define TAX_DEVICE_PORT_NUM_MAX			2	//设备支持的，最大端口数
#define TAX_DEVICE_GUN_NUM_MAX			8	//设备支持的，最大枪数

#define TAX_CMD_ZLJ_BYTE_SIZE				24
#define TAX_CMD_DC_BYTE_SIZE				28

#define TIME_OUT_PER_SECOND					(1*100*10)		//秒

#define TAX_TIME_TAX_CMD_GAP				(2*100*10)		//每条指令之间的间隔
#define TAX_TIME_TAX_CMD_OUT				(2*100*10)		//每条指令的超时时间
#define TAX_TIME_TSMW_CMD_GAP        (50*60*1000)  //拓盛密文查询间隔
//#define TAX_TIME_TAX_GUN_OUT				(10*1000)		//
//#define TAX_TIME_TAX_PORT_OUT				(10*1000)
#define TAX_TIME_TAX_PORT_GAP				(5*100*10)		//每条指令之间的间隔
#define TAX_TIME_TAX_JUMP_GAP				(2*100*10)		//每条指令之间的间隔

#define TAX_TIME_TAX_PORT_ONOFF_OUT	(3*60*100)
#define TAX_TIME_TAX_GUN_ONOFF_OUT	(3*60*100)

#define TAX_TIME_PORT_SLEEP_OUT			(1*60*100)

#define TAX_SEND_CMD_INTERVAL				(100)				//单位 *10ms
#define TAX_SEND_CMD_RTY_TIME				(1)					//尝试次数

//定时结构体
//定时原理，源于滴答定时器，TickCounter
typedef struct 
{
  uint32_t counter;   									  //时间计数器
  uint32_t timeOut;        							  //超时时间
}tCounterTimeOut_t;

typedef enum{
	TAX_GUN_LP_STATE_IDLE=0, //空闲状态
 	TAX_GUN_LP_STATE_83, //读取83指令，读取的是采集器有关的信息，上电读取一次就可以
	TAX_GUN_LP_STATE_89,//总金额和总油量
	TAX_GUN_LP_STATE_86,// 单价 当次金额和当次油量
	TAX_GUN_LP_STATE_CH, //切换采集器 枪 或是报税口
        TAX_GUN_LP_STATE_56, // 读取心跳
	TAX_GUN_LP_STATE_FINISH,// 完成
	TAX_GUN_LP_STATE_UNKNOWN
}tTaxGunLpState_t;


typedef enum{
	TAX_PORT_POS_STATE_IDLE=0,
	TAX_PORT_POS_STATE_SLEEP,
	TAX_PORT_POS_STATE_WAKEUP,
	TAX_PORT_POS_STATE_RUN
}tTaxPortPosState_t;

#pragma pack(push,1)
typedef union _encode__{
    uint16_t data;
    struct {
        uint16_t bcollid :4;  // 采集器索引
        uint16_t bportid :2; // 报税口索引
        uint16_t reserve :2; // 预留
        uint16_t gunstatus : 8; // 有关枪的状态，按位 位为1，则有枪，为0没有枪
    }__attribute__((packed))da;
}__attribute__((packed))enMsgDa;
typedef union _encode_num_{
    uint8_t data;
    struct{
        uint8_t tabNum : 4;// 不合表时，对应的个数
        uint8_t pEnNum : 4;// 合表后，报税口对应的个数
    }__attribute__((packed))num;
}__attribute__((packed))enNumMsg;

#pragma pack(pop)
typedef struct{
    //枪离线/在线，状态
    tTaxGunLpState_t gLpState;//小循环，轮询状态
    uint32_t seq_no;             //发送流水号
    uint8_t tsmw_flag;
}__attribute__((packed)) tTaxGun_t;

typedef struct{
    tTaxGun_t   gun[TAX_DEVICE_GUN_NUM_MAX];							//枪						//最大枪
    int  gunIndex;//当前枪索引
    uint8_t alrd_sn;//uart;//报税口对应的com口
    uint8_t tax_id;//报税口对应的编号

    uint8_t portRead;           // 此报税口是否可以读取
    uint8_t bhavenewRecord;        // 有新的记录
    enNumMsg enNum;           // 此报税口有编码器的个数
    uint8_t enIndex;          // 编码器索引，用于读取编码器
    uint8_t bEnFinish;         // 当前报税口的编码器是否完成 1:完成
    uint8_t rcvNum;             // 接收的次数
    uint8_t enPos[12];       // 存储的是在g_encodeopt.tabledDa的下标索引
    enMsgDa enMsg[8];
    uint8_t taxType;          //报税口的类型
}__attribute__((packed)) tTaxPort_t;

typedef struct{
	tTaxPort_t port[TAX_DEVICE_PORT_NUM_MAX];
	int portIndex;												//当前枪索引
}__attribute__((packed)) tTaxConf_t;

typedef struct
{
	tTaxConf_t taxconf[NODE_DEVICE_NUM_MAX];
	int nodeIndex;                              //采集器索引
}__attribute__((packed)) tNodeConf_t;

//extern tTaxConf_t taxConf;
extern tNodeConf_t nodeConf;



typedef struct _rcv_data_temp
{
	uint64_t          total_price;			  //总累计金额
	uint64_t          total_oil_volume;		//总体积
	uint32_t          unit_price;		      //最后一次加油的单价
	uint32_t          oil_volume;		      //最后一次加油的油量
	uint32_t          price;			        //最后一次加油的油价
	uint32_t          prev_seq;           //上次流水号
        uint8_t time_day;
        uint8_t time_hour;
        uint8_t time_minute;
        uint8_t           prev_collect;       //上次采集器号
	uint8_t           prev_tax;           //上次报税口号
	uint8_t           prev_gun;           //上次枪号
	uint8_t           rcv_no;         //接收的第几组数据
}rcv_data_temp_t;

extern rcv_data_temp_t rcv_data_temp;

#define TAX_DATA_MAX_SIZE	128


extern void tax_process(void);
extern void encoder_read_data(uint8_t node, uint8_t port, uint8_t gun);
extern void tax_process_encoder(void);


extern int tax_send_cmd(uint8_t cmd, uint8_t node, uint8_t port, uint8_t gun);

extern void tax_send_cmd_0x56_first(void);
extern uint8_t read_collect_index(uint32_t id);
extern uint8_t read_tax_index(uint8_t node, uint8_t uart);
extern uint8_t read_gun_index(uint8_t node, uint8_t port, uint8_t gun);
extern int gateway_collect_init(void);
//extern int tsms_gun_read_finish(void);
extern void tax_0x83_process(void);
extern void tax_send_cmd_0x58(void);
extern void tax_send_cmd_0x5B(void);
extern void tax_manual_0x83_process(void);
extern void init_gun_status(void);
extern void set_gun_status(uint8_t nodecur, uint8_t port, uint8_t gun);
extern uint8_t get_gun_status(void);
extern void encoder_enable_read_data(uint8_t node, uint8_t port, uint8_t gun);

extern void net_to_read_cmd(void);

extern void tax_send_cmd_0x01_0x1053(void);

extern void encoder_printf(void);
#endif



