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  String getProductType() { return SinricProDevice::getProductType() + String("TV"); }
32  // callback
33 
48  typedef std::function<bool(const String&, int&)> SetVolumeCallback;
49 
64  typedef std::function<bool(const String&, int&)> AdjustVolumeCallback;
65 
80  typedef std::function<bool(const String&, bool&)> MuteCallback;
81 
96  typedef std::function<bool(const String&, String&)> MediaControlCallback;
97 
112  typedef std::function<bool(const String&, String&)> SelectInputCallback;
113 
128  typedef std::function<bool(const String&, String&)> ChangeChannelCallback;
129 
144  typedef std::function<bool(const String&, int, String&)> ChangeChannelNumberCallback;
145 
160  typedef std::function<bool(const String&, int, String&)> SkipChannelsCallback;
161 
164  void onMute(MuteCallback cb);
170 
171  // event
172  bool sendVolumeEvent(int volume, String cause = "PHYSICAL_INTERACTION");
173  bool sendMuteEvent(bool mute, String cause = "PHYSICAL_INTERACTION");
174  bool sendMediaControlEvent(String mediaControl, String cause = "PHYSICAL_INTERACTION");
175  bool sendSelectInputEvent(String intput, String cause = "PHYSICAL_INTERACTION");
176  bool sendChangeChannelEvent(String channelName, String cause = "PHYSICAL_INTERACTION");
177  // handle
178  bool handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) override;
179  private:
180  SetVolumeCallback volumeCallback;
181  AdjustVolumeCallback adjustVolumeCallback;
182  MuteCallback muteCallback;
183  MediaControlCallback mediaControlCallback;
184  SelectInputCallback selectInputCallback;
185  ChangeChannelCallback changeChannelCallback;
186  ChangeChannelNumberCallback changeChannelNumberCallback;
187  SkipChannelsCallback skipChannelsCallback;
188 };
189 
190 
191 SinricProTV::SinricProTV(const char* deviceId, unsigned long eventWaitTime) : SinricProDevice(deviceId, eventWaitTime),
192  volumeCallback(nullptr),
193  adjustVolumeCallback(nullptr),
194  muteCallback(nullptr),
195  mediaControlCallback(nullptr),
196  selectInputCallback(nullptr),
197  changeChannelCallback(nullptr),
198  changeChannelNumberCallback(nullptr),
199  skipChannelsCallback(nullptr) {
200 }
201 
202 bool SinricProTV::handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) {
203  if (strcmp(deviceId, this->deviceId) != 0) return false;
204  if (SinricProDevice::handleRequest(deviceId, action, request_value, response_value)) return true;
205 
206  bool success = false;
207  String actionString = String(action);
208 
209  if (volumeCallback && actionString == "setVolume") {
210  int volume = request_value["volume"];
211  success = volumeCallback(String(deviceId), volume);
212  response_value["volume"] = volume;
213  return success;
214  }
215 
216  if (adjustVolumeCallback && actionString == "adjustVolume") {
217  int volume = request_value["volume"];
218  success = adjustVolumeCallback(String(deviceId), volume);
219  response_value["volume"] = volume;
220  return success;
221  }
222 
223  if (muteCallback && actionString == "setMute") {
224  bool mute = request_value["mute"];
225  success = muteCallback(String(deviceId), mute);
226  response_value["mute"] = mute;
227  return success;
228  }
229 
230  if (mediaControlCallback && actionString == "mediaControl") {
231  String mediaControl = request_value["control"];
232  success = mediaControlCallback(String(deviceId), mediaControl);
233  response_value["control"] = mediaControl;
234  return success;
235  }
236 
237  if (selectInputCallback && actionString == "selectInput") {
238  String input = request_value["input"];
239  success = selectInputCallback(String(deviceId), input);
240  response_value["input"] = input;
241  return success;
242  }
243 
244  if (actionString == "changeChannel") {
245 
246  if (changeChannelCallback && request_value["channel"].containsKey("name")) {
247  String channelName = request_value["channel"]["name"] | "";
248  success = changeChannelCallback(String(deviceId), channelName);
249  response_value["channel"]["name"] = channelName;
250  }
251 
252  if (changeChannelNumberCallback && request_value["channel"].containsKey("number")) {
253  int channelNumber = request_value["channel"]["number"];
254  String channelName("");
255  success = changeChannelNumberCallback(String(deviceId), channelNumber, channelName);
256  response_value["channel"]["name"] = channelName;
257  }
258  return success;
259  }
260 
261  if (skipChannelsCallback && actionString == "skipChannels") {
262  int channelCount = request_value["channelCount"] | 0;
263  String channelName;
264  success = skipChannelsCallback(String(deviceId), channelCount, channelName);
265  response_value["channel"]["name"] = channelName;
266  return success;
267  }
268 
269  return success;
270 }
271 
279 void SinricProTV::onSetVolume(SetVolumeCallback cb) { volumeCallback = cb; }
280 
288 void SinricProTV::onAdjustVolume(AdjustVolumeCallback cb) { adjustVolumeCallback = cb; }
289 
297 void SinricProTV::onMute(MuteCallback cb) { muteCallback = cb; }
298 
306 void SinricProTV::onMediaControl(MediaControlCallback cb) { mediaControlCallback = cb; }
307 
315 void SinricProTV::onSelectInput(SelectInputCallback cb) { selectInputCallback = cb; }
316 
324 void SinricProTV::onChangeChannel(ChangeChannelCallback cb) { changeChannelCallback = cb; }
325 
333 void SinricProTV::onChangeChannelNumber(ChangeChannelNumberCallback cb) { changeChannelNumberCallback = cb; }
334 
342 void SinricProTV::onSkipChannels(SkipChannelsCallback cb) { skipChannelsCallback = cb; }
343 
353 bool SinricProTV::sendVolumeEvent(int volume, String cause) {
354  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "setVolume", cause.c_str());
355  JsonObject event_value = eventMessage["payload"]["value"];
356  event_value["volume"] = volume;
357  return sendEvent(eventMessage);
358 }
359 
369 bool SinricProTV::sendMuteEvent(bool mute, String cause) {
370  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "setMute", cause.c_str());
371  JsonObject event_value = eventMessage["payload"]["value"];
372  event_value["mute"] = mute;
373  return sendEvent(eventMessage);
374 }
375 
385 bool SinricProTV::sendMediaControlEvent(String mediaControl, String cause) {
386  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "mediaControl", cause.c_str());
387  JsonObject event_value = eventMessage["payload"]["value"];
388  event_value["control"] = mediaControl;
389  return sendEvent(eventMessage);
390 }
391 
401 bool SinricProTV::sendSelectInputEvent(String input, String cause) {
402  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "selectInput", cause.c_str());
403  JsonObject event_value = eventMessage["payload"]["value"];
404  event_value["input"] = input;
405  return sendEvent(eventMessage);
406 }
407 
417 bool SinricProTV::sendChangeChannelEvent(String channelName, String cause) {
418  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "changeChannel", cause.c_str());
419  JsonObject event_value = eventMessage["payload"]["value"];
420  event_value["channel"]["name"] = channelName;
421  return sendEvent(eventMessage);
422 }
423 
424 #endif
425 
SinricProTV::onMediaControl
void onMediaControl(MediaControlCallback cb)
Set callback function for mediaControl request.
Definition: SinricProTV.h:306
SinricProTV::onMute
void onMute(MuteCallback cb)
Set callback function for setMute request.
Definition: SinricProTV.h:297
SinricProTV::ChangeChannelNumberCallback
std::function< bool(const String &, int, String &)> ChangeChannelNumberCallback
Callback definition for onChangeChannelNumber function.
Definition: SinricProTV.h:144
SinricProTV::sendVolumeEvent
bool sendVolumeEvent(int volume, String cause="PHYSICAL_INTERACTION")
Send setVolume event to SinricPro Server indicating actual volume has changed.
Definition: SinricProTV.h:353
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:369
SinricProTV::MediaControlCallback
std::function< bool(const String &, String &)> MediaControlCallback
Callback definition for onMediaControl function.
Definition: SinricProTV.h:96
SinricProTV::onChangeChannelNumber
void onChangeChannelNumber(ChangeChannelNumberCallback cb)
Set callback function for changeChannel request.
Definition: SinricProTV.h:333
SinricProTV::SetVolumeCallback
std::function< bool(const String &, int &)> SetVolumeCallback
Callback definition for onSetVolume function.
Definition: SinricProTV.h:48
SinricProTV::onSetVolume
void onSetVolume(SetVolumeCallback cb)
Set callback function for setVolume request.
Definition: SinricProTV.h:279
SinricProTV::ChangeChannelCallback
std::function< bool(const String &, String &)> ChangeChannelCallback
Callback definition for onChangeChannel function.
Definition: SinricProTV.h:128
SinricProTV::sendChangeChannelEvent
bool sendChangeChannelEvent(String channelName, String cause="PHYSICAL_INTERACTION")
Send changeChannel event to SinricPro Server to report selected channel.
Definition: SinricProTV.h:417
SinricProTV::MuteCallback
std::function< bool(const String &, bool &)> MuteCallback
Callback definition for onMute function.
Definition: SinricProTV.h:80
SinricProTV::onSelectInput
void onSelectInput(SelectInputCallback cb)
Set callback function for selectInput request.
Definition: SinricProTV.h:315
SinricProTV::SkipChannelsCallback
std::function< bool(const String &, int, String &)> SkipChannelsCallback
Callback definition for onSkipChannels function.
Definition: SinricProTV.h:160
SinricProTV::onChangeChannel
void onChangeChannel(ChangeChannelCallback cb)
Set callback function for changeChannel request.
Definition: SinricProTV.h:324
SinricProTV::onSkipChannels
void onSkipChannels(SkipChannelsCallback cb)
Set callback function for skipChannels request.
Definition: SinricProTV.h:342
SinricProTV::sendSelectInputEvent
bool sendSelectInputEvent(String intput, String cause="PHYSICAL_INTERACTION")
Send selectInput event to SinricPro Server to report selected input.
Definition: SinricProTV.h:401
SinricProTV::SelectInputCallback
std::function< bool(const String &, String &)> SelectInputCallback
Callback definition for onSelectInput function.
Definition: SinricProTV.h:112
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:288
SinricProTV::sendMediaControlEvent
bool sendMediaControlEvent(String mediaControl, String cause="PHYSICAL_INTERACTION")
Send mediaControl event to SinricPro Server indicating devices media control state.
Definition: SinricProTV.h:385
SinricProTV::AdjustVolumeCallback
std::function< bool(const String &, int &)> AdjustVolumeCallback
Callback definition for onAdjustVolume function.
Definition: SinricProTV.h:64