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