diff --git a/.vscode/settings.json b/.vscode/settings.json index eee375e..b1ce7db 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -50,6 +50,7 @@ "streambuf": "cpp", "cinttypes": "cpp", "typeinfo": "cpp", - "iostream": "cpp" + "iostream": "cpp", + "*.dist": "cpp" } } \ No newline at end of file diff --git a/include/hardware.h b/include/hardware.h index 07c6b3f..ba081fd 100644 --- a/include/hardware.h +++ b/include/hardware.h @@ -1,6 +1,12 @@ #ifndef HARDWARE_H #define HARDWARE_H #include + #ifdef WIFI + #ifdef WEBHOOKS + #include "webhooks.h" + #include "secrets.h" + #endif + #endif #ifdef BOARD1 #ifdef WG_READER @@ -37,10 +43,10 @@ #endif - void controlRelay(); - void unlockDoor(bool silent); - void lockDoor(bool silent); - void toggleDoor(); - String stateDoor(); + extern void controlRelay(); + extern void unlockDoor(bool silent); + extern void lockDoor(bool silent); + extern void toggleDoor(); + extern String stateDoor(); #endif diff --git a/include/main.h b/include/main.h index f90dd03..4c14d86 100644 --- a/include/main.h +++ b/include/main.h @@ -11,10 +11,10 @@ #ifdef WIFI #include "WiFi.h" #ifdef WEBHOOKS - // #include "webhooks.h" + #include "webhooks.h" #endif - #endif + #ifdef LOCAL_ACL #include "acl.h" ACL acl = { @@ -22,9 +22,7 @@ #endif #ifdef TTL_READER - #include - #include - #include + #include "ttl_reader_utils.h" #endif #ifdef TINANCE2_BACKEND diff --git a/include/secrets.h b/include/secrets.h index 90cfb4e..78a5a81 100644 --- a/include/secrets.h +++ b/include/secrets.h @@ -20,10 +20,10 @@ #endif #ifdef WEBHOOKS - #ifdef WEBHOOKS_UNLOCK + #ifdef WEBHOOK_UNLOCK extern const char* webhook_unlock_url; #endif - #ifdef WEBHOOKS_LOCK + #ifdef WEBHOOK_LOCK extern const char* webhook_lock_url; #endif #endif diff --git a/include/secrets.h.dist b/include/secrets.h.dist deleted file mode 100644 index 9c854e4..0000000 --- a/include/secrets.h.dist +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef SECRETS_H - #define SECRETS_H - - #ifdef WIFI - const char* ssid = "WIFI"; - const char* password = "123"; - #endif - - #ifdef WEB_SERVER - const char* http_username = "admin"; - const char* http_password = "admin"; - #endif - - #ifdef TINANCE2_BACKEND - const char* tinance2_url = "https://tinance2.local/accesscontrol/api/check-card-id"; - const char* tinance2_reader_identifer = "mx-test-reader-1"; - const char* tinance2_reader_key = "access-key"; - #endif - - - #ifdef LOCAL_ACL - #include "ACL.h" - - ACL acl = { - {"1234", "Card 1"}, - {"4567", "Card 2"}, - {"7891", "Card 3"} - }; - - #endif - -#endif \ No newline at end of file diff --git a/include/tinance2.h b/include/tinance2.h index 67a1436..4325c44 100644 --- a/include/tinance2.h +++ b/include/tinance2.h @@ -14,5 +14,6 @@ void tinance2SyncTaskFunction(void *parameter); void tinance2authrequest(String fullCardID, String cardID); +extern HTTPClient tinance2_http; #endif diff --git a/include/ttl_reader_utils.h b/include/ttl_reader_utils.h new file mode 100644 index 0000000..a3eec85 --- /dev/null +++ b/include/ttl_reader_utils.h @@ -0,0 +1,10 @@ +#ifndef TTL_READER_UTILS_H + #define TTL_READER_UTILS_H + #include + #include + #include + #include + + extern String parseStringSTXandETX(const String& incoming); + extern uint64_t hexToBinary(const String& hexString); +#endif \ No newline at end of file diff --git a/include/webhooks.h b/include/webhooks.h new file mode 100644 index 0000000..7393289 --- /dev/null +++ b/include/webhooks.h @@ -0,0 +1,7 @@ +#ifndef WEBHOOKS_H + #define WEBHOOKS_H + + #include + extern HTTPClient webhooks_http; + extern void ExecuteHTTPRequest(String url); +#endif \ No newline at end of file diff --git a/src/hardware.cpp b/src/hardware.cpp index dee186f..d7ae45c 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -9,6 +9,13 @@ void unlockDoor(bool silent) { if (!silent) { granted_beep(); } + + #ifdef WEBHOOKS + #ifdef WEBHOOK_UNLOCK + ExecuteHTTPRequest(String(webhook_unlock_url)); + #endif + #endif + #endif #ifdef RELAY1 #ifdef RELAY1_REVERSED @@ -32,6 +39,13 @@ void lockDoor(bool silent) { if (!silent) { short_beep(); } + + #ifdef WEBHOOKS + #ifdef WEBHOOK_LOCK + ExecuteHTTPRequest(String(webhook_lock_url)); + #endif + #endif + #endif #ifdef RELAY1 #ifdef RELAY1_REVERSED diff --git a/src/main.cpp b/src/main.cpp index fbb7628..47376d2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -175,11 +175,8 @@ void setup() { Serial.begin(9600); #endif - #ifdef TTL_READER Serial2.begin(9600, SERIAL_8N1, RXD, TXD); - - #endif #ifdef WEB_SERVER @@ -190,6 +187,13 @@ void setup() { xTaskCreatePinnedToCore(tinance2SyncTaskFunction, "Task", 10000, NULL, 1, NULL, 1); #endif + #ifdef TINANCE2_BACKEND + tinance2_http.setReuse(true); + #endif + + #ifdef WEBHOOKS + webhooks_http.setReuse(true); + #endif #ifdef LOCAL_ACL acl.loadFromEEPROM(); @@ -480,112 +484,6 @@ void setup() { * @note This function assumes the availability of certain functions like updateDoorStatus, checkSerialCommand, tinance2authrequest, and localAcl. */ -#ifdef TTL_READER - String parseStringSTXandETX(const String& incoming) { - int stxPos = incoming.indexOf('\x02'); - int etxPos = incoming.indexOf('\x03'); - - if (stxPos == -1 || etxPos == -1) { - return ""; - } - String parsedString = incoming.substring(stxPos + 1, etxPos); - // Remove whitespace and hidden characters - parsedString.replace(" ", ""); - parsedString.replace("\t", ""); - parsedString.replace("\n", ""); - parsedString.replace("\r", ""); - - return parsedString; - } - uint64_t hexToBinary(const String& hexString) { - String binaryString; - for (char c : hexString) { - switch (c) { - case '0': - binaryString += "0000"; - break; - case '1': - binaryString += "0001"; - break; - case '2': - binaryString += "0010"; - break; - case '3': - binaryString += "0011"; - break; - case '4': - binaryString += "0100"; - break; - case '5': - binaryString += "0101"; - break; - case '6': - binaryString += "0110"; - break; - case '7': - binaryString += "0111"; - break; - case '8': - binaryString += "1000"; - break; - case '9': - binaryString += "1001"; - break; - case 'A': - case 'a': - binaryString += "1010"; - break; - case 'B': - case 'b': - binaryString += "1011"; - break; - case 'C': - case 'c': - binaryString += "1100"; - break; - case 'D': - case 'd': - binaryString += "1101"; - break; - case 'E': - case 'e': - binaryString += "1110"; - break; - case 'F': - case 'f': - binaryString += "1111"; - break; - default: - // Invalid hexadecimal digit - #ifdef SERIAL_DEBUG - Serial.print("Invalid Hex Char: "); - Serial.println(c); - Serial.print("Hex String: "); - Serial.println(hexString); - Serial.print("Hex Char: "); - Serial.println(c); - Serial.print("Binary String: "); - Serial.println(binaryString); - #endif - return 0ULL; - } - } - uint64_t binaryValue = 0ULL; - for (char bit : binaryString) { - binaryValue <<= 1; - binaryValue |= (bit - '0'); - } - - #ifdef SERIAL_DEBUG - Serial.print("Binary Value: "); - Serial.println(binaryValue); - #endif - return binaryValue; - } -#endif - - - void authenticationFlow(uint64_t cardData) { uint64_t facilityID = (cardData >> 17) & 0xFFFF; uint64_t cardID = (cardData >> 1) & 0xFFFF; diff --git a/src/secrets.cpp.dist b/src/secrets.cpp.dist new file mode 100644 index 0000000..0f72927 --- /dev/null +++ b/src/secrets.cpp.dist @@ -0,0 +1,28 @@ +#include "secrets.h" + + #ifdef WIFI + const char* ssid = "SECURE WIFI"; + const char* password = "PASSWORD"; + #endif + + #ifdef TINANCE2_BACKEND + const char* tinance2_url_validatecard = "https://tinance2.domain.nl/accesscontrol/api/check-card-id"; + const char* tinance2_url_readerinfo = "https://tinance2.domain.nl/accesscontrol/api/readerinfo"; + const char* tinance2_url_acls = "https://tinance2.domain.nl/accesscontrol/api/acls"; + const char* tinance2_reader_identifer = "RDR-DOOR-01"; + const char* tinance2_reader_key = "REPLACE-WITH-UUID"; + #endif + + #ifdef WEB_SERVER + const char* http_username = "admin"; + const char* http_password = "PASSWORD"; + #endif + + #ifdef WEBHOOKS + #ifdef WEBHOOK_UNLOCK + const char* webhook_unlock_url = "http://powerbar.ti/powerbar-space/12/on"; + #endif + #ifdef WEBHOOK_LOCK + const char* webhook_lock_url = "http://powerbar.ti/powerbar-space/12/off"; + #endif + #endif diff --git a/src/tinance2.cpp b/src/tinance2.cpp index d4607b1..d7b53bd 100644 --- a/src/tinance2.cpp +++ b/src/tinance2.cpp @@ -3,7 +3,6 @@ HTTPClient tinance2_http; - class Tinance2HttpClient { public: Tinance2HttpClient() {} diff --git a/src/ttl_reader_utils.cpp b/src/ttl_reader_utils.cpp new file mode 100644 index 0000000..193bcd2 --- /dev/null +++ b/src/ttl_reader_utils.cpp @@ -0,0 +1,103 @@ +#include "ttl_reader_utils.h" + + String parseStringSTXandETX(const String& incoming) { + int stxPos = incoming.indexOf('\x02'); + int etxPos = incoming.indexOf('\x03'); + + if (stxPos == -1 || etxPos == -1) { + return ""; + } + String parsedString = incoming.substring(stxPos + 1, etxPos); + // Remove whitespace and hidden characters + parsedString.replace(" ", ""); + parsedString.replace("\t", ""); + parsedString.replace("\n", ""); + parsedString.replace("\r", ""); + + return parsedString; + } + uint64_t hexToBinary(const String& hexString) { + String binaryString; + for (char c : hexString) { + switch (c) { + case '0': + binaryString += "0000"; + break; + case '1': + binaryString += "0001"; + break; + case '2': + binaryString += "0010"; + break; + case '3': + binaryString += "0011"; + break; + case '4': + binaryString += "0100"; + break; + case '5': + binaryString += "0101"; + break; + case '6': + binaryString += "0110"; + break; + case '7': + binaryString += "0111"; + break; + case '8': + binaryString += "1000"; + break; + case '9': + binaryString += "1001"; + break; + case 'A': + case 'a': + binaryString += "1010"; + break; + case 'B': + case 'b': + binaryString += "1011"; + break; + case 'C': + case 'c': + binaryString += "1100"; + break; + case 'D': + case 'd': + binaryString += "1101"; + break; + case 'E': + case 'e': + binaryString += "1110"; + break; + case 'F': + case 'f': + binaryString += "1111"; + break; + default: + // Invalid hexadecimal digit + #ifdef SERIAL_DEBUG + Serial.print("Invalid Hex Char: "); + Serial.println(c); + Serial.print("Hex String: "); + Serial.println(hexString); + Serial.print("Hex Char: "); + Serial.println(c); + Serial.print("Binary String: "); + Serial.println(binaryString); + #endif + return 0ULL; + } + } + uint64_t binaryValue = 0ULL; + for (char bit : binaryString) { + binaryValue <<= 1; + binaryValue |= (bit - '0'); + } + + #ifdef SERIAL_DEBUG + Serial.print("Binary Value: "); + Serial.println(binaryValue); + #endif + return binaryValue; + } diff --git a/src/webhooks.cpp b/src/webhooks.cpp new file mode 100644 index 0000000..b9ea9ef --- /dev/null +++ b/src/webhooks.cpp @@ -0,0 +1,39 @@ +#include "webhooks.h" + +HTTPClient webhooks_http; + +void ExecuteHTTPRequest(String url) { + // Create an instance of the HTTPClient class + #ifdef SERIAL_DEBUG + Serial.println("Executing Webhook:"); + Serial.println(url); + #endif + + webhooks_http.begin(url); + + // Send a GET request to the specified URL + int httpResponseCode = webhooks_http.GET(); + + // Check the response code + if (httpResponseCode > 0) { + // Successful request + String response = webhooks_http.getString(); + // Process the response here + #ifdef SERIAL_DEBUG + Serial.println("Response:"); + Serial.println(response); + #endif + + } else { + // Error handling + #ifdef SERIAL_DEBUG + Serial.print("HTTP request failed with error code: "); + Serial.println(httpResponseCode); + #endif + } + // Clean up + webhooks_http.end(); + #ifdef SERIAL_DEBUG + Serial.println("End of Webhook Execution"); + #endif +}