Skip to content

Commit

Permalink
Merge pull request #632 from esprfid/fix-accesslog
Browse files Browse the repository at this point in the history
Fix access logs and visualization
  • Loading branch information
matjack1 authored Jun 11, 2024
2 parents 3c9b4be + 54e4405 commit 33a44b7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/log.esp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String d
#endif
}

void ICACHE_FLASH_ATTR writeLatest(String uid, String username, int acctype)
void ICACHE_FLASH_ATTR writeLatest(String uid, String username, int acctype, int access = ACCESS_GRANTED)
{
DynamicJsonDocument root(512);
root["uid"] = uid;
root["username"] = username;
root["acctype"] = acctype;
root["access"] = access;
root["timestamp"] = epoch;
File latestlog = SPIFFS.open("/latestlog.json", "a");
serializeJson(root, latestlog);
Expand Down
2 changes: 1 addition & 1 deletion src/magicnumbers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#define ACCESS_GRANTED 1
#define ACCESS_ADMIN 99
#define ACCCESS_DENIED 0
#define ACCESS_DENIED 0

// Reader defines

Expand Down
11 changes: 8 additions & 3 deletions src/rfid.esp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ void rfidProcess()
if (validUntilL < nowL || validSinceL > nowL)
{
processingState = expired;
} else if (config.openingHours[weekdayFromMonday(weekday())][hourTz] != '1')
} else if (config.openingHours[weekdayFromMonday(weekday(epoch))][hourTz] != '1')
{
processingState = notValid;
} else
Expand Down Expand Up @@ -485,6 +485,7 @@ void rfidOutsideMessaging()
#ifdef DEBUG
Serial.println(" has access relay");
#endif
writeLatest(uid, username, accountType, ACCESS_GRANTED);
if (config.numRelays == 1) {
mqttPublishAccess(epoch, "true", "Always", username, uid);
} else {
Expand All @@ -495,6 +496,7 @@ void rfidOutsideMessaging()
if (processingState == validAdmin)
{
ws.textAll("{\"command\":\"giveAccess\"}");
writeLatest(uid, username, accountType, ACCESS_GRANTED);
if (config.numRelays == 1) {
mqttPublishAccess(epoch, "true", "Admin", username, uid);
} else {
Expand All @@ -507,12 +509,14 @@ void rfidOutsideMessaging()
#ifdef DEBUG
Serial.println(" expired");
#endif
writeLatest(uid, username, accountType, ACCESS_DENIED);
mqttPublishAccess(epoch, "true", "Expired", username, uid);
ledAccessDeniedOn();
beeperAccessDenied();
}
if (processingState == wrongPincode)
{
writeLatest(uid, username, accountType, ACCESS_DENIED);
mqttPublishAccess(epoch, "true", "Wrong pin code", username, uid);
ledAccessDeniedOn();
beeperAccessDenied();
Expand All @@ -522,6 +526,7 @@ void rfidOutsideMessaging()
#ifdef DEBUG
Serial.println(" does not have access");
#endif
writeLatest(uid, username, accountType, ACCESS_DENIED);
mqttPublishAccess(epoch, "true", "Disabled", username, uid);
ledAccessDeniedOn();
beeperAccessDenied();
Expand All @@ -530,7 +535,7 @@ void rfidOutsideMessaging()
{
String data = String(uid) += " " + String(type);
writeEvent("WARN", "rfid", "Unknown rfid tag is scanned", data);
writeLatest(uid, "Unknown", 98);
writeLatest(uid, "Unknown", 98, ACCESS_DENIED);
DynamicJsonDocument root(512);
root["command"] = "piccscan";
root["uid"] = uid;
Expand All @@ -548,7 +553,7 @@ void rfidOutsideMessaging()
beeperAccessDenied();
} else if (uid != "" && processingState != waitingProcessing)
{
writeLatest(uid, username, accountType);
// message to the web UI to tell who has opened the door
DynamicJsonDocument root(512);
root["command"] = "piccscan";
root["uid"] = uid;
Expand Down
Loading

0 comments on commit 33a44b7

Please sign in to comment.