Subversion Repositories Projects

Rev

Rev 394 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 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"
272 KeyOz 20
#include "ToolBox.h"
227 KeyOz 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
 
440 Brean 60
void cQMK_Server::NewPosition(sNaviData Pos)
227 KeyOz 61
{
62
    if (1==1)
63
    {
394 Brean 64
        QString data = QString::number(Pos.Longitude).toAscii() + ":" +
65
                       QString::number(Pos.Latitude).toAscii() + ":" +
66
                       QString::number(Pos.Altitude).toAscii();
67
        SendData(100, data);
227 KeyOz 68
    }
69
}
70
 
272 KeyOz 71
void cQMK_Server::send_RawData(QString Data)
72
{
73
    Data = Data + "\n";
74
 
75
    QByteArray SendText = Data.toAscii();
76
 
77
    TcpSocket->write(SendText);
78
}
79
 
227 KeyOz 80
void cQMK_Server::slot_Connected()
81
{
82
    connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_ReadLine()));
83
    connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater()));
84
    connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_Disconnect()));
85
 
86
    emit sig_Connected();
87
}
88
 
89
void cQMK_Server::slot_Disconnect()
90
{
91
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
92
    emit sig_Disconnected(1);
93
}
94
 
95
void cQMK_Server::slot_Error(QAbstractSocket::SocketError Error)
96
{
97
    switch (Error)
98
    {
99
        case QAbstractSocket::ConnectionRefusedError:
100
            emit sig_Disconnected(2);
101
        break;
102
        case QAbstractSocket::RemoteHostClosedError:
103
            emit sig_Disconnected(1);
104
        break;
105
        default:
106
            emit sig_Disconnected(0);
107
        break;
108
    }
109
 
110
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
111
    emit sig_Disconnected(2);
112
}
113
 
114
void cQMK_Server::slot_ReadLine()
115
{
116
    QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar('\n'));
117
 
118
    QStringList Data = Input.split(":");
119
 
120
    if (Data.count() > 1)
121
    {
122
        int CMD = Data[0].toInt();
123
 
124
        switch(CMD)
125
        {
126
            case 1 :
127
            {
305 KeyOz 128
                SendData(1, QString(QA_NAME + " " + QA_VERSION + " [" + QA_OS + "]:" + Username + ":" + Password + ":"));
227 KeyOz 129
            }
130
            break;
131
            case 2 :
132
            {
133
//                qDebug("Login OK");
134
            }
135
            break;
136
            case 3 :
137
            {
138
                emit sig_Disconnected(3);
139
            }
140
            break;
141
        }
142
    }
143
}
144
 
145
 
146