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.

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