SinricPro Library
ContactSensor.h
1 #pragma once
2 
3 #include "../EventLimiter.h"
4 #include "../SinricProStrings.h"
5 
6 #include "../SinricProNamespace.h"
7 namespace SINRICPRO_NAMESPACE {
8 
9 FSTR(CONTACT, setContactState); // "setContactState"
10 FSTR(CONTACT, state); // "state"
11 FSTR(CONTACT, closed); // "closed"
12 FSTR(CONTACT, open); // "open"
13 
18 template <typename T>
20  public:
21  ContactSensor();
22  bool sendContactEvent(bool detected, String cause = FSTR_SINRICPRO_PHYSICAL_INTERACTION);
23  private:
24  EventLimiter event_limiter;
25 };
26 
27 template <typename T>
29 : event_limiter(EVENT_LIMIT_SENSOR_STATE) {}
30 
39 template <typename T>
40 bool ContactSensor<T>::sendContactEvent(bool detected, String cause) {
41  if (event_limiter) return false;
42  T* device = static_cast<T*>(this);
43 
44  JsonDocument eventMessage = device->prepareEvent(FSTR_CONTACT_setContactState, cause.c_str());
45  JsonObject event_value = eventMessage[FSTR_SINRICPRO_payload][FSTR_SINRICPRO_value];
46  event_value[FSTR_CONTACT_state] = detected ? FSTR_CONTACT_closed : FSTR_CONTACT_open;
47  return device->sendEvent(eventMessage);
48 }
49 
50 } // SINRICPRO_NAMESPACE
51 
52 template <typename T>
53 using ContactSensor = SINRICPRO_NAMESPACE::ContactSensor<T>;
ContactSensor.
Definition: ContactSensor.h:19
bool sendContactEvent(bool detected, String cause=FSTR_SINRICPRO_PHYSICAL_INTERACTION)
Send setContactState event to SinricPro Server indicating actual power state.
Definition: ContactSensor.h:40