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 "SinricProRequest.h"
12 #include "SinricProDeviceInterface.h"
13 #include "LeakyBucket.h"
14 #include "SinricProId.h"
15 
16 #include <map>
17 
25 class SinricProDevice : public SinricProDeviceInterface {
26  friend class SinricProClass;
27 public:
28  SinricProDevice(const DeviceId &deviceId, const String &productType = "");
29  bool operator==(const DeviceId& other);
30 
31  virtual DeviceId getDeviceId();
32 protected:
33  unsigned long getTimestamp();
34  virtual bool sendEvent(JsonDocument &event);
35  virtual DynamicJsonDocument prepareEvent(const char *action, const char *cause);
36 
37  virtual ~SinricProDevice();
38  virtual String getProductType();
39  virtual void begin(SinricProInterface *eventSender);
40  bool handleRequest(SinricProRequest &request);
41  DeviceId deviceId;
42  std::vector<SinricProRequestHandler> requestHandlers;
43 
44 private : SinricProInterface *eventSender;
45  std::map<String, LeakyBucket_t> eventFilter;
46  String productType;
47 };
48 
49 SinricProDevice::SinricProDevice(const DeviceId &deviceId, const String &productType) :
50  deviceId(deviceId),
51  eventSender(nullptr),
52  productType(productType) {
53 }
54 
55 SinricProDevice::~SinricProDevice() {}
56 
57 void SinricProDevice::begin(SinricProInterface* eventSender) {
58  this->eventSender = eventSender;
59 }
60 
61 DeviceId SinricProDevice::getDeviceId() {
62  return deviceId;
63 }
64 
65 bool SinricProDevice::operator==(const DeviceId &other) {
66  return other == deviceId;
67 }
68 
69 DynamicJsonDocument SinricProDevice::prepareEvent(const char* action, const char* cause) {
70  if (eventSender) return eventSender->prepareEvent(deviceId, action, cause);
71  DEBUG_SINRIC("[SinricProDevice:prepareEvent()]: Device \"%s\" isn't configured correctly! The \'%s\' event will be ignored.\r\n", deviceId.toString().c_str(), action);
72  return DynamicJsonDocument(1024);
73 }
74 
75 
76 bool SinricProDevice::sendEvent(JsonDocument& event) {
77  if (!eventSender) return false;
78  if (!eventSender->isConnected()) {
79  DEBUG_SINRIC("[SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server.\r\n");
80  return false;
81  }
82  String eventName = event["payload"]["action"] | ""; // get event name
83 
84  LeakyBucket_t bucket; // leaky bucket algorithm is used to prevent flooding the server
85 
86  // get leaky bucket for event from eventFilter
87  if (eventFilter.find(eventName) == eventFilter.end()) { // if there is no bucket ...
88  eventFilter[eventName] = bucket; // ...add a new bucket
89  } else {
90  bucket = eventFilter[eventName]; // else get bucket
91  }
92 
93  if (bucket.addDrop()) { // if we can add a new drop
94  eventSender->sendMessage(event); // send event
95  eventFilter[eventName] = bucket; // update bucket on eventFilter
96  return true;
97  }
98 
99  eventFilter[eventName] = bucket; // update bucket on eventFilter
100  return false;
101 }
102 
103 unsigned long SinricProDevice::getTimestamp() {
104  if (eventSender) return eventSender->getTimestamp();
105  return 0;
106 }
107 
108 String SinricProDevice::getProductType() {
109  return String("sinric.device.type.")+productType;
110 }
111 
112 bool SinricProDevice::handleRequest(SinricProRequest &request) {
113  for (auto& requestHandler : requestHandlers) {
114  if (requestHandler(request)) return true;
115  }
116  return false;
117 }
118 
119 
120 #endif
The main class of this library, handling communication between SinricPro Server and your devices.
Definition: SinricPro.h:25
void begin(AppKey socketAuthToken, AppSecret signingKey, String serverURL=SINRICPRO_SERVER_URL)
Initializing SinricProClass to be able to connect to SinricPro Server.
Definition: SinricPro.h:182
unsigned long getTimestamp() override
Get the current timestamp.
Definition: SinricPro.h:91
Base class for all device types.
Definition: SinricProDevice.h:25