firmware for tinance2 done

This commit is contained in:
Matthew Frost 2023-11-21 11:29:27 +01:00
parent 71340b05f1
commit afc1a519a5

View file

@ -383,10 +383,51 @@ void setup() {
#endif
#endif
#ifdef TINANCE2_BACKEND
#include <HTTPClient.h>
// Function to send the authentication request to the endpoint
void tinance2authrequest(String fullCardID) {
#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
@ -395,53 +436,121 @@ void setup() {
// Set the endpoint URL
String url = tinance2_url;
http.begin(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);
Serial.println("Payload: " + payload);
#endif
// Send the POST request
int httpResponseCode = http.POST(payload);
#ifdef SERIAL_DEBUG
Serial.println("response code: " + httpResponseCode);
Serial.println("response body: " + http.getString());
// 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
// Check the response code
if (httpResponseCode == 200) {
// Unlock the door
unlockDoor(false);
delay(RELAY_DELAY);
lockDoor();
} else {
#ifdef SERIAL_DEBUG
Serial.println("Tinance2 Door Access Denied");
#endif
// Perform actions for denied access
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;
}
#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);
Serial.println("End of Tinance2 Request");
#endif
// Cleanup
if (httpResponseCode == 500 || httpResponseCode == 502) {
// Unlock the door
http.end();
}
#endif
#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() {
@ -514,37 +623,13 @@ void loop() {
#ifdef TINANCE2_BACKEND
tinance2authrequest(String(fullCardID));
tinance2authrequest(String(fullCardID), String(cardID));
#endif
#ifdef LOCAL_ACL
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
}
#ifndef TINANCE2_BACKEND
localAcl(String(cardID));
#endif
#endif
}