Subversion Repositories Projects

Rev

Rev 391 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 391 Rev 394
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) 2008-2009 by Manuel Schrape                             *
2
 *   Copyright (C) 2008-2009 by Manuel Schrape                             *
3
 *   manuel.schrape@gmx.de                                                 *
3
 *   manuel.schrape@gmx.de                                                 *
4
 *                                                                         *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU General Public License as published by  *
6
 *   it under the terms of the GNU General Public License as published by  *
7
 *   the Free Software Foundation; either version 2 of the License.        *
7
 *   the Free Software Foundation; either version 2 of the License.        *
8
 *                                                                         *
8
 *                                                                         *
9
 *   This program is distributed in the hope that it will be useful,       *
9
 *   This program is distributed in the hope that it will be useful,       *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 *   GNU General Public License for more details.                          *
12
 *   GNU General Public License for more details.                          *
13
 *                                                                         *
13
 *                                                                         *
14
 *   You should have received a copy of the GNU General Public License     *
14
 *   You should have received a copy of the GNU General Public License     *
15
 *   along with this program; if not, write to the                         *
15
 *   along with this program; if not, write to the                         *
16
 *   Free Software Foundation, Inc.,                                       *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
18
 ***************************************************************************/
19
#include "cQMK_Server.h"
19
#include "cQMK_Server.h"
20
#include "ToolBox.h"
20
#include "ToolBox.h"
21
 
21
 
22
cQMK_Server::cQMK_Server()
22
cQMK_Server::cQMK_Server()
23
{
23
{
24
}
24
}
25
 
25
 
26
void cQMK_Server::Connect(QString IP, int Port, QString User, QString Pass)
26
void cQMK_Server::Connect(QString IP, int Port, QString User, QString Pass)
27
{
27
{
28
    Username = User;
28
    Username = User;
29
    Password = Pass;
29
    Password = Pass;
30
 
30
 
31
    TcpSocket = new(QTcpSocket);
31
    TcpSocket = new(QTcpSocket);
32
    TcpSocket->connectToHost (IP, Port);
32
    TcpSocket->connectToHost (IP, Port);
33
 
33
 
34
    connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_Connected()) );
34
    connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_Connected()) );
35
    connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError)));
35
    connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError)));
36
}
36
}
37
 
37
 
38
void cQMK_Server::Disconnect()
38
void cQMK_Server::Disconnect()
39
{
39
{
40
    TcpSocket->disconnectFromHost();
40
    TcpSocket->disconnectFromHost();
41
    disconnect(TcpSocket, SIGNAL(connected()), 0, 0);
41
    disconnect(TcpSocket, SIGNAL(connected()), 0, 0);
42
    disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0);
42
    disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0);
43
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
43
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
44
}
44
}
45
 
45
 
46
void cQMK_Server::SendData(int CMD, QString Data)
46
void cQMK_Server::SendData(int CMD, QString Data)
47
{
47
{
48
    QString SendString = QString(QString("%1").arg(CMD) + ":" + Data + ":");
48
    QString SendString = QString(QString("%1").arg(CMD) + ":" + Data + ":");
49
 
49
 
50
    int CRC = qChecksum(SendString.toLatin1().data(), SendString.length());
50
    int CRC = qChecksum(SendString.toLatin1().data(), SendString.length());
51
    QString sCRC = QString("%1").arg(CRC);
51
    QString sCRC = QString("%1").arg(CRC);
52
 
52
 
53
    SendString = SendString + sCRC + "\n";
53
    SendString = SendString + sCRC + "\n";
54
 
54
 
55
    QByteArray SendText = SendString.toAscii();
55
    QByteArray SendText = SendString.toAscii();
56
 
56
 
57
    TcpSocket->write(SendText);
57
    TcpSocket->write(SendText);
58
}
58
}
59
 
59
 
60
void cQMK_Server::NewPosition(sNaviString Pos)
60
void cQMK_Server::NewPosition(sNaviString Pos)
61
{
61
{
62
    if (1==1)
62
    if (1==1)
63
    {
63
    {
64
        SendData(100, QString(Pos.Longitude + ":" + Pos.Latitude + ":" + Pos.Altitude));
64
        QString data = QString::number(Pos.Longitude).toAscii() + ":" +
-
 
65
                       QString::number(Pos.Latitude).toAscii() + ":" +
-
 
66
                       QString::number(Pos.Altitude).toAscii();
-
 
67
        SendData(100, data);
65
    }
68
    }
66
}
69
}
67
 
