Skip to content

Commit

Permalink
Add NFC tag MQTT ignore option (#147)
Browse files Browse the repository at this point in the history
### TL;DR
Added a new option to ignore NFC tag MQTT publishing

### What changed?
- Added a new toggle in the MQTT configuration page to enable/disable NFC tag MQTT publishing
- Introduced `nfcTagNoPublish` boolean flag in the ESP configuration
- Added corresponding HTML processing and form handling for the new setting

### How to test?
1. Navigate to the MQTT configuration page
2. Locate the new "Ignore NFC Tags" dropdown under Core Topics
3. Toggle between Enabled/Disabled states
4. Save the configuration
5. Verify that NFC tag events are published or suppressed based on the selected setting

### Why make this change?
This change provides users with the ability to control whether NFC tag events are published via MQTT, offering more granular control over MQTT traffic and potentially reducing unnecessary network communication when NFC tag publishing isn't needed.
  • Loading branch information
rednblkx authored Nov 17, 2024
2 parents 56c056c + fcb57b7 commit b2007fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion data/mqtt.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ <h3 style="margin-top: 0;">Core Topics</h3>
<label for="mqtt-hktopic">NFC/HK Topic</label>
<input type="text" name="mqtt-hktopic" id="mqtt-hktopic" placeholder="topic/auth" required value="%HKTOPIC%">
</div>
<div style="display: flex;flex-direction: column;">
<label for="nfc-tags-ignore-mqtt">Ignore NFC Tags</label>
<select name="nfc-tags-ignore-mqtt" id="nfc-tags-ignore-mqtt">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
</div>
<div style="display: flex; flex-direction: column;">
<label for="mqtt-statetopic">Lock State Topic</label>
<input type="text" name="mqtt-statetopic" id="mqtt-statetopic" placeholder="topic/state" required value="%STATETOPIC%">
Expand All @@ -75,7 +82,7 @@ <h3 style="margin-top: 0;">Core Topics</h3>
<div style="display:flex;flex-direction: column;border: 2px #8e8271 dashed;padding: 1rem;">
<h3 style="margin-top: 0;">Custom State Topics</h3>
<div style="display: flex;flex-direction: column;gap: 8px;">
<div>
<div style="display: flex;flex-direction: column;">
<label for="mqtt-customstate-enable">MQTT Custom State</label>
<select name="mqtt-customstate-enable" id="mqtt-customstate-enable">
<option value="0">Disabled</option>
Expand Down Expand Up @@ -149,6 +156,7 @@ <h3 style="margin-top: 0;">Custom State Topics</h3>
<script>
document.getElementById("mqtt-customstate-enable").selectedIndex = "%CUSTOMSTATE_ENABLED%";
document.getElementById("mqtt-discovery-enable").selectedIndex = "%DISCOVERY_ENABLED%";
document.getElementById("nfc-tags-ignore-mqtt").selectedIndex = "%NFCTAGSNOPUBLISH%";
let form = document.getElementById("mqttConfig");
async function handleForm(event) {
event.preventDefault();
Expand Down
10 changes: 8 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ namespace espConfig
/* Flags */
bool lockEnableCustomState = MQTT_CUSTOM_STATE_ENABLED;
bool hassMqttDiscoveryEnabled = MQTT_DISCOVERY;
bool nfcTagNoPublish = false;
std::map<std::string, int> customLockStates = { {"C_LOCKED", C_LOCKED}, {"C_UNLOCKING", C_UNLOCKING}, {"C_UNLOCKED", C_UNLOCKED}, {"C_LOCKING", C_LOCKING}, {"C_JAMMED", C_JAMMED}, {"C_UNKNOWN", C_UNKNOWN} };
std::map<std::string, int> customLockActions = { {"UNLOCK", UNLOCK}, {"LOCK", LOCK} };
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(espConfig::mqttConfig_t, mqttBroker, mqttPort, mqttUsername, mqttPassword, mqttClientId, lwtTopic, hkTopic, lockStateTopic, lockStateCmd, lockCStateCmd, lockTStateCmd, lockCustomStateTopic, lockCustomStateCmd, lockEnableCustomState, hassMqttDiscoveryEnabled, customLockStates, customLockActions)
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(espConfig::mqttConfig_t, mqttBroker, mqttPort, mqttUsername, mqttPassword, mqttClientId, lwtTopic, hkTopic, lockStateTopic, lockStateCmd, lockCStateCmd, lockTStateCmd, lockCustomStateTopic, lockCustomStateCmd, lockEnableCustomState, hassMqttDiscoveryEnabled, customLockStates, customLockActions, nfcTagNoPublish)
} mqttData;

struct misc_config_t
Expand Down Expand Up @@ -871,6 +872,8 @@ String mqttHtmlProcess(const String& var) {
return String(espConfig::mqttData.customLockStates["C_JAMMED"]);
} else if (var == "CSTATEUNKNOWN") {
return String(espConfig::mqttData.customLockStates["C_UNKNOWN"]);
} else if (var == "NFCTAGSNOPUBLISH") {
return String(espConfig::mqttData.nfcTagNoPublish);
}
return "";
}
Expand Down Expand Up @@ -1014,6 +1017,8 @@ void setupWeb() {
espConfig::mqttData.customLockStates["C_JAMMED"] = p->value().toInt();
} else if (!strcmp(p->name().c_str(), "cstate-unknown")) {
espConfig::mqttData.customLockStates["C_UNKNOWN"] = p->value().toInt();
} else if (!strcmp(p->name().c_str(), "nfc-tags-ignore-mqtt")) {
espConfig::mqttData.nfcTagNoPublish = p->value().toInt();
}
}
json json_mqtt_config = espConfig::mqttData;
Expand Down Expand Up @@ -1302,6 +1307,7 @@ void nfc_thread_entry(void* arg) {
uint8_t data[13] = { 0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0, 0x00, 0x00, 0x08, 0x58, 0x01, 0x01, 0x0 };
uint8_t selectCmdRes[9];
uint16_t selectCmdResLength = 9;
LOG(I, "Requesting supported HomeKey versions");
LOG(D, "SELECT HomeKey Applet, APDU: ");
ESP_LOG_BUFFER_HEX_LEVEL(TAG, data, sizeof(data), ESP_LOG_VERBOSE);
bool status = nfc.inDataExchange(data, sizeof(data), selectCmdRes, &selectCmdResLength);
Expand Down Expand Up @@ -1373,7 +1379,7 @@ void nfc_thread_entry(void* arg) {
LOG(W, "We got status FlowFailed, mqtt untouched!");
}
nfc.setRFField(0x02, 0x01);
} else {
} else if(!espConfig::mqttData.nfcTagNoPublish) {
LOG(W, "Invalid Response, probably not Homekey, publishing target's UID");
bool status = false;
if (espConfig::miscConfig.nfcSuccessPin != 255) {
Expand Down

0 comments on commit b2007fc

Please sign in to comment.