SinricPro Library
SinricProGarageDoor.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 _SINRICGARAGEDOOR_H_
9 #define _SINRICGARAGEDOOR_H_
10 
11 #include "SinricProDevice.h"
12 
21  public:
22  SinricProGarageDoor(const char* deviceId, unsigned long eventWaitTime=100);
23 
37  typedef std::function<bool(const String&, bool&)> DoorStateCallback;
38 
40  void onPowerState() = delete; // SinricProGarageDoor has no powerState
41 
42  // event
43  bool sendDoorStateEvent(bool mode, String cause = "PHYSICAL_INTERACTION");
44  bool sendPowerStateEvent() = delete; // SinricProGarageDoor has no powerState
45 
46  // handle
47  bool handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) override;
48  private:
49  DoorStateCallback doorStateCallback;
50 };
51 
52 SinricProGarageDoor::SinricProGarageDoor(const char* deviceId, unsigned long eventWaitTime) : SinricProDevice(deviceId, eventWaitTime),
53  doorStateCallback(nullptr) {
54 }
55 
56 bool SinricProGarageDoor::handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) {
57  if (strcmp(deviceId, this->deviceId) != 0) return false;
58  if (SinricProDevice::handleRequest(deviceId, action, request_value, response_value)) return true;
59 
60  bool success = false;
61  String actionString = String(action);
62 
63  if (actionString == "setMode" && doorStateCallback) {
64  String modeStr = request_value["mode"] | "";
65  bool mode;
66  if (modeStr == "Open") mode = false;
67  if (modeStr == "Close") mode = true;
68  success = doorStateCallback(String(deviceId), mode);
69  if (mode == false) modeStr = "Open";
70  if (mode == true) modeStr = "Close";
71  response_value["mode"] = modeStr;
72  return success;
73  }
74  return success;
75 }
76 
85  doorStateCallback = cb;
86 }
87 
97 bool SinricProGarageDoor::sendDoorStateEvent(bool state, String cause) {
98  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "setMode", cause.c_str());
99  JsonObject event_value = eventMessage["payload"]["value"];
100  state ? event_value["mode"] = "close" : event_value["mode"] = "open";
101  return sendEvent(eventMessage);
102 }
103 
104 #endif
105 
SinricProGarageDoor::sendDoorStateEvent
bool sendDoorStateEvent(bool mode, String cause="PHYSICAL_INTERACTION")
Send DoorState event to update actual door state on SinricPro Server.
Definition: SinricProGarageDoor.h:97
SinricProGarageDoor
Device to control a garage door.
Definition: SinricProGarageDoor.h:20
SinricProGarageDoor::DoorStateCallback
std::function< bool(const String &, bool &)> DoorStateCallback
Callback definition for onDoorState function.
Definition: SinricProGarageDoor.h:37
SinricProGarageDoor::onDoorState
void onDoorState(DoorStateCallback cb)
Set callback function for onDoorState request.
Definition: SinricProGarageDoor.h:84
SinricProDevice
Base class for all device types.
Definition: SinricProDevice.h:23