aboutsummaryrefslogtreecommitdiff
path: root/client/Client.cpp
blob: e50be5eeca7c0c79c8f8c0f45845ddf9a3a18bce (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
#include <stdio.h>

#include "Client.h"
#include "consts.h"
#include "../shared/util.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((QTime::currentTime().second() % WS_CLIENT_STATION_POLL_INTERVAL) != 0) return;

	this->missingRecords();
	totalRecords = WS_MAX(1, _missingRecords);

	char* msg = NULL;
	asprintf(&msg, "last-records %x %x\n", totalRecords, offsetRecords);
	QByteArray msgToSend = msg;
	free(msg);

	socket->connectToHost(networkAddress, tcpPortAddress);
	socket->write(msgToSend);
}

void Client::missingRecords() {
	QSqlQuery queryTimeData;
	queryTimeData.exec("select unix_timestamp(now()) - unix_timestamp(time) as delta from WSdb.tblMain order by time desc limit 1");
	queryTimeData.first();
	unsigned int secondsSinceLastRecord = queryTimeData.value(0).toInt();
	_missingRecords = secondsSinceLastRecord / WS_CLIENT_STATION_POLL_INTERVAL;
}