RvcAppCommandDelegate.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. *
  3. * Copyright (c) 2023 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "RvcAppCommandDelegate.h"
  19. #include <platform/PlatformManager.h>
  20. #include "rvc-device.h"
  21. #include <utility>
  22. using namespace chip;
  23. using namespace chip::app::Clusters;
  24. RvcAppCommandHandler * RvcAppCommandHandler::FromJSON(const char * json)
  25. {
  26. Json::Reader reader;
  27. Json::Value value;
  28. if (!reader.parse(json, value))
  29. {
  30. ChipLogError(NotSpecified, "RVC App: Error parsing JSON with error %s:", reader.getFormattedErrorMessages().c_str());
  31. return nullptr;
  32. }
  33. if (value.empty() || !value.isObject())
  34. {
  35. ChipLogError(NotSpecified, "RVC App: Invalid JSON command received");
  36. return nullptr;
  37. }
  38. if (!value.isMember("Name") || !value["Name"].isString())
  39. {
  40. ChipLogError(NotSpecified, "RVC App: Invalid JSON command received: command name is missing");
  41. return nullptr;
  42. }
  43. return Platform::New<RvcAppCommandHandler>(std::move(value));
  44. }
  45. void RvcAppCommandHandler::HandleCommand(intptr_t context)
  46. {
  47. auto * self = reinterpret_cast<RvcAppCommandHandler *>(context);
  48. std::string name = self->mJsonValue["Name"].asString();
  49. VerifyOrExit(!self->mJsonValue.empty(), ChipLogError(NotSpecified, "Invalid JSON event command received"));
  50. if (name == "Charged")
  51. {
  52. self->OnChargedHandler();
  53. }
  54. else if (name == "Charging")
  55. {
  56. self->OnChargingHandler();
  57. }
  58. else if (name == "Docked")
  59. {
  60. self->OnDockedHandler();
  61. }
  62. else if (name == "ChargerFound")
  63. {
  64. self->OnChargerFoundHandler();
  65. }
  66. else if (name == "LowCharge")
  67. {
  68. self->OnLowChargeHandler();
  69. }
  70. else if (name == "ActivityComplete")
  71. {
  72. self->OnActivityCompleteHandler();
  73. }
  74. else if (name == "ErrorEvent")
  75. {
  76. std::string error = self->mJsonValue["Error"].asString();
  77. self->OnErrorEventHandler(error);
  78. }
  79. else if (name == "ClearError")
  80. {
  81. self->OnClearErrorHandler();
  82. }
  83. else
  84. {
  85. ChipLogError(NotSpecified, "Unhandled command: Should never happens");
  86. }
  87. exit:
  88. Platform::Delete(self);
  89. }
  90. void RvcAppCommandHandler::SetRvcDevice(chip::app::Clusters::RvcDevice * aRvcDevice)
  91. {
  92. mRvcDevice = aRvcDevice;
  93. }
  94. void RvcAppCommandHandler::OnChargedHandler()
  95. {
  96. mRvcDevice->HandleChargedMessage();
  97. }
  98. void RvcAppCommandHandler::OnChargingHandler()
  99. {
  100. mRvcDevice->HandleChargingMessage();
  101. }
  102. void RvcAppCommandHandler::OnDockedHandler()
  103. {
  104. mRvcDevice->HandleDockedMessage();
  105. }
  106. void RvcAppCommandHandler::OnChargerFoundHandler()
  107. {
  108. mRvcDevice->HandleChargerFoundMessage();
  109. }
  110. void RvcAppCommandHandler::OnLowChargeHandler()
  111. {
  112. mRvcDevice->HandleLowChargeMessage();
  113. }
  114. void RvcAppCommandHandler::OnActivityCompleteHandler()
  115. {
  116. mRvcDevice->HandleActivityCompleteEvent();
  117. }
  118. void RvcAppCommandHandler::OnErrorEventHandler(const std::string & error)
  119. {
  120. mRvcDevice->HandleErrorEvent(error);
  121. }
  122. void RvcAppCommandHandler::OnClearErrorHandler()
  123. {
  124. mRvcDevice->HandleClearErrorMessage();
  125. }
  126. void RvcAppCommandDelegate::SetRvcDevice(chip::app::Clusters::RvcDevice * aRvcDevice)
  127. {
  128. mRvcDevice = aRvcDevice;
  129. }
  130. void RvcAppCommandDelegate::OnEventCommandReceived(const char * json)
  131. {
  132. auto handler = RvcAppCommandHandler::FromJSON(json);
  133. if (nullptr == handler)
  134. {
  135. ChipLogError(NotSpecified, "RVC App: Unable to instantiate a command handler");
  136. return;
  137. }
  138. handler->SetRvcDevice(mRvcDevice);
  139. chip::DeviceLayer::PlatformMgr().ScheduleWork(RvcAppCommandHandler::HandleCommand, reinterpret_cast<intptr_t>(handler));
  140. }