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.

56 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,
  14. DATETIME,
  15. CONFIG
  16. };
  17. template <typename... TArgs>
  18. using TBrokerCallback = std::function<void(TArgs...)>;
  19. template <typename... TArgs>
  20. using TBrokerCallbacks = std::vector<TBrokerCallback<TArgs...>>;
  21. template <TBrokerType type, typename... TArgs>
  22. struct TBroker {
  23. static TBrokerCallbacks<TArgs...> callbacks;
  24. static void Register(TBrokerCallback<TArgs...> callback) {
  25. callbacks.push_back(callback);
  26. }
  27. static void Publish(TArgs... args) {
  28. for (auto& callback : callbacks) {
  29. callback(args...);
  30. }
  31. }
  32. };
  33. template <TBrokerType type, typename... TArgs>
  34. TBrokerCallbacks<TArgs...> TBroker<type, TArgs...>::callbacks;
  35. // --- Some known types. Bind them here to avoid .ino screwing with order ---
  36. using StatusBroker = TBroker<TBrokerType::STATUS, const String&, unsigned char, unsigned int>;
  37. using SensorBroker = TBroker<TBrokerType::SENSOR, const String&, unsigned char, double, const char*>;
  38. using TimeBroker = TBroker<TBrokerType::DATETIME, const String&, time_t, const String&>;
  39. using ConfigBroker = TBroker<TBrokerType::CONFIG, const String&, const String&>;
  40. #endif // BROKER_SUPPORT