aboutsummaryrefslogtreecommitdiff
path: root/client/Client.cpp
blob: 61e51e4dc3671047ee0f1236ad7369e865be483e (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
63
64
#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() {
	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() {
	if((QTime::currentTime().second() % WS_CLIENT_STATION_POLL_INTERVAL) != 0) return;

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

	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;
}