implemented disable remotely
This commit is contained in:
parent
ba90623c3a
commit
ef7e49ca18
2 changed files with 127 additions and 45 deletions
161
src/main.cpp
161
src/main.cpp
|
@ -172,55 +172,78 @@ void handleData1Interrupt() {
|
|||
|
||||
|
||||
// Function to send the authentication request to the endpoint
|
||||
std::pair<String, int> sendTinance2HttpRequest(String url, String method, String payload) {
|
||||
HTTPClient http;
|
||||
#include <ArduinoJson.h> // Include the ArduinoJson library
|
||||
|
||||
if (!http.begin(url)) {
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("Failed to begin HTTP request");
|
||||
#endif
|
||||
class Tinance2HttpClient {
|
||||
public:
|
||||
Tinance2HttpClient() {}
|
||||
|
||||
std::pair<String, int> sendHttpRequest(String url, String method, String payload) {
|
||||
HTTPClient http;
|
||||
|
||||
if (!http.begin(url)) {
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("Failed to begin HTTP request");
|
||||
#endif
|
||||
http.end();
|
||||
return std::make_pair("", -1);
|
||||
}
|
||||
|
||||
// Set the headers
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
http.addHeader("X-Identifier", tinance2_reader_identifer);
|
||||
http.addHeader("X-Access-Key", tinance2_reader_key);
|
||||
|
||||
// Set the HTTP timeout to 5 seconds
|
||||
http.setTimeout(5000);
|
||||
|
||||
// Send the POST request
|
||||
int httpResponseCode;
|
||||
if (method == "POST") {
|
||||
httpResponseCode = http.POST(payload);
|
||||
} else if (method == "GET") {
|
||||
httpResponseCode = http.GET();
|
||||
} else {
|
||||
// Handle invalid method
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("Invalid HTTP method");
|
||||
#endif
|
||||
http.end();
|
||||
return std::make_pair("", -1);
|
||||
}
|
||||
|
||||
if (httpResponseCode <= 0) {
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("Request Failed (response less than 0)");
|
||||
#endif
|
||||
http.end();
|
||||
return std::make_pair("", httpResponseCode);
|
||||
}
|
||||
|
||||
String responseBody = http.getString();
|
||||
|
||||
// Cleanup
|
||||
http.end();
|
||||
return std::make_pair("", -1);
|
||||
|
||||
return std::make_pair(responseBody, httpResponseCode);
|
||||
}
|
||||
|
||||
// Set the headers
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
http.addHeader("X-Identifier", tinance2_reader_identifer);
|
||||
http.addHeader("X-Access-Key", tinance2_reader_key);
|
||||
// Function to decode JSON response
|
||||
DynamicJsonDocument decodeJsonResponse(const String& json) {
|
||||
DynamicJsonDocument doc(1024); // Adjust the size as per your JSON data
|
||||
|
||||
// Set the HTTP timeout to 5 seconds
|
||||
http.setTimeout(5000);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
|
||||
// Send the POST request
|
||||
int httpResponseCode;
|
||||
if (method == "POST") {
|
||||
httpResponseCode = http.POST(payload);
|
||||
} else if (method == "GET") {
|
||||
httpResponseCode = http.GET();
|
||||
} else {
|
||||
// Handle invalid method
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("Invalid HTTP method");
|
||||
#endif
|
||||
http.end();
|
||||
return std::make_pair("", -1);
|
||||
if (error) {
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.print("Failed to parse JSON: ");
|
||||
Serial.println(error.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
if (httpResponseCode <= 0) {
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.println("Request Failed (response less than 0)");
|
||||
#endif
|
||||
http.end();
|
||||
return std::make_pair("", httpResponseCode);
|
||||
}
|
||||
|
||||
String responseBody = http.getString();
|
||||
|
||||
// Cleanup
|
||||
http.end();
|
||||
|
||||
return std::make_pair(responseBody, httpResponseCode);
|
||||
}
|
||||
};
|
||||
|
||||
// Function to send the authentication request to Tinance2
|
||||
void tinance2authrequest(String fullCardID, String cardID) {
|
||||
|
@ -238,7 +261,8 @@ void handleData1Interrupt() {
|
|||
// Create the JSON payload
|
||||
String payload = "{\"full_card_id\":\"" + String(fullCardID) + "\"}";
|
||||
// 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;
|
||||
int httpResponseCode = responsePair.second;
|
||||
|
||||
|
@ -303,6 +327,29 @@ void handleData1Interrupt() {
|
|||
Serial.println("Syncing Tinance2");
|
||||
#endif
|
||||
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
|
||||
|
@ -556,8 +603,36 @@ void setup() {
|
|||
#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() {
|
||||
|
||||
|
||||
#ifdef TINANCE2_BACKEND
|
||||
updateDoorStatus();
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef LOCAL_ACL
|
||||
#ifdef SERIAL_ACL
|
||||
checkSerialCommand();
|
||||
|
|
|
@ -6,6 +6,7 @@ Preferences settings_preferences;
|
|||
Settings::Settings() {
|
||||
// Set default value for disableDoor
|
||||
disableDoor = false;
|
||||
|
||||
}
|
||||
|
||||
void Settings::loadFromEEPROM() {
|
||||
|
@ -21,8 +22,14 @@ void Settings::saveToEEPROM() {
|
|||
}
|
||||
|
||||
void Settings::setDisableDoor(bool value) {
|
||||
disableDoor = value;
|
||||
saveToEEPROM(); // Save the setting immediately to Preferences
|
||||
if (disableDoor != value) {
|
||||
disableDoor = value;
|
||||
#ifdef SERIAL_DEBUG
|
||||
Serial.print("Settings::setDisableDoor: ");
|
||||
Serial.println(disableDoor);
|
||||
#endif
|
||||
saveToEEPROM(); // Save the setting immediately to Preferences
|
||||
}
|
||||
}
|
||||
|
||||
bool Settings::DoorDisabled() {
|
||||
|
|
Loading…
Reference in a new issue