7#include "../configuration/Configuration.h"
8#define ARDUINOJSON_USE_LONG_LONG 1
9#define ARDUINOJSON_DECODE_UNICODE 1
12#include <ArduinoJson.h>
13#include <HTTPClient.h>
15#include <WiFiClientSecure.h>
17#include <condition_variable>
31 const String
host =
"api.emlalock.com";
92 Serial.println(
"Starting EmlaLock API in offline mode..");
95 Serial.println(
"Starting EmlaLock API in online mode..");
106 std::unique_lock<std::mutex> lock(
mtx);
124 std::unique_lock<std::mutex> lock(
mtx);
127 condVar.wait_for(lock, std::chrono::seconds(600), [
this]() ->
bool {
143 uint32_t numberOfFailedSessionNew =
jsonDocument[
"user"][
"failedsessions"].as<uint32_t>();
144 if (numberOfFailedSessionOld != numberOfFailedSessionNew) {
150 Serial.println(
"Abort rejected");
174 Serial.printf(
"End-date: %s\n", buf);
178 String s =
jsonDocument[
"chastitysession"][
"enddate"].as<String>();
179 s.replace(
"{{localization.",
"");
186 if (
jsonDocument[
"chastitysession"][
"incleaning"].as<int>() != 0) {
188 jsonDocument[
"chastitysession"][
"timeforcleaning"].as<time_t>());
201 Serial.println(
"Connection failed.");
215 WiFiClientSecure client;
216 client.setInsecure();
217 url = String(
"https://") +
host + url;
222 http.useHTTP10(
true);
223 http.begin(client, url);
226 int httpResponseCode = http.GET();
229 if (httpResponseCode > 0) {
230 DeserializationError error = deserializeJson(
jsonDocument, http.getStream());
233 Serial.print(F(
"deserializeJson() failed: "));
234 Serial.println(error.c_str());
239 Serial.print(
"Error code: ");
240 Serial.println(httpResponseCode);
static void setStartDate(const time_t &startDate)
Set the Start Date.
Definition: LockState.h:311
static const time_t & getEndDate()
Get the End Date.
Definition: LockState.h:325
DisplayTimeLeft
States for the selection how the time left should be displayed.
Definition: LockState.h:47
static const Mode & getMode()
Get the mode.
Definition: LockState.h:233
static const uint32_t & getNumberOfFailedSessions()
Get the number of failed sessions.
Definition: LockState.h:348
static void setLastUpdateTime(const time_t &lastUpdateTime)
Set the time of the last update over the emlalock api.
Definition: LockState.h:447
static const DisplayTimePassed & getDisplayTimePassed()
Get how the time passed should be displayed.
Definition: LockState.h:256
static void setEndDate(const time_t &endDate)
Set the End Date.
Definition: LockState.h:334
static void setTemperatureString(const String &temperatureString)
Set the temperature string.
Definition: LockState.h:380
static void setNumberOfFailedSessions(const uint32_t &numberOfFailedSessions)
Set the number of failed sessions.
Definition: LockState.h:357
static void setMode(const Mode &mode)
Set the mode.
Definition: LockState.h:242
static void setDisplayTimePassed(const DisplayTimePassed &displayTimePassed)
Set how the time passed should be displayed.
Definition: LockState.h:265
static void setDisplayTimeLeft(const DisplayTimeLeft &displayTimeLeft)
Set how the time left should be displayed.
Definition: LockState.h:288
static void setCleaningEndDate(const time_t &cleaningEndDate)
Set the End Date of the current cleaning opening.
Definition: LockState.h:425
static const DisplayTimeLeft & getDisplayTimeLeft()
Get how the time left should be displayed.
Definition: LockState.h:279
DisplayTimePassed
States for the selection how the passed time should be displayed.
Definition: LockState.h:41
static Configuration & getSingleton()
Get the Singleton object.
Definition: Configuration.h:162
Class interfacing Arduino to the EmlaLock API.
Definition: EmlaLockApi.h:26
void threadFunction()
The thread functions which is communicating asynchronously with the Emlalock API.
Definition: EmlaLockApi.h:122
bool triggered
Helper variable to detect spurious wakeups of thread.
Definition: EmlaLockApi.h:56
EmlaLockApi(bool offlineMode)
Construct a new EmlaLock Api Object. Use the singleton EmlaLock or getInstance() instead of creating ...
Definition: EmlaLockApi.h:88
bool requestUrl(String &url)
Loads the requested url and parses the json result.
Definition: EmlaLockApi.h:214
StaticJsonDocument< 10000 > jsonDocument
JSON parser.
Definition: EmlaLockApi.h:37
const String host
The base address to the API.
Definition: EmlaLockApi.h:31
bool offlineMode
If true the thread requesting data from emlalock won't be started.
Definition: EmlaLockApi.h:62
std::mutex mtx
Mutex used to ensure thread safety of the API.
Definition: EmlaLockApi.h:43
void triggerRefresh()
Triggers the client to reload the current state from the EmlaLock server.
Definition: EmlaLockApi.h:105
static EmlaLockApi & getSingleton(bool offlineMode=false)
Get the singleton instance of the API handler.
Definition: EmlaLockApi.h:71
std::condition_variable condVar
Condition variable to which can be triggered to immediately request the status of the EmlaLock Sessio...
Definition: EmlaLockApi.h:50
Definition: EmlaLockApi.h:21