#include "my_string.h"
#include "MqttData.h"
#include "MainCtl.h"
#include "gvalue.h"
	
#include <stdio.h>
#include <string.h>
	
extern "C"{
	#include "HW_Config.h"
	#include "uart_wifi_drv.h"
	#include "cjson.h"
}

void TMqttData::Init()
{
	unsigned int i = 0;
	
	LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_8);
	LL_GPIO_SetOutputPin(GPIOG, LL_GPIO_PIN_3);
	
	commPrm.moduleOnline = 0;
	commPrm.moduleOfflineTick = 0;
	
	RxFlag = 0;
	
	commPrm.commState = MODULE_INIT_STATE;
	commPrm.commType = Idle_State;
	commPrm.wifiConnectState = Disconnect;
	commPrm.mqttConnectState = Disconnect;
	
	TxPrm.commType = Idle_State;
	
	RxPrm.rxFlag = 0;
	RxPrm.rxLen = 0;
	RxPrm.rpyFlag = 0;
	RxPrm.rpyLen = 0;
	RxPrm.rpyTimeout = 0;
	
	for(i=0;i<128;i++){
		RxPrm.rxData.d32[i] = 0;
		RxPrm.rpyData.d32[i] = 0;
		TxPrm.data.d32[i] = 0;
	}
	
	pRxPkg = uart_info_8bit[WIFI_UART].rx_buf;
	
	HAL_Delay(1000);
}

void TMqttData::ClearTxString()
{
	unsigned int i = 0;
	
	for(i=0;i<128;i++){
		TxPrm.data.d32[i] = 0;
	}
}

void TMqttData::oBufProcess()
{
	unsigned int len = 0;
	
	for(len = 0;len < 512; len++){
		if(0 == TxPrm.data.d8[len]){
			break;
		}
	}
	TxPrm.dataLen = len;
	set_wifi_tx_data(TxPrm.data.d8, TxPrm.dataLen);
}

void TMqttData::iBufProcess()
{
	unsigned int aLen;
	unsigned int i;
	
	if(RxFlag){
		RxFlag = 0;
		aLen = uart_info_8bit[WIFI_UART].rx_len;
		for(i=aLen;i<512;i++){
			pRxPkg[i] = 0;
		}
		
		pEventRxPkg = strstr((char*)pRxPkg, "+EVENT:");
		if(pEventRxPkg != NULL){
			RxPrm.eventStartTick = (unsigned int)pEventRxPkg - (unsigned int)pRxPkg;
			pEndRxPkg = strstr(pEventRxPkg, "\r\n");
			RxPrm.eventEndTick = (unsigned int)pEndRxPkg - (unsigned int)pRxPkg;
			
			if(RxPrm.eventEndTick - RxPrm.eventStartTick <= 7){//event data invalid
				;
			}else{
				RxPrm.rxLen = RxPrm.eventEndTick - RxPrm.eventStartTick;
				for(i=0;i<RxPrm.rxLen;i++){
					RxPrm.rxData.d8[i] = pRxPkg[RxPrm.eventStartTick+i];	
				}
				for(i=RxPrm.rxLen;i<512;i++){
					RxPrm.rxData.d8[i] = 0;
				}
				RxPrm.rxFlag = 1;
			}
		}else{
			RxPrm.eventStartTick = 0;
			RxPrm.eventEndTick = 0;
			RxPrm.rxLen = 0;
		}
		
		if(TxPrm.commType){
			if(RxPrm.rxLen){
				for(i=0;i<RxPrm.eventStartTick;i++){
					if(RxPrm.rpyLen < 512){
						if((pRxPkg[i] != 0x0A)&&(pRxPkg[i] != 0x0D)){
							RxPrm.rpyData.d8[RxPrm.rpyLen] = pRxPkg[i];
							RxPrm.rpyLen++;
						}
					}
				}
				for(i=(RxPrm.eventEndTick+2);i<aLen;i++){
					if(RxPrm.rpyLen < 512){
						if((pRxPkg[i] != 0x0A)&&(pRxPkg[i] != 0x0D)){
							RxPrm.rpyData.d8[RxPrm.rpyLen] = pRxPkg[i];
							RxPrm.rpyLen++;
						}
					}
				}
			}else{
				for(i=0;i<aLen;i++){
					if(RxPrm.rpyLen < 512){
						if((pRxPkg[i] != 0x0A)&&(pRxPkg[i] != 0x0D)){
							RxPrm.rpyData.d8[RxPrm.rpyLen] = pRxPkg[i];
							RxPrm.rpyLen++;
						}
					}
				}
			}
		}
	}
}

