allow configure query interval via web server

This commit is contained in:
technyon
2022-03-27 12:15:09 +02:00
parent ba864e0b67
commit a8ce3fc44c
7 changed files with 80 additions and 48 deletions

View File

@@ -44,7 +44,7 @@ void Network::initialize()
const char* brokerAddr = _preferences->getString(preference_mqtt_broker).c_str(); const char* brokerAddr = _preferences->getString(preference_mqtt_broker).c_str();
strcpy(_mqttBrokerAddr, brokerAddr); strcpy(_mqttBrokerAddr, brokerAddr);
Serial.print("MQTT Broker: "); Serial.print(F("MQTT Broker: "));
Serial.println(_mqttBrokerAddr); Serial.println(_mqttBrokerAddr);
_mqttClient.setServer(_mqttBrokerAddr, 1883); _mqttClient.setServer(_mqttBrokerAddr, 1883);
_mqttClient.setCallback(Network::onMqttDataReceivedCallback); _mqttClient.setCallback(Network::onMqttDataReceivedCallback);
@@ -116,7 +116,7 @@ void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &len
{ {
if(strcmp(value, "") == 0) return; if(strcmp(value, "") == 0) return;
Serial.print("lockstate setpoint received: "); Serial.print(F("Lockstate action received: "));
Serial.println(value); Serial.println(value);
if(_lockActionReceivedCallback != NULL) if(_lockActionReceivedCallback != NULL)
{ {

View File

@@ -1,11 +1,13 @@
#include "Nuki.h" #include "Nuki.h"
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include "PreferencesKeys.h"
Nuki* nukiInst; Nuki* nukiInst;
Nuki::Nuki(const std::string& name, uint32_t id, Network* network) Nuki::Nuki(const std::string& name, uint32_t id, Network* network, Preferences* preferences)
: _nukiBle(name, id), : _nukiBle(name, id),
_network(network) _network(network),
_preferences(preferences)
{ {
nukiInst = this; nukiInst = this;
@@ -20,6 +22,25 @@ Nuki::Nuki(const std::string& name, uint32_t id, Network* network)
void Nuki::initialize() void Nuki::initialize()
{ {
_nukiBle.initialize(); _nukiBle.initialize();
_intervalLockstate = _preferences->getInt(preference_query_interval_lockstate);
_intervalBattery = _preferences->getInt(preference_query_interval_battery);
if(_intervalLockstate == 0)
{
_intervalLockstate = 30;
_preferences->putInt(preference_query_interval_lockstate, _intervalLockstate);
}
if(_intervalBattery == 0)
{
_intervalBattery = 60 * 30;
_preferences->putInt(preference_query_interval_battery, _intervalBattery);
}
Serial.print(F("Lock state interval: "));
Serial.print(_intervalLockstate);
Serial.print(F("| Battery interval: "));
Serial.println(_intervalBattery);
} }
void Nuki::update() void Nuki::update()
@@ -44,35 +65,33 @@ void Nuki::update()
if(_nextLockStateUpdateTs == 0 || ts >= _nextLockStateUpdateTs) if(_nextLockStateUpdateTs == 0 || ts >= _nextLockStateUpdateTs)
{ {
_nextLockStateUpdateTs = ts + 5000; _nextLockStateUpdateTs = ts + _intervalLockstate * 1000;
updateKeyTurnerState(); updateKeyTurnerState();
} }
if(_nextBatteryReportTs == 0 || ts > _nextBatteryReportTs) if(_nextBatteryReportTs == 0 || ts > _nextBatteryReportTs)
{ {
_nextBatteryReportTs = ts + 60000 * 5; _nextBatteryReportTs = ts + _intervalBattery * 1000;
updateBatteryState(); updateBatteryState();
} }
if(_nextLockAction != (LockAction)0xff) if(_nextLockAction != (LockAction)0xff)
{ {
_nukiBle.lockAction(_nextLockAction, 0, 0); _nukiBle.lockAction(_nextLockAction, 0, 0);
_nextLockAction = (LockAction)0xff; _nextLockAction = (LockAction)0xff;
// _nextLockStateUpdateTs = ts + 11000;
} }
} }
void Nuki::updateKeyTurnerState() void Nuki::updateKeyTurnerState()
{ {
_nukiBle.requestKeyTurnerState(&_keyTurnerState); _nukiBle.requestKeyTurnerState(&_keyTurnerState);
char str[20]; char str[20];
lockstateToString(_keyTurnerState.lockState, str); lockstateToString(_keyTurnerState.lockState, str);
Serial.print(F("Nuki lock state: "));
Serial.println(str);
if(_keyTurnerState.lockState != _lastKeyTurnerState.lockState) if(_keyTurnerState.lockState != _lastKeyTurnerState.lockState)
{ {
_network->publishKeyTurnerState(str); _network->publishKeyTurnerState(str);
Serial.print(F("Nuki lock state: "));
Serial.println(str);
} }
memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(KeyTurnerState)); memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(KeyTurnerState));
@@ -83,12 +102,12 @@ void Nuki::updateBatteryState()
{ {
_nukiBle.requestBatteryReport(&_batteryReport); _nukiBle.requestBatteryReport(&_batteryReport);
Serial.print("Voltage: "); Serial.println(_batteryReport.batteryVoltage); Serial.print(F("Voltage: ")); Serial.println(_batteryReport.batteryVoltage);
Serial.print("Drain: "); Serial.println(_batteryReport.batteryDrain); Serial.print(F("Drain: ")); Serial.println(_batteryReport.batteryDrain);
Serial.print("Resistance: "); Serial.println(_batteryReport.batteryResistance); Serial.print(F("Resistance: ")); Serial.println(_batteryReport.batteryResistance);
Serial.print("Max Current: "); Serial.println(_batteryReport.maxTurnCurrent); Serial.print(F("Max Current: ")); Serial.println(_batteryReport.maxTurnCurrent);
Serial.print("Crit. State: "); Serial.println(_batteryReport.criticalBatteryState); Serial.print(F("Crit. State: ")); Serial.println(_batteryReport.criticalBatteryState);
Serial.print("Lock Dist: "); Serial.println(_batteryReport.lockDistance); Serial.print(F("Lock Dist: ")); Serial.println(_batteryReport.lockDistance);
_network->publishBatteryVoltage((float)_batteryReport.batteryVoltage / (float)1000); _network->publishBatteryVoltage((float)_batteryReport.batteryVoltage / (float)1000);
} }
@@ -152,6 +171,6 @@ LockAction Nuki::lockActionToEnum(const char *str)
void Nuki::onLockActionReceived(const char *value) void Nuki::onLockActionReceived(const char *value)
{ {
nukiInst->_nextLockAction = nukiInst->lockActionToEnum(value); nukiInst->_nextLockAction = nukiInst->lockActionToEnum(value);
Serial.print("Action: "); Serial.print(F("Action: "));
Serial.println((int)nukiInst->_nextLockAction); Serial.println((int)nukiInst->_nextLockAction);
} }

5
Nuki.h
View File

@@ -7,7 +7,7 @@
class Nuki class Nuki
{ {
public: public:
Nuki(const std::string& name, uint32_t id, Network* network); Nuki(const std::string& name, uint32_t id, Network* network, Preferences* preferences);
void initialize(); void initialize();
void update(); void update();
@@ -23,6 +23,9 @@ private:
NukiBle _nukiBle; NukiBle _nukiBle;
Network* _network; Network* _network;
Preferences* _preferences;
int _intervalLockstate = 0; // seconds
int _intervalBattery = 0; // seconds
KeyTurnerState _lastKeyTurnerState; KeyTurnerState _lastKeyTurnerState;
KeyTurnerState _keyTurnerState; KeyTurnerState _keyTurnerState;

View File

@@ -1,3 +1,8 @@
#pragma once #pragma once
#define preference_mqtt_broker "mqttbroker" #define preference_mqtt_broker "mqttbroker"
#define preference_query_interval_lockstate "lockStInterval"
#define preference_query_interval_battery "batInterval"

View File

@@ -61,12 +61,21 @@ void WebCfgServer::update()
TokenType lastTokenType = getParameterType(lastToken); TokenType lastTokenType = getParameterType(lastToken);
TokenType tokenType = getParameterType(token); TokenType tokenType = getParameterType(token);
if(lastTokenType == TokenType::MQTT_SERVER && tokenType == TokenType::NONE) if(lastTokenType == TokenType::MqttServer && tokenType == TokenType::None)
{ {
configChanged = true;
_preferences->putString(preference_mqtt_broker, token); _preferences->putString(preference_mqtt_broker, token);
configChanged = true; configChanged = true;
} }
else if(lastTokenType == TokenType::QueryIntervalLockstate && tokenType == TokenType::None)
{
_preferences->putInt(preference_query_interval_lockstate, String(token).toInt());
configChanged = true;
}
else if(lastTokenType == TokenType::QueryIntervalBattery && tokenType == TokenType::None)
{
_preferences->putInt(preference_query_interval_battery, String(token).toInt());
configChanged = true;
}
} }
lastToken = token; lastToken = token;
token = strtok(NULL, "?=&"); token = strtok(NULL, "?=&");
@@ -104,25 +113,13 @@ void WebCfgServer::serveHtml(WiFiClient &client)
client.print(_preferences->getString(preference_mqtt_broker)); client.print(_preferences->getString(preference_mqtt_broker));
client.println("\" NAME=\"MQTTSERVER\" SIZE=\"25\" MAXLENGTH=\"40\"><BR>"); client.println("\" NAME=\"MQTTSERVER\" SIZE=\"25\" MAXLENGTH=\"40\"><BR>");
// client.print("DNS Server: <INPUT TYPE=TEXT VALUE=\""); client.print("Query interval lock state (seconds): <INPUT TYPE=TEXT VALUE=\"");
// client.print(_configuration->dnsServerAddress); client.print(_preferences->getInt(preference_query_interval_lockstate));
// client.println("\" NAME=\"DNSSERVER\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>"); client.println("\" NAME=\"LSTINT\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
//
// client.print("Gateway: <INPUT TYPE=TEXT VALUE=\""); client.print("Query interval battery (seconds): <INPUT TYPE=TEXT VALUE=\"");
// client.print(_configuration->gatewayAddress); client.print(_preferences->getInt(preference_query_interval_battery));
// client.println("\" NAME=\"GATEWAY\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>"); client.println("\" NAME=\"BATINT\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
//
// client.print("IP Address: <INPUT TYPE=TEXT VALUE=\"");
// client.print(_configuration->ipAddress);
// client.println("\" NAME=\"IPADDRESS\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
//
// client.print("Subnet mask: <INPUT TYPE=TEXT VALUE=\"");
// client.print(_configuration->subnetMask);
// client.println("\" NAME=\"SUBNET\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
//
// client.print("MQTT publish interval (ms): <INPUT TYPE=TEXT VALUE=\"");
// client.print(_configuration->mqttPublishInterval);
// client.println("\" NAME=\"INTERVAL\" SIZE=\"25\" MAXLENGTH=\"6\"><BR>");
client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Save\">"); client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Save\">");
@@ -132,17 +129,22 @@ void WebCfgServer::serveHtml(WiFiClient &client)
client.println("</BODY>"); client.println("</BODY>");
client.println("</HTML>"); client.println("</HTML>");
} }
TokenType WebCfgServer::getParameterType(char *&token) TokenType WebCfgServer::getParameterType(char *&token)
{ {
if (strcmp(token, "MQTTSERVER") == 0) if (strcmp(token, "MQTTSERVER") == 0)
{ {
return TokenType::MQTT_SERVER; return TokenType::MqttServer;
}
if (strcmp(token, "LSTINT") == 0)
{
return TokenType::QueryIntervalLockstate;
}
if (strcmp(token, "BATINT") == 0)
{
return TokenType::QueryIntervalBattery;
} }
return TokenType::NONE; return TokenType::None;
} }

View File

@@ -5,8 +5,10 @@
enum class TokenType enum class TokenType
{ {
NONE, None,
MQTT_SERVER, MqttServer,
QueryIntervalLockstate,
QueryIntervalBattery,
}; };
class WebCfgServer class WebCfgServer
@@ -21,6 +23,7 @@ public:
private: private:
void serveHtml(WiFiClient& client); void serveHtml(WiFiClient& client);
TokenType getParameterType(char*& token); TokenType getParameterType(char*& token);
WiFiServer _wifiServer; WiFiServer _wifiServer;

View File

@@ -40,7 +40,7 @@ void setup()
preferences->begin("nukihub", false); preferences->begin("nukihub", false);
network = new Network(preferences); network = new Network(preferences);
webCfgServer = new WebCfgServer(preferences); webCfgServer = new WebCfgServer(preferences);
nuki = new Nuki("Main Door", 2020001, network); nuki = new Nuki("Main Door", 2020001, network, preferences);
network->initialize(); network->initialize();
webCfgServer->initialize(); webCfgServer->initialize();