From ec59ad69d42548fe7e40a3071ac8a77508c8e84f Mon Sep 17 00:00:00 2001 From: Daniel TARTAVEL Date: Sat, 18 Oct 2025 17:51:53 +0200 Subject: [PATCH] added save/restore history --- pws2mqtt-qt.pro | 20 ++++++------- src/barometertrend.h | 71 ++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 1 - 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/pws2mqtt-qt.pro b/pws2mqtt-qt.pro index cb7cc15..5f83235 100644 --- a/pws2mqtt-qt.pro +++ b/pws2mqtt-qt.pro @@ -14,10 +14,10 @@ CONFIG -= app_bundle #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ -src/main.cpp \ -src/mqtt.cpp \ -src/pws2mqtt.cpp \ -src/utcicalculator.cpp + src/main.cpp \ + src/mqtt.cpp \ + src/pws2mqtt.cpp \ + src/utcicalculator.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin @@ -25,10 +25,10 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ -src/barometertrend.h \ -src/httpserver.h \ -src/mqtt.h \ -src/pws2mqtt.h \ -src/utcicalculator.h \ -src/version.h + src/barometertrend.h \ + src/httpserver.h \ + src/mqtt.h \ + src/pws2mqtt.h \ + src/utcicalculator.h \ + src/version.h diff --git a/src/barometertrend.h b/src/barometertrend.h index bfe170a..146eced 100644 --- a/src/barometertrend.h +++ b/src/barometertrend.h @@ -28,6 +28,10 @@ public: { m_values.reserve(360 + 16); // évite reallocs fréquentes m_timestamps.reserve(360 + 16); + restoreHistory(); + // Sauvegarde automatique toutes les 10 minutes + connect(&m_saveTimer, &QTimer::timeout, this, &BarometerTrend::saveHistory); + m_saveTimer.start(10 * 60 * 1000); } void addPressure(double pressure_hPa) @@ -345,4 +349,71 @@ public: private: QVector m_values; QVector m_timestamps; + QTimer m_saveTimer; + +private slots: + void saveHistory() + { + QJsonArray array; + for (int i = 0; i < m_values.size(); ++i) + { + QJsonObject obj; + obj["timestamp"] = m_timestamps[i].toString(Qt::ISODate); + obj["pressure"] = m_values[i]; + array.append(obj); + } + + QJsonDocument doc(array); + QFile file("/usr/local/share/pws2mqtt/barometer_history.json"); + if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) + { + file.write(doc.toJson()); + file.close(); + debug(DEBUGMACRO, "Barometer history saved (" + QByteArray::number(m_values.size()) + " samples)", DEBUG); + }else + { + debug(DEBUGMACRO, "File not open : " + file.errorString() , WARNING); + } + } + + void restoreHistory() + { + QFile file("/usr/local/share/pws2mqtt/barometer_history.json"); + if (!file.exists()) + { + debug(DEBUGMACRO, "File not open : " + file.errorString() , WARNING); + return; + } + if (file.open(QIODevice::ReadOnly)) + { + QByteArray data = file.readAll(); + file.close(); + + QJsonDocument doc = QJsonDocument::fromJson(data); + if (!doc.isArray()) return; + + QJsonArray array = doc.array(); + for (const QJsonValue &val : array) + { + QJsonObject obj = val.toObject(); + double pressure = obj["pressure"].toDouble(); + QDateTime ts = QDateTime::fromString(obj["timestamp"].toString(), Qt::ISODate); + if (ts.isValid()) + { + m_values.append(pressure); + m_timestamps.append(ts); + } + } + + // On ne garde que les 3 dernières heures + QDateTime limit = QDateTime::currentDateTime().addSecs(-3 * 3600); + while (!m_timestamps.isEmpty() && m_timestamps.first() < limit) + { + m_timestamps.removeFirst(); + m_values.removeFirst(); + } + + debug(DEBUGMACRO, "Barometer history restored (" + QByteArray::number(m_values.size()) + " samples)", DEBUG); + } + } }; diff --git a/src/main.cpp b/src/main.cpp index bda905d..482bf70 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,6 @@ #include #include #include -#include "utcicalculator.h" #define CLIENT_ID "Client_ID" #define BROKER_ADDRESS "localhost"