123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #include <cstdio>
- #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;
- }
|