SinricPro Library
Loading...
Searching...
No Matches
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#pragma once
9
10#include "SinricProRequest.h"
11#include "SinricProDeviceInterface.h"
12#include <map>
13
14#include "SinricProNamespace.h"
15namespace SINRICPRO_NAMESPACE {
16
24class SinricProDevice : public SinricProDeviceInterface {
25 friend class SinricProClass;
26public:
27 SinricProDevice(const String &deviceId, const String &productType = "");
28 bool operator==(const String& other);
29
30 virtual String getDeviceId();
31protected:
32 virtual ~SinricProDevice();
33
34 void registerRequestHandler(const SinricProRequestHandler &requestHandler);
35 unsigned long getTimestamp();
36 String sign(const String& message);
37 virtual bool sendEvent(JsonDocument &event);
38 virtual JsonDocument prepareEvent(const char *action, const char *cause);
39
40 virtual String getProductType();
41 virtual void begin(SinricProInterface *eventSender);
42 bool handleRequest(SinricProRequest &request);
43
44 String deviceId;
45 std::vector<SinricProRequestHandler> requestHandlers;
46
47private:
48 SinricProInterface *eventSender;
49 String productType;
50};
51
52SinricProDevice::SinricProDevice(const String &deviceId, const String &productType) :
53 deviceId(deviceId),
54 eventSender(nullptr),
55 productType(productType) {
56}
57
58SinricProDevice::~SinricProDevice() {}
59
60void SinricProDevice::begin(SinricProInterface* eventSender) {
61 this->eventSender = eventSender;
62}
63
64String SinricProDevice::getDeviceId() {
65 return deviceId;
66}
67
68bool SinricProDevice::operator==(const String &other) {
69 return other == deviceId;
70}
71
72JsonDocument SinricProDevice::prepareEvent(const char* action, const char* cause) {
73 if (eventSender) return eventSender->prepareEvent(deviceId, action, cause);
74 DEBUG_SINRIC("[SinricProDevice:prepareEvent()]: Device \"%s\" isn't configured correctly! The \'%s\' event will be ignored.\r\n", deviceId.c_str(), action);
75 return JsonDocument();
76}
77
78String SinricProDevice::sign(const String& message) {
79 if (eventSender) return eventSender->sign(message);
80 return "";
81}
82
83bool SinricProDevice::sendEvent(JsonDocument& event) {
84 if (!SinricPro.isConnected()) {
85 DEBUG_SINRIC("[SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server.\r\n");
86 return false;
87 }
88
89 if (eventSender) {
90 eventSender->sendMessage(event);
91 return true;
92 }
93
94 return false;
95}
96
97void SinricProDevice::registerRequestHandler(const SinricProRequestHandler &requestHandler) {
98 requestHandlers.push_back(requestHandler);
99}
100
101unsigned long SinricProDevice::getTimestamp() {
102 if (eventSender) return eventSender->getTimestamp();
103 return 0;
104}
105
106String SinricProDevice::getProductType() {
107 return String("sinric.device.type.")+productType;
108}
109
110bool SinricProDevice::handleRequest(SinricProRequest &request) {
111 for (auto& requestHandler : requestHandlers) {
112 if (requestHandler(request)) return true;
113 }
114 return false;
115}
116
117} // SINRICPRO_NAMESPACE
118
119using SinricProDevice = SINRICPRO_NAMESPACE::SinricProDevice;
AirQuality.
Definition AirQualitySensor.h:19
The main class of this library, handling communication between SinricPro Server and your devices.
Definition SinricPro.h:89
Base class for all device types.
Definition SinricProDevice.h:24