File: SpotClient.cpp

package info (click to toggle)
js8call 2.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,416 kB
  • sloc: cpp: 563,285; f90: 9,265; ansic: 937; python: 132; sh: 93; makefile: 6
file content (130 lines) | stat: -rw-r--r-- 3,539 bytes parent folder | download | duplicates (3)
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
128
129
130
#include "SpotClient.h"
#include "Message.h"

#include "moc_SpotClient.cpp"

SpotClient::SpotClient(MessageClient *client, QObject *parent):
    QObject(parent),
    m_client { client }
{
    prepare();

    connect(&m_timer, &QTimer::timeout, this, &SpotClient::processSpots);
    m_timer.setInterval(60 * 1000);
    m_timer.setSingleShot(false);
    m_timer.start();
}

void SpotClient::prepare(){
    QHostInfo::lookupHost("spot.js8call.com", this, SLOT(dnsLookupResult(QHostInfo)));
}

void SpotClient::dnsLookupResult(QHostInfo info){
    if (info.addresses().isEmpty()) {
        qDebug() << "SpotClient Error:" << info.errorString();
        return;
    }

    m_address = info.addresses().at(0);
    qDebug() << "SpotClient Resolve:" << m_address.toString();
}

void SpotClient::setLocalStation(QString callsign, QString grid, QString info, QString version){
    bool changed = false;

    if(m_call != callsign){
        m_call = callsign;
        changed = true;
    }

    if(m_grid != grid){
        m_grid = grid;
        changed = true;
    }

    if(m_info != info){
        m_info = info;
        changed = true;
    }

    if(m_version != version){
        m_version = version;
        changed = true;
    }

    // send local information to network on change, or once every 15 minutes
    if(changed || m_seq % 15 == 0){
        enqueueLocalSpot(callsign, grid, info, version);
    }
}

void SpotClient::enqueueLocalSpot(QString callsign, QString grid, QString info, QString version){
    auto m = Message("RX.LOCAL", "", {
        {"CALLSIGN", QVariant(callsign)},
        {"GRID", QVariant(grid)},
        {"INFO", QVariant(info)},
        {"VERSION", QVariant(version)},
    });

    m_queue.enqueue(m.toJson());
}

void SpotClient::enqueueSpot(QString callsign, QString grid, int submode, int dial, int offset, int snr){
    auto m = Message("RX.SPOT", "", {
         {"BY", QVariant(QMap<QString, QVariant>{
              {"CALLSIGN", QVariant(m_call)},
              {"GRID", QVariant(m_grid)},
         })},
         {"CALLSIGN", QVariant(callsign)},
         {"GRID", QVariant(grid)},
         {"FREQ", QVariant(dial+offset)},
         {"DIAL", QVariant(dial)},
         {"OFFSET", QVariant(offset)},
         {"SNR", QVariant(snr)},
         {"SPEED", QVariant(submode)},
    });

    m_queue.enqueue(m.toJson());
}

void SpotClient::enqueueCmd(QString cmd, QString from, QString to, QString relayPath, QString text, QString grid, QString extra, int submode, int dial, int offset, int snr){
    auto m = Message("RX.DIRECTED", "", {
         {"BY", QVariant(QMap<QString, QVariant>{
              {"CALLSIGN", QVariant(m_call)},
              {"GRID", QVariant(m_grid)},
         })},
         {"CMD", QVariant(cmd)},
         {"FROM", QVariant(from)},
         {"TO", QVariant(to)},
         {"PATH", QVariant(relayPath)},
         {"TEXT", QVariant(text)},
         {"GRID", QVariant(grid)},
         {"EXTRA", QVariant(extra)},
         {"FREQ", QVariant(dial+offset)},
         {"DIAL", QVariant(dial)},
         {"OFFSET", QVariant(offset)},
         {"SNR", QVariant(snr)},
         {"SPEED", QVariant(submode)},
    });

    m_queue.enqueue(m.toJson());
}

void SpotClient::processSpots(){
    if(m_address.isNull()){
        prepare();
        return;
    }

    while(!m_queue.isEmpty()){
        sendRawSpot(m_queue.dequeue());
    }

    m_seq++;
}

void SpotClient::sendRawSpot(QByteArray payload){
    if(!m_address.isNull()){
        m_client->send_raw_datagram(payload, m_address, 50000);
    }
}