2018-09-23 17:54:20 +02:00
|
|
|
#include "headers/baseconn.h"
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
BaseConn * pGlobalBaseConn = nullptr;
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
BaseConn::BaseConn(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2019-03-07 17:18:24 +01:00
|
|
|
pGlobalBaseConn = this;
|
2019-08-20 22:55:37 +02:00
|
|
|
|
2019-08-20 10:19:35 +02:00
|
|
|
socket = new QTcpSocket(this);
|
2019-08-20 22:55:37 +02:00
|
|
|
|
|
|
|
this->timeoutTimer = new QTimer(this);
|
|
|
|
this->timeoutTimer->setSingleShot(true);
|
|
|
|
|
|
|
|
this->state = "disconnected";
|
2019-03-02 20:20:13 +01:00
|
|
|
|
|
|
|
connect(this->socket, SIGNAL(error(QAbstractSocket::SocketError)),
|
|
|
|
this, SLOT(gotError(QAbstractSocket::SocketError)));
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-03-29 23:42:56 +01:00
|
|
|
connect(this->socket, &QAbstractSocket::stateChanged, this, &BaseConn::socketStateChanged);
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
this->nextConnectionId = 1;
|
2019-03-27 22:26:59 +01:00
|
|
|
this->connections = QVariantList({});
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 22:55:37 +02:00
|
|
|
void BaseConn::connectToHost() {
|
2018-10-04 18:35:29 +02:00
|
|
|
qDebug() << "connecting";
|
2018-09-23 17:54:20 +02:00
|
|
|
setState("connecting");
|
|
|
|
this->connection_progress = 0;
|
2019-08-19 15:14:11 +02:00
|
|
|
|
2019-08-20 22:55:37 +02:00
|
|
|
connect(this->timeoutTimer, SIGNAL(timeout()), this, SLOT(connectionTimeout()));
|
2019-08-19 15:14:11 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
//connect
|
|
|
|
this->socket->connectToHost(this->ip, this->port);
|
|
|
|
|
2019-08-20 22:55:37 +02:00
|
|
|
timeoutTimer->start(3000);
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2019-08-20 22:55:37 +02:00
|
|
|
void BaseConn::connectionTimeout() {
|
|
|
|
this->socket->abort();
|
|
|
|
disconnect(this->timeoutTimer, SIGNAL(timeout()), this, SLOT(connectionTimeout()));
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
2019-04-27 22:50:22 +02:00
|
|
|
bool BaseConn::init() {
|
2019-08-20 22:55:37 +02:00
|
|
|
disconnect(this->timeoutTimer, SIGNAL(timeout()), this, SLOT(connectionTimeout()));
|
|
|
|
this->timeoutTimer->stop();
|
|
|
|
|
2019-08-19 15:14:11 +02:00
|
|
|
connect(this->socket, &QTcpSocket::readyRead, this, &BaseConn::readyRead);
|
|
|
|
this->connection_progress = 50;
|
|
|
|
|
2019-04-27 22:50:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::deInit() {
|
|
|
|
this->connections.clear();
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
void BaseConn::closeConnection()
|
|
|
|
{
|
2019-03-29 23:42:56 +01:00
|
|
|
this->connections = QVariantList({});
|
|
|
|
emit this->connectionsChanged();
|
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
qDebug() << "closing connection";
|
|
|
|
switch (socket->state())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
socket->disconnectFromHost();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
socket->abort();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
socket->abort();
|
|
|
|
}
|
2019-04-27 22:50:22 +02:00
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
setState("disconnected");
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::gotError(QAbstractSocket::SocketError err)
|
|
|
|
{
|
|
|
|
//qDebug() << "got error";
|
|
|
|
QString strError = "unknown";
|
|
|
|
switch (err)
|
|
|
|
{
|
2019-03-27 22:26:59 +01:00
|
|
|
case 0:
|
|
|
|
strError = "Connection was refused";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
strError = "Remote host closed the connection";
|
|
|
|
this->closeConnection();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
strError = "Host address was not found";
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
strError = "Connection timed out";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strError = "Unknown error";
|
2019-03-02 20:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
emit gotError(strError);
|
|
|
|
qDebug() << "got socket error: " << strError;
|
|
|
|
}
|
|
|
|
|
2019-05-21 18:40:45 +02:00
|
|
|
// -------------------------------------
|
|
|
|
// --- socket communication handling ---
|
|
|
|
// -------------------------------------
|
|
|
|
|
2019-08-19 15:14:11 +02:00
|
|
|
void BaseConn::socketStateChanged(QAbstractSocket::SocketState socketState) {
|
|
|
|
switch (socketState) {
|
|
|
|
case QAbstractSocket::UnconnectedState:
|
|
|
|
{
|
|
|
|
this->setState("disconnected");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QAbstractSocket::ConnectedState:
|
|
|
|
{
|
|
|
|
if(this->init()) {
|
|
|
|
this->setState("connected");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->closeConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
//qDebug() << "+ --- UNKNOWN SOCKET STATE: " << socketState;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
QVariantMap BaseConn::sendCommand(int header, QJsonValue data){
|
2018-10-04 18:35:29 +02:00
|
|
|
if(this->state != "connected"){
|
2019-03-02 20:20:13 +01:00
|
|
|
return {{"status", 910}, {"data", "not connected"}};
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
// generate id and witing requests entry
|
|
|
|
int thisId = nextConnectionId;
|
2019-08-20 22:55:37 +02:00
|
|
|
//qDebug() << "sending command: " << header << " with data: " << data << " and id: " << thisId;
|
2018-10-14 18:39:39 +02:00
|
|
|
nextConnectionId ++;
|
2019-03-02 20:20:13 +01:00
|
|
|
|
2019-08-20 10:19:35 +02:00
|
|
|
QEventLoop *loop = new QEventLoop(this);
|
|
|
|
QTimer *timer = new QTimer(this);
|
2019-03-02 20:20:13 +01:00
|
|
|
QJsonObject reply;
|
|
|
|
|
2019-08-20 10:19:35 +02:00
|
|
|
this->waitingRequests.append({thisId, loop, reply});
|
2019-03-02 20:20:13 +01:00
|
|
|
|
|
|
|
QJsonObject requestObj;
|
|
|
|
requestObj.insert("id", thisId);
|
|
|
|
requestObj.insert("header", header);
|
|
|
|
requestObj.insert("data", data);
|
|
|
|
|
|
|
|
QString jsonRequest = QJsonDocument(requestObj).toJson();
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-08-20 10:19:35 +02:00
|
|
|
timer->setSingleShot(true);
|
2018-10-14 18:39:39 +02:00
|
|
|
// quit the loop when the timer times out
|
2019-08-20 10:19:35 +02:00
|
|
|
loop->connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
|
2019-03-02 20:20:13 +01:00
|
|
|
// quit the loop when the connection was established
|
2018-10-14 18:39:39 +02:00
|
|
|
// loop.connect(this, &BaseConn::gotReply, &loop, &QEventLoop::quit);
|
|
|
|
// start the timer before starting to connect
|
2019-08-20 10:19:35 +02:00
|
|
|
timer->start(3000);
|
2018-10-14 18:39:39 +02:00
|
|
|
|
|
|
|
//write data
|
2019-03-24 21:16:16 +01:00
|
|
|
socket->write(jsonRequest.toLatin1());
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
//wait for an answer to finish (programm gets stuck in here)
|
2019-08-20 10:19:35 +02:00
|
|
|
loop->exec();
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2019-08-20 10:19:35 +02:00
|
|
|
bool replyFound = false;
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2019-08-20 10:19:35 +02:00
|
|
|
// find reply and delete the request from waiting list
|
2018-10-14 18:39:39 +02:00
|
|
|
for(int i = 0; i<this->waitingRequests.length(); i++){
|
|
|
|
if(this->waitingRequests[i].id == thisId){
|
2019-08-20 10:19:35 +02:00
|
|
|
// request was found
|
|
|
|
replyFound = true;
|
|
|
|
// delete event loop
|
|
|
|
if(this->waitingRequests[i].loop != nullptr) {
|
|
|
|
delete this->waitingRequests[i].loop;
|
|
|
|
}
|
|
|
|
// store reply
|
2018-10-14 18:39:39 +02:00
|
|
|
reply = this->waitingRequests[i].reply;
|
2019-08-20 10:19:35 +02:00
|
|
|
// remove reply from waiting list
|
|
|
|
this->waitingRequests.removeAt(i);
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 10:19:35 +02:00
|
|
|
if(!replyFound) {
|
|
|
|
// some internal error occured
|
|
|
|
return {{"status", 900}, {"data", ""}};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(timer->remainingTime() == -1){
|
|
|
|
//the time has been triggered -> timeout
|
|
|
|
return {{"status", 911}, {"data", ""}};
|
|
|
|
}
|
|
|
|
|
|
|
|
delete timer;
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
return {{"status", reply.value("header").toInt()}, {"data", reply.value("data").toVariant()}};
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::readyRead() {
|
|
|
|
|
2019-05-19 14:06:05 +02:00
|
|
|
//qDebug() << "ready to ready " << socket->bytesAvailable() << " bytes" ;
|
2019-03-24 21:16:16 +01:00
|
|
|
QString reply = socket->readAll();
|
2019-05-19 14:06:05 +02:00
|
|
|
|
2019-05-21 18:40:45 +02:00
|
|
|
//qWarning() << "socket read: " << reply;
|
|
|
|
|
|
|
|
processSocketMessage(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::processSocketMessage(QString message){
|
|
|
|
QString startKey = "<message>";
|
|
|
|
QString endKey = "</message>";
|
|
|
|
|
|
|
|
//qWarning() << "... processing message now ... : " << message;
|
|
|
|
|
|
|
|
if(message == ""){
|
2019-05-19 14:06:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-05-21 18:40:45 +02:00
|
|
|
|
|
|
|
if((message.startsWith(startKey) && message.endsWith(endKey)) && (message.count(startKey) == 1 && message.count(endKey) == 1)){
|
|
|
|
// non-split message ( e.g.: <message>123456789</message>
|
|
|
|
}
|
|
|
|
else if(!message.contains(endKey) && (!this->readBuffer.isEmpty() || message.startsWith(startKey))){
|
|
|
|
// begin of a split message ( e.g.: <message>123 )
|
|
|
|
// or middle of a split message ( e.g.: 456 )
|
|
|
|
//qWarning() << "this is a begin or middle of split a message";
|
|
|
|
this->readBuffer += message;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(!message.contains(startKey) && message.endsWith(endKey)) {
|
|
|
|
// end of a split message ( e.g.: 789</message> )
|
|
|
|
|
2019-05-19 14:06:05 +02:00
|
|
|
if(!this->readBuffer.isEmpty()){
|
2019-05-21 18:40:45 +02:00
|
|
|
message = readBuffer + message;
|
2019-05-19 14:06:05 +02:00
|
|
|
readBuffer.clear();
|
|
|
|
}
|
|
|
|
}
|
2019-05-21 18:40:45 +02:00
|
|
|
else if((message.count(startKey) > 1 || message.count(endKey) > 1) || (message.contains(endKey) && !message.endsWith(endKey) && message.contains(startKey) && !message.startsWith(startKey))) {
|
|
|
|
// multiple messages in one packet ( e.g.: <message>123456789</message><message>987654321</message> )
|
|
|
|
// or multiple message fragments in one message ( e.g.: 56789</message><message>987654321</message> or 56789</message><message>98765 )
|
|
|
|
//qDebug() << "detected multiple messages";
|
|
|
|
|
|
|
|
int startOfSecondMessage = message.lastIndexOf(startKey);
|
|
|
|
// process first part of message
|
|
|
|
QString firstMessage = message.left(startOfSecondMessage);
|
|
|
|
this->processSocketMessage(firstMessage);
|
|
|
|
// process second part of message
|
|
|
|
QString secondMessage = message.right(message.length() - startOfSecondMessage);
|
|
|
|
this->processSocketMessage(secondMessage);
|
2019-05-19 14:06:05 +02:00
|
|
|
|
2019-05-21 18:40:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// invalid message
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//qWarning() << "... done processing, message: " << message;
|
|
|
|
this->socketReplyRecieved(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::socketReplyRecieved(QString reply) {
|
2019-05-19 14:06:05 +02:00
|
|
|
reply.replace("<message>", "");
|
|
|
|
reply.replace("</message>", "");
|
|
|
|
|
2019-03-24 21:16:16 +01:00
|
|
|
int id = 0;
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2019-03-24 21:16:16 +01:00
|
|
|
QJsonDocument jsonReply = QJsonDocument::fromJson(reply.toUtf8());
|
|
|
|
QJsonObject replyObj = jsonReply.object();
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-05-19 14:06:05 +02:00
|
|
|
//qDebug() << "got: " << reply;
|
|
|
|
|
2019-03-24 21:16:16 +01:00
|
|
|
if(!replyObj.isEmpty()){
|
|
|
|
id = replyObj.value("id").toInt();
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-03-24 21:16:16 +01:00
|
|
|
for(int i = 0; i < this->waitingRequests.length(); i++){
|
|
|
|
if(this->waitingRequests[i].id == id){
|
|
|
|
this->waitingRequests[i].reply = replyObj;
|
2019-05-21 18:40:45 +02:00
|
|
|
if(this->waitingRequests[i].loop != nullptr){
|
|
|
|
this->waitingRequests[i].loop->quit();
|
|
|
|
}
|
2019-03-24 21:16:16 +01:00
|
|
|
return;
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
2019-03-24 21:16:16 +01:00
|
|
|
|
|
|
|
latestReadReply = reply;
|
|
|
|
emit gotUnexpectedReply(reply);
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
2019-05-21 18:40:45 +02:00
|
|
|
// ------------------------
|
|
|
|
// --- helper functions ---
|
|
|
|
// ------------------------
|
|
|
|
|
2019-03-03 17:31:24 +01:00
|
|
|
int BaseConn::writeRemoteSetting(QString key, QString value) {
|
|
|
|
QJsonArray requestData;
|
|
|
|
requestData.append(key);
|
|
|
|
requestData.append(value);
|
|
|
|
return this->sendCommand(3000, requestData)["status"].toInt();
|
|
|
|
}
|
2019-03-02 23:01:05 +01:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
void BaseConn::setIP(const QString &ipAdress){
|
|
|
|
this->ip = ipAdress;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BaseConn::getIP() const
|
|
|
|
{
|
|
|
|
return(this->ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BaseConn::getState() const
|
|
|
|
{
|
|
|
|
return(this->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::setState(QString newState){
|
2019-08-19 15:14:11 +02:00
|
|
|
if(this->state != newState) {
|
2019-08-20 22:55:37 +02:00
|
|
|
qDebug() << "+--- BaseConn state changed: " << newState;
|
2019-08-19 15:14:11 +02:00
|
|
|
this->state = newState;
|
|
|
|
emit stateChanged();
|
|
|
|
if(this->state == "disconnected") {
|
|
|
|
this->deInit();
|
|
|
|
}
|
2019-03-29 23:42:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
int BaseConn::getProgress() const
|
|
|
|
{
|
|
|
|
return(connection_progress);
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
bool BaseConn::refreshConnections() {
|
2019-03-02 23:01:05 +01:00
|
|
|
QVariantMap reply = this->sendCommand(2006);
|
|
|
|
|
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
2019-03-29 23:42:56 +01:00
|
|
|
if(reply["status"] == 910){
|
|
|
|
this->connections = QVariantList({});
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-07 17:18:24 +01:00
|
|
|
qDebug() << "+ --- error refreshing connections: " << reply["status"];
|
2018-10-04 18:35:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
QVariantList tmpConnections = reply["data"].toList();
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2019-03-29 23:42:56 +01:00
|
|
|
if(this->connections != reply["data"].toList()){
|
|
|
|
this->connections = reply["data"].toList();
|
|
|
|
emit this->connectionsChanged();
|
|
|
|
}
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
return true;
|
2018-10-14 18:39:39 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
QVariant BaseConn::getConnections() {
|
|
|
|
return(connections);
|
2019-03-27 22:26:59 +01:00
|
|
|
/*
|
|
|
|
"id": "id of the extention (int)",
|
2019-03-29 23:42:56 +01:00
|
|
|
"type": "type of the extention (can be: 'STARTPAD', 'TOPPAD')",
|
2019-03-27 22:26:59 +01:00
|
|
|
"name": "name of the extention",
|
|
|
|
"ip": "ip-adress of he extention (string)",
|
|
|
|
"state": "state of the extention (can be: 'disconnected', 'connecting', 'connected')"
|
|
|
|
*/
|
|
|
|
//QVariantMap conn = {{"id",0}, {"type","STARTPAD"}, {"name", "startpad1"}, {"ip", "192.168.4.11"}, {"state", "connected"}};
|
|
|
|
//QVariantMap conn1 = {{"id",0}, {"type","TOPPAD"}, {"name", "buzzer1"}, {"ip", "192.168.4.10"}, {"state", "connected"}};
|
|
|
|
//QVariantList conns = {conn, conn1};
|
|
|
|
//return conns;
|
2019-03-07 17:18:24 +01:00
|
|
|
}
|