Root/gpsclient.cpp

1/*
2 * Copyright 2008, 2010 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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 Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19
20#include "gpsclient.h"
21
22#include <QtCore/QDebug>
23#include <QtCore/QStringList>
24#include <QtCore/QTimer>
25
26GpsClient::GpsClient(QObject *parent) : QObject(parent),
27    m_socket(new QTcpSocket(this))
28{
29    connect(m_socket, SIGNAL(connected()), this, SIGNAL(connected()));
30    connect(m_socket, SIGNAL(connected()), this, SLOT(conn()));
31    connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
32    connect(m_socket, SIGNAL(readyRead()), this, SLOT(readData()));
33}
34
35void GpsClient::connectGps()
36{
37    m_socket->connectToHost("127.0.0.1", 2947);
38}
39
40void GpsClient::disconnectGps()
41{
42    QTextStream out(m_socket);
43    out << "?WATCH={\"enable\":false}\n";
44    m_socket->disconnectFromHost();
45}
46
47void GpsClient::readData()
48{
49    QTextStream in(m_socket);
50    QString reply = in.readLine();
51    if (reply.contains("TPV")) {
52        reply.remove("{");
53        reply.remove("}");
54        qreal lat = 0;
55        qreal lon = 0;
56        int mode = 0;
57        QStringList entries = reply.split(",");
58        foreach (const QString &entry, entries) {
59            if (entry.contains("lat")) {
60                lat = entry.section(":", 1, 1).toDouble();
61            } else if (entry.contains("lon")) {
62                lon = entry.section(":", 1, 1).toDouble();
63            } else if (entry.contains("alt")) {
64                qreal alt = entry.section(":", 1, 1).toDouble();
65                emit altitude(alt);
66            } else if (entry.contains("track")) {
67                qreal track = entry.section(":", 1, 1).toDouble();
68                emit direction(track);
69            } else if (entry.contains("speed")) {
70                qreal currentSpeed = entry.section(":", 1, 1).toDouble();
71                emit speed(currentSpeed);
72            } else if (entry.contains("mode")) {
73                // 0: no mode value yet seen
74                // 1: no fix
75                // 2: 2D
76                // 3: 3D
77                mode = entry.section(":", 1, 1).toInt();
78            }
79        }
80        if (mode > 1) {
81            emit fixed(true);
82            emit position(QPointF(lon, lat));
83        } else {
84            emit fixed(false);
85        }
86    }
87}
88
89void GpsClient::conn()
90{
91    QTextStream out(m_socket);
92    out << "?WATCH={\"enable\":true,\"json\":true}\n";
93}
94
95

Archive Download this file

Branches:
master



interactive