This commit is contained in:
Matthew Frost 2024-01-30 18:21:48 +01:00
parent cdd41bb660
commit a8ff7b905f
14 changed files with 227 additions and 155 deletions

View file

@ -50,6 +50,7 @@
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"iostream": "cpp"
"iostream": "cpp",
"*.dist": "cpp"
}
}

View file

@ -1,6 +1,12 @@
#ifndef HARDWARE_H
#define HARDWARE_H
#include <Arduino.h>
#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

View file

@ -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 <iostream>
#include <string>
#include <sstream>
#include "ttl_reader_utils.h"
#endif
#ifdef TINANCE2_BACKEND

View file

@ -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

View file

@ -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

View file

@ -14,5 +14,6 @@
void tinance2SyncTaskFunction(void *parameter);
void tinance2authrequest(String fullCardID, String cardID);
extern HTTPClient tinance2_http;
#endif

View file

@ -0,0 +1,10 @@
#ifndef TTL_READER_UTILS_H
#define TTL_READER_UTILS_H
#include <iostream>
#include <string>
#include <sstream>
#include <ArduinoJson.h>
extern String parseStringSTXandETX(const String& incoming);
extern uint64_t hexToBinary(const String& hexString);
#endif

7
include/webhooks.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef WEBHOOKS_H
#define WEBHOOKS_H
#include <HTTPClient.h>
extern HTTPClient webhooks_http;
extern void ExecuteHTTPRequest(String url);
#endif

View file

@ -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

View file

@ -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;

28
src/secrets.cpp.dist Normal file
View file

@ -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

View file

@ -3,7 +3,6 @@
HTTPClient tinance2_http;
class Tinance2HttpClient {
public:
Tinance2HttpClient() {}

103
src/ttl_reader_utils.cpp Normal file
View file

@ -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;
}

39
src/webhooks.cpp Normal file
View file

@ -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
}