aboutsummaryrefslogtreecommitdiff
path: root/client/Client.cpp
blob: 01c3867401b840d7589f0d39225caaf946377a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "Client.h"
#include "consts.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()
{

    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 currentSeconds = time.second();
    auto _intervals = intervals;
    if(currentSeconds % _intervals==0){

        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 `WSdb`.`tblMain` limit 1");

    _missingRecords = queryTimeData.value(0).toInt();

}