void TMqttData::RxEventHandle()
{
	if(RxPrm.rxFlag){
		RxPrm.rxFlag = 0;

		if(strstr((char*)RxPrm.rxData.d8, "WIFI_CONNECT") != NULL){
			commPrm.wifiConnectState = Connected;
		}else
		if(strstr((char*)RxPrm.rxData.d8, "WIFI_DISCONNECT") != NULL){
			commPrm.wifiConnectState = Disconnect;
			commPrm.mqttConnectState = Disconnect;
		}else
		if(strstr((char*)RxPrm.rxData.d8, "MQTT_CONNECT") != NULL){
			commPrm.mqttConnectState = Connected;
		}else
		if(strstr((char*)RxPrm.rxData.d8, "MQTT_DISCONNECT") != NULL){
			commPrm.mqttConnectState = Disconnect;
		}else
		if(strstr((char*)RxPrm.rxData.d8, "MQTT_SUB") != NULL){
			;
		}
	}
}

void TMqttData::RpyDataHandle()
{
	if(RxPrm.rpyFlag){
		RxPrm.rpyFlag = 0;
		
		switch(TxPrm.commType){
			case Idle_State:
				break;
//-----------------------------Module------------------------------------------------------------			
			case Module_Heart_Beat:
				if(strstr((char*)RxPrm.rpyData.d8, "AT") != NULL){
					commPrm.moduleOnline = 1;
					commPrm.moduleOfflineTick = 0;
				}
				TxPrm.commType = Idle_State;
				break;

			case Module_RST :
				TxPrm.commType = Idle_State;
				break;
			
			case Module_Get_Version :
				TxPrm.commType = Idle_State;
				break;
			
			case Module_Restore_Default :
				TxPrm.commType = Idle_State;
				break;
//-----------------------------Wifi------------------------------------------------------------					
			case Wifi_Set_Mode :
				TxPrm.commType = Idle_State;
				break;
			
			case Wifi_Set_Connect :
				TxPrm.commType = Idle_State;
				break;
			
			case Wifi_Set_Disconnect :
				TxPrm.commType = Idle_State;
				break;
			
			case Wifi_Set_AutoConnectFlag :
				TxPrm.commType = Idle_State;
				break;
			
			case Wifi_Get_Mode :
				TxPrm.commType = Idle_State;
				break;

			case Wifi_Get_StaInfo :
				if(strstr((char*)RxPrm.rpyData.d8, "+STAINFO:3") != NULL){
					commPrm.wifiConnectState = Connected;
					WifiGetAutoConnect();
				}else{
					commPrm.wifiConnectState = Disconnect;
					commPrm.mqttConnectState = Disconnect;
					WifiConnentAP();
				}
				break;
			
			case Wifi_Get_AutoConnectFlag :
				if(strstr((char*)RxPrm.rpyData.d8, "+WAUTOCONN:0") != NULL){
					WifiSetAutoConnect();
				}else{
					TxPrm.commType = Idle_State;
				}
				break;
//-----------------------------ble------------------------------------------------------------					
			case BLE_Set_Mode :
				TxPrm.commType = Idle_State;
				break;
			
			case BLE_Set_Name :
				TxPrm.commType = Idle_State;
				break;
			
			case BLE_Enter_Transparent :
				break;
//-----------------------------Mqtt------------------------------------------------------------			
			case MQTT_Get_Connect_Cfg :
				if(strstr((char*)RxPrm.rpyData.d8, "+MQTT:3") != NULL){
					commPrm.mqttConnectState = Connected;
				}else
				if(strstr((char*)RxPrm.rpyData.d8, "emqx.novix.fun,21883,1,28004E000951333039353739,28004E000951333039353739,tmp_09021457") != NULL){
					commPrm.mqttConnectState = Disconnect;
					MqttSetConnect();
				}else{
					commPrm.mqttConnectState = Disconnect;
					MqttSetConnectCfg1();
				}
				break;
			
			case MQTT_Set_Connect_Cfg1 :
				if(strstr((char*)RxPrm.rpyData.d8, "OK") != NULL){
					MqttSetConnectCfg2();
				}else{
					TxPrm.commType = Idle_State;
				}
				break;
			
			case MQTT_Set_Connect_Cfg2 :
				if(strstr((char*)RxPrm.rpyData.d8, "OK") != NULL){
					MqttSetConnectCfg3();
				}else{
					TxPrm.commType = Idle_State;
				}
				break;
			
			case MQTT_Set_Connect_Cfg3 :
				if(strstr((char*)RxPrm.rpyData.d8, "OK") != NULL){
					MqttSetConnectCfg4();
				}else{
					TxPrm.commType = Idle_State;
				}
				break;
			
			case MQTT_Set_Connect_Cfg4 :
				if(strstr((char*)RxPrm.rpyData.d8, "OK") != NULL){
					MqttSetConnectCfg5();
				}else{
					TxPrm.commType = Idle_State;
				}
				break;
			
			case MQTT_Set_Connect_Cfg5 :
				if(strstr((char*)RxPrm.rpyData.d8, "OK") != NULL){
					MqttSetConnectCfg6();
				}else{
					TxPrm.commType = Idle_State;
				}
				break;
			
			case MQTT_Set_Connect_Cfg6 :
				if(strstr((char*)RxPrm.rpyData.d8, "OK") != NULL){
					MqttSetConnect();
				}else{
					TxPrm.commType = Idle_State;
				}
				break;
			
			case MQTT_Set_Connect :
				TxPrm.commType = Idle_State;
				break;
			
			case MQTT_Set_Disconnect :
				TxPrm.commType = Idle_State;
				break;
			
			case MQTT_Publish :
				if(strstr((char*)RxPrm.rpyData.d8, "OK") != NULL){
					TxPrm.commType = Idle_State;
					//MqttSetConnect();
				}else{
					TxPrm.commType = Idle_State;
				}
				TxPrm.commType = Idle_State;
				break;
			
			case MQTT_Subscribe :
				TxPrm.commType = Idle_State;
				break;
			
			case MQTT_Unsubscribe :
				TxPrm.commType = Idle_State;
				break;
			
			case MQTT_Check_Message :
				TxPrm.commType = Idle_State;
				break;	
			
			default : 
				TxPrm.commType = Idle_State;
				break;
		}
	}
}

