Refactor DeviceManager
This commit is contained in:
@@ -59,7 +59,7 @@ void DeviceManager::initDevice() {
|
|||||||
// empty scan
|
// empty scan
|
||||||
connect(EventCenter::Default(), &EventCenter::RequestEmptyScan, [=](QObject* sender, QObject* detail) {
|
connect(EventCenter::Default(), &EventCenter::RequestEmptyScan, [=](QObject* sender, QObject* detail) {
|
||||||
std::string json = getJsonFromPatInf(detail);
|
std::string json = getJsonFromPatInf(detail);
|
||||||
processScan(json.c_str(), true);
|
startScan(json.c_str(), true);
|
||||||
});
|
});
|
||||||
// Patient scan
|
// Patient scan
|
||||||
connect(EventCenter::Default(), &EventCenter::RequestPatientScan, [=](QObject* sender, QObject* detail) {
|
connect(EventCenter::Default(), &EventCenter::RequestPatientScan, [=](QObject* sender, QObject* detail) {
|
||||||
@@ -67,7 +67,7 @@ void DeviceManager::initDevice() {
|
|||||||
qDebug() << json.c_str();
|
qDebug() << json.c_str();
|
||||||
if (!json.empty())
|
if (!json.empty())
|
||||||
{
|
{
|
||||||
processScan(json.c_str());
|
startScan(json.c_str());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Continue Scan
|
// Continue Scan
|
||||||
@@ -117,7 +117,6 @@ void DeviceManager::initDevice() {
|
|||||||
TRIGGER_EVENT(GUIEvents::ResponseStop, nullptr, nullptr);
|
TRIGGER_EVENT(GUIEvents::ResponseStop, nullptr, nullptr);
|
||||||
AppGlobalValues::setInProcessing(false);
|
AppGlobalValues::setInProcessing(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
// preview
|
// preview
|
||||||
connect(EventCenter::Default(), &EventCenter::RequestPreviewScan, [=]() {
|
connect(EventCenter::Default(), &EventCenter::RequestPreviewScan, [=]() {
|
||||||
// check device status=========================================
|
// check device status=========================================
|
||||||
@@ -148,7 +147,7 @@ void DeviceManager::initDevice() {
|
|||||||
THROW_ERROR(msg);
|
THROW_ERROR(msg);
|
||||||
qDebug() << "Error thrown!";
|
qDebug() << "Error thrown!";
|
||||||
});
|
});
|
||||||
|
// init the preview data caller thread
|
||||||
previewDataCaller = QThread::create([=]() {
|
previewDataCaller = QThread::create([=]() {
|
||||||
while (!endLoop)
|
while (!endLoop)
|
||||||
{
|
{
|
||||||
@@ -184,120 +183,131 @@ void DeviceManager::initDevice() {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * getPhaseName(int phase){
|
const char * getPhaseName(int phase){
|
||||||
const char* names[3] = {"Initializing","Scanning", "CE Measuring"};
|
const char* names[3] = {"Initializing","Scanning", "CE Measuring"};
|
||||||
return names[phase-1];
|
return names[phase-1];
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeviceManager::timerEvent(QTimerEvent* event) {
|
void DeviceManager::timerEvent(QTimerEvent* event) {
|
||||||
if (event->timerId() == deviceInfTimerID) {
|
if (event->timerId() == deviceInfTimerID) {
|
||||||
QString temp = QString(GetDeviceInfo(MEAN_TEMPERATURE));
|
QString temp = QString(GetDeviceInfo(MEAN_TEMPERATURE));
|
||||||
TRIGGER_EVENT(GUIEvents::ResponseDeviceTemperature, nullptr, (QObject*)&temp);
|
TRIGGER_EVENT(GUIEvents::ResponseDeviceTemperature, nullptr, (QObject *) &temp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//scanning progress timer
|
//scanning progress timer
|
||||||
{
|
//error exit, callback error
|
||||||
//error exit, callback error
|
if (errorOccurred) {
|
||||||
if (errorOccurred)
|
exitScanTimer();
|
||||||
{
|
return;
|
||||||
goto exitTimer;
|
}
|
||||||
}
|
// previewing exit
|
||||||
// previewing exit
|
if (previewing) {
|
||||||
if (previewing)
|
QString msg("Device is previewing, exit current operation!");
|
||||||
{
|
THROW_ERROR(msg);
|
||||||
QString msg("Device is previewing, exit current operation!");
|
} else {
|
||||||
THROW_ERROR(msg);
|
// check device status=========================================
|
||||||
goto exitTimer;
|
qDebug() << "GetStatus";
|
||||||
}
|
StatusInfo inf = GetStatus();
|
||||||
else {
|
qDebug() << "Scanning request status, status:" << getStatusString(inf.status);
|
||||||
// check device status=========================================
|
//设备正常扫描中
|
||||||
qDebug() << "GetStatus";
|
if (inf.status == SCANNING) {
|
||||||
StatusInfo inf = GetStatus();
|
scanProcess(inf.progress);
|
||||||
qDebug() << "Scanning request status, status:" << getStatusString(inf.status);
|
return;
|
||||||
//设备正常扫描中
|
} else {
|
||||||
if (inf.status == SCANNING) {
|
//未发生错误并且,之前状态是扫描,代表正常扫描完成
|
||||||
qDebug() << "current output path:" << getScanOutputPath();
|
if (lastStatus == SCANNING) {
|
||||||
lastStatus = SCANNING;
|
prepareFinishScan();
|
||||||
|
|
||||||
//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;
|
|
||||||
}
|
}
|
||||||
else {
|
//一般不会出现其他情况
|
||||||
//未发生错误并且,之前状态是扫描,代表正常扫描完成
|
else {
|
||||||
if (lastStatus == SCANNING) {
|
QString msg("Unknown error in scanning progress timer");
|
||||||
qDebug() << "Scan finished";
|
THROW_ERROR(msg);
|
||||||
QVariant var(true);
|
}
|
||||||
qDebug() << "InvokeOperationEnd";
|
}
|
||||||
|
}
|
||||||
TRIGGER_EVENT(GUIEvents::InvokeOperationEnd, nullptr, (QObject*)&var);
|
exitScanTimer();
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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
|
//clear last error state first
|
||||||
errorOccurred = false;
|
errorOccurred = false;
|
||||||
// check device status=========================================
|
// check device status=========================================
|
||||||
|
|||||||
@@ -15,13 +15,33 @@ public:
|
|||||||
static DeviceManager manager;
|
static DeviceManager manager;
|
||||||
return &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();
|
void initDevice();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* close and release the device reference resource.
|
||||||
|
*/
|
||||||
void close();
|
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){
|
void setErrorOccurred(bool v){
|
||||||
errorOccurred = v;
|
errorOccurred = v;
|
||||||
}
|
}
|
||||||
QString getSoftwareVersion();
|
|
||||||
QString getScanOutputPath();
|
|
||||||
bool getErrorOccurred(){
|
bool getErrorOccurred(){
|
||||||
return errorOccurred;
|
return errorOccurred;
|
||||||
}
|
}
|
||||||
@@ -33,9 +53,40 @@ protected:
|
|||||||
void timerEvent(QTimerEvent* event) override;
|
void timerEvent(QTimerEvent* event) override;
|
||||||
|
|
||||||
private:
|
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();
|
void postScanCommand();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post Continue Scan command to Shimlib
|
||||||
|
* @param useTimer start a new timer flag
|
||||||
|
*/
|
||||||
void postContinueCommand(bool useTimer = false);
|
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 scanPhase = 1;
|
||||||
int timerID = -1;
|
int timerID = -1;
|
||||||
int deviceInfTimerID = -1;
|
int deviceInfTimerID = -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user