This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgcodedialog.cpp
163 lines (143 loc) · 4.9 KB
/
gcodedialog.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
#include "gcodedialog.h"
#include "ui_gcodedialog.h"
#include <QTreeWidgetItem>
#include <QScrollBar>
//#include <QSizeF>
#include <QTimer>
#include <QStringList>
#include <mainwindow.h>
#include <QTextCodec>
#include "inputdialog.h"
#include <QWebSocket>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
GcodeDialog::GcodeDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::GcodeDialog)
{
ui->setupUi(this);
FUI = (MainWindow*)parent;//((MainWindow*)FUI)->octonetwork.SendGCode(_GCode_Setting);
ui->Sender->setFocus();
_ptimer = new QTimer();
connect(_pdataRecvWS,SIGNAL(disconnected()),this,SLOT(onWSDisconnected()),Qt::AutoConnection);
connect(_pdataRecvWS,SIGNAL(textMessageReceived(QString)),this,SLOT(onWSTextReceived(QString)),Qt::AutoConnection);
connect(_pdataRecvWS,SIGNAL(connected()),this,SLOT(onWSConnected()),Qt::AutoConnection);
connect(_ptimer,SIGNAL(timeout()),this,SLOT(onWSReconnect()),Qt::AutoConnection);
_pdataRecvWS->open(QUrl("ws://"+((MainWindow*)FUI)->octonetwork.MainIP+"/sockjs/websocket"));
// _pdataRecvWS->open(QUrl("ws://192.168.100.111/sockjs/websocket"));
this->resize((int)(SizePercent.width()*800),(int)(SizePercent.height()*480));
this->setMaximumSize((int)(SizePercent.width()*800),(int)(SizePercent.height()*480));
this->setFixedSize((int)(SizePercent.width()*800),(int)(SizePercent.height()*480));
}
GcodeDialog::~GcodeDialog()
{
delete ui;
}
void GcodeDialog::showEvent(QShowEvent *event)
{
Q_UNUSED(event);
}
void GcodeDialog::on_Btn_Back_clicked()
{
this->close();
}
void GcodeDialog::on_Btn_Send_clicked()
{
if(ui->Sender->text().isEmpty())
{
// ui->Sender->setFocus();
return;
}
ui->Sender->setText(ui->Sender->text().trimmed());
// SendCMD(ui->Sender->text());
((MainWindow*)FUI)->octonetwork.SendGCode(ui->Sender->text());
ui->Sender->setFocus();
}
void GcodeDialog::on_pushButton_clicked()
{
ui->Receiver->clear();
}
void GcodeDialog::on_Btn_Logo_clicked()
{
}
//断开连接会触发这个槽函数
void GcodeDialog::onWSDisconnected()
{
_ptimer->start();
}
//连接成功会触发这个槽函数
void GcodeDialog::onWSConnected()
{
_ptimer->stop();
}
//收到服务发来的消息会触发这个槽函数
void GcodeDialog::onWSTextReceived(QString msg)
{
// msg = msg.replace("\\","\"");
if(!msg.trimmed().startsWith("{\"current\":") || msg.trimmed().length() == 0)
return;
QJsonDocument loadDoc(QJsonDocument::fromJson(msg.toUtf8()));
QJsonObject QObj = loadDoc.object();
QList<QString> SearchLink ;
SearchLink << "current" << "logs";
foreach(QJsonValue _value, SearchWSJsonValue(SearchLink , QObj).toArray())
{
ui->Receiver->append(_value.toString());
// recveData(_value.toString());
}
// recveData(SearchWSJsonValue(SearchLink , QObj)[0].toString());
// recvData(SearchJsonValue(SearchLink , QObj).toString());
}
//断开连接会启动定时器,触发这个槽函数重新连接
void GcodeDialog::onWSReconnect()
{
_pdataRecvWS->abort();
_pdataRecvWS->open(QUrl("ws://"+((MainWindow*)FUI)->octonetwork.MainIP+"/sockjs/websocket"));
}
//Find_List_Name : Retrieved List Sequence --> [Grandma,Mother,Me]
//_QObj:Retrieved Retrieved Json Objecet
QJsonValue GcodeDialog::SearchWSJsonValue(QList<QString> Find_List_Name , QJsonObject _QObj)
{
QJsonValue returnValue;
QList<QString> SearchLink ;
SearchLink << Find_List_Name;
if(SearchLink.count() > 1)
{
QString MotherKey = SearchLink[0];
SearchLink.removeFirst();
if(MotherKey.indexOf("*") >= 0)//wildcard
{
QJsonArray ArrayValue;
for(int i = 0 ; i <= 9 ; i++)
{
QString TempMotherKey = MotherKey;
if(_QObj.find(TempMotherKey.replace("*",QString::number(i))).value().isUndefined())continue;
// if(QSysInfo::productType() != "raspbian"){qDebug()<<_QObj.find(TempMotherKey.replace("*",QString::number(i))).value().isUndefined();};
QJsonValue data = SearchWSJsonValue(SearchLink , _QObj.constFind(TempMotherKey.replace("*",QString::number(i))).value().toObject());
ArrayValue.append(data);
}
returnValue = ArrayValue;
}
else
returnValue = SearchWSJsonValue(SearchLink , _QObj.constFind(MotherKey).value().toObject());
}
else
{
if(SearchLink[0].indexOf("*") >= 0)//wildcard
{
}
else
returnValue = _QObj.constFind(SearchLink[0]).value();
}
return returnValue;
}
QList<QJsonValue> GcodeDialog::SearchWSJsonValue(QList<QList<QString>> Find_List_Names , QJsonObject _QObj)
{
QList<QJsonValue> _returnValue ;
foreach(QList<QString> item , Find_List_Names)
{
_returnValue.append(SearchWSJsonValue(item,_QObj));
}
return _returnValue;
}