summaryrefslogtreecommitdiff
path: root/noncore/net/wellenreiter/gui/gps.cpp
blob: a47b4ec5dfb96a681a40b8455c41dcae130d9b07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**********************************************************************
** Copyright (C) 2003 Michael 'Mickey' Lauer.  All rights reserved.
**
** This file is part of Wellenreiter II.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
**********************************************************************/

#include "gps.h"

/* QT */
#include <qtextstream.h>

/* STD */
#include <stdlib.h>
#include <unistd.h>

GPS::GPS( QObject* parent, const char * name )
    :QObject( parent, name )
{
    qDebug( "GPS::GPS()" );
    _socket = new QSocket( this, "gpsd commsock" );
}


GPS::~GPS()
{
    qDebug( "GPS::~GPS()" );
}


bool GPS::open( const QString& host, int port )
{
    _socket->connectToHost( host, port );
}


GpsLocation GPS::position() const
{
    char buf[256];
    double lat = -111.0;
    double lon = -111.0;

    int result = _socket->writeBlock( "p\r\n", 3 );
    _socket->flush();
    if ( result )
    {
        int numAvail = _socket->bytesAvailable();
        qDebug( "GPS write succeeded, %d bytes available for reading...", numAvail );
        if ( numAvail )
        {                     
            int numRead = _socket->readBlock( &buf[0], sizeof buf );
            int numScan = ::sscanf( &buf[0], "GPSD,P=%lg %lg", &lat, &lon);
            
            if ( numRead < 7 || numScan != 2 )
            {
                qDebug( "GPS read %d bytes succeeded, invalid response: '%s'", numRead, &buf[0] );
                return GpsLocation( -111, -111 );
            }
            else
            {
                return GpsLocation( lat, lon );
            }
        }
    }
    return GpsLocation( -111, -111 );
}


QString GpsLocation::dmsPosition() const
{
    if ( _latitude == -111 || _longitude == -111 )
        return "N/A";
    if ( _latitude == 0.0 && _longitude == 0.0 )
        return "NULL";

    /* compute latitude */

    QString dms = "N";
    if ( _latitude >= 0 ) dms.append( "+" );

    int trunc = int( _latitude );
    float rest = _latitude - trunc;

    float minf = rest * 60;
    int minutes = int( minf );

    rest = minf - minutes;
    int seconds = int( rest * 60 );

    dms.append( QString::number( trunc ) );
    dms.append( "° " );
    dms.append( QString::number( ::abs( minutes ) ) );
    dms.append( "' " );
    dms.append( QString::number( ::abs( seconds ) ) );
    dms.append( "'' " );

    /* compute longitude */

    dms.append( " | W" );
    if ( _longitude > 0 ) dms.append( "+" );

    trunc = int( _longitude );
    rest = _longitude - trunc;

    minf = rest * 60;
    minutes = int( minf );

    rest = minf - minutes;
    seconds = int( rest * 60 );

    dms.append( QString::number( trunc ) );
    dms.append( "° " );
    dms.append( QString::number( ::abs( minutes ) ) );
    dms.append( "' " );
    dms.append( QString::number( ::abs( seconds ) ) );
    dms.append( "'' " );

    return dms;
}