//------------------------------------module-----------------------------------------------------------
void TMqttData::AssembleModuleHeartBeatPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleModuleRstPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+RST\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleModuleGetVersionPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+GMR\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::ModuleHeartBeat()
{
	AssembleModuleHeartBeatPkg();
	TxPrm.commType = Module_Heart_Beat;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::ModuleRST()
{
	AssembleModuleRstPkg();
	TxPrm.commType = Module_RST;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
}

void TMqttData::ModuleGetVersionPkg()
{
	AssembleModuleGetVersionPkg();
	TxPrm.commType = Module_Get_Version;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

//------------------------------------wifi-----------------------------------------------------------
void TMqttData::AssembleWifiSetModePkg()//1:STA  1:save flash
{
	ClearTxString();
	TMyString::sAddOnStr("AT+WMODE:1,1\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleWifiGetModePkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+WMODE?\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleWifiSetConnectPkg()
{
	const char ssid[] = "YG";
	const char password[] = "8888888888";
	
	ClearTxString();
	sprintf((char*)TxPrm.data.d8, "AT+WJAP=%s,%s\r\n", ssid, password);
}

void TMqttData::AssembleWifiGetStaInfoPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+STAINFO?\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleWifiSetAutoConnectPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+WAUTOCONN=1\r\n",(char *)TxPrm.data.d8);
}
	
void TMqttData::AssembleWifiGetAutoConnectPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+WAUTOCONN?\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::WifiSetMode()
{
	AssembleWifiSetModePkg();
	TxPrm.commType = Wifi_Set_Mode;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::WifiGetMode()
{
	AssembleWifiGetModePkg();
	TxPrm.commType = Wifi_Get_Mode;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::WifiConnentAP()
{
	AssembleWifiSetConnectPkg();
	TxPrm.commType = Wifi_Set_Connect;
	
	RxPrm.rpyTimeout = 1000;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::WifiGetStaStatus()
{
	AssembleWifiGetStaInfoPkg();
	TxPrm.commType = Wifi_Get_StaInfo;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::WifiSetAutoConnect()
{
	AssembleWifiSetAutoConnectPkg();
	TxPrm.commType = Wifi_Set_AutoConnectFlag;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::WifiGetAutoConnect()
{
	AssembleWifiGetAutoConnectPkg();
	TxPrm.commType = Wifi_Get_AutoConnectFlag;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

//-------------------------------------Mqtt-----------------------------------------------------
void TMqttData::AssembleMqttGetConnectCfgPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTT?\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSetConnectCfg1Pkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTT=1,emqx.novix.fun\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSetConnectCfg2Pkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTT=2,21883\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSetConnectCfg3Pkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTT=3,1\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSetConnectCfg4Pkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTT=4,28004E000951333039353739\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSetConnectCfg5Pkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTT=5,28004E000951333039353739\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSetConnectCfg6Pkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTT=6,tmp_09021457\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSetConnectPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTT\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSetDisconnectPkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTTDISCONN\r\n",(char *)TxPrm.data.d8);
}

void TMqttData::AssembleMqttSubscribePkg()
{
	ClearTxString();
	TMyString::sAddOnStr("AT+MQTTSUB=xiaoyi/28004E000951333039353739/ctlDownLoad,1\r\n",(char *)TxPrm.data.d8);
}

/**
 * @brief  转义JSON字符串中的双引号（将"替换为\"）
 * @param  src: 原始JSON字符串（如{"temp":25}）
 * @param  dest: 转义后的字符串（如{\"temp\":25}）
 * @param  dest_len: 目标缓冲区大小
 */
static void escape_json_quotes(const char *src, char *dest, uint16_t dest_len) {
    uint16_t i = 0, j = 0;
    if (src == NULL || dest == NULL || dest_len == 0) return;

    // 遍历原始字符串，处理双引号
    while (src[i] != '\0' && j < dest_len - 1) {
        if (src[i] == ',') {
            // 遇到双引号，先添加转义符\，再添加"
            if (j + 1 < dest_len - 1) {
                dest[j++] = '\\';
                dest[j++] = ',';
            } else {
                break;  // 缓冲区不足，退出
            }
        } else {
            // 其他字符直接复制
            dest[j++] = src[i];
        }
        i++;
    }
    dest[j] = '\0';  // 字符串结束符
}

char* TMqttData::Set_cJSON_PrintUnformatted()
{
	cJSON *root = NULL;
	char *json_str = NULL;
	
	// 创建根JSON对象
	root = cJSON_CreateObject();
	if (root == NULL) {
		//创建JSON对象失败\r\n");
		return NULL;
	}
	
	cJSON_AddStringToObject(root, "msgId", "req_20250902_001");
	cJSON_AddNumberToObject(root, "ts", 1756791510);
	cJSON_AddStringToObject(root, "func", "heartbeat");
	cJSON_AddNumberToObject(root, "sign", 5510);
	json_str = cJSON_PrintUnformatted(root);
	
	// 释放cJSON对象占用的内存
	cJSON_Delete(root);
	
	return json_str;
}

/**
 * @brief  通过WB2模块发送JSON格式的MQTT消息
 * @param  topic: MQTT主题（如"sensor/data"）
 * @param  qos: 消息质量等级（0/1/2）
 * @param  retained: 保留消息标志（0=不保留，1=保留）
 * @param  json_str: 原始JSON字符串（如{"temp":25,"humi":60}）
 * @return WB2_SUCCESS=成功，WB2_FAIL=失败
 */
void TMqttData::WB2_MQTT_Publish_JSON(const char *topic, const char *json_str) {
    char escaped_json[256];  // 存储转义后的JSON（根据需要调整大小）

    // 参数合法性检查
    if (topic == NULL || json_str == NULL) {
        return ;
    }

    // 1. 转义JSON中的双引号（核心步骤）
    escape_json_quotes(json_str, escaped_json, sizeof(escaped_json));

    // 2. 构建MQTT发送的AT指令
    // 格式：AT+MQTTPUB="topic",qos,retained,"escaped_json"\r\n
    sprintf((char *)TxPrm.data.d8, 
             "AT+MQTTPUB=%s,1,0,%s\r\n",
             topic, escaped_json);
}

void TMqttData::AssembleMqttPublishPkg()
{
	char *json_str = Set_cJSON_PrintUnformatted();
	
	ClearTxString();
	
	WB2_MQTT_Publish_JSON("xiaoyi/28004E000951333039353739/ctlUpLoad",json_str);
	//strcpy((char *)TxPrm.data.d8,"AT+MQTTPUB=xiaoyi/28004E000951333039353739/ctlUpLoad,1,0,{\"msgId\":req_20250902_001\,\"ts\":1756791510\,\"func\":\"heartbeat\"\,\"sign\":5510}\r\n");
}

void TMqttData::MqttGetConnectCfg()
{
	AssembleMqttGetConnectCfgPkg();
	TxPrm.commType = MQTT_Get_Connect_Cfg;
	
	RxPrm.rpyTimeout = 100;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttSetConnectCfg1()
{
	AssembleMqttSetConnectCfg1Pkg();
	TxPrm.commType = MQTT_Set_Connect_Cfg1;
	
	RxPrm.rpyTimeout = 100;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttSetConnectCfg2()
{
	AssembleMqttSetConnectCfg2Pkg();
	TxPrm.commType = MQTT_Set_Connect_Cfg2;
	
	RxPrm.rpyTimeout = 100;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttSetConnectCfg3()
{
	AssembleMqttSetConnectCfg3Pkg();
	TxPrm.commType = MQTT_Set_Connect_Cfg3;
	
	RxPrm.rpyTimeout = 100;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttSetConnectCfg4()
{
	AssembleMqttSetConnectCfg4Pkg();
	TxPrm.commType = MQTT_Set_Connect_Cfg4;
	
	RxPrm.rpyTimeout = 100;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttSetConnectCfg5()
{
	AssembleMqttSetConnectCfg5Pkg();
	TxPrm.commType = MQTT_Set_Connect_Cfg5;
	
	RxPrm.rpyTimeout = 100;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttSetConnectCfg6()
{
	AssembleMqttSetConnectCfg6Pkg();
	TxPrm.commType = MQTT_Set_Connect_Cfg6;
	
	RxPrm.rpyTimeout = 100;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttSetConnect()
{
	AssembleMqttSetConnectPkg();
	TxPrm.commType = MQTT_Set_Connect;
	
	RxPrm.rpyTimeout = 2000;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttDisconnect()
{
	AssembleMqttSetDisconnectPkg();
	TxPrm.commType = MQTT_Set_Disconnect;
	
	RxPrm.rpyTimeout = 500;
	RxPrm.rpyLen = 0;
}

void TMqttData::MqttSubscribe()
{
	AssembleMqttSubscribePkg();
	TxPrm.commType = MQTT_Subscribe;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::MqttUnsubscribe()
{
	;
}

void TMqttData::MqttPublish()
{
	AssembleMqttPublishPkg();
	TxPrm.commType = MQTT_Publish;
	
	RxPrm.rpyTimeout = 200;
	RxPrm.rpyLen = 0;
	
	oBufProcess();
}

void TMqttData::Task10Ms()
{
	unsigned int i;
	
	if(TxPrm.commType){
		if(RxPrm.rpyTimeout < 1){
			if(RxPrm.rpyLen){
				RxPrm.rpyFlag = 1;
				for(i=RxPrm.rpyLen;i<512;i++){
					RxPrm.rpyData.d8[i] = 0;
				}
			}else{
				TxPrm.commType = Idle_State;
			}
		}else{
			RxPrm.rpyTimeout--;
		}
		/*
		if(RxPrm.rpyTimeout){
			RxPrm.rpyTimeout--;
		}else{
			if(RxPrm.rpyLen){
				RxPrm.rpyFlag = 1;
			}else{
				TxPrm.commType = Idle_State;
			}
		}
		*/
	}
}

void TMqttData::Task1000Ms()
{
	static unsigned int tick = 0;
	static unsigned int onceFlag = 1;
	static unsigned int config = 0;
	
	commPrm.moduleOfflineTick++;
	if(commPrm.moduleOfflineTick > 10){
		commPrm.moduleOfflineTick = 20;
		commPrm.moduleOnline = 0;
	}
	
	//ModuleHeartBeat();
	//ModuleGetVersionPkg();
	//WifiGetMode();
	//WifiGetStaStatus();
	//WifiGetAutoConnect();
	
	if(config){
		if(3 == tick)WifiGetStaStatus();
		else if(15 == tick){
			if(Connected == commPrm.wifiConnectState)MqttGetConnectCfg();
		}else
		if(55 == tick){
			if((Connected == commPrm.wifiConnectState)&&(Connected == commPrm.mqttConnectState))MqttPublish();
		}
	}
	
	if(tick > 60){
		tick = 0;
		if(0 == config){
			if(Connected != commPrm.wifiConnectState){
				WifiGetStaStatus();
			}else
			if(Connected != commPrm.mqttConnectState){
				MqttGetConnectCfg();
			}else{
				if(onceFlag){
					onceFlag = 0;
					MqttSubscribe();
				}else{
					MqttPublish();
					config = 1;
				}
			}
		}
	}else{
		tick++;
	}
}

void TMqttData::CommModuleInitTask()
{
	;
}

void TMqttData::CommWifiDisconnectTask()
{
	;
}

void TMqttData::CommWifiCfgTask()
{
	;
}

void TMqttData::CommWifiConnectTask()
{
	;
}

void TMqttData::CommMqttCfgTask()
{
}

void TMqttData::CommMqttConnectTask()
{
	;
}

void TMqttData::CommTask()
{
	switch(commPrm.commState){
		case MODULE_INIT_STATE:
			CommModuleInitTask();
			break;
		
		case WIFI_DISCONNECT_STATE:
			CommWifiDisconnectTask();
			break;
		
		case WIFI_CONFIG_STATE:
			CommWifiCfgTask();
			break;
		
		case WIFI_CONNECT_STATE:
			CommWifiConnectTask();
			break;
		
		case MQTT_CONFIG_STATE:
			CommMqttCfgTask();
			break;
		
		case MQTT_CONNECT_STATE:
			CommMqttConnectTask();
			break;
		
		default:
			break;
	}
}

void TMqttData::OperationTask()
{
	iBufProcess();
	
	RxEventHandle();
	
	RpyDataHandle();
}
