SinricPro Library
PowerLevelController.h
1 #ifndef _POWERLEVELCONTROLLER_H_
2 #define _POWERLEVELCONTROLLER_H_
3 
4 #include "./SinricProRequest.h"
5 
10 template <typename T>
12  public:
13  PowerLevelController() { static_cast<T &>(*this).requestHandlers.push_back(std::bind(&PowerLevelController<T>::handlePowerLevelController, this, std::placeholders::_1)); }
28  using SetPowerLevelCallback = std::function<bool(const String &, int &)>;
29 
44  using AdjustPowerLevelCallback = std::function<bool(const String &, int &)>;
45 
48  bool sendPowerLevelEvent(int powerLevel, String cause = "PHYSICAL_INTERACTION");
49 
50  protected:
51  bool handlePowerLevelController(SinricProRequest &request);
52 
53  private:
54  SetPowerLevelCallback setPowerLevelCallback;
55  AdjustPowerLevelCallback adjustPowerLevelCallback;
56 };
57 
64 template <typename T>
66  setPowerLevelCallback = cb;
67 }
68 
75 template <typename T>
77 {
78  adjustPowerLevelCallback = cb;
79 }
80 
90 template <typename T>
91 bool PowerLevelController<T>::sendPowerLevelEvent(int powerLevel, String cause)
92 {
93  T& device = static_cast<T&>(*this);
94 
95  DynamicJsonDocument eventMessage = device.prepareEvent("setPowerLevel", cause.c_str());
96  JsonObject event_value = eventMessage["payload"]["value"];
97  event_value["powerLevel"] = powerLevel;
98  return device.sendEvent(eventMessage);
99 }
100 
101 template <typename T>
102 bool PowerLevelController<T>::handlePowerLevelController(SinricProRequest &request) {
103  T &device = static_cast<T &>(*this);
104 
105  bool success = false;
106 
107  if (setPowerLevelCallback && request.action == "setPowerLevel") {
108  int powerLevel = request.request_value["powerLevel"];
109  success = setPowerLevelCallback(device.deviceId, powerLevel);
110  request.response_value["powerLevel"] = powerLevel;
111  }
112 
113  if (adjustPowerLevelCallback && request.action == "adjustPowerLevel") {
114  int powerLevelDelta = request.request_value["powerLevelDelta"];
115  success = adjustPowerLevelCallback(device.deviceId, powerLevelDelta);
116  request.response_value["powerLevel"] = powerLevelDelta;
117  }
118  return success;
119 }
120 
121 #endif
PowerLevelController.
Definition: PowerLevelController.h:11
void onPowerLevel(SetPowerLevelCallback cb)
Set callback function for setPowerLevel request.
Definition: PowerLevelController.h:65
void onAdjustPowerLevel(AdjustPowerLevelCallback cb)
Set callback function for adjustPowerLevel request.
Definition: PowerLevelController.h:76
std::function< bool(const String &, int &)> AdjustPowerLevelCallback
Definition for onAdjustPowerLevel callback.
Definition: PowerLevelController.h:44
std::function< bool(const String &, int &)> SetPowerLevelCallback
Definition for setPowerLevel callback.
Definition: PowerLevelController.h:28
bool sendPowerLevelEvent(int powerLevel, String cause="PHYSICAL_INTERACTION")
Send setPowerLevel event to SinricPro Server indicating actual power level.
Definition: PowerLevelController.h:91