/*
 * KK_MQTTSDK.h
 *
 *  Created on: Nov 29, 2018
 *      Author: sds
 */

#ifndef KK_MQTTSDK_H_
#define KK_MQTTSDK_H_

#include <string>
#include <map>
#include <vector>
#include <set>
#include "ace/Task.h"

using namespace std;

class KK_MQTTSDK : public ACE_Event_Handler {
public:
	class EH_SDKCallbackHandler {
		public:
			virtual void onCloudConnect() = 0;
			virtual void onCloudDisConnect() = 0;
			virtual void onCloudMsgRecv(string payload,string topicName) = 0;
	};
public:
	static void* run0(void* opt);

public:
		static KK_MQTTSDK* GetInstance();
		int init(string brokerAddr,string clientId,string username,string password,string willTopic,string heartbeat_Topic,string PTPO_Topic,string gw_id);

		int subTopic(string topic,int qos = 0,int waitTimeout = 0);
		int unsubTopic(string topic,int waitTimeout = 0);
		int sendMsg(string topic,string payload,int qos = 0);

		int reinit(string brokerAddr);
		int con(int autoRecon,int timeoutInSec = 2);
		int discon();
		int destroy();

		void setSdkCallbackHandler(EH_SDKCallbackHandler* handler);
		EH_SDKCallbackHandler* getSdkCallbackHandler();
		void recon(int delayInSec);


private:
	KK_MQTTSDK();
	void* run(void* opt);
	int handle_timeout(const ACE_Time_Value &current_time,const void *act = 0);

private:
	static KK_MQTTSDK* ms_Instance ;

    string gw_id;

	string clientId;
	string brokerAddr;
	string username;
	string password;
	string willTopic;
	string PTPO_Topic; //ptpo运维
	string heartbeat_Topic;
	string topic1 ,topic2,topic3,topic4,topic5,topic6,topic7,topic8,topic9,topic10; //订阅的主题
	long reconTimerId;

	EH_SDKCallbackHandler* callbackHandler;
};



#endif /* KK_MQTTSDK_H_ */
