#ifndef MQTTDATA_H_
#define MQTTDATA_H_

#define MQTT_SERVER     		"emqx.novix.fun"
#define MQTT_PORT_NON_SSL  	21883    // 非SSL端口
#define MQTT_PORT_SSL      	28883    // SSL端口

//xiaoyi/{ctlKey}/ctlUpLoad // 控制器上传主题
//xiaoyi/{ctlKey}/ctlDownLoad // 控制器下载主题

typedef enum{
	Idle_State = 0,
	
	Module_Heart_Beat = 1,
	Module_RST,
	Module_Get_Version,
	Module_Restore_Default,
	
	Wifi_Set_Mode,
	Wifi_Set_Connect,
	Wifi_Set_Disconnect,
	Wifi_Set_AutoConnectFlag,
	Wifi_Get_Mode,
	Wifi_Get_StaInfo,
	Wifi_Get_AutoConnectFlag,
	
	BLE_Set_Mode,
	BLE_Set_Name,
	BLE_Enter_Transparent,
	
	MQTT_Get_Connect_Cfg,
	MQTT_Set_Connect_Cfg1,
	MQTT_Set_Connect_Cfg2,
	MQTT_Set_Connect_Cfg3,
	MQTT_Set_Connect_Cfg4,
	MQTT_Set_Connect_Cfg5,
	MQTT_Set_Connect_Cfg6,
	MQTT_Set_Connect,
	MQTT_Set_Disconnect,
	MQTT_Subscribe,
	MQTT_Unsubscribe,
	MQTT_Publish,
	MQTT_Check_Message,
}TWifiCommType;

typedef enum{
	MODULE_INIT_STATE = 0,
	WIFI_DISCONNECT_STATE = 1,
	WIFI_CONFIG_STATE,
	WIFI_CONNECT_STATE,
	MQTT_CONFIG_STATE,
	MQTT_CONNECT_STATE,
}TWifiCommState;

typedef enum{
	Disconnect = 0,
	Connected = 1,
	Connecting = 2,
}TConnectState;

typedef union{
	unsigned char d8[512];
	unsigned short d16[256];
	unsigned int d32[128];
}TCommDta;

typedef struct{
	unsigned int seq;
	TWifiCommType commType;
	unsigned short dataLen; 
	TCommDta data;
}TTxBodyFormat;

typedef struct{
	unsigned int seq;
	unsigned int rpyTimeout;
	unsigned int eventStartTick;
	unsigned int eventEndTick;
	unsigned short rpyFlag;
	unsigned short rxFlag;
	unsigned short rpyLen;
	unsigned short rxLen;
	TCommDta rpyData;
	TCommDta rxData;
}TRxBodyFormat;

typedef struct{
	TWifiCommState commState;
	TWifiCommType commType;
	
	TConnectState wifiConnectState;
	TConnectState mqttConnectState;
	
	unsigned int moduleOnline;
	unsigned int moduleOfflineTick;
}TWifiCommPrm;

class TMqttData{
	public:
		unsigned int RxFlag;
	
		TTxBodyFormat TxPrm;
		TRxBodyFormat RxPrm;
	
		unsigned char * pRxPkg;
		char * pEventRxPkg;
		char * pEndRxPkg;
	
		TWifiCommPrm commPrm;
	
	public:
		TMqttData(){};
			
		void Init();	
		void ClearTxString();
			
		void iBufProcess();
		void oBufProcess();
			
		void RxEventHandle();
		void RpyDataHandle();
			
		//module
		void AssembleModuleHeartBeatPkg();
		void AssembleModuleRstPkg();
		void AssembleModuleGetVersionPkg();
		void AssembleModuleRestoreDefaultPkg();
			
		//wifi
		void AssembleWifiSetModePkg();
		void AssembleWifiSetConnectPkg();
		void AssembleWifiSetDisconnectPkg();
		void AssembleWifiSetAutoConnectPkg();
		void AssembleWifiGetModePkg();
		void AssembleWifiGetStaInfoPkg();
		void AssembleWifiGetAutoConnectPkg();
			
		//MQTT
		void AssembleMqttGetConnectCfgPkg();
		void AssembleMqttSetConnectCfg1Pkg();
		void AssembleMqttSetConnectCfg2Pkg();
		void AssembleMqttSetConnectCfg3Pkg();
		void AssembleMqttSetConnectCfg4Pkg();
		void AssembleMqttSetConnectCfg5Pkg();
		void AssembleMqttSetConnectCfg6Pkg();
		void AssembleMqttSetConnectPkg();
		void AssembleMqttSetDisconnectPkg();
		void AssembleMqttSubscribePkg();
		void AssembleMqttUnsubscribePkg();
		void AssembleMqttPublishPkg();
		
		void ModuleHeartBeat();
		void ModuleRST();
		void ModuleRestore();
		void ModuleGetVersionPkg();
			
		void WifiSetMode();
		void WifiConnentAP();
		void WifiDisconnent();
		void WifiSetAutoConnect();
		void WifiGetMode();
		void WifiGetStaStatus();
		void WifiGetAutoConnect();
			
		void MqttGetConnectCfg();
		void MqttSetConnectCfg1();
		void MqttSetConnectCfg2();
		void MqttSetConnectCfg3();
		void MqttSetConnectCfg4();
		void MqttSetConnectCfg5();
		void MqttSetConnectCfg6();
		void MqttSetConnect();
		void MqttDisconnect();
		void MqttSubscribe();
		void MqttUnsubscribe();
		void MqttPublish();
		
		void WB2_MQTT_Publish_JSON(const char *topic, const char *json_str);
		char* Set_cJSON_PrintUnformatted();
			
		void CommModuleInitTask();
		void CommWifiDisconnectTask();
		void CommWifiCfgTask();
		void CommWifiConnectTask();
		void CommMqttCfgTask();
		void CommMqttConnectTask();
		void CommTask();
			
		void Task10Ms();
		void Task1000Ms();
		void OperationTask();
};

#endif
