From edfe09a38362a50189d5b62a1e5915a05f8bb4d2 Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 11 Apr 2020 15:36:24 +0200 Subject: [PATCH] add global/scan topic revealing status of --scanconfig option initiated scan (fixes #304) --- src/ebusd/datahandler.h | 6 ++++++ src/ebusd/mainloop.cpp | 16 ++++++++++++++++ src/ebusd/mqtthandler.cpp | 11 ++++++++++- src/ebusd/mqtthandler.h | 8 +++++++- 4 files changed, 39 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/ebusd/mqtthandler.cpp diff --git a/src/ebusd/datahandler.h b/src/ebusd/datahandler.h index 253386c6..64af17bb 100755 --- a/src/ebusd/datahandler.h +++ b/src/ebusd/datahandler.h @@ -160,6 +160,12 @@ class DataSink : virtual public DataHandler { */ virtual void notifyUpdateCheckResult(const string& checkResult) {} + /** + * Notify the sink of the latest scan status. + * @param scanStatus a string describing the scan status. + */ + virtual void notifyScanStatus(const string& scanStatus) {} + protected: /** the allowed access levels. */ string m_levels; diff --git a/src/ebusd/mainloop.cpp b/src/ebusd/mainloop.cpp index a295e41f..a2579527 100644 --- a/src/ebusd/mainloop.cpp +++ b/src/ebusd/mainloop.cpp @@ -217,6 +217,7 @@ void MainLoop::run() { time_t lastTaskRun, now, start, lastSignal = 0, since, sinkSince = 1, nextCheckRun; int taskDelay = 5; symbol_t lastScanAddress = 0; // 0 is known to be a master + string lastScanStatus = "."; time(&now); start = now; lastTaskRun = now; @@ -252,12 +253,16 @@ void MainLoop::run() { } if (m_scanConfig) { bool loadDelay = false; + string scanStatus = lastScanStatus; if (m_initialScan != ESC && reload && m_busHandler->hasSignal()) { loadDelay = true; result_t result; if (m_initialScan == SYN) { logNotice(lf_main, "starting initial full scan"); result = m_busHandler->startScan(true, "*"); + if (result == RESULT_OK) { + scanStatus = "running"; + } } else if (m_initialScan == BROADCAST) { logNotice(lf_main, "starting initial broadcast scan"); Message* message = m_messages->getScanMessage(BROADCAST); @@ -280,6 +285,7 @@ void MainLoop::run() { if (m_busHandler->formatScanResult(m_initialScan, false, &ret)) { logNotice(lf_main, "initial scan result: %s", ret.str().c_str()); } + scanStatus = "running"; } } if (result != RESULT_OK) { @@ -294,7 +300,11 @@ void MainLoop::run() { if (lastScanAddress == SYN) { taskDelay = 5; lastScanAddress = 0; + scanStatus = "finished"; } else { + if (scanStatus!="running") { + scanStatus = "running"; + } nextCheckRun = now + CHECK_INITIAL_DELAY; result_t result = m_busHandler->scanAndWait(lastScanAddress, true); taskDelay = (result == RESULT_ERR_NO_SIGNAL) ? 10 : 1; @@ -305,6 +315,12 @@ void MainLoop::run() { } } } + if (scanStatus != lastScanStatus && !dataSinks.empty()) { + lastScanStatus = scanStatus; + for (const auto dataSink : dataSinks) { + dataSink->notifyScanStatus(scanStatus); + } + } } else if (reload && m_busHandler->hasSignal()) { reload = false; // execute initial instructions diff --git a/src/ebusd/mqtthandler.cpp b/src/ebusd/mqtthandler.cpp old mode 100644 new mode 100755 index 82e7c362..45722fe6 --- a/src/ebusd/mqtthandler.cpp +++ b/src/ebusd/mqtthandler.cpp @@ -441,7 +441,7 @@ void on_message( MqttHandler::MqttHandler(UserInfo* userInfo, BusHandler* busHandler, MessageMap* messages) : DataSink(userInfo, "mqtt"), DataSource(busHandler), WaitThread(), m_messages(messages), m_connected(false), - m_initialConnectFailed(false), m_lastUpdateCheckResult("."), m_lastErrorLogTime(0) { + m_initialConnectFailed(false), m_lastUpdateCheckResult("."), m_lastScanStatus("."), m_lastErrorLogTime(0) { m_publishByField = false; m_mosquitto = nullptr; if (g_topicFields.empty()) { @@ -722,6 +722,14 @@ void MqttHandler::notifyUpdateCheckResult(const string& checkResult) { } } +void MqttHandler::notifyScanStatus(const string& scanStatus) { + if (scanStatus != m_lastScanStatus) { + m_lastScanStatus = scanStatus; + const string sep = (g_publishFormat & OF_JSON) ? "\"" : ""; + publishTopic(m_globalTopic+"scan", sep + (scanStatus.empty() ? "OK" : scanStatus) + sep, true); + } +} + void MqttHandler::run() { time_t lastTaskRun, now, start, lastSignal = 0, lastUpdates = 0; bool signal = false; @@ -798,6 +806,7 @@ void MqttHandler::run() { } } publishTopic(signalTopic, "false", true); + publishTopic(m_globalTopic+"scan", "", true); // clear retain of scan status } bool MqttHandler::handleTraffic(bool allowReconnect) { diff --git a/src/ebusd/mqtthandler.h b/src/ebusd/mqtthandler.h index 12f70cf5..b432d30b 100644 --- a/src/ebusd/mqtthandler.h +++ b/src/ebusd/mqtthandler.h @@ -91,7 +91,10 @@ class MqttHandler : public DataSink, public DataSource, public WaitThread { // @copydoc void notifyUpdateCheckResult(const string& checkResult) override; - protected: + // @copydoc + void notifyScanStatus(const string& scanStatus) override; + +protected: // @copydoc void run() override; @@ -159,6 +162,9 @@ class MqttHandler : public DataSink, public DataSource, public WaitThread { /** the last update check result. */ string m_lastUpdateCheckResult; + /** the last scan status. */ + string m_lastScanStatus; + /** the last system time when a communication error was logged. */ time_t m_lastErrorLogTime; };