-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudplistener.cpp
More file actions
73 lines (61 loc) · 2.24 KB
/
udplistener.cpp
File metadata and controls
73 lines (61 loc) · 2.24 KB
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
#include "UDPListener.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
UDPListener::UDPListener(QObject *parent)
: QObject(parent)
{
connect(&udpSocket, &QUdpSocket::readyRead, this, &UDPListener::onReadyRead);
}
void UDPListener::startListening(quint16 port)
{
if (!udpSocket.bind(QHostAddress::AnyIPv4, port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint)) {
qWarning() << "Failed to bind UDP port:" << port;
} else {
qDebug() << "Listening for UDP on port" << port;
}
}
void UDPListener::onReadyRead()
{
while (udpSocket.hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket.pendingDatagramSize());
udpSocket.readDatagram(datagram.data(), datagram.size());
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(datagram, &err);
if (err.error != QJsonParseError::NoError) {
qWarning() << "JSON parse error:" << err.errorString();
continue;
}
if (!doc.isObject()) {
qWarning() << "Invalid JSON root.";
continue;
}
QJsonObject root = doc.object();
QString appid = root.value("appid").toString();
QString msgtype = root.value("msgtype").toString();
if (appid == "QLog" && msgtype == "rigstatus") {
QJsonObject data = root.value("data").toObject();
parseRigStatus(data);
emit rigStatusReceived(data); // Optional original signal
}
}
}
void UDPListener::parseRigStatus(const QJsonObject &data)
{
QString profile = data.value("profile").toString();
QJsonArray vfoStates = data.value("vfostates").toArray();
if (!vfoStates.isEmpty()) {
QJsonObject vfo = vfoStates.first().toObject();
QString freqStr = vfo.value("freq").toString();
bool ptt = vfo.value("ptt").toBool();
double frequency = freqStr.toDouble();
// qDebug() << "Profile:" << profile;
// qDebug() << "Frequency:" << frequency << "MHz";
// qDebug() << "PTT:" << (ptt ? "ON" : "OFF");
emit rigStatusParsed(profile, frequency, ptt); // optional signal
} else {
qWarning() << "No VFO state available in rigstatus.";
}
}