ModuleClient.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "ModuleClient.h"
  2. #include "PacketAnalizer.h"
  3. #include "devGrpcClient.h"
  4. #include "LogLocalHelper.h"
  5. #include "Log4CPP.h"
  6. ModuleClient::ModuleClient(string szClientName, string szTransaction, string szType, bool bNeedNotify):
  7. LogicClient(szClientName, szTransaction, szType, bNeedNotify)
  8. {
  9. m_pGrpcClient = nullptr;
  10. }
  11. ModuleClient::~ModuleClient()
  12. {
  13. }
  14. RET_STATUS ModuleClient::ConnectGrpc(string strDevHost, string devPath)
  15. {
  16. if (strDevHost.length() <= 0)
  17. return RET_NOSUPPORT;
  18. int httpPort = atoi(strDevHost.c_str());
  19. if ( 0 != httpPort)
  20. {
  21. //纯数字,比如9051
  22. //grpc端口:-1
  23. httpPort--;
  24. strDevHost = "localhost:" + to_string(httpPort);
  25. cout << "Try connect " << strDevHost << endl;
  26. }
  27. if (m_pGrpcClient != nullptr)
  28. FreeClient(m_pGrpcClient);
  29. m_pGrpcClient = CreateClient(strDevHost.c_str());
  30. if (devPath.length() > 0)
  31. {
  32. FINFO("Test Open Device {$}", devPath);
  33. int ret = m_pGrpcClient->OpenDevice(devPath, "");
  34. FINFO("Open Dev Result {$} resource {$}", ret, ret == 2 ? m_pGrpcClient->GetOpendDeviceResource() : "Failed.");
  35. }
  36. //
  37. /*
  38. * channel is idle
  39. GRPC_CHANNEL_IDLE, 0
  40. channel is connecting
  41. GRPC_CHANNEL_CONNECTING, 1
  42. channel is ready for work
  43. GRPC_CHANNEL_READY, 2
  44. channel has seen a failure but expects to recover
  45. GRPC_CHANNEL_TRANSIENT_FAILURE, 3
  46. channel has seen a failure that it cannot recover from
  47. GRPC_CHANNEL_SHUTDOWN 4
  48. */
  49. int status = m_pGrpcClient->GetClientStatus();
  50. if (status <= 2)
  51. return RET_SUCCEED;
  52. return RET_FAILED;
  53. }
  54. RET_STATUS ModuleClient::ModuleReq(PACKET_CMD cmd, const char* pszResource, ResDataObject* pReqParam, ResDataObject& pResponse, const char* pszDevPath)
  55. {
  56. ResDataObject req;
  57. PacketAnalizer::MakeRequest(req, pszResource, cmd, pReqParam);
  58. string topic;
  59. if (pszDevPath == nullptr)
  60. {
  61. //默认设备路径
  62. topic = m_strDevicePath;
  63. }
  64. else
  65. {
  66. topic = pszDevPath;
  67. }
  68. topic += "/";
  69. topic += _Packet_Cmd_String[cmd] + "/";
  70. topic += pszResource;
  71. PublishAction(&req, topic.c_str(), m_pMqttConn);
  72. return RET_SUCCEED;
  73. }
  74. RET_STATUS ModuleClient::DevGet(const char* pszPropties, ResDataObject& pResponse, const char* pszDevPath)
  75. {
  76. ResDataObject param;
  77. if(m_pGrpcClient == nullptr)
  78. return ModuleReq(PACKET_CMD_GET, pszPropties, &param, pResponse, pszDevPath);
  79. string devRes, resMsg;
  80. RET_STATUS ret = (RET_STATUS)m_pGrpcClient->Get(pszPropties, devRes, resMsg, pszDevPath);
  81. if (ret == RET_SUCCEED)
  82. {
  83. pResponse.decode(devRes.c_str());
  84. return ret;
  85. }
  86. return RET_FAILED;
  87. }
  88. RET_STATUS ModuleClient::DevSet(const char* pszPropties, ResDataObject* pNewValue, ResDataObject& pResponse, const char* pszDevPath)
  89. {
  90. if (m_pGrpcClient == nullptr)
  91. return ModuleReq(PACKET_CMD_SET, pszPropties, pNewValue, pResponse, pszDevPath);
  92. string reqParam, devRes, resMsg;
  93. if (pNewValue->size() > 0)
  94. reqParam = pNewValue->encode();
  95. else
  96. reqParam = (const char*)(*pNewValue);
  97. RET_STATUS ret = (RET_STATUS)m_pGrpcClient->Add(pszPropties, reqParam, devRes, resMsg, pszDevPath);
  98. if (ret == RET_SUCCEED)
  99. {
  100. pResponse.decode(devRes.c_str());
  101. return ret;
  102. }
  103. return ret;
  104. }
  105. RET_STATUS ModuleClient::DevAdd(const char* pszPropties, ResDataObject* pAddValue, ResDataObject& pResponse, const char* pszDevPath)
  106. {
  107. if (m_pGrpcClient == nullptr)
  108. return ModuleReq(PACKET_CMD_ADD, pszPropties, pAddValue , pResponse, pszDevPath);
  109. string reqParam, devRes, resMsg;
  110. if (pAddValue->size() > 0)
  111. reqParam = pAddValue->encode();
  112. else
  113. reqParam = (const char*)(*pAddValue);
  114. RET_STATUS ret = (RET_STATUS)m_pGrpcClient->Add(pszPropties, reqParam, devRes, resMsg, pszDevPath);
  115. if (ret == RET_SUCCEED)
  116. {
  117. pResponse.decode(devRes.c_str());
  118. return ret;
  119. }
  120. return ret;
  121. }
  122. RET_STATUS ModuleClient::DevDel(const char* pszPropties, ResDataObject* pDelValue, ResDataObject& pResponse, const char* pszDevPath)
  123. {
  124. if (m_pGrpcClient == nullptr)
  125. return ModuleReq(PACKET_CMD_DEL, pszPropties,pDelValue , pResponse, pszDevPath);
  126. string reqParam, devRes, resMsg;
  127. if (pDelValue->size() > 0)
  128. reqParam = pDelValue->encode();
  129. else
  130. reqParam = (const char*)(*pDelValue);
  131. RET_STATUS ret = (RET_STATUS)m_pGrpcClient->Del(pszPropties, reqParam, devRes, resMsg, pszDevPath);
  132. if (ret == RET_SUCCEED)
  133. {
  134. pResponse.decode(devRes.c_str());
  135. return ret;
  136. }
  137. return ret;
  138. }
  139. RET_STATUS ModuleClient::DevAction(const char* pszActionName, ResDataObject* pReqParam, ResDataObject& pResponse, const char* pszDevPath)
  140. {
  141. if (m_pGrpcClient == nullptr)
  142. return (RET_STATUS)Action( pszActionName, *pReqParam , pResponse,11000, pszDevPath);
  143. string reqParam, devRes, resMsg;
  144. if (pReqParam->size() > 0)
  145. reqParam = pReqParam->encode();
  146. else
  147. reqParam = (const char*)(*pReqParam);
  148. RET_STATUS ret = (RET_STATUS)m_pGrpcClient->Action(pszActionName,reqParam, devRes, resMsg, pszDevPath);
  149. if (ret == RET_SUCCEED)
  150. {
  151. pResponse.decode(devRes.c_str());
  152. return ret;
  153. }
  154. return ret;
  155. }
  156. RET_STATUS ModuleClient::DevMessage(const char* pszTopics, ResDataObject* pMessage, ResDataObject& pResponse, const char* pszDevPath)
  157. {
  158. if (m_pGrpcClient == nullptr)
  159. return ModuleReq(PACKET_CMD_MSG, pszTopics, pMessage, pResponse, pszDevPath);
  160. string reqParam, devRes, resMsg;
  161. if (pMessage->size() > 0)
  162. reqParam = pMessage->encode();
  163. else
  164. reqParam = (const char*)(*pMessage);
  165. RET_STATUS ret = (RET_STATUS)m_pGrpcClient->Message(pszTopics, reqParam, devRes, resMsg, pszDevPath);
  166. if (ret == RET_SUCCEED)
  167. {
  168. pResponse.decode(devRes.c_str());
  169. return ret;
  170. }
  171. return ret;
  172. }
  173. int ModuleClient::BeginAyncWait() {
  174. try
  175. {
  176. m_nWaitResultCount = 0;
  177. if (m_pGrpcClient != nullptr)
  178. return m_pGrpcClient->BeginAyncWait();
  179. }
  180. catch (...)
  181. {
  182. FWARN("Exception occurred ");
  183. }
  184. return 0;
  185. }
  186. void ModuleClient::WaitAllComplete()
  187. {
  188. try
  189. {
  190. if (m_pGrpcClient != nullptr)
  191. m_pGrpcClient->WaitAllComplete();
  192. }
  193. catch (...)
  194. {
  195. FWARN("Exception occurred ");
  196. }
  197. }
  198. int ModuleClient::GetAsyncResult(int idx, string& devResource, string& devRes, string& calResMsg)
  199. {
  200. try
  201. {
  202. if (m_pGrpcClient != nullptr)
  203. return m_pGrpcClient->GetAsyncResult(idx, devResource, devRes, calResMsg);
  204. }
  205. catch (...)
  206. {
  207. FWARN("Exception occurred ");
  208. }
  209. return 0;
  210. }
  211. void ModuleClient::EndAync()
  212. {
  213. try
  214. {
  215. if (m_pGrpcClient != nullptr)
  216. m_pGrpcClient->EndAync();
  217. }
  218. catch (...)
  219. {
  220. FWARN("Exception occurred ");
  221. }
  222. }
  223. int ModuleClient::AsyncAction(string devResource, ResDataObject* pReqParam,
  224. string devUri, string calSection, string calClientID)
  225. {
  226. try
  227. {
  228. string reqParam;
  229. if (pReqParam->size() > 0)
  230. reqParam = pReqParam->encode();
  231. else
  232. reqParam = (const char*)(*pReqParam);
  233. if (m_pGrpcClient != nullptr)
  234. return m_nWaitResultCount= m_pGrpcClient->AsyncAction(devResource, reqParam, devUri, calSection, calClientID);
  235. }
  236. catch (...)
  237. {
  238. FWARN("Exception occurred ");
  239. }
  240. return 0;
  241. }
  242. int ModuleClient::AsyncMessage(string devResource, ResDataObject* pReqParam,
  243. string devUri, string calSection, string calClientID)
  244. {
  245. try
  246. {
  247. string reqParam;
  248. if (pReqParam->size() > 0)
  249. reqParam = pReqParam->encode();
  250. else
  251. reqParam = (const char*)(*pReqParam);
  252. if (m_pGrpcClient != nullptr)
  253. return m_nWaitResultCount = m_pGrpcClient->AsyncMessage(devResource, reqParam, devUri, calSection, calClientID);
  254. }
  255. catch (...)
  256. {
  257. FWARN("Exception occurred ");
  258. }
  259. return 0;
  260. }
  261. int ModuleClient::ClearFilter()
  262. {
  263. if (!Lock())
  264. return 0;
  265. m_mpNotifyIdx.clear();
  266. UnLock();
  267. return 2;
  268. }
  269. int ModuleClient::InstallFilterNotify(string strKey, bool install)
  270. {
  271. string topic = m_strDevicePath + "/Notify/" + strKey;
  272. SubScribeTopic(topic.c_str(), install);
  273. //CCOS/DEVICE/Detector/ecomdemo/demo/1234 /Notify/DetectorStatus
  274. std::vector<string> nods;
  275. SplitCcosDevicePath(m_strDevicePath, nods);
  276. if (nods.size() > 5 && m_strWS.length() > 0)
  277. {
  278. //CCOS/Table/Detector /Notify/DetectorStatus
  279. topic = "CCOS/";
  280. topic += m_strWS + "/" + nods[2] + "/Notify/" + strKey;
  281. SubScribeTopic(topic.c_str(), install);
  282. }
  283. return 2;
  284. }
  285. PACKET_CMD ModuleClient::ReadFilterNotify(ResDataObject& CmdObject)
  286. {
  287. PACKET_CMD cmd = PACKET_CMD_NONE;
  288. //ResDataObject resPacket;
  289. cmd = ReadNotify(CmdObject);
  290. if (cmd != PACKET_CMD_NONE )
  291. {
  292. PACKET_TYPE type = PacketAnalizer::GetPacketType(&CmdObject);
  293. if (type == PACKET_TYPE_NOTIFY)
  294. {
  295. DWORD idx = PacketAnalizer::GetPacketIdx(&CmdObject);
  296. string key = PacketAnalizer::GetPacketKey(&CmdObject);
  297. if (Lock())
  298. {
  299. auto itf = m_mpNotifyIdx.find(key);
  300. if (itf != m_mpNotifyIdx.end())
  301. {
  302. if (itf->second != idx)
  303. {
  304. // idx不同
  305. m_mpNotifyIdx[key] = idx;
  306. UnLock();
  307. return cmd;
  308. }
  309. else
  310. {
  311. UnLock();
  312. FDEBUG("Same Packet no process.. {$} with idx:[{$}]",key, idx);
  313. return PACKET_CMD_NONE;
  314. }
  315. }
  316. else
  317. {
  318. //首次抵达
  319. m_mpNotifyIdx[key] = idx;
  320. UnLock();
  321. return cmd;
  322. }
  323. }
  324. else
  325. {
  326. FERROR("Lock timeout for packet {$}", CmdObject.encode());
  327. }
  328. }
  329. }
  330. return PACKET_CMD_NONE;
  331. }
  332. int ModuleClient::Notify(const char* pszTopic, const char* pszSender, const char* pszType, const char* pszCmd, const char* pMsg)
  333. {
  334. ResDataObject req,resMsg;
  335. resMsg.add("DevUri", pszSender);
  336. resMsg.add("Type", pszType);
  337. resMsg.add("Message", pMsg);
  338. PacketAnalizer::MakeNotify(req, PACKET_CMD_MSG, pszCmd, "");
  339. PacketAnalizer::UpdatePacketContext(req, resMsg);
  340. PacketAnalizer::UpdatePacketTopic(&req, pszTopic, pszSender );
  341. return PublishAction(&req, pszTopic, m_pMqttConn);
  342. }
  343. void ModuleClient::SplitCcosDevicePath(string DevicePath, vector<string>& resTopicParams)
  344. {
  345. // 使用 stringstream 和 getline 按照 "/" 切割路径
  346. std::stringstream ss(DevicePath);
  347. std::string token;
  348. // 清空之前的数据
  349. resTopicParams.clear();
  350. // 按照"/"切割路径并存入resTopicParams
  351. while (std::getline(ss, token, '/')) {
  352. resTopicParams.push_back(token);
  353. }
  354. }