udphandler.cpp
3.71 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "udphandler.h"
#include <QtDebug>
#include "config.h"
#define IPC_UDP_SYS_HOST "127.0.0.1"
#define IPC_UDP_SYS_PORT 4001
#define IPC_UDP_GUI_PORT 4000
typedef struct {
int header;
char body[];
} STRUCT_PACK packet_t;
UdpHandler *UdpHandler::instance = 0;
UdpHandler::UdpHandler(QObject *parent) : QObject(parent)
{
bzero(&control, sizeof(control));
bzero(&state, sizeof(state));
emitted = false;
emitTimeoutTimer.setSingleShot(true);
emitTimeoutTimer.setInterval(10 * 1000);
connect(&emitTimeoutTimer, SIGNAL(timeout()), SLOT(emitTimeout()));
emitTimeoutTimer.start();
}
bool UdpHandler::init()
{
sock = new QUdpSocket(this);
if (!sock->bind(IPC_UDP_GUI_PORT))
return false;
connect(sock, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
return true;
}
void UdpHandler::readPendingDatagrams()
{
while (sock->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(sock->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
sock->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
processDatagram(datagram);
}
}
void UdpHandler::processDatagram(QByteArray &datagram)
{
sock->writeDatagram(datagram, QHostAddress("localhost"), 40001);
if (emitted)
{
emitted = false;
emit recovered();
}
packet_t *packet = (packet_t *) datagram.data();
switch (packet->header)
{
case HDR_OVEN_CONTROL:
qDebug() << "Received Control";
processControl((oven_control_t *) packet->body);
break;
case HDR_OVEN_STATE:
qDebug() << "Received State";
processState((oven_state_t *) packet->body);
break;
case HDR_ERROR_CODE:
qDebug() << "Received Error";
break;
}
emitTimeoutTimer.start();
}
void UdpHandler::processControl(oven_control_t *control)
{
if (memcmp(&this->control, control, sizeof(this->control)))
{
memcpy(&this->control, control, sizeof(this->control));
emit changed();
}
}
void UdpHandler::processState(oven_state_t *state)
{
Define::config_item item = Config::getInstance()->getConfigValue(Define::config_model);
bool isElectric = false;
switch (item.d32)
{
case Define::model_electric_10:
case Define::model_electric_20:
case Define::model_electric_24:
case Define::model_electric_40:
isElectric = true;
break;
}
if (isElectric)
{
if (memcmp(&this->state, state, 100))
{
memcpy(&this->state, state, 100);
bzero(((char *) &this->state) + 100, sizeof(this->state) - 100);
emit changed();
}
}
else
{
if (memcmp(&this->state, state, sizeof(this->state)))
{
memcpy(&this->state, state, sizeof(this->state));
emit changed();
}
}
}
void UdpHandler::turnOn(int target)
{
sendCommand(CMD_ONOFF, target, SWITCH_ON);
}
void UdpHandler::turnOff(int target)
{
sendCommand(CMD_ONOFF, target, SWITCH_OFF);
}
void UdpHandler::set(int target, int value)
{
sendCommand(CMD_VALUE, target, value);
}
void UdpHandler::sendCommand(int cmd, int target, int value)
{
command_t command = { cmd, target, value };
qDebug() << "Send" << cmd << target << value;
sock->writeDatagram((const char *) &command, sizeof(command), QHostAddress(IPC_UDP_SYS_HOST), IPC_UDP_SYS_PORT);
}
void UdpHandler::emitTimeout()
{
emitted = true;
emit timeout();
}
void UdpHandler::fillControl(oven_control_t &container)
{
memcpy(&container, &control, sizeof(control));
}
void UdpHandler::fillData(oven_state_t &container)
{
memcpy(&container, &state, sizeof(state));
}