From b5322d74d1839c4af490f5c7297e439a8433ba9b Mon Sep 17 00:00:00 2001 From: NielsCoding Date: Sat, 29 Oct 2022 21:38:47 +0200 Subject: merging TCPcode with main QT code --- client/Client.cpp | 65 ++++++++++++ client/Client.h | 45 ++++++++ client/HandleMessage.cpp | 47 +++++++++ client/HandleMessage.h | 26 +++++ client/client.pro | 12 +-- client/client.pro.user | 260 +++++++++++++++++++++++++++++++++++++++++++++++ client/main.cpp | 5 - client/mainwindow.cpp | 6 ++ client/mainwindow.h | 5 +- client/mainwindow.ui | 6 +- client/mytcpsocket.cpp | 44 -------- client/mytcpsocket.h | 32 ------ client/timetest.cpp | 18 ---- client/timetest.h | 27 ----- client/ui_mainwindow.h | 2 +- 15 files changed, 462 insertions(+), 138 deletions(-) create mode 100644 client/Client.cpp create mode 100644 client/Client.h create mode 100644 client/HandleMessage.cpp create mode 100644 client/HandleMessage.h create mode 100644 client/client.pro.user delete mode 100644 client/mytcpsocket.cpp delete mode 100644 client/mytcpsocket.h delete mode 100644 client/timetest.cpp delete mode 100644 client/timetest.h (limited to 'client') diff --git a/client/Client.cpp b/client/Client.cpp new file mode 100644 index 0000000..46952b6 --- /dev/null +++ b/client/Client.cpp @@ -0,0 +1,65 @@ +#include "Client.h" + + + +Client::Client(QObject *parent) : QObject(parent) +{ + // initislise timer and socket + socket = new QTcpSocket(this); + timer = new QTimer(this); +} +Client::~Client() +{ + // delete if called again + delete socket; + delete timer; +} + +void Client::ClientEcho() +{ + QTime time1 = QTime::currentTime(); + NextMinute = time1.minute()+1; + + 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::timeFunction() +{ + if(_missingRecords>1){ + totalRecords = _missingRecords; + } + else{ + totalRecords=1; + } + QByteArray msgToSend= (msg.toUtf8() + totalRecords + offsetRecords +'\n'); + + QTime time = QTime::currentTime(); + qint16 currentMinute = time.minute(); + + if(currentMinute==NextMinute){ + socket->connectToHost(networkAddress, tcpPortAddress); + + socket->write(msgToSend); + NextMinute++; + } +} + +void Client::missingRecords() +{ + QSqlQuery queryTimeData; + queryTimeData.exec("SELECT (unix_timestamp(now()) - unix_timestamp(`time`))/60 as delta FROM `WSdb`.`tblMain` limit 1"); + + _missingRecords = queryTimeData.value(0).toInt(); + +} diff --git a/client/Client.h b/client/Client.h new file mode 100644 index 0000000..10af3e1 --- /dev/null +++ b/client/Client.h @@ -0,0 +1,45 @@ +#ifndef CLIENT_H +#define CLIENT_H +#include +#include +#include +#include +#include + +#include "HandleMessage.h" + +// class client for wheather station +class Client : public QObject +{ + Q_OBJECT +public: + Client(QObject *parent = 0); + virtual ~Client(); + +public slots: + void ClientEcho(); // function to ask data from wheather station + void timeFunction(); // function to look every second what time currently is en handle if minute is passed + +private: + void missingRecords(); + + int _missingRecords; + QTcpSocket *socket; // tcpsocket for communicating + QTimer *timer; // timer to read every second what time it curruntly is. + + qint16 NextMinute; // timing for next minute + // qint16 currentMinute; // timing for currentMinute + HandleMessage Handlemsg; // add HandleMessage to Client.h + + int tcpPortAddress = 80; // port of communication via tcp + QString networkAddress = "192.168.137.76"; // network address for commincation via tcp + + QString msg = "last-records "; // part of mesage to send to wheather staion + char totalRecords = '1'; // total records to ask wheather station + char offsetRecords = '0'; // offset from reqeusting records + + + +}; + +#endif // CLIENT_H diff --git a/client/HandleMessage.cpp b/client/HandleMessage.cpp new file mode 100644 index 0000000..f7f14f4 --- /dev/null +++ b/client/HandleMessage.cpp @@ -0,0 +1,47 @@ +#include "HandleMessage.h" + + +HandleMessage::HandleMessage(QObject *parent) : QObject(parent) +{ + +} + +QString HandleMessage::ParseMessage(const QString Msg , int totalRecords ) +{ + QString message= Msg.section('\n',2,(3+totalRecords)); + + return message; + +} + +void HandleMessage::ParseToSQL(QString input) +{ + QSqlQuery queryInsertData; + QString output = "INSERT INTO `WSdb`.`tblMain` () VALUES "; + QVector data; + QVector list = input.split("\n"); + for (int i = 0; i < list.size(); ++i) { + + output += "("; + + data=list[i].split(","); + + for (int j = 1; j < data.size(); ++j) { + bool valid; + output.append(QString::number(data[j].toInt(&valid, 16))); + if (j <= data[j].size()) { + output.append(","); + } + + } + output.append(")"); + + if (i+1 < list.size()){ + output.append(","); + } + } + queryInsertData.exec(output); +} + + + diff --git a/client/HandleMessage.h b/client/HandleMessage.h new file mode 100644 index 0000000..bfc5063 --- /dev/null +++ b/client/HandleMessage.h @@ -0,0 +1,26 @@ +#ifndef HANDLEMESSAGE_H +#define HANDLEMESSAGE_H + +#include +#include +#include +#include +#include + +class HandleMessage : public QObject +{ + Q_OBJECT +public: + HandleMessage(QObject *parent = 0); + + QString ParseMessage(const QString , int); + void ParseToSQL(QString); + + + +private: + +}; + + +#endif // HANDLEMESSAGE_H diff --git a/client/client.pro b/client/client.pro index d680fba..76f2b56 100644 --- a/client/client.pro +++ b/client/client.pro @@ -1,18 +1,18 @@ QT += core gui sql charts network HEADERS += \ + Client.h \ + HandleMessage.h \ dbconnector.h \ main.h \ - mainwindow.h \ - mytcpsocket.h \ - timetest.h + mainwindow.h SOURCES += \ + Client.cpp \ + HandleMessage.cpp \ dbconnector.cpp \ main.cpp \ - mainwindow.cpp \ - mytcpsocket.cpp \ - timetest.cpp + mainwindow.cpp FORMS += \ diff --git a/client/client.pro.user b/client/client.pro.user new file mode 100644 index 0000000..7ccbc7b --- /dev/null +++ b/client/client.pro.user @@ -0,0 +1,260 @@ + + + + + + EnvironmentId + {aa240e53-c124-4cf0-84a8-30bfe8a2cf83} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + false + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 4 + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop Qt 6.4.0 MinGW 64-bit + Desktop Qt 6.4.0 MinGW 64-bit + qt.qt6.640.win64_mingw_kit + 0 + 0 + 0 + + 0 + D:\Github2\avans-whether-station\build-client-Desktop_Qt_6_4_0_MinGW_64_bit-Debug + D:/Github2/avans-whether-station/build-client-Desktop_Qt_6_4_0_MinGW_64_bit-Debug + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + D:\Github2\avans-whether-station\build-client-Desktop_Qt_6_4_0_MinGW_64_bit-Release + D:/Github2/avans-whether-station/build-client-Desktop_Qt_6_4_0_MinGW_64_bit-Release + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + + + 0 + D:\Github2\avans-whether-station\build-client-Desktop_Qt_6_4_0_MinGW_64_bit-Profile + D:/Github2/avans-whether-station/build-client-Desktop_Qt_6_4_0_MinGW_64_bit-Profile + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + 0 + + 3 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + true + + 2 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/client/main.cpp b/client/main.cpp index 8561786..157abe6 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -14,17 +14,12 @@ #include "mainwindow.h" #include "main.h" #include "ui_mainwindow.h" -#include "mytcpsocket.h" -#include "timetest.h" QSqlDatabase dbRef = QSqlDatabase(); int main(int argc, char *argv[]) { QApplication a(argc, argv); - TimeTest time; - MyTcpSocket s; - // s.doConnect(); MainWindow w; dbRef = QSqlDatabase::addDatabase("QMYSQL"); diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index e1736e6..49fcc26 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -11,6 +11,7 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) { ui->setupUi(this); + client.ClientEcho(); } MainWindow::~MainWindow() @@ -19,6 +20,11 @@ MainWindow::~MainWindow() delete ui; } +void MainWindow::timeFunction() +{ + client.timeFunction(); +} + void MainWindow::on_actionConnection_triggered() { _dbConenctor = new dbConnector(this); diff --git a/client/mainwindow.h b/client/mainwindow.h index 25e22ec..6bcc329 100644 --- a/client/mainwindow.h +++ b/client/mainwindow.h @@ -13,6 +13,7 @@ #include #include "main.h" +#include "Client.h" QT_BEGIN_NAMESPACE @@ -33,7 +34,7 @@ private slots: // void on_actionAbout_triggered(); // void on_pushButton_clicked(); - + void timeFunction(); void on_actionConnection_triggered(); void on_actionRefresh_triggered(); @@ -42,7 +43,7 @@ private slots: private: Ui::MainWindow *ui; - + Client client; dbConnector *_dbConenctor; QChart *_pChart; diff --git a/client/mainwindow.ui b/client/mainwindow.ui index a31ffdc..bbcdf9c 100644 --- a/client/mainwindow.ui +++ b/client/mainwindow.ui @@ -17,8 +17,8 @@ - 270 - 190 + 260 + 240 181 51 @@ -34,7 +34,7 @@ 0 0 800 - 24 + 26 diff --git a/client/mytcpsocket.cpp b/client/mytcpsocket.cpp deleted file mode 100644 index 92dd67a..0000000 --- a/client/mytcpsocket.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -MyTcpSocket::MyTcpSocket(QObject *parent) : - QObject(parent) -{ -} - -void MyTcpSocket::doConnect() -{ - socket = new QTcpSocket(this); - - connect(socket, SIGNAL(connected()),this, SLOT(connected())); - connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected())); - // connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64))); - connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead())); - qDebug() << "connectig..."; - - socket->connectToHost("192.168.137.141",80); - - if(!socket->waitForConnected(5000)){ - qDebug()<<"Error: "<< socket->errorString(); - } -} - -void MyTcpSocket::connected(){ - qDebug() << "connected..."; - - socket->write("Weerdata: Temp:?\r\n\r\n\r\n\r\n"); - -} -void MyTcpSocket::disconnected(){ - qDebug() << "disconnected..."; - -} - -void MyTcpSocket::bytesWritten(qint64 bytes){ - qDebug() << bytes << "bytes written..."; - -} -void MyTcpSocket::readyRead(){ - qDebug() << "reading..."; - - qDebug() << socket->readAll(); -} diff --git a/client/mytcpsocket.h b/client/mytcpsocket.h deleted file mode 100644 index 4a7e543..0000000 --- a/client/mytcpsocket.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef MYTCPSOCKET_H -#define MYTCPSOCKET_H - -#include -#include -#include -#include - -class MyTcpSocket : public QObject -{ - Q_OBJECT -public: - explicit MyTcpSocket(QObject *parent = 0); - - void doConnect(); -signals: -public slots: - void connected(); - void disconnected(); - void bytesWritten(qint64 bytes); - void readyRead(); -private: - QTcpSocket *socket; - - - - - -}; - - -#endif // MYTCPSOCKET_H diff --git a/client/timetest.cpp b/client/timetest.cpp deleted file mode 100644 index 2e575f2..0000000 --- a/client/timetest.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "timetest.h" - -TimeTest::TimeTest(QObject *parent) : QObject(parent) -{ - timer = new QTimer(this); - connect(timer, SIGNAL(timeout()),this,SLOT(myfunction())); - timer->start(5000); -} - -qint16 TimeTest::myfunction() -{ - QTime time = QTime::currentTime(); - qint16 time_text = time.minute(); - - return time_text; - - -} diff --git a/client/timetest.h b/client/timetest.h deleted file mode 100644 index aa1d8a5..0000000 --- a/client/timetest.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef TIMETEST_H -#define TIMETEST_H -#include -#include -#include -#include - -class TimeTest : public QObject -{ - Q_OBJECT -public: - explicit TimeTest(QObject *parent = 0); - -signals: -public slots: - qint16 myfunction(); - -private: - QTimer *timer; - - - - -}; - - -#endif // TIMETEST_H diff --git a/client/ui_mainwindow.h b/client/ui_mainwindow.h index 46bb29a..69c476b 100644 --- a/client/ui_mainwindow.h +++ b/client/ui_mainwindow.h @@ -10,7 +10,7 @@ #define UI_MAINWINDOW_H #include -#include +// #include #include #include #include -- cgit v1.2.3