acq_task.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "acq_task.h"
  2. namespace iot_acq {
  3. #define TASK_MAX_TIMES (13)
  4. std::vector<int32_t> PRIME_NUMBER = {
  5. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 720};
  6. bool AcqTask::IsInRetry(int32_t times)
  7. {
  8. if (std::find(PRIME_NUMBER.begin(), PRIME_NUMBER.end(), times) !=
  9. PRIME_NUMBER.end()) {
  10. return true;
  11. }
  12. if (times % 720 == 0) {
  13. return true;
  14. }
  15. return false;
  16. }
  17. void AcqTask::OnTimerTask()
  18. {
  19. for (auto &pair : mapRpc_) {
  20. std::shared_ptr<leoyun::YunRpc> rpc = pair.second;
  21. if (rpc == nullptr) {
  22. HTELINK_LOG_INFO("sth error about %s", pair.first.c_str());
  23. continue;
  24. }
  25. if (!rpc->IsRun() && !IsInRetry(rpc->Times()++)) {
  26. continue;
  27. }
  28. rpc->Run();
  29. if (rpc->IsRun()) {
  30. rpc->Times() = 0;
  31. }
  32. }
  33. }
  34. AcqTask::AcqTask()
  35. : vendor::VendorIf([&]() { OnTimerTask(); })
  36. {
  37. }
  38. void AcqTask::PushRpc(const std::string& name,
  39. std::shared_ptr<leoyun::YunRpc> rpc) {
  40. mapRpc_.insert(
  41. std::pair<std::string, std::shared_ptr<leoyun::YunRpc> >(name, rpc));
  42. }
  43. void AcqTask::ForEach(
  44. const std::function<void(const std::shared_ptr<leoyun::YunRpc> &)> &rpcCall)
  45. {
  46. for (auto &pair : mapRpc_) {
  47. std::shared_ptr<leoyun::YunRpc> rpc = pair.second;
  48. if (rpc == nullptr) {
  49. HTELINK_LOG_INFO("sth error about %s", pair.first.c_str());
  50. continue;
  51. }
  52. rpcCall(rpc);
  53. }
  54. }
  55. } // namespace iot_acq