firmware for tinance2 done
This commit is contained in:
parent
71340b05f1
commit
afc1a519a5
1 changed files with 146 additions and 61 deletions
207
src/main.cpp
207
src/main.cpp
|
@ -383,10 +383,51 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef TINANCE2_BACKEND
|
#ifdef LOCAL_ACL
|
||||||
#include <HTTPClient.h>
|
void localAcl(String cardID) {
|
||||||
// Function to send the authentication request to the endpoint
|
if (acl.validateAccess(String(cardID))) {
|
||||||
void tinance2authrequest(String fullCardID) {
|
#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
|
#ifdef SERIAL_DEBUG
|
||||||
Serial.println("Sending Request to Tinance2 for card: " + fullCardID);
|
Serial.println("Sending Request to Tinance2 for card: " + fullCardID);
|
||||||
#endif
|
#endif
|
||||||
|
@ -395,53 +436,121 @@ 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
|
#ifdef SERIAL_DEBUG
|
||||||
Serial.println("response code: " + httpResponseCode);
|
// Print the response code
|
||||||
Serial.println("response body: " + http.getString());
|
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
|
#endif
|
||||||
// Check the response code
|
|
||||||
if (httpResponseCode == 200) {
|
if (httpResponseCode <= 0) {
|
||||||
// Unlock the door
|
#ifdef SERIAL_DEBUG
|
||||||
unlockDoor(false);
|
Serial.println("Tinance2 Request Failed (response less then 0)");
|
||||||
delay(RELAY_DELAY);
|
#endif
|
||||||
lockDoor();
|
http.end();
|
||||||
} else {
|
#ifdef LOCAL_ACL
|
||||||
#ifdef SERIAL_DEBUG
|
localAcl(String(cardID));
|
||||||
Serial.println("Tinance2 Door Access Denied");
|
#endif
|
||||||
#endif
|
return;
|
||||||
// Perform actions for denied access
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SERIAL_DEBUG
|
if (httpResponseCode == 500 || httpResponseCode == 502) {
|
||||||
// Print the response code
|
// Unlock the door
|
||||||
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
|
|
||||||
http.end();
|
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() {
|
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
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue