#include #include "VirtualDevice.h" #include "PacketAnalizer.h" #include "LocalConfig.h" #include "CCOSLogicDeviceStructure.h" VirtualDevice::VirtualDevice() { } VirtualDevice::~VirtualDevice() { } RET_STATUS VirtualDevice::OnNotify(string keyStr, ResDataObject& resContext) { return RET_ONGOING; } void VirtualDevice::SetDeviceInfo(string strHostDev, string strServDev) { //mLog::FINFO("Set Value HostDev {$} ServiceDev {$}", strHostDev, strServDev); m_strHostDevicePath = strHostDev; m_strServiceDevicePath = strServDev; } void VirtualDevice::OnSetClientID() { bool bFirstLoad = true; ResDataObject resModuleConf; if (m_strHostDevicePath.length() <= 0) { bFirstLoad = false; string hostConf = GetCcosModuleConfigrateFilePath(m_strCCOSDevicePath, true); try { resModuleConf.loadFile(hostConf.c_str()); if (resModuleConf.GetKeyCount("Get") > 0) { ResDataObject resConf = resModuleConf["Get"]; //mLog::FINFO("Host DevicePath is null try get from conf {$}", hostConf); //mLog::FDEBUG("Configure Context is {$}", resConf.encode()); if (resConf.GetKeyCount("HostDevice") > 0 && resConf["HostDevice"].GetKeyCount("Value") > 0) { m_strHostDevicePath = (const char*)resConf["HostDevice"]["Value"]; //mLog::FINFO("Host DevicePath {$} is get from conf", m_strHostDevicePath); } else { //mLog::FINFO("Host DevicePath Has no conf"); } if (resConf.GetKeyCount("ServiceDevice") > 0 && resConf["ServiceDevice"].GetKeyCount("Value") > 0) { m_strServiceDevicePath = (const char*)resConf["ServiceDevice"]["Value"]; //mLog::FINFO("Service DevicePath {$} is get from conf", m_strServiceDevicePath); } else { //mLog::FINFO("Service DevicePath Has no conf"); } } } catch (...) { //mLog::FWARN("Had host config {$} , but Read Content error", hostConf); } } else { //mLog::FINFO("Host DevicePath Has Value {$}", m_strHostDevicePath); } if (m_strServiceDevicePath.length() <= 0) { bFirstLoad = false; //mLog::FINFO("Service DevicePath is null try get from conf"); if (m_resProperties.GetKeyCount("ServiceDevice") > 0 && m_resProperties["ServiceDevice"].GetKeyCount("Value") > 0) { m_strServiceDevicePath = (const char*)m_resProperties["ServiceDevice"]["Value"]; //mLog::FINFO("Service DevicePath {$} is get from conf", m_strServiceDevicePath); } else { //mLog::FINFO("Service DevicePath Has no conf"); } } else { //mLog::FINFO("Service Path Has Value {$}", m_strServiceDevicePath); } //mLog::FINFO("Change Device Path from {$} @FirstLoad=[{$}]", m_strCCOSDevicePath, bFirstLoad); //这个时候是 修改Device路径的时机 if (m_strServiceDevicePath.length() > 0) m_strCCOSDevicePath = m_strServiceDevicePath; //else if(m_strHostDevicePath.length() > 0) // m_strCCOSDevicePath = m_strHostDevicePath; //mLog::FINFO("New Device Path is {$}", m_strCCOSDevicePath); std::cout << "VirtualDevice::OnSetClientID [ New Device Path is" << m_strCCOSDevicePath << "]"<< std::endl; ModuleDevice::OnSetClientID(); if (m_strHostDevicePath.length() <= 0) { //mLog::FINFO("Host DevicePath is null try get from conf Again"); if (m_resProperties.GetKeyCount("HostDevice") > 0 && m_resProperties["HostDevice"].GetKeyCount("Value") > 0) { m_strHostDevicePath = (const char*)m_resProperties["HostDevice"]["Value"]; //mLog::FINFO("Host DevicePath {$} is get from conf", m_strHostDevicePath); } } else { //mLog::FINFO("Host DevicePath is {$}", m_strServiceDevicePath); if (m_resModuleConfig["Get"].GetKeyCount("HostDevice") > 0 && m_resModuleConfig["Get"]["HostDevice"].GetKeyCount("Value") > 0) { m_resModuleConfig["Get"]["HostDevice"].update("Value", m_strHostDevicePath.c_str()); m_resModuleConfig.SaveFile(m_strModuleFilePath.c_str()); if (bFirstLoad) { remove(m_strConfigFilePath.c_str()); m_resProperties = m_resModuleConfig["Get"]; SaveToConfigFile(); } //mLog::FINFO("Host DevicePath {$} is get from conf Save to {$}", m_strHostDevicePath, m_strModuleFilePath); } } if (m_strServiceDevicePath.length() <= 0) { //mLog::FINFO("Service DevicePath is null try get from conf Again"); if (m_resProperties.GetKeyCount("ServiceDevice") > 0 && m_resProperties["ServiceDevice"].GetKeyCount("Value") > 0) { m_strServiceDevicePath = (const char*)m_resProperties["ServiceDevice"]["Value"]; //mLog::FINFO("Service DevicePath {$} is get from conf", m_strServiceDevicePath); } } else { //mLog::FINFO("Service DevicePath is {$}", m_strServiceDevicePath); if (m_resModuleConfig["Get"].GetKeyCount("ServiceDevice") > 0 && m_resModuleConfig["Get"]["ServiceDevice"].GetKeyCount("Value") > 0) { m_resModuleConfig["Get"]["ServiceDevice"].update("Value", m_strServiceDevicePath.c_str()); m_resModuleConfig.SaveFile(m_strModuleFilePath.c_str()); //这里特殊处理下,用服务设备的配置 覆盖 宿主配置,下次读取的时候,可以纠正回来 string hostConfPath = GetCcosModuleConfigrateFilePath(m_strHostDevicePath,true); //mLog::FINFO("Save config to Host DevicePath {$} config {$}", m_strHostDevicePath, hostConfPath); m_resModuleConfig.SaveFile(hostConfPath.c_str()); if (bFirstLoad) { remove(m_strConfigFilePath.c_str()); m_resProperties = m_resModuleConfig["Get"]; SaveToConfigFile(); //mLog::FINFO("Service DevicePath {$} is get from conf Save to {$}", m_strServiceDevicePath, m_strModuleFilePath); } } } SaveToConfigFile(); } RET_STATUS VirtualDevice::Request(ResDataObject PARAM_IN* pRequest, ResDataObject PARAM_OUT* pResponse) { ResDataObject resTopic; RET_STATUS ret = RET_ONGOING; PacketAnalizer::GetPacketTopic(pRequest, resTopic); PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest); PACKET_TYPE type = PacketAnalizer::GetPacketType(pRequest); string keystr = PacketAnalizer::GetPacketKey(pRequest); ResDataObject Context; if (PacketAnalizer::GetPacketContext(pRequest, Context) == false) { return RET_FAILED; } //mLog::FINFO("Got [{$}] REQ [{$}] context [{$}] from [{$}]", _Packet_Cmd_String[cmd], keystr, Context.size() <= 0 ? (const char*)Context : Context.encode(), (const char*)resTopic); if (type == PACKET_TYPE_NOTIFY) { ret = OnNotify(keystr, Context); //通知类,不需要答复 return RET_SUCCEED; } else if (type == PACKET_TYPE_RES) { //收到了应答包。。。 return RET_SUCCEED; } else return ModuleDevice::Request(pRequest, pResponse); } RET_STATUS VirtualDevice::OnUpdate(const char* pszProperty, const char* pszValueUpdate, ResDataObject& resRespons) { string strPRoperty = pszProperty; if (strPRoperty == "ServiceDevice") { m_strServiceDevicePath = pszValueUpdate; } else if (strPRoperty == "HostDevice") { m_strHostDevicePath = pszValueUpdate; } return RET_NOSUPPORT; } RET_STATUS VirtualDevice::SetItem(const char* pszPropery, ResDataObject& resSetValue, ResDataObject& resResponse) { return RET_ONGOING; } RET_STATUS VirtualDevice::GetItem(const char* pszProperty, ResDataObject& resResponse) { string strPRoperty = pszProperty; if (strPRoperty == "ServiceDevice") { resResponse = m_strServiceDevicePath.c_str(); return RET_SUCCEED; } else if (strPRoperty == "HostDevice") { resResponse = m_strHostDevicePath.c_str(); return RET_SUCCEED; } return RET_NOSUPPORT; }