-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpidiscoverer.cpp
62 lines (52 loc) · 1.51 KB
/
pidiscoverer.cpp
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
#include "pidiscoverer.h"
PiNode::PiNode()
{
caps = PiNode::None;
capsRunning = PiNode::None;
}
bool PiNode::operator ==(const PiNode &node) const
{
return (this->addressString == node.addressString);
}
PiDiscoverer::PiDiscoverer(QObject *parent) : QObject(parent)
{
m_socket.bind(31311);
connect(&m_socket, SIGNAL(readyRead()), SLOT(datagramReceived()));
}
void PiDiscoverer::datagramReceived()
{
qint64 datagramSize = m_socket.pendingDatagramSize();
QByteArray datagram;
datagram.resize(datagramSize);
QHostAddress addr;
m_socket.readDatagram(datagram.data(), datagramSize, &addr);
qDebug() << "data received " << datagram.data();
PiNode node;
if (datagram.startsWith("raspberry")) {
if (datagram.contains("picam")) {
node.caps |= PiNode::PiCam;
}
if (datagram.contains("thermal")) {
node.caps |= PiNode::Thermal;
}
if (datagram.contains("mavproxy")) {
node.caps |= PiNode::MAVProxy;
}
node.address = addr;
node.addressString = addr.toString().split(":").last();
onNodeDiscovered(node);
Q_EMIT nodeDiscovered(node);
}
}
PiNodeList PiDiscoverer::discoveredNodes() const
{
return m_discoveredNodes;
}
void PiDiscoverer::onNodeDiscovered(const PiNode &node)
{
qDebug() << "node discovered " << node.addressString;
if (!m_discoveredNodes.contains(node)) {
qDebug() << "new unique node adding";
m_discoveredNodes << node;
}
}