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