VirtualDevice.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. ModuleDevice::OnSetClientID();
  93. if (m_strHostDevicePath.length() <= 0)
  94. {
  95. //mLog::FINFO("Host DevicePath is null try get from conf Again");
  96. if (m_resProperties.GetKeyCount("HostDevice") > 0 && m_resProperties["HostDevice"].GetKeyCount("Value") > 0)
  97. {
  98. m_strHostDevicePath = (const char*)m_resProperties["HostDevice"]["Value"];
  99. //mLog::FINFO("Host DevicePath {$} is get from conf", m_strHostDevicePath);
  100. }
  101. }
  102. else
  103. {
  104. //mLog::FINFO("Host DevicePath is {$}", m_strServiceDevicePath);
  105. if (m_resModuleConfig["Get"].GetKeyCount("HostDevice") > 0 && m_resModuleConfig["Get"]["HostDevice"].GetKeyCount("Value") > 0)
  106. {
  107. m_resModuleConfig["Get"]["HostDevice"].update("Value", m_strHostDevicePath.c_str());
  108. m_resModuleConfig.SaveFile(m_strModuleFilePath.c_str());
  109. if (bFirstLoad)
  110. {
  111. remove(m_strConfigFilePath.c_str());
  112. m_resProperties = m_resModuleConfig["Get"];
  113. SaveToConfigFile();
  114. }
  115. //mLog::FINFO("Host DevicePath {$} is get from conf Save to {$}", m_strHostDevicePath, m_strModuleFilePath);
  116. }
  117. }
  118. if (m_strServiceDevicePath.length() <= 0)
  119. {
  120. //mLog::FINFO("Service DevicePath is null try get from conf Again");
  121. if (m_resProperties.GetKeyCount("ServiceDevice") > 0 && m_resProperties["ServiceDevice"].GetKeyCount("Value") > 0)
  122. {
  123. m_strServiceDevicePath = (const char*)m_resProperties["ServiceDevice"]["Value"];
  124. //mLog::FINFO("Service DevicePath {$} is get from conf", m_strServiceDevicePath);
  125. }
  126. }
  127. else
  128. {
  129. //mLog::FINFO("Service DevicePath is {$}", m_strServiceDevicePath);
  130. if (m_resModuleConfig["Get"].GetKeyCount("ServiceDevice") > 0 && m_resModuleConfig["Get"]["ServiceDevice"].GetKeyCount("Value") > 0)
  131. {
  132. m_resModuleConfig["Get"]["ServiceDevice"].update("Value", m_strServiceDevicePath.c_str());
  133. m_resModuleConfig.SaveFile(m_strModuleFilePath.c_str());
  134. //这里特殊处理下,用服务设备的配置 覆盖 宿主配置,下次读取的时候,可以纠正回来
  135. string hostConfPath = GetCcosModuleConfigrateFilePath(m_strHostDevicePath,true);
  136. //mLog::FINFO("Save config to Host DevicePath {$} config {$}", m_strHostDevicePath, hostConfPath);
  137. m_resModuleConfig.SaveFile(hostConfPath.c_str());
  138. if (bFirstLoad)
  139. {
  140. remove(m_strConfigFilePath.c_str());
  141. m_resProperties = m_resModuleConfig["Get"];
  142. SaveToConfigFile();
  143. //mLog::FINFO("Service DevicePath {$} is get from conf Save to {$}", m_strServiceDevicePath, m_strModuleFilePath);
  144. }
  145. }
  146. }
  147. SaveToConfigFile();
  148. }
  149. RET_STATUS VirtualDevice::Request(ResDataObject PARAM_IN* pRequest, ResDataObject PARAM_OUT* pResponse)
  150. {
  151. ResDataObject resTopic;
  152. RET_STATUS ret = RET_ONGOING;
  153. PacketAnalizer::GetPacketTopic(pRequest, resTopic);
  154. PACKET_CMD cmd = PacketAnalizer::GetPacketCmd(pRequest);
  155. PACKET_TYPE type = PacketAnalizer::GetPacketType(pRequest);
  156. string keystr = PacketAnalizer::GetPacketKey(pRequest);
  157. ResDataObject Context;
  158. if (PacketAnalizer::GetPacketContext(pRequest, Context) == false)
  159. {
  160. return RET_FAILED;
  161. }
  162. //mLog::FINFO("Got [{$}] REQ [{$}] context [{$}] from [{$}]", _Packet_Cmd_String[cmd], keystr, Context.size() <= 0 ? (const char*)Context : Context.encode(), (const char*)resTopic);
  163. if (type == PACKET_TYPE_NOTIFY)
  164. {
  165. ret = OnNotify(keystr, Context);
  166. //通知类,不需要答复
  167. return RET_SUCCEED;
  168. }
  169. else if (type == PACKET_TYPE_RES)
  170. {
  171. //收到了应答包。。。
  172. return RET_SUCCEED;
  173. }
  174. else return ModuleDevice::Request(pRequest, pResponse);
  175. }
  176. RET_STATUS VirtualDevice::OnUpdate(const char* pszProperty, const char* pszValueUpdate, ResDataObject& resRespons)
  177. {
  178. string strPRoperty = pszProperty;
  179. if (strPRoperty == "ServiceDevice")
  180. {
  181. m_strServiceDevicePath = pszValueUpdate;
  182. }
  183. else if (strPRoperty == "HostDevice")
  184. {
  185. m_strHostDevicePath = pszValueUpdate;
  186. }
  187. return RET_NOSUPPORT;
  188. }
  189. RET_STATUS VirtualDevice::SetItem(const char* pszPropery, ResDataObject& resSetValue, ResDataObject& resResponse)
  190. {
  191. return RET_ONGOING;
  192. }
  193. RET_STATUS VirtualDevice::GetItem(const char* pszProperty, ResDataObject& resResponse)
  194. {
  195. string strPRoperty = pszProperty;
  196. if (strPRoperty == "ServiceDevice")
  197. {
  198. resResponse = m_strServiceDevicePath.c_str();
  199. return RET_SUCCEED;
  200. }
  201. else if (strPRoperty == "HostDevice")
  202. {
  203. resResponse = m_strHostDevicePath.c_str();
  204. return RET_SUCCEED;
  205. }
  206. return RET_NOSUPPORT;
  207. }