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.

54 lines
1.3 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. #include "espurna.h"
  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. using StatusBroker = TBroker<TBrokerType::Status, const String&, unsigned char, unsigned int>;
  37. using SensorReadBroker = TBroker<TBrokerType::SensorRead, const String&, unsigned char, double, const char*>;
  38. using SensorReportBroker = TBroker<TBrokerType::SensorReport, const String&, unsigned char, double, const char*>;
  39. using ConfigBroker = TBroker<TBrokerType::Config, const String&, const String&>;