/*
 * BusinessConfig.cpp
 *
 *  Created on: 2022年10月18日
 *      Author: Allen
 */


#include "BusinessConfig.h"
#include "base/strings.hpp"

CallingStatus::CallingStatus() {
	_busy = false;
}

CallingStatus::~CallingStatus() {
}

CallingStatus* CallingStatus::instance() {
	static CallingStatus singleton;
	return &singleton;
}

void CallingStatus::setBusy(bool busy){
	_busy = busy;
}

void CallingStatus::setTcpModel(TcpModel tcpModel){
	_tcpModel = tcpModel;
}
void CallingStatus::clearTcpModel(){
	_tcpModel.type = "";
	_tcpModel.action = "";
	_tcpModel.from_id = 0;
	_tcpModel.to_id = 0;
	_tcpModel.json = NULL;
	_tcpModel.data = "";
}

TcpModel CallingStatus::getTcpModel(){
	return _tcpModel;
}

long CallingStatus::getInteractionId(){
	if (_tcpModel.json > 0){
		if (_tcpModel.json.isMember("id")){
			return _tcpModel.json["id"].asInt();
		}
	}
	return 0;
}

bool CallingStatus::busy() {
	return instance()->_busy;
}

void sendVoiceTcp(std::string action, TcpModel model, int toId){
	//发出TCP
	TcpModel tcpModel;
	tcpModel.type = TcpType::VOICE;
	tcpModel.action = action;
	tcpModel.from_id = StoragePreferences::getInt(STORE_DEVICE_ID,0);

	tcpModel.to_id = toId;
	if (model.data!=""){
		tcpModel.data = model.data;
	} else if (model.json>0){
		tcpModel.json = model.json;
	}

	std::string req = getTcpModelString(tcpModel);
	LOGD("sendVoiceTcp : %s",req.c_str());
	TcpClient::instance()->sendMsg(req.c_str());
}

std::string GetDeviceUid() {
  unsigned char devID[16];
  int ret = SECURITYMANAGER->getDevID(devID);
  if (ret <= 0) {
    return "";
  }
  std::string str;
  for (int i = 0; i < ret; ++i) {
    str += base::format("%02X", devID[i]);
  }
  return str;
}


void addUartMsg(const char* msg, int count){
	UartMsg model;
	model.msg = msg;
	model.count = count;
	queueMsgs.push_back(model);
}

UartMsg getUartMsg(){
	UartMsg msg;
	if (queueMsgs.empty()){
		msg.count = -10;
		return msg;
	}
	//取出第一个
	msg = queueMsgs.front();
	//删除第一个
	queueMsgs.erase(queueMsgs.begin());
	return msg;
}


void addS433Msg(const std::string msg, int count, int route){
	S433Msg model;
	model.msg = msg;
	model.count = count;
	model.route = route;
	s433Msgs.push_back(model);
}

S433Msg getS433Msg(){
	S433Msg msg;
	if (s433Msgs.empty()){
		msg.count = -10;
		return msg;
	}
	//取出第一个
	msg = s433Msgs.front();
	//删除第一个
	s433Msgs.erase(s433Msgs.begin());
	return msg;
}

bool isS433Empty() {
	return s433Msgs.empty();
}




