Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

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