added save/restore history
This commit is contained in:
@@ -14,10 +14,10 @@ CONFIG -= app_bundle
|
|||||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
src/main.cpp \
|
src/main.cpp \
|
||||||
src/mqtt.cpp \
|
src/mqtt.cpp \
|
||||||
src/pws2mqtt.cpp \
|
src/pws2mqtt.cpp \
|
||||||
src/utcicalculator.cpp
|
src/utcicalculator.cpp
|
||||||
|
|
||||||
# Default rules for deployment.
|
# Default rules for deployment.
|
||||||
qnx: target.path = /tmp/$${TARGET}/bin
|
qnx: target.path = /tmp/$${TARGET}/bin
|
||||||
@@ -25,10 +25,10 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
|
|||||||
!isEmpty(target.path): INSTALLS += target
|
!isEmpty(target.path): INSTALLS += target
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
src/barometertrend.h \
|
src/barometertrend.h \
|
||||||
src/httpserver.h \
|
src/httpserver.h \
|
||||||
src/mqtt.h \
|
src/mqtt.h \
|
||||||
src/pws2mqtt.h \
|
src/pws2mqtt.h \
|
||||||
src/utcicalculator.h \
|
src/utcicalculator.h \
|
||||||
src/version.h
|
src/version.h
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ public:
|
|||||||
{
|
{
|
||||||
m_values.reserve(360 + 16); // évite reallocs fréquentes
|
m_values.reserve(360 + 16); // évite reallocs fréquentes
|
||||||
m_timestamps.reserve(360 + 16);
|
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)
|
void addPressure(double pressure_hPa)
|
||||||
@@ -345,4 +349,71 @@ public:
|
|||||||
private:
|
private:
|
||||||
QVector<double> m_values;
|
QVector<double> m_values;
|
||||||
QVector<QDateTime> m_timestamps;
|
QVector<QDateTime> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,7 +20,6 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include "utcicalculator.h"
|
|
||||||
|
|
||||||
#define CLIENT_ID "Client_ID"
|
#define CLIENT_ID "Client_ID"
|
||||||
#define BROKER_ADDRESS "localhost"
|
#define BROKER_ADDRESS "localhost"
|
||||||
|
|||||||
Reference in New Issue
Block a user