-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevicestatusmanager.cpp
133 lines (101 loc) · 3.7 KB
/
devicestatusmanager.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
#include "devicestatusmanager.h"
DeviceStatusManager::DeviceStatusManager(QObject *parent)
: QObject{parent}
{
m_port = 55600;
socket = new QUdpSocket(this);
bool bindSuccess = socket->bind(QHostAddress::Any, this->m_port);
if(bindSuccess) {
connect(socket, &QUdpSocket::readyRead, this, &DeviceStatusManager::readIncomingUdpData);
qInfo() << "SUCESS binding on Port"<< this->m_port;
}
else {
qWarning() << "NO BINDING possible on Port"<< this->m_port;
}
}
void DeviceStatusManager::setIdForNamedDevice(QString deviceName, QString deviceId)
{
if (!oscInputDevices.contains(deviceName.toUtf8())) {
qWarning() << "Please implement oscInput-device creation triggered by backend!";
}
else {
QMutableMapIterator<QString, QByteArray> iter(deviceForId);
while(iter.hasNext()) {
iter.next();
if(iter.value() == deviceName) {
iter.remove();
}
}
deviceForId.insert(deviceId, deviceName.toUtf8());
oscInputDevices.value(deviceName.toUtf8())->setUniqueId(deviceId);
}
}
void DeviceStatusManager::deleteInputDevice(QString deviceId)
{
bool _warn = false;
if(const QByteArray &devName = deviceForId.value(deviceId, ""); devName != "") {
if(oscInputDevices.contains(devName)) {
oscInputDevices[devName]->deleteLater();
oscInputDevices.remove(devName);
deviceForId.remove(deviceId);
} else _warn = true;
} else _warn = true;
if(_warn) {
qWarning() << "DeviceManager could not found device with ID" << deviceId;
}
}
void DeviceStatusManager::loadMotionDeviceFromDomElement(QDomElement element)
{
QString _id = element.tagName();
QString _name = element.attribute("deviceName", "no!!name!!");
if(!oscInputDevices.contains(_name.toUtf8())) {
OscInputDevice *newMotionDevice = new OscInputDevice(_name, this);
oscInputDevices.insert(_name.toUtf8(), newMotionDevice);
setIdForNamedDevice(_name, _id);
emit newOscInputDevice(_name, newMotionDevice, _id);
}
else {
setIdForNamedDevice(_name, _id);
}
/*
QDomNode _attrNode = element.firstChildElement("oscinput-device");
if(_attrNode.isElement()) {
QDomElement _attrElement = _attrNode.toElement();
if(_attrElement.hasAttribute("nSensors")) {
}
}*/
}
void DeviceStatusManager::readIncomingUdpData()
{
QByteArray buffer(socket->pendingDatagramSize(), char());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(),buffer.size(), &sender, &senderPort);
handleOscPacket(OSCPP::Server::Packet(buffer.data(), buffer.size()));
}
void DeviceStatusManager::handleOscPacket(const OSCPP::Server::Packet &packet)
{
if(packet.isMessage()) handleOscMessage(OSCPP::Server::Message(packet));
}
void DeviceStatusManager::handleOscMessage(const OSCPP::Server::Message &message)
{
if(message == "/glove/ping") {
OSCPP::Server::ArgStream oscArgs(message.args());
if(oscArgs.tag() == 's') {
QByteArray pingersName(oscArgs.string());
if(oscInputDevices.contains(pingersName)) {
oscInputDevices.value(pingersName)->handlePingMessage(oscArgs);
}
else {
createNewMotionDevice(pingersName, oscArgs);
}
}
}
}
void DeviceStatusManager::createNewMotionDevice(QByteArray name, OSCPP::Server::ArgStream oscArgs)
{
OscInputDevice *newMotionDevice = new OscInputDevice(name, this);
oscInputDevices.insert(name, newMotionDevice);
newMotionDevice->handlePingMessage(oscArgs);
emit newOscInputDevice(name, newMotionDevice);
}