Files
HPPA/HPPA/GonggaShanRecordCtl.cpp
tangchao0503 6b63d28d2c add,山地所贡嘎山7:
优化
2026-07-22 10:31:12 +08:00

147 lines
3.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "GonggaShanRecordCtl.h"
GonggashanTaskScheduler::GonggashanTaskScheduler(QObject* parent)
: QObject(parent)
, m_taskState(TaskState::Idle)
{
}
GonggashanTaskScheduler::~GonggashanTaskScheduler()
{
}
bool GonggashanTaskScheduler::isTaskRunning() const
{
return m_taskState == TaskState::Running;
}
void GonggashanTaskScheduler::setTaskRunning(bool running)
{
m_taskState = running ? TaskState::Running : TaskState::Idle;
Q_EMIT taskStateChanged(m_taskState);
}
void GonggaShanRecordCtl::logStatus(const QString& message, bool isHearderBlankLine, bool isTailBlankLine)
{
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
if (isHearderBlankLine)
{
ui.status_textEdit->append("\n");
}
ui.status_textEdit->append(QString("[%1] %2").arg(timestamp, message));
if (isTailBlankLine)
{
ui.status_textEdit->append("\n");
}
if (m_logStream.device())
{
if (isHearderBlankLine)
{
m_logStream << "\n";
}
m_logStream << "[" << timestamp << "] " << message << "\n";
if (isTailBlankLine)
{
m_logStream << "\n";
}
m_logStream.flush();
m_logFile.flush();
}
}
GonggaShanRecordCtl::GonggaShanRecordCtl(QWidget* parent)
: QDialog(parent)
, m_taskScheduler(new GonggashanTaskScheduler(this))
{
ui.setupUi(this);
AppSettings& settings = AppSettings::instance();
ui.spinbox_Port->setValue(settings.gonggaShanRecordPort());
connect(ui.btnListen, &QPushButton::clicked, this, &GonggaShanRecordCtl::startListen);
connect(ui.btnDisListen, &QPushButton::clicked, this, &GonggaShanRecordCtl::stopListen);
connect(ui.spinbox_Port, QOverload<int>::of(&QSpinBox::valueChanged), [this](int value) {
AppSettings::instance().setGonggaShanRecordPort(value);
});
}
GonggaShanRecordCtl::~GonggaShanRecordCtl()
{
if (m_logFile.isOpen()) {
m_logStream.flush();
m_logFile.close();
}
tcpServer6005->deleteLater();
}
void GonggaShanRecordCtl::startListen()
{
if (m_logFile.isOpen()) {
m_logStream.flush();
m_logFile.close();
}
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm-ss");
QString logFileName = QString("%1_log.txt").arg(timestamp);
m_logFilePath = AppSettings::instance().dataFolder() + QDir::separator() + logFileName;
m_logFile.setFileName(m_logFilePath);
m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
m_logStream.setDevice(&m_logFile);
logStatus("Start listen....", true, true);
MotorParams::TCPConnectionParams tcpConnectionParams6005;
tcpConnectionParams6005.port = ui.spinbox_Port->text().toInt();
tcpConnectionParams6005.serverIP = "192.168.1.2";//此ip地址没有使用,可以随便填入
tcpServer6005 = new MotorParams::CommunicationViaTCP(tcpConnectionParams6005, this);
connect(tcpServer6005, &MotorParams::CommunicationViaTCP::positionReceived, this, &GonggaShanRecordCtl::startRecord);
}
void GonggaShanRecordCtl::stopListen()
{
logStatus("Stop listen", true, true);
disconnect(tcpServer6005, &MotorParams::CommunicationViaTCP::positionReceived, this, &GonggaShanRecordCtl::startRecord);
tcpServer6005->deleteLater();
}
void GonggaShanRecordCtl::startRecord(int position)
{
if (m_taskScheduler->isTaskRunning())
{
logStatus("Data collection has begun....");
return;
}
QString pos = QString::number(position);
logStatus("Reach pos: " + pos, true);
m_taskScheduler->setTaskRunning(true);
emit startRcordSignal("pos_" + pos);
}
void GonggaShanRecordCtl::onFiberImagerStartExposureSignal()
{
logStatus("FiberImager: start exposure...");
}
void GonggaShanRecordCtl::onFiberImagerExposureCompleteSignal(int exposureTime)
{
logStatus(QString("FiberImager: exposure finished, %1").arg(exposureTime));
logStatus("Start recording...");
}
void GonggaShanRecordCtl::onRcordFinished()
{
m_taskScheduler->setTaskRunning(false);
logStatus("Record Finished.");
}