Details | 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" |
||
20 | |||
21 | cQMK_Server::cQMK_Server() |
||
22 | { |
||
23 | } |
||
24 | |||
25 | void cQMK_Server::Connect(QString IP, int Port, QString User, QString Pass) |
||
26 | { |
||
27 | Username = User; |
||
28 | Password = Pass; |
||
29 | |||
30 | TcpSocket = new(QTcpSocket); |
||
31 | TcpSocket->connectToHost (IP, Port); |
||
32 | |||
33 | connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_Connected()) ); |
||
34 | connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError))); |
||
35 | } |
||
36 | |||
37 | void cQMK_Server::Disconnect() |
||
38 | { |
||
39 | TcpSocket->disconnectFromHost(); |
||
40 | disconnect(TcpSocket, SIGNAL(connected()), 0, 0); |
||
41 | disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0); |
||
42 | disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0); |
||
43 | } |
||
44 | |||
45 | void cQMK_Server::SendData(int CMD, QString Data) |
||
46 | { |
||
47 | QString SendString = QString(QString("%1").arg(CMD) + ":" + Data + ":"); |
||
48 | |||
49 | int CRC = qChecksum(SendString.toLatin1().data(), SendString.length()); |
||
50 | QString sCRC = QString("%1").arg(CRC); |
||
51 | |||
52 | SendString = SendString + sCRC + "\n"; |
||
53 | |||
54 | QByteArray SendText = SendString.toAscii(); |
||
55 | |||
56 | TcpSocket->write(SendText); |
||
57 | } |
||
58 | |||
59 | void cQMK_Server::NewPosition(sNaviString Pos) |
||
60 | { |
||
61 | if (1==1) |
||
62 | { |
||
63 | SendData(100, QString(Pos.Longitude + ":" + Pos.Latitude + ":" + Pos.Altitude)); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | void cQMK_Server::slot_Connected() |
||
68 | { |
||
69 | // qDebug("Verbunden"); |
||
70 | |||
71 | connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_ReadLine())); |
||
72 | connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater())); |
||
73 | connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_Disconnect())); |
||
74 | |||
75 | emit sig_Connected(); |
||
76 | } |
||
77 | |||
78 | void cQMK_Server::slot_Disconnect() |
||
79 | { |
||
80 | // qDebug("Verbindung getrennt "); |
||
81 | disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0); |
||
82 | emit sig_Disconnected(1); |
||
83 | } |
||
84 | |||
85 | void cQMK_Server::slot_Error(QAbstractSocket::SocketError Error) |
||
86 | { |
||
87 | switch (Error) |
||
88 | { |
||
89 | case QAbstractSocket::ConnectionRefusedError: |
||
90 | emit sig_Disconnected(2); |
||
91 | break; |
||
92 | case QAbstractSocket::RemoteHostClosedError: |
||
93 | emit sig_Disconnected(1); |
||
94 | break; |
||
95 | default: |
||
96 | emit sig_Disconnected(0); |
||
97 | break; |
||
98 | } |
||
99 | |||
100 | disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0); |
||
101 | emit sig_Disconnected(2); |
||
102 | // qDebug("Error..!!"); |
||
103 | } |
||
104 | |||
105 | void cQMK_Server::slot_ReadLine() |
||
106 | { |
||
107 | QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar('\n')); |
||
108 | |||
109 | QStringList Data = Input.split(":"); |
||
110 | |||
111 | if (Data.count() > 1) |
||
112 | { |
||
113 | int CMD = Data[0].toInt(); |
||
114 | |||
115 | switch(CMD) |
||
116 | { |
||
117 | case 1 : |
||
118 | { |
||
119 | SendData(1, QString(QA_NAME + " " + QA_VERSION + ":" + Username + ":" + Password + ":")); |
||
120 | } |
||
121 | break; |
||
122 | case 2 : |
||
123 | { |
||
124 | // qDebug("Login OK"); |
||
125 | } |
||
126 | break; |
||
127 | case 3 : |
||
128 | { |
||
129 | emit sig_Disconnected(3); |
||
130 | } |
||
131 | break; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | |||
137 |