Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.7 KiB

  1. /*
  2. Part of MQTT and API modules
  3. Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  4. */
  5. #include "rpc.h"
  6. #include <Schedule.h>
  7. #include <cstring>
  8. #include "system.h"
  9. #include "utils.h"
  10. bool rpcHandleAction(const String& action) {
  11. bool result = false;
  12. if (action.equals("reboot")) {
  13. result = true;
  14. schedule_function([]() {
  15. deferredReset(100, CustomResetReason::Rpc);
  16. });
  17. } else if (action.equals("heartbeat")) {
  18. result = true;
  19. systemScheduleHeartbeat();
  20. }
  21. return result;
  22. }
  23. PayloadStatus rpcParsePayload(const char* payload, const rpc_payload_check_t ext_check) {
  24. // Don't parse empty strings
  25. const auto len = strlen(payload);
  26. if (!len) return PayloadStatus::Unknown;
  27. // Check most commonly used payloads
  28. if (len == 1) {
  29. if (payload[0] == '0') return PayloadStatus::Off;
  30. if (payload[0] == '1') return PayloadStatus::On;
  31. if (payload[0] == '2') return PayloadStatus::Toggle;
  32. return PayloadStatus::Unknown;
  33. }
  34. // If possible, use externally provided payload checker
  35. if (ext_check) {
  36. const PayloadStatus result = ext_check(payload);
  37. if (result != PayloadStatus::Unknown) {
  38. return result;
  39. }
  40. }
  41. // Finally, check for "OFF", "ON", "TOGGLE" (both lower and upper cases)
  42. String temp(payload);
  43. temp.trim();
  44. if (temp.equalsIgnoreCase("off")) {
  45. return PayloadStatus::Off;
  46. } else if (temp.equalsIgnoreCase("on")) {
  47. return PayloadStatus::On;
  48. } else if (temp.equalsIgnoreCase("toggle")) {
  49. return PayloadStatus::Toggle;
  50. }
  51. return PayloadStatus::Unknown;
  52. }