SinricPro Library
SinricProDevice.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 _SINRICDEVICE_H_
9 #define _SINRICDEVICE_H_
10 
11 #include "SinricProDeviceInterface.h"
12 #include "LeakyBucket.h"
13 
14 #include <map>
15 
23 class SinricProDevice : public SinricProDeviceInterface {
24  public:
25  SinricProDevice(const char* newDeviceId, unsigned long eventWaitTime=100);
26  virtual ~SinricProDevice();
27  virtual const char* getDeviceId();
28  virtual void begin(SinricProInterface* eventSender);
29  virtual void setEventWaitTime(unsigned long eventWaitTime) { if (eventWaitTime<100) {this->eventWaitTime=100;} else { this->eventWaitTime=eventWaitTime;} }
30 
31  // callback definitions
45  typedef std::function<bool(const String&, bool&)> PowerStateCallback;
46 
47 
48  // standard request handler
49  virtual bool handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value);
50 
51  // standard Callbacks
52  virtual void onPowerState(PowerStateCallback cb);
53 
54  // standard events
55  bool sendPowerStateEvent(bool state, String cause = "PHYSICAL_INTERACTION");
56 
57  protected:
58  virtual bool sendEvent(JsonDocument& event);
59  virtual DynamicJsonDocument prepareEvent(const char* deviceId, const char* action, const char* cause);
60  char* deviceId;
61  PowerStateCallback powerStateCallback;
62  private:
63  SinricProInterface* eventSender;
64  unsigned long eventWaitTime;
65  std::map<String, LeakyBucket_t> eventFilter;
66 };
67 
68 SinricProDevice::SinricProDevice(const char* newDeviceId, unsigned long eventWaitTime) :
69  powerStateCallback(nullptr),
70  eventSender(nullptr),
71  eventWaitTime(eventWaitTime) {
72  deviceId = strdup(newDeviceId);
73  if (this->eventWaitTime < 100) this->eventWaitTime = 100;
74 }
75 
76 SinricProDevice::~SinricProDevice() {
77  if (deviceId) free(deviceId);
78 }
79 
80 void SinricProDevice::begin(SinricProInterface* eventSender) {
81  this->eventSender = eventSender;
82 }
83 
84 const char* SinricProDevice::getDeviceId() {
85  return deviceId;
86 }
87 
88 bool SinricProDevice::handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) {
89  if (strcmp(deviceId, this->deviceId) != 0) return false;
90  DEBUG_SINRIC("SinricProDevice::handleRequest()\r\n");
91  bool success = false;
92  String actionString = String(action);
93 
94  if (actionString == "setPowerState" && powerStateCallback) {
95  bool powerState = request_value["state"]=="On"?true:false;
96  success = powerStateCallback(String(deviceId), powerState);
97  response_value["state"] = powerState?"On":"Off";
98  return success;
99  }
100  return success;
101 }
102 
103 DynamicJsonDocument SinricProDevice::prepareEvent(const char* deviceId, const char* action, const char* cause) {
104  if (eventSender) return eventSender->prepareEvent(deviceId, action, cause);
105  DEBUG_SINRIC("[SinricProDevice:prepareEvent()]: Device \"%s\" isn't configured correctly! The \'%s\' event will be ignored.\r\n", deviceId, action);
106  return DynamicJsonDocument(1024);
107 }
108 
109 
110 bool SinricProDevice::sendEvent(JsonDocument& event) {
111  String eventName = event["payload"]["action"] | ""; // get event name
112 
113  LeakyBucket_t bucket; // leaky bucket algorithm is used to prevent flooding the server
114 
115  // get leaky bucket for event from eventFilter
116  if (eventFilter.find(eventName) == eventFilter.end()) { // if there is no bucket ...
117  eventFilter[eventName] = bucket; // ...add a new bucket
118  } else {
119  bucket = eventFilter[eventName]; // else get bucket
120  }
121 
122  if (bucket.addDrop()) { // if we can add a new drop
123  if (eventSender) eventSender->sendMessage(event); // send event
124  eventFilter[eventName] = bucket; // update bucket on eventFilter
125  return true;
126  }
127 
128  eventFilter[eventName] = bucket; // update bucket on eventFilter
129  return false;
130 }
131 
140  powerStateCallback = cb;
141 }
142 
152 bool SinricProDevice::sendPowerStateEvent(bool state, String cause) {
153  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "setPowerState", cause.c_str());
154  JsonObject event_value = eventMessage["payload"]["value"];
155  event_value["state"] = state?"On":"Off";
156  return sendEvent(eventMessage);
157 }
158 
159 #endif
SinricProDevice::sendPowerStateEvent
bool sendPowerStateEvent(bool state, String cause="PHYSICAL_INTERACTION")
Send setPowerState event to SinricPro Server indicating actual power state.
Definition: SinricProDevice.h:152
SinricProDevice::PowerStateCallback
std::function< bool(const String &, bool &)> PowerStateCallback
Callback definition for onPowerState function.
Definition: SinricProDevice.h:45
SinricProDevice
Base class for all device types.
Definition: SinricProDevice.h:23
SinricProDevice::onPowerState
virtual void onPowerState(PowerStateCallback cb)
Set callback function for powerState request.
Definition: SinricProDevice.h:139