diff options
author | UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> | 2022-10-30 17:42:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-30 17:42:03 +0100 |
commit | b82a9c44d0112f5433c4482e2e5802969515ce7d (patch) | |
tree | 907451385d246dc61b513f3a27043f32b058268a /client | |
parent | 84341bc53bca3da581ba59f66bf4174420c83d3a (diff) | |
parent | fc95a016d13c1510a024888c1b87bbfe1a7f7601 (diff) |
Merge branch 'dev' into qt-settings
Diffstat (limited to 'client')
-rw-r--r-- | client/Client.cpp | 65 | ||||
-rw-r--r-- | client/Client.h | 2 | ||||
-rw-r--r-- | client/HandleMessage.cpp | 21 | ||||
-rw-r--r-- | client/HandleMessage.h | 2 |
4 files changed, 48 insertions, 42 deletions
diff --git a/client/Client.cpp b/client/Client.cpp index 87afe99..2075fc2 100644 --- a/client/Client.cpp +++ b/client/Client.cpp @@ -18,46 +18,49 @@ Client::~Client() delete timer; } -void Client::ClientEcho() -{ - - connect(timer, SIGNAL(timeout()),this,SLOT(timeFunction())); // connect timer to time every minute - - // connect to readyread to receive data; - connect(socket,&QTcpSocket::readyRead, [&]() { - QTextStream T(socket); - QString text = T.readAll(); // reads all data - Handlemsg.ParseToSQL(Handlemsg.ParseMessage(text, (totalRecords-'0'))); - - - }); - - timer->start(1000); +void Client::ClientEcho() { + static unsigned int lineCounter = 0; + connect(timer, SIGNAL(timeout()),this,SLOT(timeFunction())); // connect timer to time every minute + + // connect to readyread to receive data; + connect(socket,&QTcpSocket::readyRead, [&]() { + QTextStream T(socket); + QString text = T.readAll(); // reads all data + lineCounter++; + if (lineCounter <= 2) return; + Handlemsg.ParseToSQL(text); + }); + + connect(socket, &QTcpSocket::disconnected, [&]() { + socket->disconnectFromHost(); + lineCounter = 0; + }); + + timer->start(1000); } -void Client::timeFunction() -{ - totalRecords = WS_MAX(1, _missingRecords); +void Client::timeFunction() { + if((QTime::currentTime().second() % WS_CLIENT_STATION_POLL_INTERVAL) != 0) return; - char* msg = NULL; - asprintf(&msg, "last-records %x %x\n", totalRecords, offsetRecords); - QByteArray msgToSend = msg; - free(msg); + this->missingRecords(); + totalRecords = WS_MIN(WS_MAX(1, _missingRecords), 50); - QTime time = QTime::currentTime(); - qint16 currentSeconds = time.second(); - if((currentSeconds % WS_CLIENT_STATION_POLL_INTERVAL) == 1){ + char* msg = NULL; + asprintf(&msg, "last-records %x %x\n", totalRecords, offsetRecords); + QByteArray msgToSend = msg; + free(msg); - socket->connectToHost(networkAddress, tcpPortAddress); - socket->write(msgToSend); - } + socket->connectToHost(networkAddress, tcpPortAddress); + socket->write(msgToSend); } void Client::missingRecords() { QSqlQuery queryTimeData; - queryTimeData.exec( "SELECT (unix_timestamp(now()) - unix_timestamp(`time`))/60 as delta FROM `tblMain` limit 1"); - - _missingRecords = queryTimeData.value(0).toInt(); + queryTimeData.exec("SELECT (unix_timestamp(now()) - unix_timestamp(`time`))/60 as delta FROM `tblMain` limit 1"); + queryTimeData.first(); + unsigned int secondsSinceLastRecord = queryTimeData.value(0).toInt(); + _missingRecords = secondsSinceLastRecord / WS_CLIENT_STATION_POLL_INTERVAL; } + diff --git a/client/Client.h b/client/Client.h index b90e5e9..433aecf 100644 --- a/client/Client.h +++ b/client/Client.h @@ -25,7 +25,7 @@ public slots: private: void missingRecords(); - int _missingRecords; + int _missingRecords = 0; QTcpSocket *socket; // tcpsocket for communicating QTimer *timer; // timer to read every second what time it curruntly is. diff --git a/client/HandleMessage.cpp b/client/HandleMessage.cpp index 3d8823b..399abe8 100644 --- a/client/HandleMessage.cpp +++ b/client/HandleMessage.cpp @@ -1,17 +1,22 @@ #include "HandleMessage.h" -HandleMessage::HandleMessage(QObject *parent) : QObject(parent) -{ +HandleMessage::HandleMessage(QObject *parent) : QObject(parent) { } -} +void HandleMessage::ParseToSQL(QString input) { + QSqlQuery queryInsertData; + QString output = "insert into WSdb.tblMain (temperature, humidity, pressure) values "; + QStringList data; -QString HandleMessage::ParseMessage(const QString Msg , int totalRecords ) -{ - QString message= Msg.section('\n',2,(3+totalRecords)); + output.append("("); + data=input.split(","); - return message; + for (int i = 1; i < data.size(); i++) { + bool valid; + output.append(QString::number(data[i].toInt(&valid, 16))); + if (i + 1 < data.size()) output.append(","); + } -} + output.append(")"); void HandleMessage::ParseToSQL(QString input) { diff --git a/client/HandleMessage.h b/client/HandleMessage.h index f228633..f5995c7 100644 --- a/client/HandleMessage.h +++ b/client/HandleMessage.h @@ -12,8 +12,6 @@ class HandleMessage : public QObject Q_OBJECT public: HandleMessage(QObject *parent = 0); - - QString ParseMessage(const QString , int); void ParseToSQL(QString); private: |