/*
 * KK_HwDevManager.cpp
 *
 *  Created on: Dec 6, 2018
 *      Author: sds
 */
#include "KK_HwDevManager.h"

#include <fstream>
#include <string>
#include "json/writer.h"
#include "json/reader.h"
#include "json/value.h"
#include "EH_ToolKit.h"
#include "LSC_Logger.h"
#include <stdio.h>

KK_HwDevManager* KK_HwDevManager::ms_pInstance = NULL;
KK_HwDevManager* KK_HwDevManager::GetInstance() {
	if (ms_pInstance == NULL) {
		ms_pInstance = new KK_HwDevManager();
	}
	return ms_pInstance;
}

KK_HwDevManager::KK_HwDevManager() {

}

//int KK_HwDevManager::getHwProfileConfig(EH::Json::Value& config) {
//	ifstream infile(PROFILE_CONFIG_FILE);
//	if (!infile) {
//		EH_WARNING("open config info file:%s failed.\n", PROFILE_CONFIG_FILE);
//		return -1;
//	}
//	EH::Json::Reader reader;
//	bool success = reader.parse(infile, config, false);
//	infile.close();
//	if (!success || !config.isArray()) {
//		EH_WARNING("Message format is wrong.\n");
//		return -1;
//	}
//	return 0;
//}
//
//int KK_HwDevManager::loadAllSubDev(map<string,KKDevBindHwTriple> &subDevs){
//	ifstream infile(DEV_CONFIG_FILE);
//	if (!infile) {
//		EH_WARNING("open dev info file:%s failed.\n", DEV_CONFIG_FILE);
//		return 0;
//	}
//	EH::Json::Value devList;
//	EH::Json::Reader reader;
//	bool success = reader.parse(infile, devList, false);
//	infile.close();
//	if (!success || !devList.isArray()) {
//		EH_WARNING("Message format is wrong.\n");
//		return -1;
//	}
//	for (int i = 0; i < devList.size(); i++) {
//		EH::Json::Value devInfo = devList[i];
//		KKDevBindHwTriple bindDev;
//		bindDev.mac = devInfo["mac"].asString();
//		bindDev.prodId = devInfo["prodId"].asString();
//		bindDev.sn = devInfo["sn"].asString();
//		subDevs[bindDev.mac] = bindDev;
//	}
//	EH_DEBUG("load all sub dev,now dev count:%d.\n", subDevs.size());
//	return 0;
//}

//int KK_HwDevManager::loadScenePanelName(map<string,KKScenePanelName> &subDevs){ //情景按钮名称读取
//	ifstream infile(ScenePaneName_FILE);
//	if (!infile) {
//		EH_WARNING("open dev info file:%s failed.\n", ScenePaneName_FILE);
//		return 0;
//	}
//	EH::Json::Value devList;
//	EH::Json::Reader reader;
//	bool success = reader.parse(infile, devList, false);
//	infile.close();
//	if (!success || !devList.isArray()) {
//		EH_WARNING("Message format is wrong.\n");
//		return -1;
//	}
//	for (int i = 0; i < devList.size(); i++) {
//		EH::Json::Value devInfo = devList[i];
//		KKScenePanelName bindDev;
//		bindDev.devsn = devInfo["devsn"].asString();
//		bindDev.nodeID = devInfo["nodeID"].asString();
//		bindDev.name1 = devInfo["name1"].asString();
//		bindDev.name2 = devInfo["name2"].asString();
//		bindDev.name3 = devInfo["name3"].asString();
//		bindDev.name4 = devInfo["name4"].asString();
//		subDevs[bindDev.nodeID] = bindDev;
//	}
//	EH_DEBUG("load all sub dev,now dev count:%d.\n", subDevs.size());
//	return 0;
//}

//int KK_HwDevManager::saveScenePanelName(map<string,KKScenePanelName> &subDevs){ //情景按钮名称保存
//	EH::Json::Value devArray = EH::Json::Value(EH::Json::arrayValue);
//	map<string, KKScenePanelName>::iterator it = subDevs.begin();
//	for (; it != subDevs.end(); it++) {
//		EH::Json::Value devInfo;
//		devInfo["devsn"] = it->second.devsn;
//		devInfo["nodeID"] = it->second.nodeID;
//		devInfo["name1"] = it->second.name1;
//		devInfo["name2"] = it->second.name2;
//		devInfo["name3"] = it->second.name3;
//		devInfo["name4"] = it->second.name4;
//		devArray.append(devInfo);
//	}
//
//	EH::Json::StyledStreamWriter writer;
//	ofstream outFile(ScenePaneName_FILE);
//	if (!outFile) {
//		EH_WARNING("open dev info file:%s failed.\n", ScenePaneName_FILE);
//		return -1;
//	}
//	writer.write(outFile, devArray);
//	outFile.close();
//
//	EH_DEBUG("save all sub dev,now dev count:%d.\n", subDevs.size());
//	return 0;
//}

