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