add enum for PIN state

This commit is contained in:
technyon
2025-02-03 15:43:58 +01:00
parent 6e45dabd94
commit 31fed84cae
7 changed files with 48 additions and 32 deletions

View File

@@ -54,6 +54,7 @@ set(SRCFILES
../src/NukiOfficial.cpp ../src/NukiOfficial.cpp
../src/NukiPublisher.cpp ../src/NukiPublisher.cpp
../src/EspMillis.h ../src/EspMillis.h
../src/enums/NukiPinState.h
) )
file(GLOB_RECURSE SRCFILESREC file(GLOB_RECURSE SRCFILESREC

View File

@@ -5,7 +5,7 @@
#define NUKI_HUB_VERSION "9.09" #define NUKI_HUB_VERSION "9.09"
#define NUKI_HUB_VERSION_INT (uint32_t)909 #define NUKI_HUB_VERSION_INT (uint32_t)909
#define NUKI_HUB_BUILD "unknownbuildnr" #define NUKI_HUB_BUILD "unknownbuildnr"
#define NUKI_HUB_DATE "2025-01-28" #define NUKI_HUB_DATE "2025-02-03"
#define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest" #define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest"
#define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json" #define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json"

View File

@@ -5,6 +5,8 @@
#include "RestartReason.h" #include "RestartReason.h"
#include <NukiOpenerUtils.h> #include <NukiOpenerUtils.h>
#include "Config.h" #include "Config.h"
#include "enums/NukiPinState.h"
#include "enums/NukiPinState.h"
#include "hal/wdt_hal.h" #include "hal/wdt_hal.h"
#include <time.h> #include <time.h>
#include "esp_sntp.h" #include "esp_sntp.h"
@@ -439,7 +441,7 @@ bool NukiOpenerWrapper::isPinSet()
bool NukiOpenerWrapper::isPinValid() bool NukiOpenerWrapper::isPinValid()
{ {
return _preferences->getInt(preference_opener_pin_status, 4) == 1; return _preferences->getInt(preference_opener_pin_status, (int)NukiPinState::NotConfigured) == 1;
} }
void NukiOpenerWrapper::setPin(const uint16_t pin) void NukiOpenerWrapper::setPin(const uint16_t pin)
@@ -638,7 +640,7 @@ void NukiOpenerWrapper::updateConfig()
updateAuth(false); updateAuth(false);
} }
const int pinStatus = _preferences->getInt(preference_opener_pin_status, 4); const int pinStatus = _preferences->getInt(preference_opener_pin_status, (int)NukiPinState::NotConfigured);
if(isPinSet()) if(isPinSet())
{ {
@@ -665,7 +667,7 @@ void NukiOpenerWrapper::updateConfig()
Log->println(("Nuki opener PIN is invalid")); Log->println(("Nuki opener PIN is invalid"));
if(pinStatus != 2) if(pinStatus != 2)
{ {
_preferences->putInt(preference_opener_pin_status, 2); _preferences->putInt(preference_opener_pin_status, (int)NukiPinState::Invalid);
} }
} }
else else
@@ -673,7 +675,7 @@ void NukiOpenerWrapper::updateConfig()
Log->println(("Nuki opener PIN is valid")); Log->println(("Nuki opener PIN is valid"));
if(pinStatus != 1) if(pinStatus != 1)
{ {
_preferences->putInt(preference_opener_pin_status, 1); _preferences->putInt(preference_opener_pin_status, (int)NukiPinState::Valid);
} }
} }
} }
@@ -682,7 +684,7 @@ void NukiOpenerWrapper::updateConfig()
Log->println(("Nuki opener PIN is not set")); Log->println(("Nuki opener PIN is not set"));
if(pinStatus != 0) if(pinStatus != 0)
{ {
_preferences->putInt(preference_opener_pin_status, 0); _preferences->putInt(preference_opener_pin_status, (int)NukiPinState::NotSet);
} }
} }
} }
@@ -4078,7 +4080,7 @@ void NukiOpenerWrapper::notify(Nuki::EventType eventType)
} }
else if(eventType == Nuki::EventType::ERROR_BAD_PIN) else if(eventType == Nuki::EventType::ERROR_BAD_PIN)
{ {
_preferences->putInt(preference_lock_pin_status, 2); _preferences->putInt(preference_lock_pin_status, (int)NukiPinState::Invalid);
} }
else if(eventType == Nuki::EventType::BLE_ERROR_ON_DISCONNECT) else if(eventType == Nuki::EventType::BLE_ERROR_ON_DISCONNECT)
{ {

View File

@@ -5,6 +5,7 @@
#include "RestartReason.h" #include "RestartReason.h"
#include <NukiLockUtils.h> #include <NukiLockUtils.h>
#include "Config.h" #include "Config.h"
#include "enums/NukiPinState.h"
#include "hal/wdt_hal.h" #include "hal/wdt_hal.h"
#include <time.h> #include <time.h>
#include "esp_sntp.h" #include "esp_sntp.h"
@@ -464,7 +465,7 @@ bool NukiWrapper::isPinSet()
bool NukiWrapper::isPinValid() bool NukiWrapper::isPinValid()
{ {
return _preferences->getInt(preference_lock_pin_status, 4) == 1; return _preferences->getInt(preference_lock_pin_status, (int)NukiPinState::NotConfigured) == 1;
} }
void NukiWrapper::setPin(const uint16_t pin) void NukiWrapper::setPin(const uint16_t pin)
@@ -658,7 +659,7 @@ void NukiWrapper::updateConfig()
updateAuth(false); updateAuth(false);
} }
const int pinStatus = _preferences->getInt(preference_lock_pin_status, 4); const int pinStatus = _preferences->getInt(preference_lock_pin_status, (int)NukiPinState::NotConfigured);
if(isPinSet()) if(isPinSet())
{ {
@@ -684,7 +685,7 @@ void NukiWrapper::updateConfig()
Log->println(("Nuki Lock PIN is invalid")); Log->println(("Nuki Lock PIN is invalid"));
if(pinStatus != 2) if(pinStatus != 2)
{ {
_preferences->putInt(preference_lock_pin_status, 2); _preferences->putInt(preference_lock_pin_status, (int)NukiPinState::Invalid);
} }
} }
else else
@@ -692,7 +693,7 @@ void NukiWrapper::updateConfig()
Log->println(("Nuki Lock PIN is valid")); Log->println(("Nuki Lock PIN is valid"));
if(pinStatus != 1) if(pinStatus != 1)
{ {
_preferences->putInt(preference_lock_pin_status, 1); _preferences->putInt(preference_lock_pin_status, (int)NukiPinState::Valid);
} }
} }
} }
@@ -701,7 +702,7 @@ void NukiWrapper::updateConfig()
Log->println(("Nuki Lock PIN is not set")); Log->println(("Nuki Lock PIN is not set"));
if(pinStatus != 0) if(pinStatus != 0)
{ {
_preferences->putInt(preference_lock_pin_status, 0); _preferences->putInt(preference_lock_pin_status, (int)NukiPinState::NotSet);
} }
} }
} }
@@ -4194,7 +4195,7 @@ void NukiWrapper::notify(Nuki::EventType eventType)
} }
else if(eventType == Nuki::EventType::ERROR_BAD_PIN) else if(eventType == Nuki::EventType::ERROR_BAD_PIN)
{ {
_preferences->putInt(preference_lock_pin_status, 2); _preferences->putInt(preference_lock_pin_status, (int)NukiPinState::Invalid);
} }
else if(eventType == Nuki::EventType::BLE_ERROR_ON_DISCONNECT) else if(eventType == Nuki::EventType::BLE_ERROR_ON_DISCONNECT)
{ {

View File

@@ -4970,12 +4970,12 @@ esp_err_t WebCfgServer::buildHtml(PsychicRequest *request, PsychicResponse* resp
if(_nuki->isPaired()) if(_nuki->isPaired())
{ {
String lockState = pinStateToString(_preferences->getInt(preference_lock_pin_status, 4)); const String lockState = pinStateToString((NukiPinState)_preferences->getInt(preference_lock_pin_status, (int)NukiPinState::NotConfigured));
printParameter(&response, "Nuki Lock PIN status", lockState.c_str(), "", "lockPin"); printParameter(&response, "Nuki Lock PIN status", lockState.c_str(), "", "lockPin");
if(_preferences->getBool(preference_official_hybrid_enabled, false)) if(_preferences->getBool(preference_official_hybrid_enabled, false))
{ {
String offConnected = _nuki->offConnected() ? "Yes": "No"; const String offConnected = _nuki->offConnected() ? "Yes": "No";
printParameter(&response, "Nuki Lock hybrid mode connected", offConnected.c_str(), "", "lockHybrid"); printParameter(&response, "Nuki Lock hybrid mode connected", offConnected.c_str(), "", "lockHybrid");
} }
} }
@@ -4996,7 +4996,7 @@ esp_err_t WebCfgServer::buildHtml(PsychicRequest *request, PsychicResponse* resp
} }
if(_nukiOpener->isPaired()) if(_nukiOpener->isPaired())
{ {
String openerState = pinStateToString(_preferences->getInt(preference_opener_pin_status, 4)); String openerState = pinStateToString((NukiPinState)_preferences->getInt(preference_opener_pin_status, (int)NukiPinState::NotConfigured));
printParameter(&response, "Nuki Opener PIN status", openerState.c_str(), "", "openerPin"); printParameter(&response, "Nuki Opener PIN status", openerState.c_str(), "", "openerPin");
} }
} }
@@ -5549,7 +5549,7 @@ esp_err_t WebCfgServer::buildStatusHtml(PsychicRequest *request, PsychicResponse
if(_nuki->isPaired()) if(_nuki->isPaired())
{ {
json["lockPin"] = pinStateToString(_preferences->getInt(preference_lock_pin_status, 4)); json["lockPin"] = pinStateToString((NukiPinState)_preferences->getInt(preference_lock_pin_status, (int)NukiPinState::NotConfigured));
if(strcmp(lockStateArr, "undefined") != 0) if(strcmp(lockStateArr, "undefined") != 0)
{ {
lockDone = true; lockDone = true;
@@ -5583,7 +5583,7 @@ esp_err_t WebCfgServer::buildStatusHtml(PsychicRequest *request, PsychicResponse
if(_nukiOpener->isPaired()) if(_nukiOpener->isPaired())
{ {
json["openerPin"] = pinStateToString(_preferences->getInt(preference_opener_pin_status, 4)); json["openerPin"] = pinStateToString((NukiPinState)_preferences->getInt(preference_opener_pin_status, (int)NukiPinState::NotConfigured));
if(strcmp(openerStateArr, "undefined") != 0) if(strcmp(openerStateArr, "undefined") != 0)
{ {
openerDone = true; openerDone = true;
@@ -5616,16 +5616,17 @@ esp_err_t WebCfgServer::buildStatusHtml(PsychicRequest *request, PsychicResponse
return resp->send(); return resp->send();
} }
String WebCfgServer::pinStateToString(uint8_t value) const String WebCfgServer::pinStateToString(const NukiPinState& value) const
{ {
switch(value) switch(value)
{ {
case 0: case NukiPinState::NotSet:
return String("PIN not set"); return String("PIN not set");
case 1: case NukiPinState::Valid:
return String("PIN valid"); return String("PIN valid");
case 2: case NukiPinState::Invalid:
return String("PIN set but invalid"); return String("PIN set but invalid");
case NukiPinState::NotConfigured:
default: default:
return String("Unknown"); return String("Unknown");
} }
@@ -6701,12 +6702,12 @@ esp_err_t WebCfgServer::processUnpair(PsychicRequest *request, PsychicResponse*
if(!opener && _nuki != nullptr) if(!opener && _nuki != nullptr)
{ {
_nuki->unpair(); _nuki->unpair();
_preferences->putInt(preference_lock_pin_status, 4); _preferences->putInt(preference_lock_pin_status, (int)NukiPinState::NotConfigured);
} }
if(opener && _nukiOpener != nullptr) if(opener && _nukiOpener != nullptr)
{ {
_nukiOpener->unpair(); _nukiOpener->unpair();
_preferences->putInt(preference_opener_pin_status, 4); _preferences->putInt(preference_opener_pin_status, (int)NukiPinState::NotConfigured);
} }
_network->disableHASS(); _network->disableHASS();
@@ -7052,7 +7053,7 @@ const std::vector<std::pair<String, String>> WebCfgServer::getGpioOptions() cons
return options; return options;
} }
String WebCfgServer::getPreselectionForGpio(const uint8_t &pin) const String WebCfgServer::getPreselectionForGpio(const uint8_t &pin) const
{ {
const std::vector<PinEntry>& pinConfiguration = _gpio->pinConfiguration(); const std::vector<PinEntry>& pinConfiguration = _gpio->pinConfiguration();

View File

@@ -2,6 +2,8 @@
#include <Preferences.h> #include <Preferences.h>
#include <PsychicHttp.h> #include <PsychicHttp.h>
#include "enums/NukiPinState.h"
#ifdef CONFIG_ESP_HTTPS_SERVER_ENABLE #ifdef CONFIG_ESP_HTTPS_SERVER_ENABLE
#include <PsychicHttpsServer.h> #include <PsychicHttpsServer.h>
#endif #endif
@@ -83,8 +85,8 @@ private:
const std::vector<std::pair<String, String>> getNetworkCustomCLKOptions() const; const std::vector<std::pair<String, String>> getNetworkCustomCLKOptions() const;
#endif #endif
String getPreselectionForGpio(const uint8_t& pin); const String getPreselectionForGpio(const uint8_t& pin) const;
String pinStateToString(uint8_t value); const String pinStateToString(const NukiPinState& value) const;
void printParameter(PsychicStreamResponse *response, const char* description, const char* value, const char *link = "", const char *id = ""); void printParameter(PsychicStreamResponse *response, const char* description, const char* value, const char *link = "", const char *id = "");

9
src/enums/NukiPinState.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
enum class NukiPinState
{
NotSet = 0,
Valid = 1,
Invalid = 2,
NotConfigured = 4 // default value used for preferences.getInt() when not configured by user
};