177 lines
5.6 KiB
C++
177 lines
5.6 KiB
C++
//
|
||
// Created by Krad on 2021/10/12.
|
||
//
|
||
#include "ShimLib/ShimLib.h"
|
||
#include "DeviceManager.h"
|
||
#include "../event/EventCenter.h"
|
||
#include <QVariant>
|
||
#include <QTimerEvent>
|
||
|
||
#define TRIGGER_EVENT EventCenter::Default()->triggerEvent
|
||
|
||
#define THROW_ERROR(errormsg)\
|
||
TRIGGER_EVENT(GUIEvents::GUIErrorRaise, nullptr, (QObject*)&errormsg);
|
||
|
||
|
||
const char * getStatusString(int status)
|
||
{
|
||
switch (status) {
|
||
case SCANNING:
|
||
return "SCANNING";
|
||
case READY:
|
||
return "Ready";
|
||
case BUSY:
|
||
return "BUSY";
|
||
case ERROR:
|
||
return "ERROR";
|
||
}
|
||
return "";
|
||
}
|
||
|
||
|
||
std::string getJsonFromPatInf(QObject* obj)
|
||
{
|
||
return ((QString*)obj)->toStdString();
|
||
}
|
||
|
||
void ErrorCallback(const char * msg)
|
||
{
|
||
printf("Error Callback , message:%s\r\n", msg);
|
||
QString m(msg);
|
||
THROW_ERROR(m);
|
||
}
|
||
|
||
|
||
|
||
void DeviceManager::initDevice() {
|
||
InitLib(ErrorCallback);
|
||
|
||
deviceInfTimerID = startTimer(1000);
|
||
// empty scan
|
||
connect(EventCenter::Default(),&EventCenter::RequestEmptyScan,[=](QObject* sender, QObject* detail){
|
||
std::string json= getJsonFromPatInf(detail);
|
||
printf("%s\r\n",json.c_str());
|
||
processScan(json.c_str(), true);
|
||
});
|
||
// Patient scan
|
||
connect(EventCenter::Default(),&EventCenter::RequestPatientScan,[=](QObject* sender, QObject* detail){
|
||
std::string json= getJsonFromPatInf(detail);
|
||
printf("%s\r\n",json.c_str());
|
||
if (!json.empty())
|
||
{
|
||
processScan(json.c_str());
|
||
}
|
||
});
|
||
// stop
|
||
connect(EventCenter::Default(),&EventCenter::RequestStop,[=]() {
|
||
TRIGGER_EVENT(GUIEvents::InvokeOperationStart, nullptr, nullptr);
|
||
StatusInfo inf = GetStatus();
|
||
printf("Stop request status, status:%s\r\n",getStatusString(inf.status));
|
||
if (inf.status == SCANNING) {
|
||
//ScanControl fail
|
||
if (ScanControl(STOP)) {
|
||
QString msg("Stop operation fail!");
|
||
THROW_ERROR(msg);
|
||
lastStatus = -1;
|
||
previewing = false;
|
||
return;
|
||
}
|
||
lastStatus = -1;
|
||
previewing = false;
|
||
}
|
||
TRIGGER_EVENT(GUIEvents::InvokeOperationEnd, nullptr, nullptr);
|
||
TRIGGER_EVENT(GUIEvents::ResponseStop, nullptr, nullptr);
|
||
});
|
||
// preview
|
||
connect(EventCenter::Default(),&EventCenter::RequestPreviewScan,[=](){
|
||
TRIGGER_EVENT(GUIEvents::InvokeOperationStart, nullptr, nullptr);
|
||
StatusInfo inf = GetStatus();
|
||
printf("PreviewScan request status, status:%s\r\n",getStatusString(inf.status));
|
||
if (inf.status==READY)
|
||
{
|
||
//ScanControl
|
||
if(!ScanControl(PREVIEW_SCAN))
|
||
{
|
||
lastStatus = SCANNING;
|
||
previewing = true;
|
||
timerID = startTimer(500);
|
||
TRIGGER_EVENT(GUIEvents::ResponsePreview, nullptr, nullptr);
|
||
TRIGGER_EVENT(GUIEvents::InvokeOperationEnd, nullptr, nullptr);
|
||
return;
|
||
}
|
||
}
|
||
QString msg(inf.status!=READY?"Can't start preview,Device is not ready!":"Start preview operation fail!");
|
||
THROW_ERROR(msg);
|
||
});
|
||
}
|
||
|
||
void DeviceManager::timerEvent(QTimerEvent *event) {
|
||
if (event->timerId() !=deviceInfTimerID)
|
||
{
|
||
StatusInfo inf = GetStatus();
|
||
printf("Timer request status, status:%s\r\n",getStatusString(inf.status));
|
||
if (inf.status==SCANNING)
|
||
{
|
||
lastStatus = SCANNING;
|
||
//normal scan
|
||
if(!previewing)
|
||
{
|
||
QVariant var(inf.progress);
|
||
TRIGGER_EVENT(GUIEvents::InvokeOperationProgress, nullptr, (QObject*)&var);
|
||
}
|
||
//preview scan
|
||
else{
|
||
const char * data = GetPreviewData();
|
||
if (!data) return;
|
||
QByteArray bytes =QByteArray::fromRawData(data,140*140);
|
||
TRIGGER_EVENT(GUIEvents::ResponsePreviewData, nullptr, (QObject*)(&bytes));
|
||
delete [] data;
|
||
}
|
||
|
||
} else{
|
||
if (lastStatus == SCANNING && !errorOccured)
|
||
{
|
||
QVariant var(true);
|
||
TRIGGER_EVENT(GUIEvents::InvokeOperationEnd, nullptr, (QObject*)&var);
|
||
lastStatus = -1;
|
||
previewing = false;
|
||
}
|
||
//内部错误,导致Scan停止
|
||
else if (lastStatus == SCANNING && errorOccured)
|
||
{
|
||
//正常情况下,设备应该已经调用的错误callback,然后会回归Ready状态,清理lastStatus即可
|
||
lastStatus = -1;
|
||
previewing = false;
|
||
}
|
||
killTimer(timerID);
|
||
}
|
||
} else{
|
||
QString temp = QString(GetDeviceInfo(MEAN_TEMPERATURE));
|
||
TRIGGER_EVENT(GUIEvents::ResponseDeviceTemperature, nullptr, (QObject*)&temp);
|
||
}
|
||
}
|
||
|
||
void DeviceManager::processScan(const char *json, bool empty) {
|
||
static QString msg = "Start scan...";
|
||
TRIGGER_EVENT(GUIEvents::InvokeOperationStart, nullptr, (QObject*)&msg);
|
||
int ret = SetScanInfo(json,empty?1:0);
|
||
if (ret){
|
||
QString errmsg("Transfer patient information fail!");
|
||
THROW_ERROR(errmsg);
|
||
return;
|
||
}
|
||
StatusInfo inf = GetStatus();
|
||
printf("ProcessScan request status, status:%s\r\n",getStatusString(inf.status));
|
||
if (inf.status==READY)
|
||
{
|
||
//ScanControl fail
|
||
if(!ScanControl(SCAN)) {
|
||
lastStatus = SCANNING;
|
||
timerID = startTimer(300);
|
||
return;
|
||
}
|
||
}
|
||
QString errmsg("Start scan operation fail!");
|
||
THROW_ERROR(errmsg);
|
||
}
|