SinricPro Library
MediaController.h
1 #ifndef _MEDIACONTROLLER_H_
2 #define _MEDIACONTROLLER_H_
3 
4 #include "SinricProRequest.h"
5 
10 template <typename T>
12  public:
13  MediaController() { static_cast<T &>(*this).requestHandlers.push_back(std::bind(&MediaController<T>::handleMediaController, this, std::placeholders::_1)); }
28  using MediaControlCallback = std::function<bool(const String &, String &)>;
29 
31  bool sendMediaControlEvent(String mediaControl, String cause = "PHYSICAL_INTERACTION");
32 
33  protected:
34  bool handleMediaController(SinricProRequest &request);
35 
36  private:
37  MediaControlCallback mediaControlCallback;
38 };
39 
47 template <typename T>
49  mediaControlCallback = cb;
50 }
51 
61 template <typename T>
62 bool MediaController<T>::sendMediaControlEvent(String mediaControl, String cause) {
63  T& device = static_cast<T&>(*this);
64 
65  DynamicJsonDocument eventMessage = device.prepareEvent("mediaControl", cause.c_str());
66  JsonObject event_value = eventMessage["payload"]["value"];
67  event_value["control"] = mediaControl;
68  return device.sendEvent(eventMessage);
69 }
70 
71 template <typename T>
72 bool MediaController<T>::handleMediaController(SinricProRequest &request) {
73  T &device = static_cast<T &>(*this);
74 
75  bool success = false;
76 
77  if (mediaControlCallback && request.action == "mediaControl") {
78  String mediaControl = request.request_value["control"];
79  success = mediaControlCallback(device.deviceId, mediaControl);
80  request.response_value["control"] = mediaControl;
81  return success;
82  }
83 
84  return success;
85 }
86 
87 #endif
MediaController.
Definition: MediaController.h:11
std::function< bool(const String &, String &)> MediaControlCallback
Callback definition for onMediaControl function.
Definition: MediaController.h:28
bool sendMediaControlEvent(String mediaControl, String cause="PHYSICAL_INTERACTION")
Send mediaControl event to SinricPro Server indicating devices media control state.
Definition: MediaController.h:62
void onMediaControl(MediaControlCallback cb)
Set callback function for mediaControl request.
Definition: MediaController.h:48