Skip to content

Commit

Permalink
Merge pull request #654 from esprfid/v2.0.x
Browse files Browse the repository at this point in the history
Review of #644
  • Loading branch information
matjack1 authored Sep 21, 2024
2 parents 1a65540 + 8f14702 commit 703e799
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/platformio-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
run: |
platformio run -e generic -e debug
- name: Export bins
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: bins
path: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tools-build-lin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
pkg -t node16-linux -C GZip .
- name: Export Linux binary
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: bins
path: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tools-build-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
pkg -t node16-mac -C GZip .
- name: Export Mac binary
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: bins
path: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tools-build-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
pkg -t node16-win -C GZip .
- name: Export Windows binary
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: bins
path: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# platformio

.pio
.pioenvs
.piolibdeps
.clang_complete
Expand Down
4 changes: 4 additions & 0 deletions README-MQTT.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ If the UID is in the users list, there can be a set of possible "access" configu
"access":"the access state",
"username":"username",
"uid":"token UID",
"pincode":"user pincode",
"hostname":"your esp-rfid hostname",
"doorName":"your door name"
}
Expand All @@ -280,6 +281,7 @@ If instead the UID is not present in the users list the message will be:
"access":"Denied",
"username":"Unknown",
"uid":"token UID",
"pincode":"user pincode",
"hostname":"your esp-rfid hostname"
}
```
Expand Down Expand Up @@ -308,6 +310,7 @@ In case of multiple doors managed by one esp-rfid, you'll get an array for doorn
"access":["the access state door 1", "access state door 2"],
"username":"username",
"uid":"token UID",
"pincode":"user pincode",
"hostname":"your esp-rfid hostname",
"doorName":["door 1", "door 2"]
}
Expand Down Expand Up @@ -622,6 +625,7 @@ And the message looks like this:
{
"uid":"token UID",
"username":"username",
"pincode":"user pincode",
"access":"the access state",
"time":1605991375,
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void ICACHE_RAM_ATTR loop()
if (config.openlockpin != 255 && openLockButton.fell())
{
writeLatest(" ", "Button", 1);
mqttPublishAccess(epoch, "true", "Always", "Button", " ");
mqttPublishAccess(epoch, "true", "Always", "Button", " ", " ");
activateRelay[0] = true;
beeperValidAccess();
// TODO: handle other relays
Expand Down
64 changes: 59 additions & 5 deletions src/mqtt.esp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ char mqttBuffer[MAX_MQTT_BUFFER];
struct MqttMessage {
char command[20];
char uid[20];
char pinCode[20];
char user[64];
char door[20];
char serializedMessage[MAX_MQTT_BUFFER];
Expand Down Expand Up @@ -229,6 +230,54 @@ void mqttPublishDiscovery()
user["dev"] = via;
mqttPublishEventHA(&user, topic);

DynamicJsonDocument pincode(512);
topic = "sensor/" + deviceName + "/pincode/config";
pincode["name"] = "PinCode";
pincode["uniq_id"] = deviceName + "/pincode";
pincode["stat_t"] = mtopic + "/tag";
pincode["avty_t"] = mtopic + "/avty";
pincode["val_tpl"] = "{{ value_json.pincode }}";
pincode["json_attr_t"] = mtopic + "/tag";
pincode["icon"] = "mdi:dialpad";
pincode["dev"] = via;
mqttPublishEventHA(&pincode, topic);

DynamicJsonDocument accessquestion(512);
topic = "sensor/" + deviceName + "/access/config";
accessquestion["name"] = "Access";
accessquestion["uniq_id"] = deviceName + "/access";
accessquestion["stat_t"] = mtopic + "/tag";
accessquestion["avty_t"] = mtopic + "/avty";
accessquestion["val_tpl"] = "{{ value_json.access }}";
accessquestion["json_attr_t"] = mtopic + "/tag";
accessquestion["icon"] = "mdi:lock-question";
accessquestion["dev"] = via;
mqttPublishEventHA(&accessquestion, topic);

DynamicJsonDocument doorname(512);
topic = "sensor/" + deviceName + "/doorname/config";
doorname["name"] = "DoorName";
doorname["uniq_id"] = deviceName + "/doorname";
doorname["stat_t"] = mtopic + "/tag";
doorname["avty_t"] = mtopic + "/avty";
doorname["val_tpl"] = "{{ value_json.doorName }}";
doorname["json_attr_t"] = mtopic + "/tag";
doorname["icon"] = "mdi:door";
doorname["dev"] = via;
mqttPublishEventHA(&doorname, topic);

DynamicJsonDocument timestamp(512);
topic = "sensor/" + deviceName + "/time/config";
timestamp["name"] = "Time";
timestamp["uniq_id"] = deviceName + "/time";
timestamp["stat_t"] = mtopic + "/tag";
timestamp["avty_t"] = mtopic + "/avty";
timestamp["val_tpl"] = "{{ value_json.time }}";
timestamp["json_attr_t"] = mtopic + "/tag";
timestamp["icon"] = "mdi:lock-clock";
timestamp["dev"] = via;
mqttPublishEventHA(&timestamp, topic);

DynamicJsonDocument dellog(512);
topic = "button/" + deviceName + "/dellog/config";
dellog["name"] = deviceName + " Delete Log";
Expand Down Expand Up @@ -262,7 +311,7 @@ void mqttPublishHeartbeat(time_t heartbeat, time_t uptime)
mqttPublishEvent(&root);
}

void mqttPublishAccess(time_t accesstime, String const &isknown, String const &type, String const &user, String const &uid)
void mqttPublishAccess(time_t accesstime, String const &isknown, String const &type, String const &user, String const &uid, String const &pincode)
{
DynamicJsonDocument root(512);
if (config.mqttEvents)
Expand All @@ -271,9 +320,12 @@ void mqttPublishAccess(time_t accesstime, String const &isknown, String const &t
}

root["uid"] = uid;
root["pincode"] = pincode;
root["username"] = user;
root["access"] = type;

root["time"] = accesstime;

root["doorName"] = config.doorName[0];

if (!config.mqttHA)
Expand All @@ -289,7 +341,7 @@ void mqttPublishAccess(time_t accesstime, String const &isknown, String const &t
}
}

void mqttPublishAccess(time_t accesstime, String const &isknown, int types[MAX_NUM_RELAYS], String const &user, String const &uid)
void mqttPublishAccess(time_t accesstime, String const &isknown, int types[MAX_NUM_RELAYS], String const &user, String const &uid, String const &pincode)
{
DynamicJsonDocument root(512);
JsonArray access = root.createNestedArray("access");
Expand All @@ -316,7 +368,9 @@ void mqttPublishAccess(time_t accesstime, String const &isknown, int types[MAX_N
}

root["uid"] = uid;
root["pincode"] = pincode;
root["username"] = user;

root["time"] = accesstime;

if (!config.mqttHA)
Expand Down Expand Up @@ -520,7 +574,7 @@ void processMqttMessage(MqttMessage *incomingMessage)
Serial.println("[ INFO ] Door open");
#endif
writeLatest(" ", "MQTT", 1);
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ");
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ", " ");
for (int currentRelay = 0; currentRelay < config.numRelays; currentRelay++)
{
activateRelay[currentRelay] = true;
Expand All @@ -533,7 +587,7 @@ void processMqttMessage(MqttMessage *incomingMessage)
Serial.println("[ INFO ] Door open");
#endif
writeLatest(" ", "MQTT", 1);
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ");
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ", " ");
const char *door = incomingMessage->door;
String stringDoor = String(door);
int currentRelay = stringDoor.toInt();
Expand All @@ -549,7 +603,7 @@ void processMqttMessage(MqttMessage *incomingMessage)
Serial.println("[ INFO ] Door close");
#endif
writeLatest(" ", "MQTT", 1);
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ");
mqttPublishAccess(epoch, "true", "Always", "MQTT", " ", " ");
const char *door = incomingMessage->door;
String stringDoor = String(door);
int currentRelay = stringDoor.toInt();
Expand Down
16 changes: 8 additions & 8 deletions src/rfid.esp
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,9 @@ void rfidOutsideMessaging()
#endif
writeLatest(uid, username, accountType, ACCESS_GRANTED);
if (config.numRelays == 1) {
mqttPublishAccess(epoch, "true", "Always", username, uid);
mqttPublishAccess(epoch, "true", "Always", username, uid, pinCode);
} else {
mqttPublishAccess(epoch, "true", accountTypes, username, uid);
mqttPublishAccess(epoch, "true", accountTypes, username, uid, pinCode);
}
beeperValidAccess();
}
Expand All @@ -498,9 +498,9 @@ void rfidOutsideMessaging()
ws.textAll("{\"command\":\"giveAccess\"}");
writeLatest(uid, username, accountType, ACCESS_GRANTED);
if (config.numRelays == 1) {
mqttPublishAccess(epoch, "true", "Admin", username, uid);
mqttPublishAccess(epoch, "true", "Admin", username, uid, pinCode);
} else {
mqttPublishAccess(epoch, "true", accountTypes, username, uid);
mqttPublishAccess(epoch, "true", accountTypes, username, uid, pinCode);
}
beeperAdminAccess();
}
Expand All @@ -510,14 +510,14 @@ void rfidOutsideMessaging()
Serial.println(" expired");
#endif
writeLatest(uid, username, accountType, ACCESS_DENIED);
mqttPublishAccess(epoch, "true", "Expired", username, uid);
mqttPublishAccess(epoch, "true", "Expired", username, uid, pinCode);
ledAccessDeniedOn();
beeperAccessDenied();
}
if (processingState == wrongPincode)
{
writeLatest(uid, username, accountType, ACCESS_DENIED);
mqttPublishAccess(epoch, "true", "Wrong pin code", username, uid);
mqttPublishAccess(epoch, "true", "Wrong pin code", username, uid, pinCode);
ledAccessDeniedOn();
beeperAccessDenied();
}
Expand All @@ -527,7 +527,7 @@ void rfidOutsideMessaging()
Serial.println(" does not have access");
#endif
writeLatest(uid, username, accountType, ACCESS_DENIED);
mqttPublishAccess(epoch, "true", "Disabled", username, uid);
mqttPublishAccess(epoch, "true", "Disabled", username, uid, pinCode);
ledAccessDeniedOn();
beeperAccessDenied();
}
Expand All @@ -548,7 +548,7 @@ void rfidOutsideMessaging()
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
mqttPublishAccess(epoch, "false", "Denied", "Unknown", uid);
mqttPublishAccess(epoch, "false", "Denied", "Unknown", uid, " ");
ledAccessDeniedOn();
beeperAccessDenied();
} else if (uid != "" && processingState != waitingProcessing)
Expand Down
Loading

0 comments on commit 703e799

Please sign in to comment.