Refactor DeviceManager

This commit is contained in:
Krad
2022-05-17 16:15:20 +08:00
parent 60fa56cc18
commit 0bfa7b2377
2 changed files with 170 additions and 109 deletions

View File

@@ -59,7 +59,7 @@ void DeviceManager::initDevice() {
// empty scan
connect(EventCenter::Default(), &EventCenter::RequestEmptyScan, [=](QObject* sender, QObject* detail) {
std::string json = getJsonFromPatInf(detail);
processScan(json.c_str(), true);
startScan(json.c_str(), true);
});
// Patient scan
connect(EventCenter::Default(), &EventCenter::RequestPatientScan, [=](QObject* sender, QObject* detail) {
@@ -67,7 +67,7 @@ void DeviceManager::initDevice() {
qDebug() << json.c_str();
if (!json.empty())
{
processScan(json.c_str());
startScan(json.c_str());
}
});
// Continue Scan
@@ -117,7 +117,6 @@ void DeviceManager::initDevice() {
TRIGGER_EVENT(GUIEvents::ResponseStop, nullptr, nullptr);
AppGlobalValues::setInProcessing(false);
});
// preview
connect(EventCenter::Default(), &EventCenter::RequestPreviewScan, [=]() {
// check device status=========================================
@@ -148,7 +147,7 @@ void DeviceManager::initDevice() {
THROW_ERROR(msg);
qDebug() << "Error thrown!";
});
// init the preview data caller thread
previewDataCaller = QThread::create([=]() {
while (!endLoop)
{
@@ -184,120 +183,131 @@ void DeviceManager::initDevice() {
}
const char * getPhaseName(int phase){
const char* names[3] = {"Initializing","Scanning", "CE Measuring"};
return names[phase-1];
}
void DeviceManager::timerEvent(QTimerEvent* event) {
if (event->timerId() == deviceInfTimerID) {
QString temp = QString(GetDeviceInfo(MEAN_TEMPERATURE));
TRIGGER_EVENT(GUIEvents::ResponseDeviceTemperature, nullptr, (QObject*)&temp);
return;
}
//scanning progress timer
{
//error exit, callback error
if (errorOccurred)
{
goto exitTimer;
}
// previewing exit
if (previewing)
{
QString msg("Device is previewing, exit current operation!");
THROW_ERROR(msg);
goto exitTimer;
}
else {
// check device status=========================================
qDebug() << "GetStatus";
StatusInfo inf = GetStatus();
qDebug() << "Scanning request status, status:" << getStatusString(inf.status);
//设备正常扫描中
if (inf.status == SCANNING) {
qDebug() << "current output path:" << getScanOutputPath();
lastStatus = SCANNING;
//normal scan pending
int phase = inf.progress/100 + 1;
int progress = inf.progress % 100;
QString extraMsg = (AppGlobalValues::EmptyScanFlag().toBool()||(scanPhase != 3))?"":", patient can leave";
QVariant var(QString("%1%3\r\n progress:%2%").arg(getPhaseName(scanPhase)).arg(progress).arg(extraMsg));
TRIGGER_EVENT(GUIEvents::InvokeOperationProgress, nullptr, (QObject *) &var);
if (scanPhase != phase) {
if (phase > 3 || scanPhase > phase ){
QString errorMsg = QString("Error Scan Phase code, current Phase code:%1, new Phase code:%2!").arg(scanPhase,phase);
THROW_ERROR(errorMsg)
goto exitTimer;
}
scanPhase = phase;
if (scanPhase == 2){
if (!AppGlobalValues::EmptyScanFlag().toBool() && JsonObject::Instance()->getScanConfirm()) {
var.setValue(QString("Waiting for patient to start scan!\r\n Click \"Next\" to continue!"));
TRIGGER_EVENT(GUIEvents::InvokeOperationPending, nullptr, (QObject *) &var);
goto exitTimer;
}
//empty scan no pending, auto continue
else {
postContinueCommand();
}
}
}
return;
if (event->timerId() == deviceInfTimerID) {
QString temp = QString(GetDeviceInfo(MEAN_TEMPERATURE));
TRIGGER_EVENT(GUIEvents::ResponseDeviceTemperature, nullptr, (QObject *) &temp);
return;
}
//scanning progress timer
//error exit, callback error
if (errorOccurred) {
exitScanTimer();
return;
}
// previewing exit
if (previewing) {
QString msg("Device is previewing, exit current operation!");
THROW_ERROR(msg);
} else {
// check device status=========================================
qDebug() << "GetStatus";
StatusInfo inf = GetStatus();
qDebug() << "Scanning request status, status:" << getStatusString(inf.status);
//设备正常扫描中
if (inf.status == SCANNING) {
scanProcess(inf.progress);
return;
} else {
//未发生错误并且,之前状态是扫描,代表正常扫描完成
if (lastStatus == SCANNING) {
prepareFinishScan();
}
else {
//未发生错误并且,之前状态是扫描,代表正常扫描完成
if (lastStatus == SCANNING) {
qDebug() << "Scan finished";
QVariant var(true);
qDebug() << "InvokeOperationEnd";
TRIGGER_EVENT(GUIEvents::InvokeOperationEnd, nullptr, (QObject*)&var);
AppGlobalValues::setInProcessing(false);
lastStatus = -1;
previewing = false;
QString s("%1 %2");
s = s.arg(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss")).arg("Scan finished");
TRIGGER_EVENT(GUIEvents::GlobalBannerMessage, nullptr, (QObject*)&s);
QString outputPath = GetDeviceInfo(DeviceInfo::DEV_OUTPATH);
outputPath = outputPath.replace("\\","/");
if (outputPath.endsWith('/')) outputPath = outputPath.remove(outputPath.length()-1,1);
QStringList list = outputPath.split('/');
if (list.length()){
if (AppGlobalValues::EmptyScanFlag().toBool()){
ScanJson::Current()->setEmptyScanID(list.last().toStdString().c_str());
}else{
ScanJson::Current()->setScanID(list.last().toStdString().c_str());
}
ScanJson::Current()->save();
}
else{
QString msg("Scan Output Path error!");
THROW_ERROR(msg);
}
}
//一般不会出现其他情况
else {
QString msg("Unknown error in scanning progress timer");
THROW_ERROR(msg);
}
}
}
exitTimer:
qDebug() << "Scanning progress Timer exit";
killTimer(timerID);
timerID = -1;
lastStatus = -1;
previewing = false;
return;
}
//一般不会出现其他情况
else {
QString msg("Unknown error in scanning progress timer");
THROW_ERROR(msg);
}
}
}
exitScanTimer();
}
void DeviceManager::scanProcess(int sProgress) {
qDebug() << "current output path:" << getScanOutputPath();
lastStatus = SCANNING;
//normal scan pending
int phase = sProgress / 100 + 1;
int progress = sProgress % 100;
// scan with phase 3 has a different message
QString extraMsg = (AppGlobalValues::EmptyScanFlag().toBool() ||
(scanPhase != 3)) ? "": ", patient can leave";
QVariant var(QString("%1%3\r\n progress:%2%").arg(getPhaseName(scanPhase)).arg(progress).arg(extraMsg));
TRIGGER_EVENT(InvokeOperationProgress, nullptr, (QObject *) &var);
//phase control
//no change return
if (scanPhase == phase) return;
// error phase
if (phase > 3 || scanPhase > phase) {
QString errorMsg = QString("Error Scan Phase code, current Phase code:%1, new Phase code:%2!").arg(
scanPhase, phase);
THROW_ERROR(errorMsg)
exitScanTimer();
return;
}
// enter phase 2
if ((scanPhase = phase) == 2) {
if (!AppGlobalValues::EmptyScanFlag().toBool() && JsonObject::Instance()->getScanConfirm()) {
var.setValue(QString("Waiting for patient to start scan!\r\n Click \"Next\" to continue!"));
TRIGGER_EVENT(InvokeOperationPending, nullptr, (QObject *) &var);
exitScanTimer();
}
//empty scan no pending, auto continue
else {
postContinueCommand();
}
}
}
void DeviceManager::processScan(const char* json, bool empty) {
void DeviceManager::exitScanTimer() {
qDebug() << "Scanning progress Timer exit";
killTimer(timerID);
timerID = -1;
lastStatus = -1;
previewing = false;
}
void DeviceManager::prepareFinishScan() {
qDebug() << "Scan finished";
QVariant var(true);
qDebug() << "InvokeOperationEnd";
TRIGGER_EVENT(InvokeOperationEnd, nullptr, (QObject *) &var);
AppGlobalValues::setInProcessing(false);
// log, no need
// QString s("%1 %2");
// s = s.arg(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss")).arg("Scan finished");
// TRIGGER_EVENT(GlobalBannerMessage, nullptr, (QObject *) &s);
// get output data path
QString outputPath = GetDeviceInfo(DEV_OUTPATH);
outputPath = outputPath.replace("\\", "/");
// get scan ID from path
if (outputPath.endsWith('/')) outputPath = outputPath.remove(outputPath.length() - 1, 1);
QStringList list = outputPath.split('/');
if (list.length()) {
if (AppGlobalValues::EmptyScanFlag().toBool()) {
ScanJson::Current()->setEmptyScanID(list.last().toStdString().c_str());
} else {
ScanJson::Current()->setScanID(list.last().toStdString().c_str());
}
// save json
ScanJson::Current()->save();
} else {
QString msg("Scan Output Path error!");
THROW_ERROR(msg);
}
}
void DeviceManager::startScan(const char* json, bool empty) {
//clear last error state first
errorOccurred = false;
// check device status=========================================

View File

@@ -15,13 +15,33 @@ public:
static DeviceManager manager;
return &manager;
}
/**
* init device, include Shimlib and it's error call back,
* deviceInfTimer to get temperature of water, and the
* preview data caller thread.
*/
void initDevice();
/**
* close and release the device reference resource.
*/
void close();
/**
* Get Firm ware version
* @return Firm ware version
*/
QString getSoftwareVersion();
/**
* Get Scan data output path
* @return Scan data output path
*/
QString getScanOutputPath();
void setErrorOccurred(bool v){
errorOccurred = v;
}
QString getSoftwareVersion();
QString getScanOutputPath();
bool getErrorOccurred(){
return errorOccurred;
}
@@ -33,9 +53,40 @@ protected:
void timerEvent(QTimerEvent* event) override;
private:
void processScan(const char* json, bool empty = false);
/**
* To start a new scan operation
* @param json The patient information json string
* @param empty Empty scan flag
*/
void startScan(const char* json, bool empty = false);
/**
* Post Scan start command to Shimlib
*/
void postScanCommand();
/**
* Post Continue Scan command to Shimlib
* @param useTimer start a new timer flag
*/
void postContinueCommand(bool useTimer = false);
/**
* Prepare for finishing the Scan
*/
void prepareFinishScan();
/**
* exit the current Scan process timer
*/
void exitScanTimer();
/**
* Process scan progress change
* @param Scan progress
*/
void scanProcess(int progress);
int scanPhase = 1;
int timerID = -1;
int deviceInfTimerID = -1;