Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NFC tag MQTT ignore option #147

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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