VirtualDevice.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include <cstdio>
  2. #include "VirtualDevice.h"
  3. #include "PacketAnalizer.h"
  4. #include "LocalConfig.h"
  5. #include "CCOSLogicDeviceStructure.h"
  6. VirtualDevice::VirtualDevice()
  7. {
  8. }
  9. VirtualDevice::~VirtualDevice()
  10. {
  11. }
  12. RET_STATUS VirtualDevice::OnNotify(string keyStr, ResDataObject& resContext)
  13. {
  14. return RET_ONGOING;
  15. }
  16. void VirtualDevice::SetDeviceInfo(string strHostDev, string strServDev)
  17. {
  18. //mLog::FINFO("Set Value HostDev {$} ServiceDev {$}", strHostDev, strServDev);
  19. m_strHostDevicePath = strHostDev;
  20. m_strServiceDevicePath = strServDev;
  21. }
  22. void VirtualDevice::OnSetClientID()
  23. {
  24. bool bFirstLoad = true;
  25. ResDataObject resModuleConf;
  26. if (m_strHostDevicePath.length() <= 0)
  27. {
  28. bFirstLoad = false;
  29. string hostConf = GetCcosModuleConfigrateFilePath(m_strCCOSDevicePath, true);
  30. try
  31. {
  32. resModuleConf.loadFile(hostConf.c_str());
  33. if (resModuleConf.GetKeyCount("Get") > 0)
  34. {
  35. ResDataObject resConf = resModuleConf["Get"];
  36. //mLog::FINFO("Host DevicePath is null try get from conf {$}", hostConf);
  37. //mLog::FDEBUG("Configure Context is {$}", resConf.encode());
  38. if (resConf.GetKeyCount("HostDevice") > 0 && resConf["HostDevice"].GetKeyCount("Value") > 0)
  39. {
  40. m_strHostDevicePath = (const char*)resConf["HostDevice"]["Value"];
  41. //mLog::FINFO("Host DevicePath {$} is get from conf", m_strHostDevicePath);
  42. }
  43. else
  44. {
  45. //mLog::FINFO("Host DevicePath Has no conf");
  46. }
  47. if (resConf.GetKeyCount("ServiceDevice") > 0 && resConf["ServiceDevice"].GetKeyCount("Value") > 0)
  48. {
  49. m_strServiceDevicePath = (const char*)resConf["ServiceDevice"]["Value"];
  50. //mLog::FINFO("Service DevicePath {$} is get from conf", m_strServiceDevicePath);
  51. }
  52. else
  53. {
  54. //mLog::FINFO("Service DevicePath Has no conf");
  55. }
  56. }
  57. }
  58. catch (...)
  59. {
  60. //mLog::FWARN("Had host config {$} , but Read Content error", hostConf);
  61. }
  62. }
  63. else
  64. {
  65. //mLog::FINFO("Host DevicePath Has Value {$}", m_strHostDevicePath);
  66. }
  67. if (m_strServiceDevicePath.length() <= 0)
  68. {
  69. bFirstLoad = false;
  70. //mLog::FINFO("Service DevicePath is null try get from conf");
  71. if (m_resProperties.GetKeyCount("ServiceDevice") > 0 && m_resProperties["ServiceDevice"].GetKeyCount("Value") > 0)
  72. {
  73. m_strServiceDevicePath = (const char*)m_resProperties["ServiceDevice"]["Value"];
  74. //mLog::FINFO("Service DevicePath {$} is get from conf", m_strServiceDevicePath);
  75. }
  76. else
  77. {
  78. //mLog::FINFO("Service DevicePath Has no conf");
  79. }
  80. }
  81. else
  82. {
  83. //mLog::FINFO("Service Path Has Value {$}", m_strServiceDevicePath);
  84. }
  85. //mLog::FINFO("Change Device Path from {$} @FirstLoad=[{$}]", m_strCCOSDevicePath, bFirstLoad);
  86. //这个时候是 修改Device路径的时机
  87. if (m_strServiceDevicePath.length() > 0)
  88. m_strCCOSDevicePath = m_strServiceDevicePath;
  89. //else if(m_strHostDevicePath.length() > 0)
  90. // m_strCCOSDevicePath = m_strHostDevicePath;
  91. //mLog::FINFO("New Device Path is {$}", m_strCCOSDevicePath);
  92. std::cout << "VirtualDevice::OnSetClientID [ New Device Path is" << m_strCCOSDevicePath << "]"<< std::endl;
  93. ModuleDevice::OnSetClientID();
  94. if (m_strHostDevicePath.length() <= 0)
  95. {
  96. //mLog::FINFO("Host DevicePath is null try get from conf Again");
  97. if (m_resProperties.GetKeyCount("HostDevice") > 0 && m_resProperties["HostDevice"].GetKeyCount("Value") > 0)
  98. {
  99. m_strHostDevicePath = (const char*)m_resProperties["HostDevice"]["Value"];
  100. //mLog::FINFO("Host DevicePath {$} is get from conf", m_strHostDevicePath);
  101. }
  102. }
  103. else
  104. {
  105. //mLog::FINFO("Host DevicePath is {$}", m_strServiceDevicePath);
  106. if (m_resModuleConfig["Get"].GetKeyCount("HostDevice") > 0 && m_resModuleConfig["Get"]["HostDevice"].GetKeyCount("Value") > 0)
  107. {
  108. m_resModuleConfig["Get"]["HostDevice"].update("Value", m_strHostDevicePath.c_str());
  109. m_resModuleConfig.SaveFile(m_strModuleFilePath.c_str());
  110. if (bFirstLoad)
  111. {
  112. remove(m_strConfigFilePath.c_str());
  113. m_resProperties = m_resModuleConfig["Get"];
  114. SaveToConfigFile();
  115. }
  116. //mLog::FINFO("Host DevicePath {$} is get from conf Save to {$}", m_strHostDevicePath, m_strModuleFilePath);
  117. }
  118. }
  119. if (m_strServiceDevicePath.length() <= 0)
  120. {
  121. //mLog::FINFO("Service DevicePath is null try get from conf Again");
  122. if (m_resProperties.GetKeyCount("ServiceDevice") > 0 && m_resProperties["ServiceDevice"].GetKeyCount("Value") > 0)
  123. {
  124. m_strServiceDevicePath = (const char*)m_resProperties["ServiceDevice"]["Value"];
  125. //mLog::FINFO("Service DevicePath {$} is get from conf", m_strServiceDevicePath);
  126. }
  127. }
  128. else
  129. {
  130. //mLog::FINFO("Service DevicePath is {$}", m_strServiceDevicePath);
  131. if (m_resModuleConfig["Get"].GetKeyCount("ServiceDevice") > 0 && m_resModuleConfig["Get"]["ServiceDevice"].GetKeyCount("Value") > 0)
  132. {
  133. m_resModuleConfig["Get"]["ServiceDevice"].update("Value", m_strServiceDevicePath.c_str());
  134. m_resModuleConfig.SaveFile(m_strModuleFilePath.c_str());
  135. //这里特殊处理下,用服务设备的配置 覆盖 宿主配置,下次读取的时候,可以纠正回来
  136. string hostConfPath = GetCcosModuleConfigrateFilePath(m_strHostDevicePath,true);
  137. //mLog::FINFO("Save config to Host DevicePath {$} config {$}", m_strHostDevicePath, hostConfPath);
  138. m_resModuleConfig.SaveFile(hostConfPath.c_str());
  139. if (bFirstLoad)
  140. {
  141. remove(m_strConfigFilePath.c_str());
  142. m_resProperties = m_resModuleConfig["Get"];
  143. SaveToConfigFile();
  144. //mLog::FINFO("Service DevicePath {$} is get from conf Save to {$}", m_strServiceDevicePath, m_strModuleFilePath);
  145. }
  146. }
  147. }
  148. SaveToConfigFile();
  149. }
  150. RET_STATUS VirtualDevice::Request(ResDataObject PARAM_IN* pRequest, ResDataObject PARAM_OUT* pResponse)
  151. {
  152. ResDataObject resTopic;
  153. RET_STATUS ret = RET_ONGOING;
  154. PacketAnalizer::GetPacketTopic(pRequest, resTopic);
  155. PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
  156. PACKET_TYPE type = PacketAnalizer::GetPacketType(pRequest);
  157. string keystr = PacketAnalizer::GetPacketKey(pRequest);
  158. ResDataObject Context;
  159. if (PacketAnalizer::GetPacketContext(pRequest, Context) == false)
  160. {
  161. return RET_FAILED;
  162. }
  163. //mLog::FINFO("Got [{$}] REQ [{$}] context [{$}] from [{$}]", _Packet_Cmd_String[cmd], keystr, Context.size() <= 0 ? (const char*)Context : Context.encode(), (const char*)resTopic);
  164. if (type == PACKET_TYPE_NOTIFY)
  165. {
  166. ret = OnNotify(keystr, Context);
  167. //通知类,不需要答复
  168. return RET_SUCCEED;
  169. }
  170. else if (type == PACKET_TYPE_RES)
  171. {
  172. //收到了应答包。。。
  173. return RET_SUCCEED;
  174. }
  175. else return ModuleDevice::Request(pRequest, pResponse);
  176. }
  177. RET_STATUS VirtualDevice::OnUpdate(const char* pszProperty, const char* pszValueUpdate, ResDataObject& resRespons)
  178. {
  179. string strPRoperty = pszProperty;
  180. if (strPRoperty == "ServiceDevice")
  181. {
  182. m_strServiceDevicePath = pszValueUpdate;
  183. }
  184. else if (strPRoperty == "HostDevice")
  185. {
  186. m_strHostDevicePath = pszValueUpdate;
  187. }
  188. return RET_NOSUPPORT;
  189. }
  190. RET_STATUS VirtualDevice::SetItem(const char* pszPropery, ResDataObject& resSetValue, ResDataObject& resResponse)
  191. {
  192. return RET_ONGOING;
  193. }
  194. RET_STATUS VirtualDevice::GetItem(const char* pszProperty, ResDataObject& resResponse)
  195. {
  196. string strPRoperty = pszProperty;
  197. if (strPRoperty == "ServiceDevice")
  198. {
  199. resResponse = m_strServiceDevicePath.c_str();
  200. return RET_SUCCEED;
  201. }
  202. else if (strPRoperty == "HostDevice")
  203. {
  204. resResponse = m_strHostDevicePath.c_str();
  205. return RET_SUCCEED;
  206. }
  207. return RET_NOSUPPORT;
  208. }