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
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 <ArduinoJson.h>
  7. #include <queue>
  8. #include <functional>
  9. #include <vector>
  10. #include "utils.h"
  11. using ws_on_send_callback_f = std::function<void(JsonObject& root)>;
  12. using ws_on_action_callback_f = std::function<void(uint32_t client_id, const char * action, JsonObject& data)>;
  13. using ws_on_keycheck_callback_f = std::function<bool(const char * key, JsonVariant& value)>;
  14. using ws_on_send_callback_list_t = std::vector<ws_on_send_callback_f>;
  15. using ws_on_action_callback_list_t = std::vector<ws_on_action_callback_f>;
  16. using ws_on_keycheck_callback_list_t = std::vector<ws_on_keycheck_callback_f>;
  17. struct ws_callbacks_t {
  18. ws_on_send_callback_list_t on_visible;
  19. ws_on_send_callback_list_t on_connected;
  20. ws_on_send_callback_list_t on_data;
  21. ws_on_action_callback_list_t on_action;
  22. ws_on_keycheck_callback_list_t on_keycheck;
  23. ws_callbacks_t& onVisible(ws_on_send_callback_f);
  24. ws_callbacks_t& onConnected(ws_on_send_callback_f);
  25. ws_callbacks_t& onData(ws_on_send_callback_f);
  26. ws_callbacks_t& onAction(ws_on_action_callback_f);
  27. ws_callbacks_t& onKeyCheck(ws_on_keycheck_callback_f);
  28. };
  29. ws_callbacks_t& wsRegister();
  30. void wsSetup();
  31. void wsSend(uint32_t client_id, const char* data);
  32. void wsSend(uint32_t client_id, JsonObject& root);
  33. void wsSend(JsonObject& root);
  34. void wsSend(ws_on_send_callback_f callback);
  35. void wsSend_P(PGM_P data);
  36. void wsSend_P(uint32_t client_id, PGM_P data);
  37. void INLINE wsPost(const ws_on_send_callback_f& callback);
  38. void INLINE wsPost(uint32_t client_id, const ws_on_send_callback_f& callback);
  39. void INLINE wsPost(const ws_on_send_callback_list_t& callbacks);
  40. void INLINE wsPost(uint32_t client_id, const ws_on_send_callback_list_t& callbacks);
  41. void INLINE wsPostAll(uint32_t client_id, const ws_on_send_callback_list_t& callbacks);
  42. void INLINE wsPostAll(const ws_on_send_callback_list_t& callbacks);
  43. void INLINE wsPostSequence(uint32_t client_id, const ws_on_send_callback_list_t& callbacks);
  44. void INLINE wsPostSequence(uint32_t client_id, ws_on_send_callback_list_t&& callbacks);
  45. void INLINE wsPostSequence(const ws_on_send_callback_list_t& callbacks);
  46. bool INLINE wsConnected();
  47. bool INLINE wsConnected(uint32_t client_id);
  48. bool wsDebugSend(const char* prefix, const char* message);