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.

71 lines
2.3 KiB

  1. /*
  2. WEBSOCKET MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #include "espurna.h"
  7. #include <ArduinoJson.h>
  8. #include <queue>
  9. #include <functional>
  10. #include <vector>
  11. #include "web.h"
  12. #include "utils.h"
  13. using ws_on_send_callback_f = std::function<void(JsonObject& root)>;
  14. using ws_on_action_callback_f = std::function<void(uint32_t client_id, const char * action, JsonObject& data)>;
  15. using ws_on_keycheck_callback_f = std::function<bool(const char * key, JsonVariant& value)>;
  16. using ws_on_send_callback_list_t = std::vector<ws_on_send_callback_f>;
  17. using ws_on_action_callback_list_t = std::vector<ws_on_action_callback_f>;
  18. using ws_on_keycheck_callback_list_t = std::vector<ws_on_keycheck_callback_f>;
  19. struct ws_callbacks_t {
  20. ws_on_send_callback_list_t on_visible;
  21. ws_on_send_callback_list_t on_connected;
  22. ws_on_send_callback_list_t on_data;
  23. ws_on_action_callback_list_t on_action;
  24. ws_on_keycheck_callback_list_t on_keycheck;
  25. ws_callbacks_t& onVisible(ws_on_send_callback_f);
  26. ws_callbacks_t& onConnected(ws_on_send_callback_f);
  27. ws_callbacks_t& onData(ws_on_send_callback_f);
  28. ws_callbacks_t& onAction(ws_on_action_callback_f);
  29. ws_callbacks_t& onKeyCheck(ws_on_keycheck_callback_f);
  30. };
  31. ws_callbacks_t& wsRegister();
  32. void wsSetup();
  33. void wsSend(uint32_t client_id, const char* data);
  34. void wsSend(uint32_t client_id, JsonObject& root);
  35. void wsSend(JsonObject& root);
  36. void wsSend(ws_on_send_callback_f callback);
  37. void wsSend(const char* data);
  38. void wsSend_P(PGM_P data);
  39. void wsSend_P(uint32_t client_id, PGM_P data);
  40. void wsPost(const ws_on_send_callback_f& callback);
  41. void wsPost(uint32_t client_id, const ws_on_send_callback_f& callback);
  42. void wsPost(const ws_on_send_callback_list_t& callbacks);
  43. void wsPost(uint32_t client_id, const ws_on_send_callback_list_t& callbacks);
  44. void wsPostAll(uint32_t client_id, const ws_on_send_callback_list_t& callbacks);
  45. void wsPostAll(const ws_on_send_callback_list_t& callbacks);
  46. void wsPostSequence(uint32_t client_id, const ws_on_send_callback_list_t& callbacks);
  47. void wsPostSequence(uint32_t client_id, ws_on_send_callback_list_t&& callbacks);
  48. void wsPostSequence(const ws_on_send_callback_list_t& callbacks);
  49. bool wsConnected();
  50. bool wsConnected(uint32_t client_id);
  51. bool wsDebugSend(const char* prefix, const char* message);