-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensordata.cpp
311 lines (273 loc) · 8.58 KB
/
sensordata.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "sensordata.h"
#include "ui_sensordata.h"
#include "api/app.h"
#include "webdata.h"
#define TextColor QColor(255,255,255)
#define TextWidth 1
#define LineWidth 2
#define DotWidth 10
#define Plot_NoColor QColor(0,0,0,0)
#define Plot1_DotColor QColor(5,189,251)
#define Plot1_LineColor QColor(41,138,220)
#define Plot1_BGColor QColor(41,138,220,80)
#define Plot2_DotColor QColor(236,110,0)
#define Plot2_LineColor QColor(246,98,0)
#define Plot2_BGColor QColor(246,98,0,80)
#define Plot3_DotColor QColor(65,225,36)
#define Plot3_LineColor QColor(70,210,30)
#define Plot3_BGColor QColor(70,210,30,80)
frmSensorData::frmSensorData(QWidget *parent)
: QWidget(parent), ui(new Ui::frmSensorData)
{
ui->setupUi(this);
webData = new WebData(this);
plots.append(ui->plot_temperature);
plots.append(ui->plot_humidity);
plots.append(ui->plot_light);
isOff = true;
index = 0;
ledState = -1;
ledStateNum = 0;
InitPlot();
InitPlot1();
InitPlot2();
InitPlot3();
ui->ckBackground->setChecked(true);
ui->ckLegend->setChecked(true);
}
frmSensorData::~frmSensorData()
{
delete ui;
}
void frmSensorData::InitPlot()
{
//设置纵坐标名称
plots.at(0)->yAxis->setLabel("温度数据统计");
plots.at(1)->yAxis->setLabel("湿度数据统计");
plots.at(2)->yAxis->setLabel("光照强度数据统计");
QFont font = QFont(App::AppFontName, 8);
foreach (QCustomPlot * plot, plots) {
//设置字体大小
plot->legend->setFont(font);
plot->xAxis->setLabelFont(font);
plot->yAxis->setLabelFont(font);
plot->xAxis->setTickLabelFont(font);
plot->yAxis->setTickLabelFont(font);
//设置坐标颜色/坐标名称颜色
plot->yAxis->setLabelColor(TextColor);
plot->xAxis->setTickLabelColor(TextColor);
plot->yAxis->setTickLabelColor(TextColor);
plot->xAxis->setBasePen(QPen(TextColor, TextWidth));
plot->yAxis->setBasePen(QPen(TextColor, TextWidth));
plot->xAxis->setTickPen(QPen(TextColor, TextWidth));
plot->yAxis->setTickPen(QPen(TextColor, TextWidth));
plot->xAxis->setSubTickPen(QPen(TextColor, TextWidth));
plot->yAxis->setSubTickPen(QPen(TextColor, TextWidth));
//设置画布背景色
QLinearGradient plotGradient;
plotGradient.setStart(0, 0);
plotGradient.setFinalStop(0, 350);
plotGradient.setColorAt(0, QColor(80, 80, 80));
plotGradient.setColorAt(1, QColor(50, 50, 50));
plot->setBackground(plotGradient);
//设置图例提示位置及背景色
plot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
plot->legend->setBrush(QColor(255, 255, 255, 200));
plot->replot();
}
}
void frmSensorData::InitPlot1()
{
plots.at(0)->addGraph();
plots.at(0)->graph(0)->setName("温度值(单位:℃)");
plots.at(0)->graph(0)->setPen(QPen(Plot1_LineColor, LineWidth));
plots.at(0)->graph(0)->setScatterStyle(
QCPScatterStyle(QCPScatterStyle::ssCircle,
QPen(Plot1_DotColor, LineWidth),
QBrush(Plot1_DotColor), DotWidth));
plots.at(0)->xAxis->setRange(0, 40);
plots.at(0)->yAxis->setRange(0, 40);
}
void frmSensorData::InitPlot2()
{
plots.at(1)->addGraph();
plots.at(1)->graph(0)->setName("湿度值(单位:%)");
plots.at(1)->graph(0)->setPen(QPen(Plot2_LineColor,LineWidth));
plots.at(1)->graph(0)->setScatterStyle(
QCPScatterStyle(QCPScatterStyle::ssCircle,
QPen(Plot2_DotColor,LineWidth),
QBrush(Plot2_DotColor), DotWidth));
plots.at(1)->xAxis->setRange(0, 40);
plots.at(1)->yAxis->setRange(0, 100);
}
void frmSensorData::InitPlot3()
{
plots.at(2)->addGraph();
plots.at(2)->graph(0)->setName("光照值(单位:lx)");
plots.at(2)->graph(0)->setPen(QPen(Plot3_LineColor, LineWidth));
plots.at(2)->graph(0)->setScatterStyle(
QCPScatterStyle(QCPScatterStyle::ssCircle,
QPen(Plot3_DotColor,LineWidth),
QBrush(Plot3_DotColor), DotWidth));
plots.at(2)->xAxis->setRange(0, 40);
plots.at(2)->yAxis->setRange(0, 1000);
}
void frmSensorData::on_ckMove_stateChanged(int state)
{
bool value = (state == 0 ? false : true);
foreach(QCustomPlot *plot, plots){
if (value) {
plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
} else {
plot->setInteractions(QCP::iSelectOther);
}
plot->replot();
}
}
void frmSensorData::on_ckBackground_stateChanged(int state)
{
bool value = (state == 0 ? false : true);
if (value) {
plots[0]->graph(0)->setBrush(QBrush(Plot1_BGColor));
plots[1]->graph(0)->setBrush(QBrush(Plot2_BGColor));
plots[2]->graph(0)->setBrush(QBrush(Plot3_BGColor));
} else {
plots[0]->graph(0)->setBrush(QBrush(Plot_NoColor));
plots[1]->graph(0)->setBrush(QBrush(Plot_NoColor));
plots[2]->graph(0)->setBrush(QBrush(Plot_NoColor));
}
plots[0]->replot();
plots[1]->replot();
plots[2]->replot();
}
void frmSensorData::on_ckLegend_stateChanged(int state)
{
bool value = (state == 0 ? false : true);
foreach(QCustomPlot *plot, plots){
plot->legend->setVisible(value);
plot->replot();
}
}
void frmSensorData::on_ckXGrid_stateChanged(int state)
{
bool value = (state == 0 ? false : true);
foreach(QCustomPlot *plot, plots){
plot->xAxis->grid()->setSubGridVisible(value);
plot->replot();
}
}
void frmSensorData::on_ckYGrid_stateChanged(int state)
{
bool value = (state == 0 ? false : true);
foreach(QCustomPlot *plot, plots){
plot->yAxis->grid()->setSubGridVisible(value);
plot->replot();
}
}
void frmSensorData::on_ckTemperatureData_stateChanged(int state)
{
bool value = (state == 0 ? false : true);
if(value){
plots.at(0)->show();
}
else{
plots.at(0)->hide();
}
}
void frmSensorData::on_ckHumidityData_stateChanged(int state)
{
bool value = (state == 0 ? false : true);
if(value){
plots.at(1)->show();
}
else{
plots.at(1)->hide();
}
}
void frmSensorData::on_ckLightData_stateChanged(int state)
{
bool value = (state == 0 ? false : true);
if(value){
plots.at(2)->show();
}
else{
plots.at(2)->hide();
}
}
void frmSensorData::on_btnSwitch_clicked()
{
if(isOff)
{
ui->btnSwitch->setIcon(QIcon(":/image/switch_on.png"));
isOff = false;
}
else
{
ui->btnSwitch->setIcon(QIcon(":/image/switch_off.png"));
isOff = true;
}
methodSensorData = "sensorSwitchRequest";
argSensorData.insert("sensorSwitch", QString("%1").arg(!isOff));
argSensorData.insert("sensorMac", ui->le_mac->text());
webData->submitRequest(methodSensorData, argSensorData);
}
void frmSensorData::getSensorData(QString sensorMac, QString sensorTemperature, QString sensorHumidity, QString sensorLight, QString sensorSwitch)
{
ui->le_mac->setText(sensorMac);
dataInput1(sensorTemperature.toFloat());
dataInput2(sensorHumidity.toFloat());
dataInput3(sensorLight.toFloat());
// if(ledState != sensorSwitch.toInt())
// {
// ledState = sensorSwitch.toInt();
// ledStateNum = 1;
// }
// else
ledStateNum++;
qDebug() << "hzq:" << ledState;
if(ledStateNum == 5)
{
qDebug() << "hzq";
setSwitchState(sensorSwitch.toInt());
ledStateNum = 1;
}
index++;
}
void frmSensorData::dataInput1(float sensorTemperature)
{
if(plots.at(0) != NULL)
{
plots.at(0)->graph(0)->addData(index, sensorTemperature);
ui->le_temperature->setText(QString("%1").arg(sensorTemperature));
plots.at(0)->replot();
}
}
void frmSensorData::dataInput2(float sensorHumidity)
{
if(plots.at(1) != NULL)
{
plots.at(1)->graph(0)->addData(index, sensorHumidity);
ui->le_humidity->setText(QString("%1").arg(sensorHumidity));
plots.at(1)->replot();
}
}
void frmSensorData::dataInput3(float sensorLight)
{
if(plots.at(2) != NULL)
{
plots.at(2)->graph(0)->addData(index, sensorLight);
ui->le_light->setText(QString("%1").arg(sensorLight));
plots.at(2)->replot();
}
}
void frmSensorData::setSwitchState(int flag)
{
if(flag == 0)
{
ui->btnSwitch->setIcon(QIcon(":/image/switch_off.png"));
}
else if(flag == 1)
{
ui->btnSwitch->setIcon(QIcon(":/image/switch_on.png"));
}
}