SinricPro Library
SinricProLock.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 _SINRICLOCK_H_
9 #define _SINRICLOCK_H_
10 
11 #include "SinricProDevice.h"
12 
22  public:
23  SinricProLock(const char* deviceId, unsigned long eventWaitTime=100);
24  // callback
25 
45  typedef std::function<bool(const String&, bool&)> LockStateCallback; // void onLockState(const char* deviceId, bool& lockState);
46 
48  void onPowerState() = delete; // SinricProLock has no powerState
49  // event
50  bool sendPowerStateEvent() = delete; // SinricProLock has no powerState
51  bool sendLockStateEvent(bool state, String cause = "PHYSICAL_INTERACTION");
52 
53  // handle
54  bool handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) override;
55  private:
56  LockStateCallback lockStateCallback;
57 };
58 
59 SinricProLock::SinricProLock(const char* deviceId, unsigned long eventWaitTime) : SinricProDevice(deviceId, eventWaitTime),
60  lockStateCallback(nullptr) {}
61 
62 bool SinricProLock::handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) {
63  if (strcmp(deviceId, this->deviceId) != 0) return false;
64  bool success = false;
65  String actionString = String(action);
66 
67  if (actionString == "setLockState" && lockStateCallback) {
68  bool lockState = request_value["state"]=="lock"?true:false;
69  success = lockStateCallback(String(deviceId), lockState);
70  response_value["state"] = success?lockState?"LOCKED":"UNLOCKED":"JAMMED";
71  return success;
72  }
73  return success;
74 }
75 
84  lockStateCallback = cb;
85 }
86 
96 bool SinricProLock::sendLockStateEvent(bool state, String cause) {
97  DynamicJsonDocument eventMessage = prepareEvent(deviceId, "setLockState", cause.c_str());
98  JsonObject event_value = eventMessage["payload"]["value"];
99  state ? event_value["state"] = "LOCKED" : event_value["state"] = "UNLOCKED";
100  return sendEvent(eventMessage);
101 }
102 #endif
103 
SinricProLock::LockStateCallback
std::function< bool(const String &, bool &)> LockStateCallback
Callback definition for onLockState function.
Definition: SinricProLock.h:45
SinricProLock::onLockState
void onLockState(LockStateCallback cb)
Set callback function for setLockState request.
Definition: SinricProLock.h:83
SinricProDevice
Base class for all device types.
Definition: SinricProDevice.h:23
SinricProLock
Device to control a smart lock.
Definition: SinricProLock.h:21
SinricProLock::sendLockStateEvent
bool sendLockStateEvent(bool state, String cause="PHYSICAL_INTERACTION")
Send lockState event to SinricPro Server indicating actual lock state.
Definition: SinricProLock.h:96