70
 
68
void cQMK_Server::send_RawData(QString Data)
71
void cQMK_Server::send_RawData(QString Data)
69
{
72
{
70
    Data = Data + "\n";
73
    Data = Data + "\n";
71
 
74
 
72
    QByteArray SendText = Data.toAscii();
75
    QByteArray SendText = Data.toAscii();
73
 
76
 
74
    TcpSocket->write(SendText);
77
    TcpSocket->write(SendText);
75
}
78
}
76
 
79
 
77
void cQMK_Server::slot_Connected()
80
void cQMK_Server::slot_Connected()
78
{
81
{
79
    connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_ReadLine()));
82
    connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_ReadLine()));
80
    connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater()));
83
    connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater()));
81
    connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_Disconnect()));
84
    connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_Disconnect()));
82
 
85
 
83
    emit sig_Connected();
86
    emit sig_Connected();
84
}
87
}
85
 
88
 
86
void cQMK_Server::slot_Disconnect()
89
void cQMK_Server::slot_Disconnect()
87
{
90
{
88
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
91
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
89
    emit sig_Disconnected(1);
92
    emit sig_Disconnected(1);
90
}
93
}
91
 
94
 
92
void cQMK_Server::slot_Error(QAbstractSocket::SocketError Error)
95
void cQMK_Server::slot_Error(QAbstractSocket::SocketError Error)
93
{
96
{
94
    switch (Error)
97
    switch (Error)
95
    {
98
    {
96
        case QAbstractSocket::ConnectionRefusedError:
99
        case QAbstractSocket::ConnectionRefusedError:
97
            emit sig_Disconnected(2);
100
            emit sig_Disconnected(2);
98
        break;
101
        break;
99
        case QAbstractSocket::RemoteHostClosedError:
102
        case QAbstractSocket::RemoteHostClosedError:
100
            emit sig_Disconnected(1);
103
            emit sig_Disconnected(1);
101
        break;
104
        break;
102
        default:
105
        default:
103
            emit sig_Disconnected(0);
106
            emit sig_Disconnected(0);
104
        break;
107
        break;
105
    }
108
    }
106
 
109
 
107
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
110
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
108
    emit sig_Disconnected(2);
111
    emit sig_Disconnected(2);
109
}
112
}
110
 
113
 
111
void cQMK_Server::slot_ReadLine()
114
void cQMK_Server::slot_ReadLine()
112
{
115
{
113
    QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar('\n'));
116
    QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar('\n'));
114
 
117
 
115
    QStringList Data = Input.split(":");
118
    QStringList Data = Input.split(":");
116
 
119
 
117
    if (Data.count() > 1)
120
    if (Data.count() > 1)
118
    {
121
    {
119
        int CMD = Data[0].toInt();
122
        int CMD = Data[0].toInt();
120
 
123
 
121
        switch(CMD)
124
        switch(CMD)
122
        {
125
        {
123
            case 1 :
126
            case 1 :
124
            {
127
            {
125
                SendData(1, QString(QA_NAME + " " + QA_VERSION + " [" + QA_OS + "]:" + Username + ":" + Password + ":"));
128
                SendData(1, QString(QA_NAME + " " + QA_VERSION + " [" + QA_OS + "]:" + Username + ":" + Password + ":"));
126
            }
129
            }
127
            break;
130
            break;
128
            case 2 :
131
            case 2 :
129
            {
132
            {
130
//                qDebug("Login OK");
133
//                qDebug("Login OK");
131
            }
134
            }
132
            break;
135
            break;
133
            case 3 :
136
            case 3 :
134
            {
137
            {
135
                emit sig_Disconnected(3);
138
                emit sig_Disconnected(3);
136
            }
139
            }
137
            break;
140
            break;
138
        }
141
        }
139
    }
142
    }
140
}
143
}
141
 
144
 
142
 
145
 
143
 
146
 
144
 
147