8 #ifndef _SINRICDEVICE_H_
9 #define _SINRICDEVICE_H_
11 #include "SinricProRequest.h"
12 #include "SinricProDeviceInterface.h"
13 #include "LeakyBucket.h"
14 #include "SinricProId.h"
28 SinricProDevice(
const DeviceId &deviceId,
const String &productType =
"");
29 bool operator==(
const DeviceId& other);
31 virtual DeviceId getDeviceId();
34 virtual bool sendEvent(JsonDocument &event);
35 virtual DynamicJsonDocument prepareEvent(
const char *action,
const char *cause);
38 virtual String getProductType();
39 virtual void begin(SinricProInterface *eventSender);
40 bool handleRequest(SinricProRequest &request);
42 std::vector<SinricProRequestHandler> requestHandlers;
44 private : SinricProInterface *eventSender;
45 std::map<String, LeakyBucket_t> eventFilter;
49 SinricProDevice::SinricProDevice(
const DeviceId &deviceId,
const String &productType) :
52 productType(productType) {
55 SinricProDevice::~SinricProDevice() {}
57 void SinricProDevice::begin(SinricProInterface* eventSender) {
58 this->eventSender = eventSender;
61 DeviceId SinricProDevice::getDeviceId() {
65 bool SinricProDevice::operator==(
const DeviceId &other) {
66 return other == deviceId;
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);
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");
82 String eventName =
event[
"payload"][
"action"] |
"";
87 if (eventFilter.find(eventName) == eventFilter.end()) {
88 eventFilter[eventName] = bucket;
90 bucket = eventFilter[eventName];
93 if (bucket.addDrop()) {
94 eventSender->sendMessage(event);
95 eventFilter[eventName] = bucket;
99 eventFilter[eventName] = bucket;
103 unsigned long SinricProDevice::getTimestamp() {
104 if (eventSender)
return eventSender->getTimestamp();
108 String SinricProDevice::getProductType() {
109 return String(
"sinric.device.type.")+productType;
112 bool SinricProDevice::handleRequest(SinricProRequest &request) {
113 for (
auto& requestHandler : requestHandlers) {
114 if (requestHandler(request))
return true;
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