Add Time Control

This commit is contained in:
iranl
2024-04-13 21:45:12 +02:00
parent 8c30de7cec
commit d9bed88ca6
12 changed files with 655 additions and 5 deletions

View File

@@ -70,6 +70,12 @@ void NetworkOpener::initialize()
_network->initTopic(_mqttPath, mqtt_topic_query_keypad, "0");
}
if(_preferences->getBool(preference_timecontrol_control_enabled))
{
_network->subscribe(_mqttPath, mqtt_topic_timecontrol_action);
_network->initTopic(_mqttPath, mqtt_topic_timecontrol_action, "--");
}
_network->addReconnectedCallback([&]()
{
_reconnected = true;
@@ -193,6 +199,18 @@ void NetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const u
}
}
}
if(comparePrefixedPath(topic, mqtt_topic_timecontrol_action))
{
if(strcmp(value, "") == 0 || strcmp(value, "--") == 0) return;
if(_timeControlCommandReceivedReceivedCallback != NULL)
{
_timeControlCommandReceivedReceivedCallback(value);
}
publishString(mqtt_topic_timecontrol_action, "--");
}
}
void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState)
@@ -282,7 +300,7 @@ void NetworkOpener::publishRing(const bool locked)
{
publishString(mqtt_topic_lock_ring, "ring");
}
publishString(mqtt_topic_lock_binary_ring, "ring");
_resetRingStateTs = millis() + 2000;
}
@@ -591,11 +609,88 @@ void NetworkOpener::publishKeypad(const std::list<NukiLock::KeypadEntry>& entrie
}
}
void NetworkOpener::publishTimeControl(const std::list<NukiLock::TimeControlEntry>& entries)
{
char str[50];
JsonDocument json;
for(const auto& entry : entries)
{
auto jsonEntry = json.add();
jsonEntry["entryId"] = entry.entryId;
jsonEntry["enabled"] = entry.enabled;
uint8_t weekdaysInt = entry.weekdays;
JsonArray weekdays = jsonEntry["weekdays"].to<JsonArray>();
while(weekdaysInt > 0) {
if(weekdaysInt >= 64)
{
weekdays.add("mon");
weekdaysInt -= 64;
continue;
}
if(weekdaysInt >= 32)
{
weekdays.add("tue");
weekdaysInt -= 32;
continue;
}
if(weekdaysInt >= 16)
{
weekdays.add("wed");
weekdaysInt -= 16;
continue;
}
if(weekdaysInt >= 8)
{
weekdays.add("thu");
weekdaysInt -= 8;
continue;
}
if(weekdaysInt >= 4)
{
weekdays.add("fri");
weekdaysInt -= 4;
continue;
}
if(weekdaysInt >= 2)
{
weekdays.add("sat");
weekdaysInt -= 2;
continue;
}
if(weekdaysInt >= 1)
{
weekdays.add("sun");
weekdaysInt -= 1;
continue;
}
}
char timeT[5];
sprintf(timeT, "%02d:%02d", entry.timeHour, entry.timeMin);
jsonEntry["time"] = timeT;
memset(str, 0, sizeof(str));
NetworkOpener::lockactionToString(entry.lockAction, str);
jsonEntry["lockAction"] = str;
}
serializeJson(json, _buffer, _bufferSize);
publishString(mqtt_topic_timecontrol_json, _buffer);
}
void NetworkOpener::publishKeypadCommandResult(const char* result)
{
publishString(mqtt_topic_keypad_command_result, result);
}
void NetworkOpener::publishTimeControlCommandResult(const char* result)
{
publishString(mqtt_topic_timecontrol_command_result, result);
}
void NetworkOpener::setLockActionReceivedCallback(LockActionResult (*lockActionReceivedCallback)(const char *))
{
_lockActionReceivedCallback = lockActionReceivedCallback;
@@ -611,6 +706,11 @@ void NetworkOpener::setKeypadCommandReceivedCallback(void (*keypadCommandReceive
_keypadCommandReceivedReceivedCallback = keypadCommandReceivedReceivedCallback;
}
void NetworkOpener::setTimeControlCommandReceivedCallback(void (*timeControlCommandReceivedReceivedCallback)(const char *))
{
_timeControlCommandReceivedReceivedCallback = timeControlCommandReceivedReceivedCallback;
}
void NetworkOpener::publishFloat(const char *topic, const float value, const uint8_t precision)
{
_network->publishFloat(_mqttPath, topic, value, precision);