VirtualDevice.cpp 7.0 KB

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