261 lines
7.9 KiB
C++
261 lines
7.9 KiB
C++
#include "NetworkOpener.h"
|
|
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
|
#include "Arduino.h"
|
|
#include "MqttTopics.h"
|
|
#include "PreferencesKeys.h"
|
|
#include "Pins.h"
|
|
|
|
NetworkOpener* nwInstOpener;
|
|
|
|
NetworkOpener::NetworkOpener(Network* network, Preferences* preferences)
|
|
: _preferences(preferences),
|
|
_network(network)
|
|
{
|
|
nwInstOpener = this;
|
|
|
|
_configTopics.reserve(5);
|
|
// _configTopics.push_back(mqtt_topic_config_button_enabled);
|
|
// _configTopics.push_back(mqtt_topic_config_led_enabled);
|
|
// _configTopics.push_back(mqtt_topic_config_led_brightness);
|
|
// _configTopics.push_back(mqtt_topic_config_auto_unlock);
|
|
// _configTopics.push_back(mqtt_topic_config_auto_lock);
|
|
}
|
|
|
|
void NetworkOpener::initialize()
|
|
{
|
|
String mqttPath = _preferences->getString(preference_mqtt_opener_path);
|
|
if(mqttPath.length() > 0)
|
|
{
|
|
size_t len = mqttPath.length();
|
|
for(int i=0; i < len; i++)
|
|
{
|
|
_mqttPath[i] = mqttPath.charAt(i);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
strcpy(_mqttPath, "nukiopener");
|
|
_preferences->putString(preference_mqtt_opener_path, _mqttPath);
|
|
}
|
|
|
|
_network->setMqttDataReceivedForwardCallback(nwInstOpener->onMqttDataReceivedCallback);
|
|
}
|
|
|
|
void NetworkOpener::update()
|
|
{
|
|
bool connected = _network->device()->mqttClient()->connected();
|
|
|
|
if(!_isConnected && connected)
|
|
{
|
|
subscribe(mqtt_topic_lock_action);
|
|
}
|
|
|
|
_isConnected = connected;
|
|
|
|
// long ts = millis();
|
|
|
|
}
|
|
|
|
void NetworkOpener::onMqttDataReceivedCallback(char *topic, byte *payload, unsigned int length)
|
|
{
|
|
nwInstOpener->onMqttDataReceived(topic, payload, length);
|
|
}
|
|
|
|
void NetworkOpener::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
|
|
{
|
|
char value[50] = {0};
|
|
size_t l = min(length, sizeof(value)-1);
|
|
|
|
for(int i=0; i<l; i++)
|
|
{
|
|
value[i] = payload[i];
|
|
}
|
|
|
|
if(comparePrefixedPath(topic, mqtt_topic_lock_action))
|
|
{
|
|
if(strcmp(value, "") == 0 || strcmp(value, "ack") == 0 || strcmp(value, "unknown_action") == 0) return;
|
|
|
|
Serial.print(F("Opener lock action received: "));
|
|
Serial.println(value);
|
|
bool success = false;
|
|
if(_lockActionReceivedCallback != NULL)
|
|
{
|
|
success = _lockActionReceivedCallback(value);
|
|
}
|
|
publishString(mqtt_topic_lock_action, success ? "ack" : "unknown_action");
|
|
}
|
|
|
|
for(auto configTopic : _configTopics)
|
|
{
|
|
if(comparePrefixedPath(topic, configTopic))
|
|
{
|
|
if(_configUpdateReceivedCallback != nullptr)
|
|
{
|
|
_configUpdateReceivedCallback(configTopic, value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState)
|
|
{
|
|
char str[50];
|
|
|
|
if((_firstTunerStatePublish || keyTurnerState.lockState != lastKeyTurnerState.lockState) && keyTurnerState.lockState != NukiOpener::LockState::Undefined)
|
|
{
|
|
memset(&str, 0, sizeof(str));
|
|
lockstateToString(keyTurnerState.lockState, str);
|
|
publishString(mqtt_topic_lock_state, str);
|
|
}
|
|
|
|
if(_firstTunerStatePublish || keyTurnerState.trigger != lastKeyTurnerState.trigger)
|
|
{
|
|
memset(&str, 0, sizeof(str));
|
|
triggerToString(keyTurnerState.trigger, str);
|
|
publishString(mqtt_topic_lock_trigger, str);
|
|
}
|
|
|
|
if(_firstTunerStatePublish || keyTurnerState.lastLockActionCompletionStatus != lastKeyTurnerState.lastLockActionCompletionStatus)
|
|
{
|
|
memset(&str, 0, sizeof(str));
|
|
NukiOpener::completionStatusToString(keyTurnerState.lastLockActionCompletionStatus, str);
|
|
publishString(mqtt_topic_lock_completionStatus, str);
|
|
}
|
|
|
|
if(_firstTunerStatePublish || keyTurnerState.doorSensorState != lastKeyTurnerState.doorSensorState)
|
|
{
|
|
memset(&str, 0, sizeof(str));
|
|
NukiLock::doorSensorStateToString(keyTurnerState.doorSensorState, str);
|
|
publishString(mqtt_topic_door_sensor_state, str);
|
|
}
|
|
|
|
if(_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState)
|
|
{
|
|
uint8_t level = (keyTurnerState.criticalBatteryState & 0b11111100) >> 1;
|
|
bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0;
|
|
bool charging = (keyTurnerState.criticalBatteryState & 0b00000010) > 0;
|
|
publishInt(mqtt_topic_battery_level, level); // percent
|
|
publishBool(mqtt_topic_battery_critical, critical);
|
|
publishBool(mqtt_topic_battery_charging, charging);
|
|
}
|
|
|
|
_firstTunerStatePublish = false;
|
|
}
|
|
|
|
void NetworkOpener::publishAuthorizationInfo(const uint32_t authId, const char *authName)
|
|
{
|
|
publishUInt(mqtt_topic_lock_auth_id, authId);
|
|
publishString(mqtt_topic_lock_auth_name, authName);
|
|
}
|
|
|
|
void NetworkOpener::publishCommandResult(const char *resultStr)
|
|
{
|
|
publishString(mqtt_topic_lock_action_command_result, resultStr);
|
|
}
|
|
|
|
void NetworkOpener::publishBatteryReport(const NukiOpener::BatteryReport& batteryReport)
|
|
{
|
|
publishFloat(mqtt_topic_battery_voltage, (float)batteryReport.batteryVoltage / 1000.0);
|
|
publishInt(mqtt_topic_battery_drain, batteryReport.batteryDrain); // milliwatt seconds
|
|
}
|
|
|
|
void NetworkOpener::publishConfig(const NukiOpener::Config &config)
|
|
{
|
|
publishBool(mqtt_topic_config_button_enabled, config.buttonEnabled == 1);
|
|
publishBool(mqtt_topic_config_led_enabled, config.ledFlashEnabled == 1);
|
|
}
|
|
|
|
void NetworkOpener::publishAdvancedConfig(const NukiOpener::AdvancedConfig &config)
|
|
{
|
|
// publishBool(mqtt_topic_config_auto_unlock, config.autoUnLockDisabled == 0);
|
|
// publishBool(mqtt_topic_config_auto_lock, config.autoLockEnabled == 1);
|
|
}
|
|
|
|
void NetworkOpener::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *))
|
|
{
|
|
_lockActionReceivedCallback = lockActionReceivedCallback;
|
|
}
|
|
|
|
void NetworkOpener::setConfigUpdateReceivedCallback(void (*configUpdateReceivedCallback)(const char *, const char *))
|
|
{
|
|
_configUpdateReceivedCallback = configUpdateReceivedCallback;
|
|
}
|
|
|
|
void NetworkOpener::publishFloat(const char* topic, const float value, const uint8_t precision)
|
|
{
|
|
char str[30];
|
|
dtostrf(value, 0, precision, str);
|
|
char path[200] = {0};
|
|
buildMqttPath(topic, path);
|
|
_network->device()->mqttClient()->publish(path, str);
|
|
}
|
|
|
|
void NetworkOpener::publishInt(const char *topic, const int value)
|
|
{
|
|
char str[30];
|
|
itoa(value, str, 10);
|
|
char path[200] = {0};
|
|
buildMqttPath(topic, path);
|
|
_network->device()->mqttClient()->publish(path, str);
|
|
}
|
|
|
|
void NetworkOpener::publishUInt(const char *topic, const unsigned int value)
|
|
{
|
|
char str[30];
|
|
utoa(value, str, 10);
|
|
char path[200] = {0};
|
|
buildMqttPath(topic, path);
|
|
_network->device()->mqttClient()->publish(path, str);
|
|
}
|
|
|
|
void NetworkOpener::publishBool(const char *topic, const bool value)
|
|
{
|
|
char str[2] = {0};
|
|
str[0] = value ? '1' : '0';
|
|
char path[200] = {0};
|
|
buildMqttPath(topic, path);
|
|
_network->device()->mqttClient()->publish(path, str);
|
|
}
|
|
|
|
void NetworkOpener::publishString(const char *topic, const char *value)
|
|
{
|
|
char path[200] = {0};
|
|
buildMqttPath(topic, path);
|
|
_network->device()->mqttClient()->publish(path, value);
|
|
}
|
|
|
|
void NetworkOpener::buildMqttPath(const char* path, char* outPath)
|
|
{
|
|
int offset = 0;
|
|
for(const char& c : _mqttPath)
|
|
{
|
|
if(c == 0x00)
|
|
{
|
|
break;
|
|
}
|
|
outPath[offset] = c;
|
|
++offset;
|
|
}
|
|
int i=0;
|
|
while(outPath[i] != 0x00)
|
|
{
|
|
outPath[offset] = path[i];
|
|
++i;
|
|
++offset;
|
|
}
|
|
outPath[i+1] = 0x00;
|
|
}
|
|
|
|
void NetworkOpener::subscribe(const char *path)
|
|
{
|
|
char prefixedPath[500];
|
|
buildMqttPath(path, prefixedPath);
|
|
_network->device()->mqttClient()->subscribe(prefixedPath);
|
|
}
|
|
|
|
bool NetworkOpener::comparePrefixedPath(const char *fullPath, const char *subPath)
|
|
{
|
|
char prefixedPath[500];
|
|
buildMqttPath(subPath, prefixedPath);
|
|
return strcmp(fullPath, prefixedPath) == 0;
|
|
} |