SinricPro Library
SinricProTV.h
1 /*
2  * Copyright (c) 2019 Sinric. All rights reserved.
3  * Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
4  *
5  * This file is part of the Sinric Pro (https://github.com/sinricpro/)
6  */
7 
8 #ifndef _SINRICTV_H_
9 #define _SINRICTV_H_
10 
11 #include "SinricProDevice.h"
12 
28 class SinricProTV : public SinricProDevice {
29  public:
30  SinricProTV(const char* deviceId, unsigned long eventWaitTime=100);
31  // callback
32 
47  typedef std::function<bool(const String&, int&)> SetVolumeCallback;
48 
63  typedef std::function<bool(const String&, int&)> AdjustVolumeCallback;
64 
79  typedef std::function<bool(const String&, bool&)> MuteCallback;
80 
95  typedef std::function<bool(const String&, String&)> MediaControlCallback;
96 
111  typedef std::function<bool(const String&, String&)> SelectInputCallback;
112 
127  typedef std::function<bool(const String&, String&)> ChangeChannelCallback;
128 
143  typedef std::function<bool(const String&, int, String&)> ChangeChannelNumberCallback;
144 
159  typedef std::function<bool(const String&, int, String&)> SkipChannelsCallback;
160 
163  void onMute(MuteCallback cb);
169 
170  // event
171  bool sendVolumeEvent(int volume, String cause = "PHYSICAL_INTERACTION");
172  bool sendMuteEvent(bool mute, String cause = "PHYSICAL_INTERACTION");
173  bool sendMediaControlEvent(String mediaControl, String cause = "PHYSICAL_INTERACTION");
174  bool sendSelectInputEvent(String intput, String cause = "PHYSICAL_INTERACTION");
175  bool sendChangeChannelEvent(String channelName, String cause = "PHYSICAL_INTERACTION");
176  // handle
177  bool handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) override;
178  private:
179  SetVolumeCallback volumeCallback;
180  AdjustVolumeCallback adjustVolumeCallback;
181  MuteCallback muteCallback;
182  MediaControlCallback mediaControlCallback;
183  SelectInputCallback selectInputCallback;
184  ChangeChannelCallback changeChannelCallback;
185  ChangeChannelNumberCallback changeChannelNumberCallback;
186  SkipChannelsCallback skipChannelsCallback;
187 };
188 
189 
190 SinricProTV::SinricProTV(const char* deviceId, unsigned long eventWaitTime) : SinricProDevice(deviceId, eventWaitTime),
191  volumeCallback(nullptr),
192  adjustVolumeCallback(nullptr),
193  muteCallback(nullptr),
194  mediaControlCallback(nullptr),
195  selectInputCallback(nullptr),
196  changeChannelCallback(nullptr),
197  changeChannelNumberCallback(nullptr),
198  skipChannelsCallback(nullptr) {
199 }
200 
201 bool SinricProTV::handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) {
202  if (strcmp(deviceId, this->deviceId) != 0) return false;
203  if (SinricProDevice::handleRequest(deviceId, action, request_value, response_value)) return true;
204 
205  bool success = false;
206  String actionString = String(action);
207 
208  if (volumeCallback && actionString == "setVolume") {
209  int volume = request_value["volume"];
210  success = volumeCallback(String(deviceId), volume);
211  response_value["volume"] = volume;
212  return success;
213  }
214 
215  if (adjustVolumeCallback && actionString == "adjustVolume") {
216  int volume = request_value["volume"];
217  success = adjustVolumeCallback(String(deviceId), volume);
218  response_value["volume"] = volume;
219  return success;
220  }
221 
222  if (muteCallback && actionString == "setMute") {
223  bool mute = request_value["mute"];
224  success = muteCallback(String(deviceId), mute);
225  response_value["mute"] = mute;
226  return success;
227  }
228 
229  if (mediaControlCallback && actionString == "mediaControl") {
230  String mediaControl = request_value["control"];
231  success = mediaControlCallback(String(deviceId), mediaControl);
232  response_value["control"] = mediaControl;
233  return success;
234  }
235 
236  if (selectInputCallback && actionString == "selectInput") {
237  String input = request_value["input"];
238  success = selectInputCallback(String(deviceId), input);
239  response_value["input"] = input;
240  return success;
241  }
242 
243  if (actionString == "changeChannel") {
244 
245  if (changeChannelCallback && request_value["channel"].containsKey("name")) {
246  String channelName = request_value["channel"]["name"] | "";
247  success = changeChannelCallback(String(deviceId), channelName);
248  response_value["channel"]["name"] = channelName;
249  }
250 
251  if (changeChannelNumberCallback && request_value["channel"].containsKey("number")) {
252  int channelNumber = request_value["channel"]["number"];
253  String channelName("");
254  success = changeChannelNumberCallback(String(deviceId), channelNumber, channelName);
255  response_value["channel"]["name"] = channelName;
256  }
257  return success;
258  }
259 
260  if (skipChannelsCallback && actionString == "skipChannels") {
261  int channelCount = request_value["channelCount"] | 0;
262  String channelName;
263  success = skipChannelsCallback(String(deviceId), channelCount, channelName);
264  response_value["channel"]["name"] = channelName;
265  return success;
266  }
267 
268  return success;
269 }
270 
278 void SinricProTV::onSetVolume(SetVolumeCallback cb) { volumeCallback = cb; }
279 
287 void SinricProTV::onAdjustVolume(AdjustVolumeCallback cb) { adjustVolumeCallback = cb; }
288 
296 void SinricProTV::onMute(MuteCallback cb) { muteCallback = cb; }
297 
305 void SinricProTV::onMediaControl(MediaControlCallback cb) { mediaControlCallback = cb; }
306 
314 void SinricProTV::onSelectInput(SelectInputCallback cb) { selectInputCallback = cb; }
315 
323 void SinricProTV::onChangeChannel(ChangeChannelCallback cb) { changeChannelCallback = cb; }
324 
332 void SinricProTV::onChangeChannelNumber(ChangeChannelNumberCallback cb) { changeChannelNumberCallback = cb; }
333 
341 void SinricProTV::onSkipChannels(SkipChannelsCallback cb) { skipChannelsCallback = cb; }
342 
352 bool SinricProTV::sendVolumeEvent(int volume, String cause) {
353  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "setVolume", cause.c_str());
354  JsonObject event_value = eventMessage["payload"]["value"];
355  event_value["volume"] = volume;
356  return sendEvent(eventMessage);
357 }
358 
368 bool SinricProTV::sendMuteEvent(bool mute, String cause) {
369  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "setMute", cause.c_str());
370  JsonObject event_value = eventMessage["payload"]["value"];
371  event_value["mute"] = mute;
372  return sendEvent(eventMessage);
373 }
374 
384 bool SinricProTV::sendMediaControlEvent(String mediaControl, String cause) {
385  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "mediaControl", cause.c_str());
386  JsonObject event_value = eventMessage["payload"]["value"];
387  event_value["control"] = mediaControl;
388  return sendEvent(eventMessage);
389 }
390 
400 bool SinricProTV::sendSelectInputEvent(String input, String cause) {
401  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "selectInput", cause.c_str());
402  JsonObject event_value = eventMessage["payload"]["value"];
403  event_value["input"] = input;
404  return sendEvent(eventMessage);
405 }
406 
416 bool SinricProTV::sendChangeChannelEvent(String channelName, String cause) {
417  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "changeChannel", cause.c_str());
418  JsonObject event_value = eventMessage["payload"]["value"];
419  event_value["channel"]["name"] = channelName;
420  return sendEvent(eventMessage);
421 }
422 
423 #endif
424 
SinricProTV::onMediaControl
void onMediaControl(MediaControlCallback cb)
Set callback function for mediaControl request.
Definition: SinricProTV.h:305
SinricProTV::onMute
void onMute(MuteCallback cb)
Set callback function for setMute request.
Definition: SinricProTV.h:296
SinricProTV::ChangeChannelNumberCallback
std::function< bool(const String &, int, String &)> ChangeChannelNumberCallback
Callback definition for onChangeChannelNumber function.
Definition: SinricProTV.h:143
SinricProTV::sendVolumeEvent
bool sendVolumeEvent(int volume, String cause="PHYSICAL_INTERACTION")
Send setVolume event to SinricPro Server indicating actual volume has changed.
Definition: SinricProTV.h:352
SinricProTV
Device to control a TV.
Definition: SinricProTV.h:28
SinricProTV::sendMuteEvent
bool sendMuteEvent(bool mute, String cause="PHYSICAL_INTERACTION")
Send setMute event to SinricPro Server indicating actual mute state.
Definition: SinricProTV.h:368
SinricProTV::MediaControlCallback
std::function< bool(const String &, String &)> MediaControlCallback
Callback definition for onMediaControl function.
Definition: SinricProTV.h:95
SinricProTV::onChangeChannelNumber
void onChangeChannelNumber(ChangeChannelNumberCallback cb)
Set callback function for changeChannel request.
Definition: SinricProTV.h:332
SinricProTV::SetVolumeCallback
std::function< bool(const String &, int &)> SetVolumeCallback
Callback definition for onSetVolume function.
Definition: SinricProTV.h:47
SinricProTV::onSetVolume
void onSetVolume(SetVolumeCallback cb)
Set callback function for setVolume request.
Definition: SinricProTV.h:278
SinricProTV::ChangeChannelCallback
std::function< bool(const String &, String &)> ChangeChannelCallback
Callback definition for onChangeChannel function.
Definition: SinricProTV.h:127
SinricProTV::sendChangeChannelEvent
bool sendChangeChannelEvent(String channelName, String cause="PHYSICAL_INTERACTION")
Send changeChannel event to SinricPro Server to report selected channel.
Definition: SinricProTV.h:416
SinricProTV::MuteCallback
std::function< bool(const String &, bool &)> MuteCallback
Callback definition for onMute function.
Definition: SinricProTV.h:79
SinricProTV::onSelectInput
void onSelectInput(SelectInputCallback cb)
Set callback function for selectInput request.
Definition: SinricProTV.h:314
SinricProTV::SkipChannelsCallback
std::function< bool(const String &, int, String &)> SkipChannelsCallback
Callback definition for onSkipChannels function.
Definition: SinricProTV.h:159
SinricProTV::onChangeChannel
void onChangeChannel(ChangeChannelCallback cb)
Set callback function for changeChannel request.
Definition: SinricProTV.h:323
SinricProTV::onSkipChannels
void onSkipChannels(SkipChannelsCallback cb)
Set callback function for skipChannels request.
Definition: SinricProTV.h:341
SinricProTV::sendSelectInputEvent
bool sendSelectInputEvent(String intput, String cause="PHYSICAL_INTERACTION")
Send selectInput event to SinricPro Server to report selected input.
Definition: SinricProTV.h:400
SinricProTV::SelectInputCallback
std::function< bool(const String &, String &)> SelectInputCallback
Callback definition for onSelectInput function.
Definition: SinricProTV.h:111
SinricProDevice
Base class for all device types.
Definition: SinricProDevice.h:23
SinricProTV::onAdjustVolume
void onAdjustVolume(AdjustVolumeCallback cb)
Set callback function for adjustVolume request.
Definition: SinricProTV.h:287
SinricProTV::sendMediaControlEvent
bool sendMediaControlEvent(String mediaControl, String cause="PHYSICAL_INTERACTION")
Send mediaControl event to SinricPro Server indicating devices media control state.
Definition: SinricProTV.h:384
SinricProTV::AdjustVolumeCallback
std::function< bool(const String &, int &)> AdjustVolumeCallback
Callback definition for onAdjustVolume function.
Definition: SinricProTV.h:63