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
#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 #ifdef TINANCE2_BACKEND
#include <HTTPClient.h> #include <HTTPClient.h>
// Function to send the authentication request to the endpoint // Function to send the authentication request to the endpoint
void tinance2authrequest(String fullCardID) { 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 #ifdef SERIAL_DEBUG
Serial.println("Sending Request to Tinance2 for card: " + fullCardID); Serial.println("Sending Request to Tinance2 for card: " + fullCardID);
#endif #endif
@ -395,36 +436,39 @@ void setup() {
// Set the endpoint URL // Set the endpoint URL
String url = tinance2_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 // Set the headers
http.addHeader("Content-Type", "application/json"); http.addHeader("Content-Type", "application/json");
http.addHeader("X-Identifier", tinance2_reader_identifer); http.addHeader("X-Identifier", tinance2_reader_identifer);
http.addHeader("X-Access-Key", tinance2_reader_key); http.addHeader("X-Access-Key", tinance2_reader_key);
// Set the HTTP timeout to 5 seconds
http.setTimeout(5000);
// Create the JSON payload // Create the JSON payload
String payload = "{\"full_card_id\":\"" + String(fullCardID) + "\"}"; String payload = "{\"full_card_id\":\"" + String(fullCardID) + "\"}";
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
Serial.println("Payload: " + payload); Serial.println("Payload: " + payload);
#endif #endif
// Send the POST request // Send the POST request
int httpResponseCode = http.POST(payload); int httpResponseCode = http.POST(payload);
#ifdef SERIAL_DEBUG
Serial.println("response code: " + httpResponseCode);
Serial.println("response body: " + http.getString());
#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
}
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
// Print the response code // Print the response code
@ -435,13 +479,78 @@ void setup() {
String responseBody = http.getString(); String responseBody = http.getString();
Serial.print("HTTP Response Body: "); Serial.print("HTTP Response Body: ");
Serial.println(responseBody); Serial.println(responseBody);
Serial.println("End of Tinance2 Request");
#endif
// Cleanup
http.end();
}
#endif #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() { void loop() {
@ -514,37 +623,13 @@ void loop() {
#ifdef TINANCE2_BACKEND #ifdef TINANCE2_BACKEND
tinance2authrequest(String(fullCardID)); tinance2authrequest(String(fullCardID), String(cardID));
#endif #endif
#ifdef LOCAL_ACL #ifdef LOCAL_ACL
if (acl.validateAccess(String(cardID))) { #ifndef TINANCE2_BACKEND
#ifdef SERIAL_DEBUG localAcl(String(cardID));
Serial.println("LOCAL_AUTH: Access granted!");
#endif #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 #endif
} }