
#ifndef MQ_CCU_ADAPTER_H_
#define MQ_CCU_ADAPTER_H_

#include <sys/types.h>
#include <vector>
#include <set>
#include <string>
#include <pthread.h>

#include <ace/Task.h>
#include <ace/Condition_Thread_Mutex.h>
#include <ace/Thread_Mutex.h>
#include <ace/Time_Value.h>

#include "json/value.h"
#include "json/reader.h"

#include "EH_Client.h"
#include "KK_MqttSdk.h"
#include "lib/EH_SDKDef.h"
#include "kk_handleDeviceInfo.h"
#include "KK_HwDevManager.h"

#define CENTRALAC_INDOORUNIT_NUM_MAX 64
#define MQ_VERSION         	  5    //整数（云智易平台有对应关系）

using namespace std;

class MQ_CCU_Adapter:public ACE_Task<ACE_MT_SYNCH>,
	public EH_Client::EH_ClientCallBackHandler,
	public KK_MQTTSDK::EH_SDKCallbackHandler{
public:
	static MQ_CCU_Adapter* GetInstance();

	int svc();
	int open();

	//与中控连接的当前状态
	void onClientStatusChanged(EH_Client::ClientStatus currentStatus);

	//接收中控的信息
	void onRecvMsg(string nodeId, string opcode, EH::Json::Value arg, string status);
	//添加到房间里
	void SendSETUPToCcu(string name,string DeviceIcon,string DevDodeId,string roomid, string opcode);
	//发送信息到Roma
	int sendtoRoma(EH::Json::Value payload,string respTopic);

	//发送设备控制命令
	int send_Control_Info(string wholeMsg);

	void onCloudConnect();
	void onCloudDisConnect();
	void onCloudMsgRecv(string payload,string topicName);

private:
	MQ_CCU_Adapter();
	int Put(ACE_Message_Block* mblk, ACE_Time_Value* timeout = NULL);
	void sendReqToCcu(string opcode,string nodeid,string arg,string requester);

	//加载主机所有已经添加的设备
	int loadAllSubDev(EH::Json::Value arg);

	//当前的设备状态
	void handleGetDevsHardwareInfo(EH::Json::Value arg);
	void handleDevOnlinePush(EH::Json::Value arg);

	void oninitmqttsdk();
	//房间概念信息
	int addCCuroom();
	int saveCCuroom(EH::Json::Value arg);
	void Specific_rooms(EH::Json::Value rooms);

	//处理新设备报文
	void handleNewDevice(EH::Json::Value devices, EH::Json::Value centralACdevices, EH::Json::Value newCentralAcGwsDevices);
	void handleCcuDeviceToHwSubDev(string operateType,string nodeid,string mac,
			string channel,string sn,EH::Json::Value status,string devroomid,string devname);
	// void handleACindoor(string devroomid,string devname,int indoorunitsdevCount,EH::Json::Value centralACdevices,int infocome);
	//当空调网关离线，其下内机也推送离线
	// void handleACindoorDevNolinePush(string mac);

	//处理已加入房间的设备报文
	void handleSyncInfo(EH::Json::Value arg,string opcode);\
	// void SYNC_handle_AcGw_Devices(EH::Json::Value CentralAcGwsDevices);
	//添加设备到云
	void addCCuDevicesToCloud(map<string,kk_handleDeviceInfo::HwSubDev*> toAddSubDevs);

	//设备本地进行了删除
	int  loadDelete(EH::Json::Value arg);
	//下发查询网关信息
	int  on_Query_GateWay_Infomation();
	//添加新设备
	int onDeviceRequestJoin(bool enableJoinSubdev);
	//删除子设备
	int onDeviceRequestDelete(string devSn);
	//删除空调网关时，也删除其下的内机。
	int onACindoorDelete(string devMac);

	//获取网关的nodeID
	void handlegwNodeId(EH::Json::Value arg);
	//Zigbee 设备入网推送的信息
	int handle_Dev_Join_Notify(EH::Json::Value arg);
private:
	 EH_Client* client;
	 KK_MQTTSDK* mqttSdk;
private:
	static MQ_CCU_Adapter* ms_pInstance;
    ACE_Thread_Mutex mtx;
    ACE_Condition_Thread_Mutex cond;

    set<string> ccuDeleteMacs;//prepare to delete ccu devs
    map<string,string>  nodeId2channelMap; //nodeid -> mac:channel
    map<string,kk_handleDeviceInfo::HwSubDev*>  hwSubDevs; //hw dev in ccu
	map<string,kk_handleDeviceInfo::KKDevmapinfo> hwBindDevMap;//
    map<string,KK_HwDevManager::KKRoomId > onlyroomid; //适配程序创建的特定房间信息。

    map<string,string> gwId2NodeId;//空调网关ID与空调网关的nodeID的对应关系。//空调网关不加入房间里，空调内机添加到房间里后，续空调网关的mac相关信息在SYNE—INFO，就可以去除
    map<string,string> NodeId2gwId; //空调网关的nodeID与空调网关ID的对应关系。
    map<string,string> operate2NodeId; //空调网关设备类型与空调网关设备nodeID的对应关系
    map<string,vector<string> > controllerModel; //mac -> id  //空调网关
    map<string,string>  indoorunitsId2AddrMap; //内机的nodeid -> 空调网关mac:内机通道channel
	map<string,string>  Join_productid_Map; //新加入设备mac -> 设备productid

    int roomidok;
    unsigned long  lastCcuReqTime;
    string ccuId;
    string gw_id;
	string gw_NodeId;//Z3的网关nodeID
    string clientId;
	string username;

	string pushTopic ;
	string subTopic;
	string willTopic;
	string PTPO_Topic; //ptpo运维
	bool KKdiscover;

	bool ccuSyncOK;
    bool handleNewDevices;
    EH::Json::Value newDevices;
    EH::Json::Value newCentralAcDevices;
    EH::Json::Value newCentralAcGwsDevices;

	string ccuStatus;
	string Deletesn;
	bool mqttOnline =true;
	bool SaveRoomEnable =false;
	bool discoveryEnable =false;

};


#endif
