summaryrefslogtreecommitdiff
path: root/map65/livecq_decode.cpp
blob: 2cecdef9a28ad052c7a58b25551057b2f972f9bd (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "livecq_decode.hpp"

#include "../Network/DecodedTime.hpp"
#include "../validators/LiveCQCallsign.hpp"

namespace
{
  bool isReportableMessageType(QString const& messageType)
  {
    return messageType == "CQ" || messageType == "QRZ"
      || messageType == "CQV" || messageType == "CQH"
      || messageType == "QRT";
  }

  bool parseMode(QString const& modeToken, QString& mode)
  {
    if (modeToken.contains('#')) {
      mode = QString {"JT65"} + modeToken.back();
      return true;
    }
    if (modeToken.contains(':')) {
      mode = QString {"Q65-60"} + modeToken.back();
      return true;
    }
    return false;
  }
}

namespace Map65LiveCQ
{
  QList<Spot> parseSpots(QStringList const& decodeLines,
                         QString const& receiverCallsign,
                         QString const& receiverLocator,
                         bool crossPolarization,
                         QDateTime const& nowUtc,
                         QStringList& seenDecodes)
  {
    QList<Spot> spots;
    if (receiverCallsign.length() < 3 || receiverLocator.length() < 4) {
      return spots;
    }

    for (auto const& line : decodeLines) {
      auto const tokens = line.split(' ', Qt::SkipEmptyParts);
      if (tokens.size() < 6
          || !isReportableMessageType(tokens.at(5).trimmed())
          || !seenDecodes.filter(line.left(53)).isEmpty()) {
        continue;
      }
      seenDecodes.append(line);

      Spot spot;
      spot.scheduledFrequency = tokens.at(0).trimmed();
      spot.receiveFrequency = tokens.at(1).trimmed();
      spot.snr = tokens.at(4).trimmed();
      spot.messageType = tokens.at(5).trimmed().toUpper();

      QString callsign;
      QString modeToken;
      QString grid {"--"};
      QString polarization {"--"};
      QString transmitPolarization {"--"};

      if (tokens.size() >= 9 && tokens.at(7).contains('.')) {
        callsign = tokens.at(6).trimmed().toUpper();
        spot.deltaTime = tokens.at(7).trimmed();
        modeToken = tokens.at(8).trimmed();
        if (crossPolarization) {
          polarization = tokens.at(2).trimmed();
        }
      } else if (tokens.size() >= 10 && tokens.at(8).contains('.')) {
        auto const firstCandidate = tokens.at(6).trimmed().toUpper();
        if (LiveCQ::isValidCallsign(firstCandidate)) {
          callsign = firstCandidate;
          grid = tokens.at(7).trimmed();
        } else {
          callsign = tokens.at(7).trimmed().toUpper();
        }
        spot.deltaTime = tokens.at(8).trimmed();
        modeToken = tokens.at(9).trimmed();
        if (crossPolarization) {
          polarization = tokens.at(2).trimmed();
          if (tokens.size() == 11) {
            transmitPolarization = tokens.at(10).trimmed();
          }
        }
      } else if (tokens.size() >= 11 && tokens.at(9).contains('.')) {
        callsign = tokens.at(7).trimmed().toUpper();
        grid = tokens.at(8).trimmed();
        spot.deltaTime = tokens.at(9).trimmed();
        modeToken = tokens.at(10).trimmed();
        if (crossPolarization) {
          polarization = tokens.at(2).trimmed();
          if (tokens.size() == 12) {
            transmitPolarization = tokens.at(11).trimmed();
          }
        }
      } else {
        continue;
      }

      if (!LiveCQ::isValidCallsign(callsign)
          || !parseMode(modeToken, spot.mode)) {
        continue;
      }

      spot.utcDateTime = DecodedTime::spotTime(tokens.at(3).trimmed(), nowUtc, 60);
      if (!spot.utcDateTime.isValid()) {
        continue;
      }

      spot.callsign = callsign;
      spot.grid = grid;
      spot.polarization = polarization;
      spot.transmitPolarization = transmitPolarization;
      spots.append(spot);
    }
    return spots;
  }

  QUrlQuery spotQuery(Spot const& spot,
                      QString const& receiverCallsign,
                      QString const& receiverLocator)
  {
    QUrlQuery query;
    query.addQueryItem("skedfreq", spot.scheduledFrequency);
    query.addQueryItem("rxfreq", spot.receiveFrequency);
    query.addQueryItem("rpol", spot.polarization);
    query.addQueryItem("dt", spot.deltaTime);
    query.addQueryItem("dB", spot.snr);
    query.addQueryItem("msgtype", spot.messageType);
    query.addQueryItem("callsign", spot.callsign);
    query.addQueryItem("grid", spot.grid.toUpper());
    query.addQueryItem("mode", spot.mode);
    query.addQueryItem("utcdatetime",
                       spot.utcDateTime.toUTC().toString("yyyy-MM-ddTHH:mm:ss") + "Z");
    query.addQueryItem("spotter", receiverCallsign.toUpper());
    query.addQueryItem("spottergrid", receiverLocator.toUpper());
    query.addQueryItem("txpol", spot.transmitPolarization);
    query.addQueryItem("apptype", "MAP65");
    return query;
  }
}