123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- #include "model_select.h"
- #include "ui_model_select.h"
- #include <QProcess>
- #include <QFile>
- #include <QTextStream>
- model_select::model_select(QWidget *parent) : QMainWindow(parent),
- ui(new Ui::model_select)
- {
- ui->setupUi(this);
- initsetting();
- }
- void model_select::handleTreeItemClicked(const QModelIndex &index)
- {
-
- emit treeItemClicked(index);
- }
- void model_select::setTreeModel(tree_model_set *model)
- {
- treeModel = model;
- }
- void model_select::setTree_init()
- {
- QStandardItemModel *model = treeModel->tree_set();
- ui->tree_set->setModel(model);
- ui->tree_set->expandAll();
-
- connect(ui->tree_set, &QTreeView::clicked, this, &model_select::handleTreeItemClicked);
- }
- model_select::~model_select()
- {
- delete ui;
- }
- void model_select::on_setListen_clicked()
- {
- unsigned short port = ui->port->text().toUShort();
- ms->listen(QHostAddress::Any, port);
-
-
- openSimulinkModel("tcp_test_link.m");
-
- sim_time_wt = ui->sim_time_wt->text();
- QString pro_sim_time_wt="sim_time_wt:";
- file_write(&pro_sim_time_wt, &sim_time_wt);
-
-
-
- ui->setListen->setText("继续仿真");
- }
- void model_select::initsetting()
- {
- this->setWindowTitle("开始设置");
- ui->port->setText("8899");
- ui->sim_time_wt->setText("100");
- ui->sim_time_qh->setText("100");
- connectNum = 0;
-
- setTree_init();
-
- cbo_select_Init();
-
-
- ms = new QTcpServer(this);
- connect(ms, &QTcpServer::newConnection, this, [=]()
- {
- QTcpSocket *clientSocket = ms->nextPendingConnection();
- mstatus->setPixmap(QPixmap(":/accessibility.svg").scaled(20, 20));
-
- connectedClients[clientSocket] = "";
-
- connectClient(clientSocket);
- receive_Client(clientSocket); });
-
- mstatus = new QLabel;
- ui->statusbar->addWidget(new QLabel("是否有用户连接:"));
- ui->statusbar->addWidget(mstatus);
- }
- void model_select::connectClient(QTcpSocket *clientSocket)
- {
- connectNum++;
- connect(clientSocket, &QTcpSocket::disconnected, this, [=]()
- {
- clientSocket->close();
- connectedClients.remove(clientSocket);
- connectNum = 0;
-
- if (connectNum == 0) {
- mstatus->setPixmap(QPixmap(":/chat-bubble-xmark.svg").scaled(20, 20));
- } });
- }
- void model_select::openSimulinkModel(const QString &modelName)
- {
- static QProcess *process = nullptr;
- if (!process)
- {
- process = new QProcess();
- QString program = "D:/app/matlab_2021b/bin/matlab.exe";
- QString model_path = "D:/desktop/NGS/model_git/model_zl/tcp";
- model_path.replace("/", "\\\\");
-
- QStringList arguments;
- arguments << "-r" << QString("cd('%1'); run('%2');").arg(model_path, modelName)
- << "-nosplash"
- << "-nodesktop";
-
-
-
-
-
-
- process->start(program, arguments);
- if (!process->waitForStarted())
- {
- qDebug() << "Failed to start MATLAB!";
- delete process;
- process = nullptr;
- }
- else
- {
- qDebug() << "MATLAB started successfully.";
- }
- }
- else
- {
- QString model_path = "D:/desktop/NGS/model_git/model_zl/tcp";
- QString command = QString("cd('%1'); run('%2');").arg(model_path, modelName);
-
- process->write(command.toUtf8() + "\n");
- process->waitForBytesWritten();
- qDebug() << "Sent command to running MATLAB: " << command;
- }
- }
- void model_select::cbo_select_Init()
- {
-
- ui->cbo_model_select->addItem("选择仿真模型");
- ui->cbo_model_select->addItem("稳态模型");
- ui->cbo_model_select->addItem("切换模型");
- ui->cbo_model_select->setCurrentIndex(0);
- connect(ui->cbo_model_select, &QComboBox::currentIndexChanged, this, &model_select::cbo_select_changed);
-
- ui->cbo_wt->addItem("选择稳态工况");
- ui->cbo_wt->addItem("27节");
- ui->cbo_wt->addItem("18节");
- ui->cbo_wt->addItem("12节");
- ui->cbo_wt->addItem("2.5节");
- ui->cbo_wt->addItem("9节");
- ui->cbo_wt->addItem("6节");
- ui->cbo_wt->addItem("6节静音");
- ui->cbo_wt->setCurrentIndex(0);
- connect(ui->cbo_wt, &QComboBox::currentIndexChanged, this, &model_select::cbo_wt_changed);
-
- ui->cbo_condition->addItem("选择工况");
- connect(ui->cbo_condition, &QComboBox::currentIndexChanged, this, &model_select::cbo_condition_changed);
- ui->cbo_qh->addItem("选择切换工况");
- ui->cbo_qh->addItem("切换1");
- ui->cbo_qh->addItem("切换2");
- ui->cbo_qh->addItem("切换3");
- ui->cbo_qh->addItem("切换4");
- ui->cbo_qh->addItem("切换5");
- ui->cbo_qh->addItem("切换6");
- ui->cbo_qh->setCurrentIndex(0);
-
- ui->cbo_lg->addItem("先开后关");
- ui->cbo_lg->addItem("预定顺序");
- ui->cbo_lg->setCurrentIndex(0);
-
- ui->cbo_target->addItem("迅速");
- ui->cbo_target->addItem("平稳");
- ui->cbo_target->addItem("缓慢");
- ui->cbo_target->setCurrentIndex(0);
-
- ui->line_gsf->setText("100");
- ui->line_gsp->setText("100");
- ui->line_nsp->setText("100");
- ui->line_nshf->setText("100");
- ui->cbo_pid->addItem("专家系统");
- ui->cbo_pid->addItem("PI");
- ui->cbo_pid->addItem("PID");
- ui->cbo_pid->setCurrentIndex(0);
- ui->cbo_pid_wt->addItem("专家系统");
- ui->cbo_pid_wt->addItem("PI");
- ui->cbo_pid_wt->addItem("PID");
- ui->cbo_pid_wt->setCurrentIndex(0);
-
- }
- void model_select::cbo_select_changed(int index)
- {
- QString modelAddress = "wt";
- QString pro_dress = "model_address:";
- if (index == 1)
- {
-
- modelAddress="wt";
-
- }
- else if (index == 2)
- {
-
- modelAddress = "qh";
- }
- file_write(&pro_dress, &modelAddress);
-
-
- file_write(&pro_dress, &modelAddress);
- }
- void model_select::cbo_wt_changed(int index)
- {
- QString wt_str = "condition_wt:";
- QString wt_num = "27";
- if (index == 1)
- {
- wt_num = "27";
- }
- else if (index == 2)
- {
- wt_num = "18";
- }
- else if (index == 3)
- {
- wt_num = "12";
- }
- else if (index == 4)
- {
- wt_num = "2.5";
- }
- else if (index == 5)
- {
- wt_num = "9";
- }
- else if (index == 6)
- {
- wt_num = "6";
- }
- else if (index == 7)
- {
- wt_num = "61";
- }
- file_write(&wt_str, &wt_num);
- }
- void model_select::cbo_condition_changed(int index)
- {
-
- switch (index)
- {
- case 0:
- ui->condition->setCurrentIndex(0);
- break;
- case 1:
- ui->condition->setCurrentIndex(1);
- break;
- case 2:
- ui->condition->setCurrentIndex(2);
- break;
- default:
- break;
- }
- }
- void model_select::file_read()
- {
- QFile file("D:/desktop/NGS/model_git/model_zl/tcp/model_address.txt");
-
- if (file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
- QTextStream in(&file);
- QString line = in.readLine();
- while (!line.isNull())
- {
-
- qDebug() << "Line: " << line;
- line = in.readLine();
- }
- file.close();
- }
- else
- {
-
- qDebug() << "Unable to open file for reading.";
- }
- }
- void model_select::file_write(QString *prs_name, QString *msg)
- {
- QString modelAddress = *prs_name + *msg;
- QFile file("D:/desktop/NGS/model_git/model_zl/tcp/model_address.txt");
-
- if (file.open(QIODevice::Append | QIODevice::Text))
- {
- QTextStream out(&file);
- out << modelAddress << '\n';
- file.close();
- }
- else
- {
-
- qDebug() << "Unable to open file for writing.";
- }
- }
- void model_select::on_btn_pause_clicked()
- {
- QString serverMsg = "pasue";
- for (auto clientSocket : connectedClients.keys()) {
- if (clientSocket->state() == QAbstractSocket::ConnectedState) {
- clientSocket->write(serverMsg.toUtf8());
- }
- }
- }
- void model_select::receive_Client(QTcpSocket *clientSocket)
- {
-
- connect(clientSocket, &QTcpSocket::readyRead, this, [=]()
- {
-
- QByteArray data = clientSocket->readAll();
- if (data.isEmpty())
- return;
- char dataType = data.at(0);
- data.remove(0, 1);
- if (dataType == 't') {
-
- QString message = QString::fromUtf8(data);
- qDebug() << "收到文本: " << message;
- }
-
- emit receive_msg(data);
- data.clear();
- } );
-
-
- }
|