/*
 * MQ_CCU_Config.cpp
 *
 *  Created on: Dec 6, 2018
 *      Author: sds
 */
#include "MQ_CCU_Config.h"
#include <fstream>
#include <string>
#include "json/writer.h"
#include "json/reader.h"
#include "json/value.h"
#include "LSC_Logger.h"
#include "EH_ToolKit.h"


MQ_CCU_Config* MQ_CCU_Config::ms_pInstance = NULL;

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

MQ_CCU_Config::MQ_CCU_Config() {

	logLevel = "D";
}

//读取配置文件里的参数
int MQ_CCU_Config::open() {
	ifstream infile(MQ_PROXY_CONFIG_FILE);
	if (!infile) {
		//打开配置文件失败
		LOGW("open config info file:%s failed.\n", MQ_PROXY_CONFIG_FILE);
		return -1;
	}
	EH::Json::Reader reader;
	EH::Json::Value config;
	bool success = reader.parse(infile, config, false);
	infile.close();
	if (!success || !config.isObject()) {
		//消息格式错误
		LOGW("Message format is wrong.\n");
		return -1;
	}
	brokerAddr = config[MQ_BROKER_ADDRESS].asString(); 	//MQ代理地址
	logLevel = config[LOG_LEVEL].asString(); 			//日志等级
	cacheSize = config[REQ_SIZE].asInt(); 				//请求队列大小

	return 0;
}

//获取代理地址
string MQ_CCU_Config::getBrokerAddr() {
	return brokerAddr;
}

//获取日志等级
string MQ_CCU_Config::getLogLevel() {
	return logLevel;
}

//获取请求队列大小
int MQ_CCU_Config::getReqCacheSize() {
	return cacheSize;
}

//获取主机的ID
string MQ_CCU_Config::getCcuId() {
	ifstream infile(CCUID_PATH);
	if (!infile) {
		return "";
	}
	char buffer[64];
	infile.getline(buffer, 64);
	infile.close();
	return string(buffer);
}

//获取Ccu访问密钥
string MQ_CCU_Config::getCcuAccesskey() {
	ifstream infile(LICENSE_PATH);
	if (!infile) {
		return "";
	}
	char buffer[512] = { 0 };
	infile.read(buffer,512);
	infile.close();
	vector<string> infos;
	EH_ToolKit::Split(string(buffer), "\n", infos);
	if(infos.size()==2){
		return infos[1];
	}
	return "";
}
