-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholaworker.cpp
138 lines (109 loc) · 3.33 KB
/
olaworker.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
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
#include "olaworker.h"
#include <ola/Callback.h>
#include <ola/client/OlaClient.h>
#include <ola/e131/E131ConfigMessages.pb.h>
#include <QDebug>
#include <QThread>
#include <string>
#include <vector>
using ola::NewSingleCallback;
using std::string;
using std::vector;
bool lessThan(const E131Source &e1, const E131Source &e2) {
return e1.cid < e2.cid;
}
OLAWorker::OLAWorker(QObject *parent)
: QObject(parent),
got_devices_(false) {
}
OLAWorker::~OLAWorker() {}
bool OLAWorker::Init() {
if (!m_wrapper.Setup()) {
return false;
}
return true;
}
void OLAWorker::process() {
qDebug() << "OLA Client thread running";
m_wrapper.GetClient()->FetchDeviceInfo(
ola::OLA_PLUGIN_E131,
NewSingleCallback(this, &OLAWorker::HandleDevices));
m_wrapper.GetSelectServer()->Run();
emit finished();
}
void OLAWorker::RefreshDiscoveredE131Sources() {
if (!got_devices_)
return;
ola::plugin::e131::Request request;
request.set_type(ola::plugin::e131::Request::E131_SOURCES_LIST);
ola::plugin::e131::SourceListRequest *source_list_request =
request.mutable_source_list();
(void) source_list_request; // no options for now.
string request_string;
request.SerializeToString(&request_string);
m_wrapper.GetClient()->ConfigureDevice(
device_alias_,
request_string,
NewSingleCallback(this, &OLAWorker::HandleConfigResponse));
}
void OLAWorker::HandleDevices(
const ola::client::Result &result,
const vector<ola::client::OlaDevice> &devices) {
if (!result.Success()) {
qWarning() << "Failed to get list of E1.33 devices";
return;
}
switch (devices.size()) {
case 0:
qWarning() << "No E1.31 devices found";
return;
case 1:
break;
default:
qWarning() << "More than one E1.31 device found, we'll use the first one";
break;
}
device_alias_ = devices[0].Alias();
got_devices_ = true;
}
void OLAWorker::HandleConfigResponse(const ola::client::Result &result,
const std::string &reply) {
if (!result.Success()) {
qWarning() << "Failed to get list of E1.33 devices";
return;
}
ola::plugin::e131::Reply reply_pb;
if (!reply_pb.ParseFromString(reply)) {
qWarning() << "Protobuf parsing failed";
return;
}
if (reply_pb.type() != ola::plugin::e131::Reply::E131_SOURCES_LIST) {
qWarning() << "Unknown reply: " << reply_pb.type();
return;
}
if (!reply_pb.has_source_list()) {
qWarning() << "Reply is missing source_list";
return;
}
if (reply_pb.source_list().unsupported()) {
qWarning() << "E1.31 discovery isn't enabled.";
return;
}
QVector<E131Source> sources;
for (int i = 0; i < reply_pb.source_list().source_size(); i++) {
const ola::plugin::e131::SourceEntry &entry =
reply_pb.source_list().source(i);
E131Source source;
source.cid = ola::acn::CID::FromString(entry.cid());
ola::network::IPV4Address::FromString(entry.ip_address(),
&source.ip_address);
source.source_name = entry.source_name();
for (int j = 0; j < entry.universe_size(); j++) {
source.universes.push_back(entry.universe(j));
}
qSort(source.universes.begin(), source.universes.end());
sources.push_back(source);
}
qSort(sources.begin(), sources.end(), lessThan);
emit E131SourceList(sources);
}