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. #if BROKER_SUPPORT
  6. #pragma once
  7. #include <functional>
  8. #include <vector>
  9. #include <utility>
  10. enum class TBrokerType {
  11. SYSTEM,
  12. STATUS,
  13. SENSOR_READ,
  14. SENSOR_REPORT,
  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::SENSOR_READ, const String&, unsigned char, double, const char*>;
  39. using SensorReportBroker = TBroker<TBrokerType::SENSOR_REPORT, const String&, unsigned char, double, const char*>;
  40. using ConfigBroker = TBroker<TBrokerType::CONFIG, const String&, const String&>;
  41. #endif // BROKER_SUPPORT