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.

59 lines
1.4 KiB

  1. /*
  2. BROKER MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #if BROKER_SUPPORT
  7. #include <functional>
  8. #include <vector>
  9. #include <utility>
  10. enum class TBrokerType {
  11. System,
  12. Status,
  13. SensorRead,
  14. SensorReport,
  15. Datetime,
  16. Config
  17. };
  18. template <typename... TArgs>
  19. using TBrokerCallback = std::function<void(TArgs...)>;
  20. template <typename... TArgs>
  21. using TBrokerCallbacks = std::vector<TBrokerCallback<TArgs...>>;
  22. template <TBrokerType type, typename... TArgs>
  23. struct TBroker {
  24. static TBrokerCallbacks<TArgs...> callbacks;
  25. static void Register(TBrokerCallback<TArgs...> callback) {
  26. callbacks.push_back(callback);
  27. }
  28. static void Publish(TArgs... args) {
  29. for (auto& callback : callbacks) {
  30. callback(args...);
  31. }
  32. }
  33. };
  34. template <TBrokerType type, typename... TArgs>
  35. TBrokerCallbacks<TArgs...> TBroker<type, TArgs...>::callbacks;
  36. // --- Some known types. Bind them here to avoid .ino screwing with order ---
  37. using StatusBroker = TBroker<TBrokerType::Status, const String&, unsigned char, unsigned int>;
  38. using SensorReadBroker = TBroker<TBrokerType::SensorRead, const String&, unsigned char, double, const char*>;
  39. using SensorReportBroker = TBroker<TBrokerType::SensorReport, const String&, unsigned char, double, const char*>;
  40. using ConfigBroker = TBroker<TBrokerType::Config, const String&, const String&>;
  41. #endif // BROKER_SUPPORT == 1