-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
152 lines (129 loc) · 4.42 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()),this,SLOT(sockReady()));
connect(socket,SIGNAL(disconnected()),this,SLOT(sockDisk()));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeOut()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
socket->connectToHost("192.168.0.136", 5555);
}
void MainWindow::sockDisk()
{
socket->deleteLater();
}
void MainWindow::sockReady()
{
if(socket->waitForConnected(500))
{
Data = socket->readAll();
doc = QJsonDocument::fromBinaryData(Data);
QJsonObject obj = doc.object();
ui->Freq->setText(QString::number(obj["freq"].toInt())+" Hz");
ui->Temp->setText(QString::number(obj["temp"].toDouble())+" °C");
if(doc.object().value("GPIOReady").toString()=="yes")
{
QStandardItemModel* model = new QStandardItemModel(nullptr);
model->setHorizontalHeaderLabels(QStringList()<<"Availabel GPIO");
QString gpio = doc.object().value("availableGPIO").toString();
QStringList listGPIO = gpio.split(',');
for(int i=0; i<listGPIO.length()-1; i++)
{
QStandardItem* col = new QStandardItem(listGPIO[i]);
model->appendRow(col);
}
ui->tableView->setModel(model);
}
else if(doc.object().value("chekMode").toString()=="GPIO")
{
ui->lineEdit_chekActiveLow->setText(doc.object().value("activeLow").toString());
ui->lineEdit_chekDirection->setText(doc.object().value("direction").toString());
ui->lineEdit_chekValue->setText(doc.object().value("value").toString());
}
else if(doc.object().value("statusGPIO").toString()=="closed")
{
QMessageBox::information(this,"Информация","GPIO с таким номером не идентифицирован");
}
}
}
void MainWindow::onTimeOut()
{
if(socket->isOpen())
{
QJsonObject freqTemp;
freqTemp.insert("type","request");
freqTemp.insert("value","freqTemp");
QJsonDocument freqTempDoc;
freqTempDoc.setObject(freqTemp);
socket->write(freqTempDoc.toBinaryData());
}
}
void MainWindow::on_pushButton_2_clicked()
{
if (socket->isOpen())
{
QJsonObject obj;
obj.insert("type","request");
obj.insert("value","GPIO");
QJsonDocument dc;
dc.setObject(obj);
socket->write(dc.toBinaryData());
}
}
void MainWindow::on_pushButton_crateGPIO_clicked()
{
if (socket->isOpen())
{
QString st = ui->lineEdit_5->text();
QJsonObject jsonCreateGPIO;
jsonCreateGPIO.insert("type","create");
jsonCreateGPIO.insert("value",st);
QJsonDocument createGPIO;
createGPIO.setObject(jsonCreateGPIO);
socket->write(createGPIO.toBinaryData());
}
}
void MainWindow::on_pushButton_changeModeGPIO_clicked()
{
if (socket->isOpen())
{
QString numberGPIO = ui->lineEdit_modeNumberGPIO->text();
QString direction = ui->lineEdit_modeDirection->text();
QString value = ui->lineEdit_modeValue->text();
QString activeLow = ui->lineEdit_modeActiveLow->text();
QJsonObject changeModeGPIO;
changeModeGPIO.insert("type","changeMode");
changeModeGPIO.insert("numberGPIO",numberGPIO);
changeModeGPIO.insert("direction",direction);
changeModeGPIO.insert("value",value);
changeModeGPIO.insert("activeLow",activeLow);
QJsonDocument changeDoc;
changeDoc.setObject(changeModeGPIO);
socket->write(changeDoc.toBinaryData());
}
}
void MainWindow::on_pushButton_chekMode_clicked()
{
if(socket->isOpen())
{
QString numberGPIO = ui->lineEdit_chekNumberGPIO->text();
QJsonObject chekMode;
chekMode.insert("type","chekMode");
chekMode.insert("numberGPIO",numberGPIO);
QJsonDocument chekDoc;
chekDoc.setObject(chekMode);
socket->write(chekDoc.toBinaryData());
}
}