implemented disable remotely
This commit is contained in:
parent
ba90623c3a
commit
ef7e49ca18
2 changed files with 127 additions and 45 deletions
79
src/main.cpp
79
src/main.cpp
|
@ -172,7 +172,13 @@ void handleData1Interrupt() {
|
||||||
|
|
||||||
|
|
||||||
// Function to send the authentication request to the endpoint
|
// Function to send the authentication request to the endpoint
|
||||||
std::pair<String, int> sendTinance2HttpRequest(String url, String method, String payload) {
|
#include <ArduinoJson.h> // Include the ArduinoJson library
|
||||||
|
|
||||||
|
class Tinance2HttpClient {
|
||||||
|
public:
|
||||||
|
Tinance2HttpClient() {}
|
||||||
|
|
||||||
|
std::pair<String, int> sendHttpRequest(String url, String method, String payload) {
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
|
|
||||||
if (!http.begin(url)) {
|
if (!http.begin(url)) {
|
||||||
|
@ -222,6 +228,23 @@ void handleData1Interrupt() {
|
||||||
return std::make_pair(responseBody, httpResponseCode);
|
return std::make_pair(responseBody, httpResponseCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to decode JSON response
|
||||||
|
DynamicJsonDocument decodeJsonResponse(const String& json) {
|
||||||
|
DynamicJsonDocument doc(1024); // Adjust the size as per your JSON data
|
||||||
|
|
||||||
|
DeserializationError error = deserializeJson(doc, json);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
#ifdef SERIAL_DEBUG
|
||||||
|
Serial.print("Failed to parse JSON: ");
|
||||||
|
Serial.println(error.c_str());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Function to send the authentication request to Tinance2
|
// Function to send the authentication request to Tinance2
|
||||||
void tinance2authrequest(String fullCardID, String cardID) {
|
void tinance2authrequest(String fullCardID, String cardID) {
|
||||||
#ifdef SERIAL_DEBUG
|
#ifdef SERIAL_DEBUG
|
||||||
|
@ -238,7 +261,8 @@ void handleData1Interrupt() {
|
||||||
// Create the JSON payload
|
// Create the JSON payload
|
||||||
String payload = "{\"full_card_id\":\"" + String(fullCardID) + "\"}";
|
String payload = "{\"full_card_id\":\"" + String(fullCardID) + "\"}";
|
||||||
// Send the HTTP request and get the response
|
// Send the HTTP request and get the response
|
||||||
std::pair<String, int> responsePair = sendTinance2HttpRequest(url, "POST", payload);
|
Tinance2HttpClient httpClient;
|
||||||
|
std::pair<String, int> responsePair = httpClient.sendHttpRequest(tinance2_url_readerinfo, "GET", "");
|
||||||
String response = responsePair.first;
|
String response = responsePair.first;
|
||||||
int httpResponseCode = responsePair.second;
|
int httpResponseCode = responsePair.second;
|
||||||
|
|
||||||
|
@ -303,6 +327,29 @@ void handleData1Interrupt() {
|
||||||
Serial.println("Syncing Tinance2");
|
Serial.println("Syncing Tinance2");
|
||||||
#endif
|
#endif
|
||||||
vTaskDelay(pdMS_TO_TICKS(15000)); // Delay for 15 seconds
|
vTaskDelay(pdMS_TO_TICKS(15000)); // Delay for 15 seconds
|
||||||
|
|
||||||
|
Tinance2HttpClient httpClient;
|
||||||
|
std::pair<String, int> responsePair = httpClient.sendHttpRequest(tinance2_url_readerinfo, "GET", "");
|
||||||
|
String response = responsePair.first;
|
||||||
|
int httpResponseCode = responsePair.second;
|
||||||
|
|
||||||
|
#ifdef SERIAL_DEBUG
|
||||||
|
// Print the response
|
||||||
|
Serial.println("HTTP Response: " + response);
|
||||||
|
Serial.println("HTTP Response Code: " + String(httpResponseCode));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Process the response
|
||||||
|
DynamicJsonDocument json = httpClient.decodeJsonResponse(response);
|
||||||
|
|
||||||
|
bool DisableDoor = json["enabled"].as<bool>();
|
||||||
|
settings.setDisableDoor(!DisableDoor);
|
||||||
|
|
||||||
|
#ifdef SERIAL_DEBUG
|
||||||
|
Serial.println("JSON Reader Enabled: " + json["enabled"].as<String>());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -556,8 +603,36 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef TINANCE2_BACKEND
|
||||||
|
bool previousDoorDisabled = false; // Declare the missing variable
|
||||||
|
bool isFirstRun = true; // Declare the missing variable
|
||||||
|
|
||||||
|
void updateDoorStatus() {
|
||||||
|
|
||||||
|
|
||||||
|
if (settings.DoorDisabled() != previousDoorDisabled || isFirstRun) {
|
||||||
|
if (settings.DoorDisabled()) {
|
||||||
|
unlockDoor(false); // Provide the required argument
|
||||||
|
} else {
|
||||||
|
lockDoor();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the previousDoorDisabled variable
|
||||||
|
previousDoorDisabled = settings.DoorDisabled();
|
||||||
|
isFirstRun = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef TINANCE2_BACKEND
|
||||||
|
updateDoorStatus();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef LOCAL_ACL
|
#ifdef LOCAL_ACL
|
||||||
#ifdef SERIAL_ACL
|
#ifdef SERIAL_ACL
|
||||||
checkSerialCommand();
|
checkSerialCommand();
|
||||||
|
|
|
@ -6,6 +6,7 @@ Preferences settings_preferences;
|
||||||
Settings::Settings() {
|
Settings::Settings() {
|
||||||
// Set default value for disableDoor
|
// Set default value for disableDoor
|
||||||
disableDoor = false;
|
disableDoor = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::loadFromEEPROM() {
|
void Settings::loadFromEEPROM() {
|
||||||
|
@ -21,9 +22,15 @@ void Settings::saveToEEPROM() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setDisableDoor(bool value) {
|
void Settings::setDisableDoor(bool value) {
|
||||||
|
if (disableDoor != value) {
|
||||||
disableDoor = value;
|
disableDoor = value;
|
||||||
|
#ifdef SERIAL_DEBUG
|
||||||
|
Serial.print("Settings::setDisableDoor: ");
|
||||||
|
Serial.println(disableDoor);
|
||||||
|
#endif
|
||||||
saveToEEPROM(); // Save the setting immediately to Preferences
|
saveToEEPROM(); // Save the setting immediately to Preferences
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool Settings::DoorDisabled() {
|
bool Settings::DoorDisabled() {
|
||||||
return disableDoor;
|
return disableDoor;
|
||||||
|
|
Loading…
Reference in a new issue