Files
GUI/src/device/DeviceManager.cpp

281 lines
9.8 KiB
C++
Raw Normal View History

//
// Created by Krad on 2021/10/12.
//
#include "ShimLib/ShimLib.h"
#include "DeviceManager.h"
#include "../event/EventCenter.h"
#include <QVariant>
#include <QTimerEvent>
2021-10-26 13:08:11 +08:00
#include <QDate>
#include <QDateTime>
2021-10-26 09:51:23 +08:00
#include <qdebug.h>
#define TRIGGER_EVENT EventCenter::Default()->triggerEvent
#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 "";
}
std::string getJsonFromPatInf(QObject* obj)
{
return ((QString*)obj)->toStdString();
}
2021-10-19 14:09:14 +08:00
void ErrorCallback(const char * msg)
{
2021-10-26 13:08:11 +08:00
DeviceManager::Default()->setErrorOccurred(true);
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);
}
void DeviceManager::initDevice() {
2021-10-19 14:09:14 +08:00
InitLib(ErrorCallback);
deviceInfTimerID = startTimer(1000);
2021-10-26 13:08:11 +08:00
// empty scan
connect(EventCenter::Default(),&EventCenter::RequestEmptyScan,[=](QObject* sender, QObject* detail){
std::string json= getJsonFromPatInf(detail);
2021-10-27 18:04:05 +08:00
// qDebug()<<json.c_str();
// void * jss = malloc(json.size());
// memcpy(jss,json.c_str(),json.size());
// processScan((char*)jss,true);
processScan(json.c_str(),true);
});
// Patient scan
connect(EventCenter::Default(),&EventCenter::RequestPatientScan,[=](QObject* sender, QObject* detail){
std::string json= getJsonFromPatInf(detail);
2021-10-26 09:51:23 +08:00
qDebug()<<json.c_str();
if (!json.empty())
{
2021-10-27 18:04:05 +08:00
// void * jss = malloc(json.size());
// memcpy(jss,json.c_str(),json.size());
// processScan((char*)jss);
processScan(json.c_str());
}
});
// stop
connect(EventCenter::Default(),&EventCenter::RequestStop,[=]() {
2021-11-02 10:32:30 +08:00
qDebug() << "GetStatus";
StatusInfo inf = GetStatus();
2021-11-02 10:32:30 +08:00
qDebug() << "Stop request status, status:%s" << getStatusString(inf.status);
2021-10-27 18:04:05 +08:00
// check device status=========================================
//device is ready return
2021-11-02 10:32:30 +08:00
if (inf.status != SCANNING) {
TRIGGER_EVENT(GUIEvents::ResponseStop, nullptr, nullptr);
2021-10-27 18:04:05 +08:00
return;
2021-11-02 10:32:30 +08:00
}
// if (inf.status == BUSY) {
// QString msg("Device is busy, Stop operation fail!");
// THROW_ERROR(msg);
// return;
// };
TRIGGER_EVENT(GUIEvents::InvokeOperationStart, nullptr, nullptr);
//ScanControl fail
qDebug() << "Request stop!";
if (timerID != -1)killTimer(timerID);
if (ScanControl(STOP)) {
qDebug() << "Stop fail!";
QString msg("Stop operation fail!");
THROW_ERROR(msg);
qDebug() << "Error thrown!";
lastStatus = -1;
previewing = false;
2021-10-26 13:08:11 +08:00
QString s("%1 %2");
2021-11-02 10:32:30 +08:00
s = s.arg(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss"), msg);
TRIGGER_EVENT(GUIEvents::GlobalBannerMessage, nullptr, (QObject *) &msg);
return;
}
2021-11-02 10:32:30 +08:00
lastStatus = -1;
previewing = false;
QString s("%1 %2");
s = s.arg(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss"), ("Scan Stopped!"));
TRIGGER_EVENT(GUIEvents::GlobalBannerMessage, nullptr, (QObject *) &s);
TRIGGER_EVENT(GUIEvents::InvokeOperationEnd, nullptr, nullptr);
TRIGGER_EVENT(GUIEvents::ResponseStop, nullptr, nullptr);
});
2021-10-27 18:04:05 +08:00
// preview
connect(EventCenter::Default(),&EventCenter::RequestPreviewScan,[=](){
2021-10-27 18:04:05 +08:00
// check device status=========================================
2021-10-26 09:51:23 +08:00
qDebug()<<"GetStatus";
StatusInfo inf = GetStatus();
2021-10-26 09:51:23 +08:00
qDebug() << "PreviewScan request status, status:" << getStatusString(inf.status);
if (inf.status==READY)
{
2021-10-27 18:04:05 +08:00
TRIGGER_EVENT(GUIEvents::InvokeOperationStart, nullptr, nullptr);
//ScanControl
2021-10-26 09:51:23 +08:00
qDebug()<<"Request preview!";
if(!ScanControl(PREVIEW_SCAN))
{
2021-10-26 09:51:23 +08:00
qDebug()<<"Preview success!";
2021-10-20 15:55:15 +08:00
lastStatus = SCANNING;
previewing = true;
2021-10-22 14:48:55 +08:00
// timerID = startTimer(500);
TRIGGER_EVENT(GUIEvents::ResponsePreview, nullptr, nullptr);
TRIGGER_EVENT(GUIEvents::InvokeOperationEnd, nullptr, nullptr);
2021-10-26 13:08:11 +08:00
QString s("Device Previewing!");
TRIGGER_EVENT(GUIEvents::GlobalBannerMessage, nullptr, (QObject*)&s);
return;
}
}
2021-10-26 09:51:23 +08:00
qDebug()<<"Preview fail!";
QString msg(inf.status!=READY?"Can't start preview,Device is not ready!":"Start preview operation fail!");
THROW_ERROR(msg);
2021-10-26 09:51:23 +08:00
qDebug()<<"Error thrown!";
});
2021-10-22 14:48:55 +08:00
previewDataCaller = QThread::create([=](){
while (!endLoop)
{
if (!previewing) {
qDebug()<<"Preview data reader long sleep!";
QThread::sleep(3);
continue;
}
2021-10-27 18:04:05 +08:00
// check device status=========================================
2021-10-26 09:51:23 +08:00
qDebug()<<"GetStatus";
StatusInfo inf = GetStatus();
qDebug() << "GetPreviewData request status, status:" << getStatusString(inf.status);
2021-10-27 18:04:05 +08:00
// device is preview scanning, try get preview data
2021-10-26 09:51:23 +08:00
if (inf.status==SCANNING) {
qDebug() << "Preview data reader read start!";
const char *data = GetPreviewData();
if (!data)continue;
qDebug() << "Preview data reader read end!";
QByteArray bytes = QByteArray::fromRawData(data, 140 * 140);
2021-10-27 18:04:05 +08:00
//double check
if (!previewing) {
qDebug()<<"Preview data reader long sleep!";
QThread::sleep(3);
continue;
}
2021-10-26 09:51:23 +08:00
qDebug() << "Preview data response event start!";
TRIGGER_EVENT(GUIEvents::ResponsePreviewData, nullptr, (QObject *) (&bytes));
qDebug() << "Preview data response event end!";
}
QThread::msleep(100);
2021-10-22 14:48:55 +08:00
}
});
previewDataCaller->start();
}
void DeviceManager::timerEvent(QTimerEvent *event) {
2021-10-26 09:51:23 +08:00
//scanning progress timer
if (event->timerId() !=deviceInfTimerID)
{
2021-10-27 18:04:05 +08:00
//error exit
2021-10-26 13:08:11 +08:00
if (errorOccurred)
2021-10-26 09:51:23 +08:00
{
2021-10-27 18:04:05 +08:00
qDebug() << "Error occurred, exit progress timer";
2021-10-26 09:51:23 +08:00
goto exitTimer;
}
2021-10-27 18:04:05 +08:00
// previewing exit
2021-10-26 09:51:23 +08:00
if (previewing)
{
//错误timer
goto exitTimer;
}
2021-10-27 18:04:05 +08:00
// check device status=========================================
qDebug()<<"GetStatus";
StatusInfo inf = GetStatus();
qDebug() << "Scanning request status, status:" << getStatusString(inf.status);
2021-10-26 09:51:23 +08:00
//设备正常扫描中
2021-10-20 15:55:15 +08:00
if (inf.status==SCANNING)
{
2021-10-20 15:55:15 +08:00
lastStatus = SCANNING;
//normal scan
2021-10-22 14:48:55 +08:00
QVariant var(inf.progress);
2021-10-26 09:51:23 +08:00
qDebug() << "Normal scan, invoke InvokeOperationProgress:" << inf.progress;
2021-10-22 14:48:55 +08:00
TRIGGER_EVENT(GUIEvents::InvokeOperationProgress, nullptr, (QObject*)&var);
2021-10-26 13:08:11 +08:00
return;
} else{
2021-10-26 09:51:23 +08:00
//未发生错误并且,之前状态是扫描,代表正常扫描完成
if (lastStatus == SCANNING)
{
2021-10-26 09:51:23 +08:00
qDebug() << "Scan finished" ;
QVariant var(true);
2021-10-26 09:51:23 +08:00
qDebug() << "InvokeOperationEnd" ;
TRIGGER_EVENT(GUIEvents::InvokeOperationEnd, nullptr, (QObject*)&var);
lastStatus = -1;
previewing = false;
2021-10-26 13:08:11 +08:00
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);
}
2021-10-27 18:04:05 +08:00
//一般不会出现其他情况
else{
QString msg("Unknown error in scanning progress timer");
THROW_ERROR(msg);
}
}
2021-10-26 09:51:23 +08:00
exitTimer:
2021-10-26 13:08:11 +08:00
qDebug() << "Scanning progress Timer exit";
2021-10-26 09:51:23 +08:00
killTimer(timerID);
timerID=-1;
lastStatus = -1;
previewing = false;
return;
} else{
2021-10-20 15:55:15 +08:00
QString temp = QString(GetDeviceInfo(MEAN_TEMPERATURE));
TRIGGER_EVENT(GUIEvents::ResponseDeviceTemperature, nullptr, (QObject*)&temp);
}
}
2021-10-20 15:55:15 +08:00
void DeviceManager::processScan(const char *json, bool empty) {
2021-10-27 18:04:05 +08:00
// check device status=========================================
qDebug() << "GetStatus";
StatusInfo inf = GetStatus();
qDebug() << "Scan start request status, status:" << getStatusString(inf.status);
if (inf.status != READY) {
QString errmsg("Device is not ready, start scan operation fail!status is %1");
errmsg = errmsg.arg(getStatusString(inf.status));
THROW_ERROR(errmsg);
return;
}
static QString msg = "Start scan...";
2021-10-27 18:04:05 +08:00
TRIGGER_EVENT(GUIEvents::InvokeOperationStart, nullptr, (QObject *) &msg);
qDebug() << "SetScanInfo>>>>>>>>>>>>>>>>>>>>";
int ret = SetScanInfo(json, empty ? 1 : 0);
if (ret) {
2021-10-26 09:51:23 +08:00
qDebug() << ">>>>>>>>>>>>>>>>>>>>SetScanInfo failed";
QString errmsg("Transfer patient information fail!");
2021-10-27 18:04:05 +08:00
qDebug() << "Error thrown";
THROW_ERROR(errmsg);
return;
}
2021-10-26 09:51:23 +08:00
qDebug() << ">>>>>>>>>>>>>>>>>>>>SetScanInfo success";
2021-10-27 18:04:05 +08:00
//ScanControl fail
qDebug() << "ScanControl start>>>>>>>>>>>>>>>>>>>>>";
if (!ScanControl(SCAN)) {
qDebug() << ">>>>>>>>>>>>>>>>>>>>>ScanControl success";
lastStatus = SCANNING;
qDebug() << "Start progress timer";
timerID = startTimer(300);
return;
}
2021-10-27 18:04:05 +08:00
QString errmsg("ScanControl start fail!");
THROW_ERROR(errmsg);
2021-10-27 18:04:05 +08:00
qDebug() << ">>>>>>>>>>>>>>>>>>>>>ScanControl failed";
}