-
Notifications
You must be signed in to change notification settings - Fork 2
/
mynetwork.cpp
107 lines (87 loc) · 2.59 KB
/
mynetwork.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
#include "mynetwork.h"
MyNetwork::MyNetwork(QObject *parent) : QObject(parent)
{
m_udpSocket = new QUdpSocket(this);
if(!m_udpSocket->bind(udpPort, QUdpSocket::ShareAddress )) //
qDebug() << "m_udpSocket : " + m_udpSocket->errorString() ;
else
qDebug() <<"UDP bind 127.0.0.1:" +QString::number(udpPort)+" LISTENING." ;
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(readyReadUDP()));
avgBPM(0);
minBPM(0);
maxBPM(0);
currentBPM(0);
currentSpO2(0);
for(int i = 0 ; i < m_max ; i++)
m_vectorBPM << 0;
}
void MyNetwork::sendUDP(QString msg)
{
if(enable_debug_udp)
qDebug() << "sendUDP: " << msg ;
QByteArray datagram = msg.toLatin1() ;
m_udpSocket->writeDatagram(datagram.data(), datagram.size(),
QHostAddress::Broadcast, udpPort);
}
void MyNetwork::removeBackgroundGridChart(QAbstractSeries *xSeries, QAbstractSeries *ySeries)
{
xSeries->setVisible(false);
ySeries->setVisible(false);
}
void MyNetwork::readyReadUDP()
{
while (m_udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(m_udpSocket->pendingDatagramSize());
m_udpSocket->readDatagram(datagram.data(), datagram.size());
QString data = datagram;
if(enable_debug_udp)
qDebug() << "readyReadUDP: " << data ;
if(data.contains("<BPM>"))
{
data.replace("<BPM>","");
currentBPM(data.toDouble());
m_vectorBPM[m_index] = data.toDouble();
m_index++ ;
if(m_index >= m_max)
m_index = 0;
minBPM(getMin(m_vectorBPM));
maxBPM(getMax(m_vectorBPM));
avgBPM(getAvg(m_vectorBPM));
}
else if(data.contains("<Beat>"))
{
data.replace("<Beat>","");
updateBPM(data.toDouble());
}
else if(data.contains("<SpO2>"))
{
data.replace("<SpO2>","");
currentSpO2(data.toDouble());
}
}
}
int MyNetwork::getMin(QList<int> vector)
{
int tmp = vector.at(0) ;
for(int i = 1 ; i < vector.length() ; i++ )
if( tmp > vector.at(i) )
tmp = vector.at(i);
return tmp ;
}
int MyNetwork::getMax(QList<int> vector)
{
int tmp = 0 ;
for(int i = 0 ; i < vector.length() ; i++ )
if( tmp < vector.at(i) )
tmp = vector.at(i);
return tmp ;
}
int MyNetwork::getAvg(QList<int> vector)
{
int tmp = 0 ;
for(int i = 0 ; i < vector.length() ; i++ )
tmp += vector.at(i);
return tmp / vector.length() ;
}