restructred http

This commit is contained in:
Matthew Frost 2023-12-26 17:53:14 +01:00
parent 6baa5781c3
commit 93b7670bbe

View file

@ -8,6 +8,39 @@ const unsigned long wifiRebootTimeout = 20000; // Delay before reboot after disc
unsigned int bitCount = 0; // Variable to keep track of the bit count
unsigned int maxReaderWaitTime = 9000; // Variable to timeout reader after too long of no data.
#ifdef LOCAL_ACL
void localAcl(String cardID) {
if (acl.validateAccess(String(cardID))) {
#ifdef SERIAL_DEBUG
Serial.println("LOCAL_AUTH: Access granted!");
#endif
// Perform actions for authorized access
#ifdef LATCH_DOOR
if (!settings.DoorDisabled()) {
unlockDoor(false);
delay(RELAY_DELAY);
lockDoor();
}
#endif
#ifdef TOGGLE_DOOR
if (!settings.DoorDisabled()) {
toggleDoor();
delay(RELAY_DELAY);
}
#endif
} else {
#ifdef SERIAL_DEBUG
Serial.println("LOCAL_AUTH: Access denied!");
#endif
// Perform actions for denied access
#ifdef BUZZER
denied_beep();
#endif
}
}
#endif
#ifdef WEB_SERVER
AsyncWebServer server(80);
#endif
@ -134,6 +167,137 @@ void handleData1Interrupt() {
handleInterrupt(1);
}
#ifdef TINANCE2_BACKEND
#include <HTTPClient.h>
// Function to send the authentication request to the endpoint
std::pair<String, int> sendTinance2HttpRequest(String url, 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 = http.POST(payload);
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) {
#ifdef SERIAL_DEBUG
Serial.println("WIFI Status: " + String(WiFi.status()));
#endif
if (WiFi.status() == WL_CONNECTED) {
#ifdef SERIAL_DEBUG
Serial.println("Sending Request to Tinance2 for card: " + fullCardID);
#endif
// Set the endpoint URL
String url = tinance2_url;
// 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, payload);
String response = responsePair.first;
int httpResponseCode = responsePair.second;
#ifdef SERIAL_DEBUG
// Print the response
Serial.println("HTTP Response: " + response);
#endif
// Process the response
if (httpResponseCode != 200 && httpResponseCode != 401 && httpResponseCode != 402 && httpResponseCode != 403) {
#ifdef LOCAL_ACL
#ifdef SERIAL_DEBUG
Serial.println("Got unexpected http response using offline auth.");
#endif
localAcl(String(cardID));
#endif
return;
}
// Check the response code
if (httpResponseCode == 200) {
Serial.println("Tinance2 Door Access Granted");
#ifdef LATCH_DOOR
if (!settings.DoorDisabled()) {
unlockDoor(false);
delay(RELAY_DELAY);
lockDoor();
}
#endif
#ifdef TOGGLE_DOOR
if (!settings.DoorDisabled()) {
toggleDoor();
delay(RELAY_DELAY);
}
#endif
} else if (httpResponseCode == 400 || httpResponseCode == 401 || httpResponseCode == 402 || httpResponseCode == 403) {
#ifdef SERIAL_DEBUG
Serial.println("Tinance2 Door Access Denied");
#endif
#ifdef BUZZER
denied_beep();
#endif
}
} else {
#ifdef SERIAL_DEBUG
Serial.println("Tinance2 Wifi Disconnected using offline processes.");
#endif
#ifdef LOCAL_ACL
localAcl(String(cardID));
#endif
}
}
#ifdef TINANCE2_BACKEND_SYNC
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
void tinance2SyncTaskFunction(void *parameter) {
while (true) {
// Your code here
#ifdef SERIAL_DEBUG
Serial.println("Syncing Tinance2");
#endif
vTaskDelay(pdMS_TO_TICKS(15000)); // Delay for 15 seconds
}
}
#endif
#endif
void setup() {
@ -145,6 +309,11 @@ void setup() {
WiFi.disconnect(true);
#endif
#ifdef TINANCE2_BACKEND_SYNC
xTaskCreatePinnedToCore(tinance2SyncTaskFunction, "Task", 10000, NULL, 1, NULL, 1);
#endif
#ifdef LOCAL_ACL
acl.loadFromEEPROM();
#endif
@ -374,175 +543,6 @@ void setup() {
#endif
#endif
#ifdef LOCAL_ACL
void localAcl(String cardID) {
if (acl.validateAccess(String(cardID))) {
#ifdef SERIAL_DEBUG
Serial.println("LOCAL_AUTH: Access granted!");
#endif
// Perform actions for authorized access
#ifdef LATCH_DOOR
if (!settings.DoorDisabled()) {
unlockDoor(false);
delay(RELAY_DELAY);
lockDoor();
}
#endif
#ifdef TOGGLE_DOOR
if (!settings.DoorDisabled()) {
toggleDoor();
delay(RELAY_DELAY);
}
#endif
} else {
#ifdef SERIAL_DEBUG
Serial.println("LOCAL_AUTH: Access denied!");
#endif
// Perform actions for denied access
#ifdef BUZZER
denied_beep();
#endif
}
}
#endif
#ifdef TINANCE2_BACKEND
#include <HTTPClient.h>
// Function to send the authentication request to the endpoint
void tinance2authrequest(String fullCardID, String cardID) {
#ifdef SERIAL_DEBUG
Serial.println("WIFI Status: " + String(WiFi.status()));
#endif
if (WiFi.status() == WL_CONNECTED) {
#ifdef SERIAL_DEBUG
Serial.println("Sending Request to Tinance2 for card: " + fullCardID);
#endif
HTTPClient http;
// Set the endpoint URL
String url = tinance2_url;
if (!http.begin(url)) {
#ifdef SERIAL_DEBUG
Serial.println("Tinance2: Failed to begin HTTP request");
#endif
http.end();
#ifdef LOCAL_ACL
localAcl(String(cardID));
#endif
return;
}
// 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);
// Create the JSON payload
String payload = "{\"full_card_id\":\"" + String(fullCardID) + "\"}";
#ifdef SERIAL_DEBUG
Serial.println("Payload: " + payload);
#endif
// Send the POST request
int httpResponseCode = http.POST(payload);
#ifdef SERIAL_DEBUG
// Print the response code
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
// Print the response body
String responseBody = http.getString();
Serial.print("HTTP Response Body: ");
Serial.println(responseBody);
#endif
if (httpResponseCode <= 0) {
#ifdef SERIAL_DEBUG
Serial.println("Tinance2 Request Failed (response less then 0)");
#endif
http.end();
#ifdef LOCAL_ACL
localAcl(String(cardID));
#endif
return;
}
if (httpResponseCode == 500 || httpResponseCode == 502) {
// Unlock the door
http.end();
#ifdef LOCAL_ACL
localAcl(String(cardID));
#endif
#ifdef SERIAL_DEBUG
Serial.println("Emergency Tinance Process");
#endif
return;
}
// Check the response code
if (httpResponseCode == 200) {
// Unlock the door
http.end();
Serial.println("Tinance2 Door Access Granted");
#ifdef LATCH_DOOR
if (!settings.DoorDisabled()) {
unlockDoor(false);
delay(RELAY_DELAY);
lockDoor();
}
#endif
#ifdef TOGGLE_DOOR
if (!settings.DoorDisabled()) {
toggleDoor();
delay(RELAY_DELAY);
}
#endif
return;
} else {
#ifdef SERIAL_DEBUG
Serial.println("Tinance2 Door Access Denied");
#endif
http.end();
#ifdef BUZZER
denied_beep();
#endif
return;
}
// Cleanup
http.end();
return;
}
else {
#ifdef SERIAL_DEBUG
Serial.println("Tinance2 Wifi Disconnected using offline processess.");
#ifdef LOCAL_ACL
localAcl(String(cardID));
#endif
#endif
}
}
#endif
void loop() {