//int KK_HwDevManager::saveAllSubDev(map<string,KKDevBindHwTriple> &subDevs){
//	EH::Json::Value devArray = EH::Json::Value(EH::Json::arrayValue);
//	map<string, KKDevBindHwTriple>::iterator it = subDevs.begin();
//	for (; it != subDevs.end(); it++) {
//		EH::Json::Value devInfo;
//		devInfo["mac"] = it->second.mac;
//		devInfo["prodId"] = it->second.prodId;
//		devInfo["sn"] = it->second.sn;
//		devArray.append(devInfo);
//	}
//
//	EH::Json::StyledStreamWriter writer;
//	ofstream outFile(DEV_CONFIG_FILE);
//	if (!outFile) {
//		EH_WARNING("open dev info file:%s failed.\n", DEV_CONFIG_FILE);
//		return -1;
//	}
//	writer.write(outFile, devArray);
//	outFile.close();
//
//	EH_DEBUG("save all sub dev,now dev count:%d.\n", subDevs.size());
//	return 0;
//}

string KK_HwDevManager::getCcuId() {
	ifstream infile(CCUID_PATH);
	if (!infile) {
		return "";
	}
	char buffer[64];
	infile.getline(buffer, 64);
	infile.close();
	return string(buffer);
}

string KK_HwDevManager::getCcuKey() {
	ifstream infile(LICENSE_PATH);
	if (!infile) {
		return "";
	}
	char buffer[100];
	infile.getline(buffer, 100);
	infile.getline(buffer, 100);
	infile.close();
	return string(buffer);
}
string KK_HwDevManager::GetConnectState(){
		ifstream infile(CONNECT_STATE_FILE);
		string ConnectState;
		if (!infile) {
			LOGW("open dev info file:%s failed.\n", CONNECT_STATE_FILE);
			return 0;
		}
		EH::Json::Value devInfo;
		EH::Json::Reader reader;
		bool success = reader.parse(infile, devInfo, false);
		infile.close();
		if (devInfo.empty() == true) {
			LOGW("Message format is wrong.\n");
			return "-1";
		}
		ConnectState = devInfo["result"].asString();

		return ConnectState;
}

EH::Json::Value KK_HwDevManager::loadConnectState(){
	ifstream infile(CONNECT_STATE_FILE);
	EH::Json::Reader reader;
	EH::Json::Value connectStateInfo;
	if (!infile) {
		LOGW("open dev info file:%s failed.\n", CONNECT_STATE_FILE);
		return 0;
	}

	bool success = reader.parse(infile, connectStateInfo, false);
	infile.close();
	if (connectStateInfo.empty() == true) {
		LOGW("Message format is wrong.\n");
		return -1;
	}
	LOGI("load connectStateInfo:%s.\n",EH_ToolKit::JsonToString(connectStateInfo).c_str());
	return connectStateInfo;
}

int KK_HwDevManager::saveConnectState(EH::Json::Value ConnectState){
	EH::Json::StyledStreamWriter writer;
	ofstream outFile(CONNECT_STATE_FILE);
	if (!outFile) {
		LOGW("open dev info file:%s failed.\n", CONNECT_STATE_FILE);
		return -1;
	}
	writer.write(outFile, ConnectState);
	outFile.close();

	LOGI("save Connect State success\n");
	return 0;
}

string KK_HwDevManager::GetRoomId(){
	ifstream infile(ROOM_ID_FILE);
	string roomid;
	if (!infile) {
		LOGW("open dev info file:%s failed.\n", ROOM_ID_FILE);
		return 0;
	}
	EH::Json::Value devList;
	EH::Json::Reader reader;
	bool success = reader.parse(infile, devList, false);
	infile.close();
	//if (!success || !devList.isNull()) {
	if (devList.empty() == true) {
		LOGW("Message format is wrong.\n");
		return "-1";
	}
	for (int i = 0; i < devList.size(); i++) {
		EH::Json::Value devInfo = devList[i];
		roomid = devInfo["roomid"].asString();
	}
	return roomid;
}

int KK_HwDevManager::loadroomid(map<string,KKRoomId> &roomid){
	ifstream infile(ROOM_ID_FILE);
	if (!infile) {
		LOGW("open dev info file:%s failed.\n", ROOM_ID_FILE);
		return 0;
	}
	EH::Json::Value devList;
	EH::Json::Reader reader;
	bool success = reader.parse(infile, devList, false);
	infile.close();
	//if (!success || !devList.isNull()) {
	if (devList.empty() == true) {
		LOGW("Message format is wrong.\n");
		return -1;
	}
	for (int i = 0; i < devList.size(); i++) {
		EH::Json::Value devInfo = devList[i];
		KKRoomId bindDev;
		bindDev.roonid = devInfo["roomid"].asString();
		bindDev.name = devInfo["name"].asString();
		roomid[bindDev.roonid] = bindDev;
	}
	LOGD("load all sub dev,now dev count:%d.\n", roomid.size());
	return 0;
}

int KK_HwDevManager::saveroomid(map<string,KKRoomId> &roomid){
	EH::Json::Value devArray = EH::Json::Value(EH::Json::arrayValue);
	map<string, KKRoomId>::iterator it = roomid.begin();
	for (; it != roomid.end(); it++) {
		EH::Json::Value devInfo;
		devInfo["roomid"] = it->second.roonid;
		devInfo["name"] =  it->second.name;
		devArray.append(devInfo);
	}
	EH::Json::StyledStreamWriter writer;
	ofstream outFile(ROOM_ID_FILE);
	if (!outFile) {
		LOGW("open dev info file:%s failed.\n", ROOM_ID_FILE);
		return -1;
	}
	writer.write(outFile, devArray);
	outFile.close();

	LOGD("roomid:%d.\n", roomid.size());
	return 0;
}
