2887 lines
119 KiB
C++
2887 lines
119 KiB
C++
#include "Logic_processtask.h"
|
||
#include "qfuture.h"
|
||
#include <QtConcurrent/qtconcurrent>
|
||
#include <qmessagebox.h>
|
||
#include <rlib/fileManager.h>
|
||
|
||
//总相机线程类列表
|
||
QList<Logic_camera_thread*> camerathreadlist;
|
||
//工站列表map
|
||
QMap<QString, HStationData*> station_list;
|
||
//显示图像的数据包
|
||
//通过extern在Logic_camera_thread和Logic_taskthread的sendDataCommond函数使用
|
||
QQueue<dataPack> ImageDataPack;
|
||
//写PLC指令,
|
||
QQueue<QByteArray> writeCommondPack;
|
||
//bool Tcp_Connect_Status = false;
|
||
//bool check_send_resultA_flag = false;
|
||
//bool check_send_resultB_flag = false;
|
||
//QString mv_trigger_signal;
|
||
|
||
//内容为配置文件read_sn_startpos;注意访问加锁
|
||
//QQueue<QString> read_sn_start_data_queue;
|
||
//总设备信息交互类列表
|
||
QList<Logic_myiothread*> Logic_myiothreadList;
|
||
//心跳、全作用域线程
|
||
Logic_myiothread* Logic_myiothread_heart;
|
||
|
||
//总任务类列表
|
||
QList<Logic_taskthread*> task_thread_list;
|
||
//总条件判断类列表
|
||
QList<Logic_hdataintegration*> dataIntegrationThread_list;
|
||
//需要一直运行的,条件判断线程;
|
||
//标准通信流程为空;
|
||
//单工站,无单独的PLC索要结果的,使用
|
||
QQueue<Logic_hdataintegration*>alwaysRun_Logic_hdataintegration;
|
||
|
||
//小线程
|
||
static QFuture<void> future1[10];
|
||
static int allfuture1Num = 0;
|
||
|
||
Logic_processtask::Logic_processtask(QObject* parent) : QObject(parent)
|
||
{
|
||
m_io_mutex = new QMutex;
|
||
m_showimage_mutex = new QMutex;
|
||
write_data_mutex = new QMutex;
|
||
LsendImgStructMutex = new QMutex;
|
||
// 初始化IO线程
|
||
//m_IoThread = new Logic_myiothread(this);
|
||
// m_IoThread->setIOMutex(m_io_mutex, write_data_mutex);
|
||
|
||
// 初始化io读取包的线程
|
||
//m_readThread = new ReadPackThread(this);
|
||
|
||
|
||
//// 创建检测通讯状态的定时器
|
||
//check_tcp_status = new QTimer;
|
||
//check_tcp_status->setInterval(5000);
|
||
//connect(check_tcp_status, &QTimer::timeout, this, [=]() {
|
||
// emit sendTcpStatus(Tcp_Connect_Status);
|
||
// });
|
||
//check_tcp_status->start(3000);
|
||
exitFlag = false;
|
||
//QFuture<void> myfuture = QtConcurrent::run(this, &Logic_processtask::reciveImageData);
|
||
}
|
||
|
||
Logic_processtask::~Logic_processtask()
|
||
{
|
||
isStartThread = false;
|
||
Sleep(20);
|
||
for (int i = 0; i < allfuture1Num; i++)
|
||
future1[i].waitForFinished();
|
||
allfuture1Num = 0;
|
||
exitFlag = true;
|
||
Sleep(100);
|
||
exitTask();
|
||
Lqueue_sendImgStruct.clear();
|
||
Sleep(100);
|
||
/*if (m_readThread){
|
||
m_readThread->deleteLater();
|
||
m_readThread = nullptr;
|
||
}*/
|
||
|
||
if (m_io_mutex){
|
||
delete m_io_mutex;
|
||
m_io_mutex = nullptr;
|
||
}
|
||
|
||
if (m_showimage_mutex){
|
||
delete m_showimage_mutex;
|
||
m_showimage_mutex = nullptr;
|
||
}
|
||
|
||
lib_list.clear();
|
||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " ~Logic_processtask success! ";
|
||
}
|
||
void Logic_processtask::setJsonObject(QJsonObject obj)
|
||
{
|
||
if (obj.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " obj is null! ";
|
||
return;
|
||
}
|
||
programObj = obj;
|
||
/*if (ifChecked == false)
|
||
{
|
||
QString errData;
|
||
check_cmeraCfg(obj, errData);
|
||
}*/
|
||
}
|
||
|
||
bool Logic_processtask::check_cmeraCfg(QJsonObject obj, QString& allMessage)
|
||
{
|
||
ifChecked = true;
|
||
QMap<QString, QStringList> station_cameraKey_map;
|
||
QMap<QString, QStringList> station_algorithmKey_map;
|
||
|
||
//QDir dir = QApplication::applicationDirPath();
|
||
//QString json_cfg_file_path = dir.path() + "/config/camera_cfg.json";
|
||
QJsonObject m_jsonObj = obj;// loadJsonCfg(json_cfg_file_path);
|
||
// 设置本地保存文件夹的路径
|
||
QString myQString = m_jsonObj.value("保存图片总文件夹").toString();
|
||
int allFailNum = 0;
|
||
//QString allMessage;
|
||
if (myQString.isEmpty())
|
||
{
|
||
//allMessage += "第 " + QString::number(allFailNum++) + "条 :保存图片总文件夹,为空\r\n";
|
||
allMessage += "第 " + QString::number(allFailNum) + "条 :保存图片总文件夹为空但已创建\r\n";
|
||
fileManager::createDirectoryIfNotExists(myQString);
|
||
}
|
||
QDir dir(myQString);
|
||
if (!dir.exists())
|
||
{
|
||
//allMessage += "第 " + QString::number(allFailNum) + "条 :保存图片总文件夹:" + myQString + " 不存在\r\n";
|
||
allMessage += "第 " + QString::number(allFailNum) + "条 :保存图片总文件夹:" + myQString + " 不存在但已创建\r\n";
|
||
fileManager::createDirectoryIfNotExists(myQString);
|
||
}
|
||
myQString = m_jsonObj.value("数据保存天数").toString();
|
||
if (myQString.toInt() <= 0)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :数据保存天数,值错误\r\n";
|
||
myQString = m_jsonObj.value("日志保存天数").toString();
|
||
if (myQString.toInt() <= 0)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :日志保存天数,值错误\r\n";
|
||
myQString = m_jsonObj.value("UI界面类").toString();
|
||
//if (myQString.isEmpty())
|
||
// allMessage += "第 " + QString::number(allFailNum++) + "条 :UI界面类,为空\r\n";
|
||
myQString = m_jsonObj.value("是否为标准通信流程").toString();
|
||
if (myQString.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :是否为标准通信流程,为空\r\n";
|
||
else if (myQString.contains("false"))
|
||
CommunicationProcessLevel = 1;
|
||
else if (myQString.contains("true"))
|
||
CommunicationProcessLevel = 2;
|
||
|
||
|
||
allMessage += "------开始检查,通信列表------\r\n";
|
||
QJsonArray myQJsonArray = m_jsonObj.value("通信列表").toArray();
|
||
if (myQJsonArray.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :通信列表,为空\r\n";
|
||
else {
|
||
for (int i = 0; i < myQJsonArray.size(); i++)
|
||
{
|
||
QJsonObject myQJsonObject = myQJsonArray.at(i).toObject();
|
||
/*myQString = myQJsonObject.value("写入类型").toString();
|
||
if (myQString != "short" && myQString != "ushort")
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :写入类型,不为short或ushort\r\n";*/
|
||
myQString = myQJsonObject.value("通信初始化").toString();
|
||
if (myQString.split("_").size() < 3)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :通信初始化,错误\r\n";
|
||
myQString = myQJsonObject.value("模块名称").toString();
|
||
if (myQString.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :模块名称,为空\r\n";
|
||
myQString = myQJsonObject.value("通信睡眠时间").toString();
|
||
if (myQString.toInt() <= 0)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :通信睡眠时间,值错误\r\n";
|
||
myQString = myQJsonObject.value("心跳指令").toString();
|
||
if (myQString.isEmpty())
|
||
//if (!myQString.contains("short"))
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :心跳指令,为空\r\n";
|
||
QString myQString1 = myQJsonObject.value("心跳间隔时间").toString();
|
||
if (myQString1.toInt() < 50 && !myQString.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :心跳间隔时间,值错误\r\n";
|
||
myQString = myQJsonObject.value("批量读指令").toString();
|
||
if (!myQString.isEmpty())
|
||
if(myQString.split("_").size()<4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :批量读指令,错误\r\n";
|
||
myQString= myQJsonObject.value("复位信号").toString();
|
||
if (!myQString.isEmpty())
|
||
{
|
||
if (myQString.split("_").size() < 2)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :复位信号,错误\r\n";
|
||
myQString1 = myQJsonObject.value("复位信号-收到后回复").toString();
|
||
if (myQString1.split("_").size() < 4 && !myQString1.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :复位信号-收到后回复,错误.正确格式例:write_short_D1000_0\r\n";
|
||
}
|
||
myQString = myQJsonObject.value("程序号").toString();
|
||
if (!myQString.isEmpty())
|
||
{
|
||
myQString1 = myQJsonObject.value("程序号-收到后回复").toString();
|
||
if (myQString1.split("_").size() < 4 )
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :程序号-收到后回复,错误.正确格式例:write_short_D1000_0\r\n";
|
||
}
|
||
|
||
|
||
/*myQString = myQJsonObject.value("发送的数据是否自动拼接PLC指令头").toString();
|
||
if (myQString != "true" && myQString != "false")
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :发送的数据是否自动拼接PLC指令头,不为true或false\r\n";
|
||
if (myQString == "true")
|
||
ifAddCmdHead = true;
|
||
else if (myQString == "false")*/
|
||
ifAddCmdHead = false;
|
||
}
|
||
}
|
||
|
||
allMessage += "------开始检查,工站列表------\r\n";
|
||
//检查:工站列表
|
||
myQJsonArray = m_jsonObj.value("工站列表").toArray();
|
||
for (int i = 0; i < myQJsonArray.size(); i++)
|
||
{
|
||
QJsonObject myQJsonObject = myQJsonArray.at(i).toObject();
|
||
QString myStationKey = myQJsonObject.value("工站名").toString();
|
||
//检查:相机列表
|
||
QJsonArray myCameraArray = myQJsonObject.value("相机模块列表").toArray();
|
||
QStringList CameraKeyList;
|
||
for (int ca = 0; ca < myCameraArray.size(); ca++)
|
||
{
|
||
//检查: 相机key
|
||
QJsonObject myCameraObject = myCameraArray.at(ca).toObject();
|
||
QString myCameraKey = myCameraObject.value("相机key").toString();
|
||
if (!myCameraKey.isEmpty())
|
||
CameraKeyList.append(myCameraKey);
|
||
}
|
||
station_cameraKey_map.insert(myStationKey, CameraKeyList);
|
||
myCameraArray = myQJsonObject.value("算法列表").toArray();
|
||
QStringList KeyList;
|
||
for (int ca = 0; ca < myCameraArray.size(); ca++)
|
||
{
|
||
//检查: 相机key
|
||
QJsonObject myCameraObject = myCameraArray.at(ca).toObject();
|
||
QString myKey = myCameraObject.value("算法名称").toString();
|
||
if (!myKey.isEmpty())
|
||
KeyList.append(myKey);
|
||
}
|
||
station_algorithmKey_map.insert(myStationKey, KeyList);
|
||
}
|
||
|
||
if (myQJsonArray.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :工站列表,为空\r\n";
|
||
else
|
||
{
|
||
for (int i = 0; i < myQJsonArray.size(); i++)
|
||
{
|
||
QJsonObject myQJsonObject = myQJsonArray.at(i).toObject();
|
||
QString myStationKey = myQJsonObject.value("工站名").toString();
|
||
allMessage += "------开始检查,工站名:: " + myStationKey + " ------\r\n";
|
||
if (myStationKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :工站名,为空\r\n";
|
||
for (int j = i + 1; j < myQJsonArray.size(); j++)
|
||
if (myStationKey == myQJsonArray.at(j).toObject().value("工站名").toString())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :工站名 " + myStationKey + " , 重复\r\n";
|
||
myQString = myQJsonObject.value("批量读指令").toString();
|
||
if (CommunicationProcessLevel == 2)
|
||
if (myQString.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :批量读指令,为空\r\n";
|
||
else if (myQString.split("_").size() != 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :批量读指令,错误\r\n";
|
||
|
||
allMessage += "------开始检查,相机列表, 工站名:: " + myStationKey + " ------\r\n";
|
||
//检查:相机列表
|
||
QJsonArray myCameraArray = myQJsonObject.value("相机模块列表").toArray();
|
||
for (int ca = 0; ca < myCameraArray.size(); ca++)
|
||
{
|
||
//检查: 相机key
|
||
QJsonObject myCameraObject = myCameraArray.at(ca).toObject();
|
||
QString myCameraKey = myCameraObject.value("相机key").toString();
|
||
if (myCameraKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :相机key,为空\r\n";
|
||
else for (int j = i; j < myQJsonArray.size(); j++)
|
||
{
|
||
int stattNum = 0;
|
||
if (j == i)stattNum = ca + 1;
|
||
QJsonArray thisQJsonArray = myQJsonArray.at(j).toObject().value("相机模块列表").toArray();
|
||
for (; stattNum < thisQJsonArray.size(); stattNum++)
|
||
if (myCameraKey == thisQJsonArray.at(stattNum).toObject().value("相机key").toString())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :相机key " + myCameraKey + " , 重复\r\n";
|
||
}
|
||
//检查: 界面窗口名称
|
||
myCameraKey = myCameraObject.value("界面窗口名称").toString();
|
||
if (myCameraKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :界面窗口名称,为空\r\n";
|
||
else for (int j = i; j < myQJsonArray.size(); j++)
|
||
{
|
||
int stattNum = 0;
|
||
if (j == i)stattNum = ca + 1;
|
||
QJsonArray thisQJsonArray = myQJsonArray.at(j).toObject().value("相机模块列表").toArray();
|
||
for (; stattNum < thisQJsonArray.size(); stattNum++)
|
||
if (myCameraKey == thisQJsonArray.at(stattNum).toObject().value("界面窗口名称").toString())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :界面窗口名称 " + myCameraKey + " , 重复\r\n";
|
||
}
|
||
|
||
//检查: 触发方式
|
||
myCameraKey = myCameraObject.value("触发方式").toString();
|
||
if (!myCameraKey.contains("software") && !myCameraKey.contains("hardware"))
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :触发方式,有误\r\n";
|
||
|
||
//检查: 取图超时
|
||
myCameraKey = myCameraObject.value("取图超时").toString();
|
||
if (myCameraKey.toInt() < 10)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :取图超时,值<10ms\r\n";
|
||
|
||
QString m_missImgae_add = myCameraObject.value("缺图时,补图宽高-张数-图像格式").toString();
|
||
QStringList missList = m_missImgae_add.split("*");
|
||
if (missList.size() < 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :缺图时,补图宽高-张数-图像格式,有误\r\n";
|
||
else if (missList[3].split("#").size() < missList[2].toInt())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :缺图时,补图宽高-张数-图像格式,有误\r\n";
|
||
|
||
//检查: 模块名称
|
||
myCameraKey = myCameraObject.value("模块名称").toString();
|
||
if (myCameraKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :模块名称,为空\r\n";
|
||
else for (int j = i; j < myQJsonArray.size(); j++)
|
||
{
|
||
int stattNum = 0;
|
||
if (j == i)stattNum = ca + 1;
|
||
QJsonArray thisQJsonArray = myQJsonArray.at(j).toObject().value("相机模块列表").toArray();
|
||
for (; stattNum < thisQJsonArray.size(); stattNum++)
|
||
{
|
||
QString myCameraKey1 = thisQJsonArray.at(stattNum).toObject().value("模块名称").toString();
|
||
if (myCameraKey == myCameraKey1)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :模块名称 " + myCameraKey + " , 重复\r\n";
|
||
else if (myCameraKey1.contains(myCameraKey))
|
||
if (!myCameraKey1.contains("#"))
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :模块名称 " + myCameraKey1 + " , 复制名称命名不对\r\n";
|
||
}
|
||
}
|
||
//检查: 拍照信号-完成写入PLC-列表
|
||
QJsonArray thisQJsonArray = myCameraObject.value("拍照信号-完成写入PLC-列表").toArray();
|
||
for (auto yyy = 0; yyy < thisQJsonArray.size(); yyy++)
|
||
{
|
||
if (thisQJsonArray.at(yyy).toString().split("_").size() != 5)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :拍照信号-完成写入PLC-列表 ,格式不对\r\n";
|
||
}
|
||
//检查: 拍照信号-完成写入PLC-列表
|
||
thisQJsonArray = myCameraObject.value("拍照信号-拍照张数-列表").toArray();
|
||
for (auto yyy = 0; yyy < thisQJsonArray.size(); yyy++)
|
||
{
|
||
QString thisString = thisQJsonArray.at(yyy).toString();
|
||
if (thisString.split("_").size() != 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :拍照信号-拍照张数-列表 ,格式不对\r\n";
|
||
}
|
||
//检查: 上料通道数
|
||
QString channelNum = myCameraObject.value("上料通道数").toString();
|
||
if (channelNum.toInt() < 1)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :上料通道数,<1\r\n";
|
||
//检查: 单通道拍照数
|
||
QString channelImgNum = myCameraObject.value("单通道拍照数").toString();
|
||
if (channelImgNum.toInt() < 1)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :单通道拍照数,<1\r\n";
|
||
//检查: 拍照面描述
|
||
thisQJsonArray = myCameraObject.value("拍照面描述").toArray();
|
||
if (thisQJsonArray.size() < 1)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :拍照面描述,为空\r\n";
|
||
//检查: 拍照信号-收到后-写值
|
||
myCameraKey = myCameraObject.value("拍照信号-收到后-写值").toString();
|
||
if (myCameraKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :拍照信号-收到后-写值, 为空\r\n";
|
||
if (myCameraKey.split("_").size() != 3)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :拍照信号-收到后-写值 ,格式不对。正确格式例:write_short_0\r\n";
|
||
|
||
|
||
//检查: 获取SN方式列表
|
||
thisQJsonArray = myCameraObject.value("获取SN方式列表").toArray();
|
||
for (auto yyy = 0; yyy < thisQJsonArray.size(); yyy++)
|
||
{
|
||
QJsonObject SNobj = thisQJsonArray.at(yyy).toObject();
|
||
//检查: 触发方式
|
||
myCameraKey = SNobj.value("触发信号").toString();
|
||
if (myCameraKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :触发信号,为空\r\n";
|
||
else if (myCameraKey.split("_").size() != 2)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :触发信号,内容有误\r\n";
|
||
//检查: 读哪个上料通道的SN
|
||
myCameraKey = SNobj.value("读哪个上料通道的SN").toString();
|
||
if (myCameraKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读哪个上料通道的SN,为空\r\n";
|
||
else if (myCameraKey.toInt() > 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读哪个上料通道的SN,值>4\r\n";
|
||
//检查: SN数据列表
|
||
QJsonArray mySNDataArray = SNobj.value("获取SN方式列表").toArray();
|
||
for (auto sd = 0; sd < mySNDataArray.size(); sd++)
|
||
{
|
||
QJsonObject sdobj = mySNDataArray.at(sd).toObject();
|
||
//检查: 触发方式
|
||
myCameraKey = SNobj.value("哪个SN").toString();
|
||
if (myCameraKey.size() < 3)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :哪个SN,为空或长度有误\r\n";
|
||
else if (myCameraKey.mid(0, 2) != "sn" || myCameraKey.mid(2).toInt() < 1 || myCameraKey.mid(2).toInt() > channelNum.toInt())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :哪个SN,内容有误\r\n";
|
||
//检查: 第几张图
|
||
QJsonArray thisQJsonArray1 = myCameraObject.value("第几张图").toArray();
|
||
for (auto yyy = 0; yyy < thisQJsonArray1.size(); yyy++)
|
||
{
|
||
if (thisQJsonArray1.at(yyy).toString().toInt() >= channelImgNum.toInt())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :第几张图 ,数值不对\r\n";
|
||
}
|
||
}
|
||
}
|
||
//检查: 相机参数
|
||
thisQJsonArray = myCameraObject.value("相机参数").toArray();
|
||
for (auto yyy = 0; yyy < thisQJsonArray.size(); yyy++)
|
||
{
|
||
QJsonObject caobj = thisQJsonArray.at(yyy).toObject();
|
||
//检查: 相机参数
|
||
myCameraKey = caobj.value("参数名").toString();
|
||
if (myCameraKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :参数名,为空\r\n";
|
||
//检查: 相机参数值
|
||
if (caobj.value("相机参数值").toArray().size() < 1)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :相机参数值,为空\r\n";
|
||
}
|
||
}
|
||
|
||
allMessage += "------开始检查,SN流程列表, 工站名:: " + myStationKey + " ------\r\n";
|
||
//检查:SN流程列表
|
||
QJsonArray mySNArray = myQJsonObject.value("SN流程列表").toArray();
|
||
for (int ss = 0; ss < mySNArray.size(); ss++)
|
||
{
|
||
QJsonObject mySNObject = mySNArray.at(ss).toObject();
|
||
//检查: 读SN信号
|
||
QString mySNstring = mySNObject.value("读SN信号").toString();
|
||
if (mySNstring.split("_").size() != 2)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读SN信号,错误\r\n";
|
||
//检查: 读SN寄存器列表
|
||
QJsonArray thisQJsonArray = mySNObject.value("读SN寄存器列表").toArray();
|
||
for (int ar = 0; ar < thisQJsonArray.size(); ar++)
|
||
if (thisQJsonArray[ar].toString().split("_").size() != 2)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读SN寄存器列表,内容错误\r\n";
|
||
//检查: 写时间戳-寄存器列表
|
||
thisQJsonArray = mySNObject.value("写时间戳-寄存器列表").toArray();
|
||
for (int ar = 0; ar < thisQJsonArray.size(); ar++)
|
||
if (thisQJsonArray[ar].toString().split("_").size() != 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :写时间戳-寄存器列表,内容错误。正确格式例:write_string_D100_24\r\n";
|
||
//检查: 读时间戳-寄存器列表
|
||
thisQJsonArray = mySNObject.value("读时间戳-寄存器列表").toArray();
|
||
for (int ar = 0; ar < thisQJsonArray.size(); ar++)
|
||
if (thisQJsonArray[ar].toString().split("_").size() != 2)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读时间戳-寄存器列表,内容错误\r\n";
|
||
//检查: SN正常-回复指令
|
||
/*mySNstring = mySNObject.value("读SN正常-回复指令").toString();
|
||
if (!mySNstring.isEmpty() && mySNstring.split("_").size() < 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读SN正常-回复指令,错误。正确格式例:write_short_D1000_0\r\n";*/
|
||
//检查: 读SN后-清0
|
||
mySNstring = mySNObject.value("读SN后-清0").toString();
|
||
if (mySNstring.split("_").size() != 3 && !mySNstring.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读SN后-清0, 格式不对,正确格式例:write_short_0\r\n";
|
||
//检查: 读时间戳后-清0
|
||
mySNstring = mySNObject.value("读时间戳后-清0").toString();
|
||
if (mySNstring.split("_").size() != 3 && !mySNstring.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读时间戳后-清0, 格式不对,正确格式例:write_short_0\r\n";
|
||
////检查: 读SN异常-回复指令
|
||
//mySNstring = mySNObject.value("读SN异常-回复指令").toString();
|
||
//if (!mySNstring.isEmpty() && mySNstring.split("_").size() != 4)
|
||
// allMessage += "第 " + QString::number(allFailNum++) + "条 :读SN异常-回复指令,错误,正确格式例:write_short_D1000_0\r\n";
|
||
//检查: 读SN异常-插值
|
||
mySNstring = mySNObject.value("读SN异常-插值").toString();
|
||
if (mySNstring.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :读SN异常-插值,为空\r\n";
|
||
//检查: 读时间戳-寄存器列表
|
||
QJsonObject cleraSNobj = mySNObject.value("清理容器数据").toObject();
|
||
//检查: 通道序号
|
||
if (cleraSNobj.value("通道序号").toString().toInt() > 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :通道序号,值>4\r\n";
|
||
//检查: 相机列表
|
||
thisQJsonArray = cleraSNobj.value("相机列表").toArray();
|
||
for (int ar = 0; ar < thisQJsonArray.size(); ar++)
|
||
{
|
||
bool ifamtch = false;
|
||
for (int ca = 0; ca < myCameraArray.size(); ca++)
|
||
{
|
||
//检查: 相机key
|
||
QJsonObject myCameraObject = myCameraArray.at(ca).toObject();
|
||
QString myCameraKey = myCameraObject.value("相机key").toString();
|
||
if (myCameraKey == thisQJsonArray[ar].toString())
|
||
ifamtch = true;
|
||
}
|
||
if (ifamtch == false)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :相机列表, 未匹配,相机key\r\n";
|
||
}
|
||
|
||
}
|
||
|
||
allMessage += "------开始检查,算法列表, 工站名:: " + myStationKey + " ------\r\n";
|
||
//检查:算法列表
|
||
QJsonArray myAIArray = myQJsonObject.value("算法列表").toArray();
|
||
for (int ai = 0; ai < myAIArray.size(); ai++)
|
||
{
|
||
QJsonObject AIobj = myAIArray[ai].toObject();
|
||
//检查:算法名称
|
||
QString myAIKey = AIobj.value("算法名称").toString();
|
||
if (myAIKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :算法名称,为空\r\n";
|
||
else for (int j = i; j < myAIArray.size(); j++)
|
||
{
|
||
int stattNum = 0;
|
||
if (j == i)stattNum = ai + 1;
|
||
QJsonArray thisQJsonArray = myAIArray.at(j).toObject().value("算法列表").toArray();
|
||
for (; stattNum < thisQJsonArray.size(); stattNum++)
|
||
if (myAIKey == thisQJsonArray.at(stattNum).toObject().value("算法名称").toString())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :算法名称 " + myAIKey + " , 重复\r\n";
|
||
}
|
||
//检查: 模块名称
|
||
myAIKey = AIobj.value("模块名称").toString();
|
||
if (myAIKey.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :模块名称,为空\r\n";
|
||
else for (int j = i; j < myQJsonArray.size(); j++)
|
||
{
|
||
int stattNum = 0;
|
||
if (j == i)stattNum = ai + 1;
|
||
QJsonArray thisQJsonArray = myQJsonArray.at(j).toObject().value("算法列表").toArray();
|
||
for (; stattNum < thisQJsonArray.size(); stattNum++)
|
||
{
|
||
QString myAIKey1 = thisQJsonArray.at(stattNum).toObject().value("模块名称").toString();
|
||
if (myAIKey == myAIKey1)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :模块名称 " + myAIKey + " , 重复\r\n";
|
||
else if (myAIKey1.contains(myAIKey))
|
||
if (!myAIKey1.contains("#"))
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :模块名称 " + myAIKey1 + " , 复制名称命名不对\r\n";
|
||
}
|
||
}
|
||
//检查: 是否保存数据
|
||
myAIKey = AIobj.value("是否保存数据").toString();
|
||
if (myAIKey != "true" && myAIKey != "false")
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :是否保存数据, 非true,false\r\n";
|
||
if(myAIKey=="false"&& AIobj.value("是否发送结果给PLC").toString()=="false")
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :是否保存数据、是否发送结果给PLC,同时为flase,请检查\r\n";
|
||
|
||
|
||
//检查: 算法超时时间
|
||
myAIKey = AIobj.value("算法超时时间").toString();
|
||
if (!myAIKey.isEmpty() && myAIKey.toInt() <= 100)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :算法超时时间, <=100\r\n";
|
||
//检查: 检测条件总表
|
||
QJsonArray checkALLarray = AIobj.value("检测条件总表").toArray();
|
||
for (int yy = 0; yy < checkALLarray.size(); yy++)
|
||
{
|
||
//检查: 检测条件列表
|
||
QJsonArray checkarrayT = checkALLarray[yy].toObject().value("检测条件列表").toArray();
|
||
for (int cca = 0; cca < checkarrayT.size(); cca++)
|
||
{
|
||
QJsonObject checkobj = checkarrayT[cca].toObject();
|
||
if (checkobj.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :检测条件列表, 为空\r\n";
|
||
else if (!checkobj.contains("and") && !checkobj.contains("or"))
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :检测条件列表, 非and,or\r\n";
|
||
else {
|
||
//检查: and或者or列表元素
|
||
QJsonArray checkArray = checkobj.value("and").toArray();
|
||
if (checkArray.isEmpty())
|
||
checkArray = checkobj.value("or").toArray();
|
||
if (checkArray.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :and或者or列表元素, 为空\r\n";
|
||
for (int cc = 0; cc < checkArray.size(); cc++)
|
||
{
|
||
QJsonObject checkobjKid = checkArray[cc].toObject();
|
||
//检查: 检测/汇总-触发条件
|
||
QString checkString = checkobjKid.value("检测/汇总-触发条件").toString();
|
||
bool ifmatch = false;
|
||
if (!checkString.isEmpty())
|
||
{
|
||
for (int mc = 0; mc < station_cameraKey_map.size(); mc++)
|
||
{
|
||
QStringList keyList = station_cameraKey_map.value(station_cameraKey_map.keys().at(mc));
|
||
for (int cm = 0; cm < keyList.size(); cm++)
|
||
{
|
||
if (checkString == keyList[cm])
|
||
ifmatch = true;
|
||
}
|
||
}
|
||
for (int mc = 0; mc < station_algorithmKey_map.size(); mc++)
|
||
{
|
||
QStringList keyList = station_algorithmKey_map.value(station_algorithmKey_map.keys().at(mc));
|
||
for (int cm = 0; cm < keyList.size(); cm++)
|
||
{
|
||
if (checkString == keyList[cm])
|
||
ifmatch = true;
|
||
}
|
||
}
|
||
if (ifmatch == false)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :检测/汇总-触发条件, 未匹配\r\n";
|
||
}
|
||
|
||
//检查: 汇总-触发类型
|
||
QString checkType = checkobjKid.value("汇总-触发类型").toString();
|
||
if (!checkType.isEmpty())
|
||
if (checkType != "signal" && checkType != "data")
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-触发类型, 非signal,data\r\n";
|
||
//检查: 汇总-触发信号
|
||
checkString = checkobjKid.value("汇总-触发信号").toString();
|
||
if (!checkString.isEmpty() && checkString.split("_").size() != 2)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-触发信号,错误\r\n";
|
||
//检查: 汇总-回复信号
|
||
checkString = checkobjKid.value("汇总-回复信号").toString();
|
||
if (!checkString.isEmpty() && checkString.split("_").size() != 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-回复信号,错误,正确格式例:write_short_D1000_0\r\n";
|
||
//检查: 汇总-工站名称
|
||
checkString = checkobjKid.value("汇总-工站名称").toString();
|
||
ifmatch = false;
|
||
if (!checkString.isEmpty())
|
||
{
|
||
for (int mc = 0; mc < station_cameraKey_map.size(); mc++)
|
||
if (checkString == station_cameraKey_map.keys().at(mc))
|
||
ifmatch = true;
|
||
if (ifmatch == false)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-工站名称,错误\r\n";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//检查: SN设置
|
||
QJsonObject snSetObj = AIobj.value("SN设置").toObject();
|
||
//检查: 汇总-触发类型
|
||
QString checkString = snSetObj.value("检测-是否传递SN到算法模块").toString();
|
||
if (!checkString.isEmpty())
|
||
if (checkString != "true" && checkString != "false")
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :检测-是否传递SN到算法模块, 非true,false\r\n";
|
||
checkString = snSetObj.value("汇总-是否删除SN容器首数据").toString();
|
||
if (!checkString.isEmpty())
|
||
{
|
||
if (checkString != "true")
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-是否删除SN容器首数据, 非true\r\n";
|
||
if (snSetObj.value("汇总-读哪个上料通道的SN").toString().isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-读哪个上料通道的SN, 为空\r\n";
|
||
if (snSetObj.value("汇总-读哪个SN").toString().isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-读哪个SN, 为空\r\n";
|
||
}
|
||
|
||
//检查: 算法初始化参数
|
||
QJsonArray AIinitArray = AIobj.value("算法初始化参数").toArray();
|
||
for (int ai = 0; ai < AIinitArray.size(); ai++)
|
||
{
|
||
QJsonObject myboj = AIinitArray[ai].toObject();
|
||
//检查: 算法参数名
|
||
if (myboj.value("算法参数名").toString().isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :算法参数名, 为空\r\n";
|
||
//检查: 算法参数值
|
||
if (myboj.value("算法参数值").toArray().isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :算法参数值, 为空\r\n";
|
||
}
|
||
myAIKey = AIobj.value("算法完成回复PLC指令").toString();
|
||
if (!myAIKey.isEmpty() && myAIKey.split("_").size() != 4)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :算法完成回复PLC指令,错误,正确格式例:write_short_D1000_1\r\n";
|
||
|
||
//检查: 算法结果列表
|
||
QJsonArray AIresultArray = AIobj.value("算法结果列表").toArray();
|
||
for (int ai = 0; ai < AIresultArray.size(); ai++)
|
||
{
|
||
QJsonObject myboj = AIresultArray[ai].toObject();
|
||
//检查: 结果标志
|
||
checkString = myboj.value("结果标志").toString();
|
||
bool ifmatch = false;
|
||
if (checkString != "resultsSummary")
|
||
{
|
||
for (int mc = 0; mc < station_cameraKey_map.size(); mc++)
|
||
{
|
||
QStringList keyList = station_cameraKey_map.value(station_cameraKey_map.keys().at(mc));
|
||
for (int cm = 0; cm < keyList.size(); cm++)
|
||
{
|
||
if (checkString.split("_")[0] == keyList[cm])
|
||
ifmatch = true;
|
||
}
|
||
}
|
||
if (ifmatch == false || checkString.isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :结果标志, 不匹配相机key\r\n";
|
||
}
|
||
//检查: 结果内容
|
||
QJsonArray resultArray = myboj.value("结果内容").toArray();
|
||
for (int ra = 0; ra < resultArray.size(); ra++)
|
||
{
|
||
QJsonObject rObj = resultArray[ra].toObject();
|
||
////检查: 结果类型
|
||
//checkString = rObj.value("结果类型").toString();
|
||
//if (checkString != "ushort" && checkString != "short")
|
||
// allMessage += "第 " + QString::number(allFailNum++) + "条 :结果类型, 非ushort,short\r\n";
|
||
//检查: 算法参数名
|
||
if (rObj.value("超时补结果").toString().isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :超时补结果, 为空\r\n";
|
||
checkString = rObj.value("写PLC地址").toString();
|
||
if (!checkString.isEmpty() && checkString.split("_").size() != 3)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-回复信号,错误,正确格式例:write_short_D1000\r\n";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
allMessage += "------开始检查,手动补信号------\r\n";
|
||
myQJsonArray = m_jsonObj.value("手动补信号").toArray();
|
||
if (myQJsonArray.isEmpty())
|
||
;// allMessage += "第 " + QString::number(allFailNum++) + "条 :手动补信号,为空\r\n";
|
||
else
|
||
{
|
||
for (int ai = 0; ai < myQJsonArray.size(); ai++)
|
||
{
|
||
QJsonObject myboj = myQJsonArray[ai].toObject();
|
||
//检查: 指令地址
|
||
if (myboj.value("指令地址").toString().isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :指令地址, 为空\r\n";
|
||
QString checkString = myboj.value("指令地址").toString();
|
||
if (!checkString.isEmpty() && checkString.split("_").size() != 3)
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :汇总-回复信号,错误,正确格式例:write_short_D1000\r\n";
|
||
//检查: 结果内容
|
||
QJsonArray addArray = myboj.value("结果内容").toArray();
|
||
for (int ra = 0; ra < addArray.size(); ra++)
|
||
{
|
||
QJsonObject addObj = addArray[ra].toObject();
|
||
//检查: 标签名
|
||
if (myboj.value("标签名").toString().isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :标签名, 为空\r\n";
|
||
//检查: 标签值
|
||
if (myboj.value("标签值").toString().isEmpty())
|
||
allMessage += "第 " + QString::number(allFailNum++) + "条 :标签值, 为空\r\n";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (allFailNum > 0)
|
||
{
|
||
//QWidget* myWidget = new QWidget;
|
||
//QMessageBox myBox;// = new QMessageBox();
|
||
////myBox->setStyleSheet("QMessageBox{background-color: rgb(30, 30, 30);}"
|
||
//// "QMessageBox{border-style: none;color:white; font-size:12px;}");
|
||
|
||
////myBox->setStyleSheet("text-color: rgb(255, 255, 255);");//rgb(30, 30, 30)
|
||
//myBox.information(myWidget, "camera_cfg出错", allMessage);
|
||
//myBox.setStyleSheet("background-color: rgb(255, 255, 255);"); //无效
|
||
//QMessageBox MyBox(QMessageBox::Information, "camera_cfg出错", allMessage);
|
||
//QMessageBox MyBox(QMessageBox::Information, "Title", "text", QMessageBox::Yes | QMessageBox::No);
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " camera_cfg出错! 详细信息为:" << allMessage;
|
||
ifChecked = false;
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
//加载模块、初始化、开启类线程
|
||
bool Logic_processtask::initTask()
|
||
{
|
||
try
|
||
{
|
||
//station里面包含cameramodulelist相机模块、algorithmlist检测模块
|
||
tcplist_array = programObj.value("通信列表").toArray();
|
||
stationlist_array = programObj.value("工站列表").toArray();
|
||
if (stationlist_array.isEmpty() || tcplist_array.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " stationlist or tcplist_array is null";
|
||
return false;
|
||
}
|
||
|
||
// 加载所有模块;初始化所有模块
|
||
if (loadAllModule())
|
||
{
|
||
qDebug() << __FUNCTION__ << __LINE__ << " loadAllModule success!";
|
||
setLoadFlag(1);
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << __LINE__ << " loadAllModule failed!";
|
||
setLoadFlag(0);
|
||
return false;
|
||
}
|
||
|
||
|
||
Sleep(100);
|
||
//startTask();
|
||
|
||
}
|
||
catch (QString ev)
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
||
return false;
|
||
}
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " create thread success!";
|
||
//startTask();
|
||
return true;
|
||
}
|
||
////获取算法的模块列表,实际只有1个
|
||
//QList<MyModule> Logic_processtask::getModuleList(int thread_index, QString stationKey)
|
||
//{
|
||
// QList<MyModule> m_module_list;
|
||
// //for (int i = 0; i < module_list.size(); i++)
|
||
// {
|
||
// //QString task_thread_index = module_list.at(i).task_thread_index;
|
||
// //QString m_stationKey = module_list.at(i).m_stationKey;
|
||
// //if (task_thread_index.toInt() == thread_index && m_stationKey == stationKey)
|
||
// m_module_list.append(module_list.at(thread_index));
|
||
// }
|
||
// return m_module_list;
|
||
//}
|
||
////获取对应算法序号的配置文件内容
|
||
//QJsonArray Logic_processtask::getTaskAlgorithmObject(int thread_index, QJsonArray m_array)
|
||
//{
|
||
// QJsonArray array;
|
||
// for (int i = 0; i < m_array.size(); i++)
|
||
// {
|
||
// QJsonObject obj = m_array.at(i).toObject();
|
||
// QString task_thread_index = obj.value("task_thread_index").toString();
|
||
// if (task_thread_index.toInt() == thread_index)
|
||
// array.append(obj);
|
||
// }
|
||
// return array;
|
||
//}
|
||
|
||
//获取通信的配置文件,
|
||
//注意,只有一个通信配置文件
|
||
QJsonObject Logic_processtask::getFinsTcpObject(QJsonArray array)
|
||
{
|
||
QJsonObject m_obj;
|
||
for (int i = 0; i < array.size(); i++)
|
||
{
|
||
QJsonObject obj = array.at(i).toObject();
|
||
//QString key = obj.value("key").toString();
|
||
//if (key == "finstcp")
|
||
{
|
||
return obj;
|
||
}
|
||
}
|
||
return m_obj;
|
||
}
|
||
|
||
// 创建线程
|
||
bool Logic_processtask::createModuleThread()
|
||
{
|
||
try
|
||
{
|
||
if (modulethread_flag == 1)
|
||
{
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " all module thread ! ";
|
||
return true;
|
||
}
|
||
|
||
QString saveImage = programObj.value("保存图片总文件夹").toString();
|
||
for (int i = 0; i < stationlist_array.size(); i++)
|
||
{
|
||
QJsonObject station_obj = stationlist_array.at(i).toObject();
|
||
QString station_name = station_obj.value("工站名").toString();
|
||
|
||
// 创建检测条件判断线程
|
||
// 创建任务线程,包括检测线程、汇总线程
|
||
//检测、汇总线程以配置文件“save_data_flag”是否为“true”在Logic_taskthread.cpp里面区分
|
||
QJsonArray algorithmlist = station_obj.value("算法列表").toArray();
|
||
QList<Logic_hdataintegration*> station_camera_dataIntegrationThread_list;
|
||
QList<Logic_hdataintegration*> station_trigger_signal_dataIntegrationThread_list;
|
||
QList<Logic_taskthread*> station_task_thread_list;
|
||
for (auto val = 0; val < algorithmlist.size(); val++)
|
||
{
|
||
//配置文件,每个task_thread_index不同,且按照顺序排列
|
||
|
||
//获得任务(检测/汇总)的对应任务序号的配置文件内容
|
||
QJsonObject m_array = algorithmlist.at(val).toObject();// getTaskAlgorithmObject(val, algorithmlist);
|
||
QString task_thread_name = algorithmlist.at(val).toObject().value("算法名称").toString();
|
||
MyModule algorithm_Module = getModule(task_thread_name); // 获取加载好的module
|
||
algorithm_Module.m_stationKey = station_name;
|
||
//QList<MyModule> m_mymodule = getModuleList(val, station_name); //其实就1个
|
||
|
||
//超时时间,应当在模块实现
|
||
//for(int i=0;i< m_mymodule.size();i++)
|
||
//algorithm_Module.module_ptr->moduleTimeout = algorithmlist.at(val).toObject().value("算法超时时间").toString().toInt();
|
||
|
||
Logic_taskthread* m_task = new Logic_taskthread(this);
|
||
//传递通信类指针,以便在需要的时候给PLC发送信息。
|
||
//如汇总线程发送结果。
|
||
//m_task->setCommunicationPtr(Communication_ptr);
|
||
//m_task->setCommunication_Type(Communication_Type);
|
||
m_task->initLogic_taskThread(task_thread_name, station_name, m_array, algorithm_Module);
|
||
m_task->setMutex(m_showimage_mutex, write_data_mutex);
|
||
//m_task->taskTimeout= algorithmlist.at(val).toObject().value("算法超时时间").toString().toInt();
|
||
task_thread_list.append(m_task);
|
||
station_task_thread_list.append(m_task);
|
||
|
||
QJsonObject algorithm_obj = algorithmlist.at(val).toObject();
|
||
QString algorithm_key = algorithm_obj.value("算法名称").toString();
|
||
QJsonArray trigger_conditions = algorithm_obj.value("检测条件总表").toArray();
|
||
//QString if_always_run = algorithm_obj.value("if_always_run").toString();
|
||
|
||
//开始信号
|
||
//QJsonArray trigger_algorithm_startpos_array = algorithm_obj.value("trigger_algorithm_startpos").toArray();
|
||
for (int m = 0; m < trigger_conditions.size(); m++)
|
||
{
|
||
//图片检测信息加入,即检测模块需要哪些输入图片
|
||
QJsonObject trigger_condition = trigger_conditions.at(m).toObject();
|
||
QJsonArray triggerCondition = trigger_condition.value("检测条件列表").toArray();
|
||
|
||
//触发信号判断,有触发信号的,放到工站的Logic_myiothread,信号满足执行;
|
||
//例:PLC发信号要结果,收到信号进行汇总
|
||
|
||
//方式一:
|
||
for (int val = 0; val < triggerCondition.size(); val++)
|
||
{
|
||
QJsonObject task_obj = triggerCondition.at(val).toObject();
|
||
|
||
//是否为信号触发的,汇总算法
|
||
bool ifsignalIntegration = false;
|
||
if (task_obj.contains("and"))
|
||
{
|
||
QJsonArray and_condition = task_obj.value("and").toArray();
|
||
int m_index = 0;
|
||
bool check_flag = false;
|
||
|
||
QVector<QString > queue_vector_sn;
|
||
Logic_hdataintegration* dataIntegrationThread = new Logic_hdataintegration();
|
||
QMutex* my_mutex = new QMutex;
|
||
//传递参数
|
||
dataIntegrationThread->setSavePath(m_savePath);
|
||
dataIntegrationThread->setMutex(my_mutex);
|
||
dataIntegrationThread->setStationKey(station_name);
|
||
dataIntegrationThread->setAlgorithmKey(algorithm_key);
|
||
dataIntegrationThread->setTriggerCondition(triggerCondition);
|
||
for (int iii = 0; iii < and_condition.size(); iii++)
|
||
{
|
||
QJsonObject task_obj = and_condition.at(iii).toObject();
|
||
QString trigger_signal = task_obj.value("汇总-触发信号").toString();
|
||
if (!trigger_signal.isEmpty() )//&& !if_always_run.contains("true"))
|
||
{
|
||
station_trigger_signal_dataIntegrationThread_list.append(dataIntegrationThread);
|
||
dataIntegrationThread->m_trigger_signal = trigger_signal;
|
||
dataIntegrationThread->m_trigger_back_signal= task_obj.value("汇总-回复信号").toString();
|
||
dataIntegrationThread->maxCheckTime= task_obj.value("汇总-最大检查时间").toString().toInt();
|
||
ifsignalIntegration = true;
|
||
//alwaysRun_Logic_hdataintegration.append(dataIntegrationThread);
|
||
//break;
|
||
}
|
||
QString summary_stationKey = task_obj.value("汇总-工站名称").toString();
|
||
if (!summary_stationKey.isEmpty())
|
||
if(!m_task->summary_stationKeyS.contains(summary_stationKey))
|
||
m_task->summary_stationKeyS.enqueue(summary_stationKey);
|
||
}
|
||
//参数:配置文件的“stationList”子项;
|
||
//因为需要配置文件的一段内容,故单独初始化
|
||
dataIntegrationThread->init(station_obj);
|
||
|
||
dataIntegrationThread->set_Logic_taskthread(m_task);
|
||
|
||
/*if(if_always_run.contains("true"))
|
||
alwaysRun_Logic_hdataintegration.append(dataIntegrationThread);*/
|
||
dataIntegrationThread_list.append(dataIntegrationThread);
|
||
|
||
//检测算法
|
||
if (ifsignalIntegration == false)
|
||
{
|
||
station_camera_dataIntegrationThread_list.append(dataIntegrationThread);
|
||
//alwaysRun_Logic_hdataintegration.append(dataIntegrationThread);
|
||
}
|
||
}
|
||
else if (task_obj.contains("or"))
|
||
{
|
||
QJsonArray or_condition = task_obj.value("or").toArray();
|
||
int m_index = 0;
|
||
bool check_flag = false;
|
||
|
||
QVector<QString > queue_vector_sn;
|
||
for (int iii = 0; iii < or_condition.size(); iii++)
|
||
{
|
||
QJsonObject task_obj = or_condition.at(iii).toObject();
|
||
|
||
Logic_hdataintegration* dataIntegrationThread = new Logic_hdataintegration();
|
||
QMutex* my_mutex = new QMutex;
|
||
//传递参数
|
||
dataIntegrationThread->setSavePath(m_savePath);
|
||
dataIntegrationThread->setMutex(my_mutex);
|
||
dataIntegrationThread->setStationKey(station_name);
|
||
dataIntegrationThread->setAlgorithmKey(algorithm_key);
|
||
dataIntegrationThread->setTriggerCondition(triggerCondition);
|
||
|
||
QString trigger_key = task_obj.value("检测/汇总-触发条件").toString();
|
||
if (!trigger_key.isEmpty())
|
||
dataIntegrationThread->cameraKey = trigger_key;
|
||
//参数:配置文件的“stationList”子项;
|
||
//因为需要配置文件的一段内容,故单独初始化
|
||
dataIntegrationThread->init(station_obj);
|
||
|
||
dataIntegrationThread->set_Logic_taskthread(m_task);
|
||
|
||
/*if(if_always_run.contains("true"))
|
||
alwaysRun_Logic_hdataintegration.append(dataIntegrationThread);*/
|
||
dataIntegrationThread_list.append(dataIntegrationThread);
|
||
|
||
//检测算法
|
||
if (ifsignalIntegration == false)
|
||
{
|
||
station_camera_dataIntegrationThread_list.append(dataIntegrationThread);
|
||
//alwaysRun_Logic_hdataintegration.append(dataIntegrationThread);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
int task_thread_num = algorithmlist.size();
|
||
QList<Logic_camera_thread*> station_camerathreadlist;
|
||
// 创建相机线程
|
||
QJsonArray cameramodulelist = station_obj.value("相机模块列表").toArray();
|
||
QString read_sn_model = station_obj.value("read_sn_model").toString();
|
||
for (auto val = 0; val < cameramodulelist.size(); val++)
|
||
{
|
||
QJsonObject camera_obj = cameramodulelist.at(val).toObject();
|
||
//相机SN或usrID
|
||
QString camera_key = camera_obj.value("相机key").toString();
|
||
// 设置 MyModule
|
||
MyModule camera_Module = getModule(camera_key); // 获取加载好的module
|
||
|
||
// 创建相机线程
|
||
Logic_camera_thread* m_thread = new Logic_camera_thread(this);
|
||
if (!read_sn_model.isEmpty())
|
||
{
|
||
m_thread->setReadSnModel(read_sn_model);
|
||
}
|
||
//传递参数
|
||
m_thread->setSaveImage(saveImage);
|
||
//设置相机key、取图超时、模块、拍照面描述、触发方式
|
||
m_thread->setModule(station_name, camera_Module, camera_obj);
|
||
m_thread->setMutex(m_showimage_mutex); // 设置锁;所有任务、相机的,传到界面显示的图像数据,都用同一个锁
|
||
m_thread->finstcp_ptr = finstcp_ptr;
|
||
m_thread->m_iomutex = m_io_mutex;
|
||
|
||
//没有检测任务,显示原图
|
||
if (task_thread_num < 1)
|
||
m_thread->ifshowSoureImg = true;
|
||
|
||
//注意,一个相机只对应在一个检测算法里
|
||
for (int s = 0; s < station_camera_dataIntegrationThread_list.size(); s++)
|
||
//判断是否包含该相机
|
||
if (station_camera_dataIntegrationThread_list[s]->cameraKey == m_thread->m_name)
|
||
m_thread->adddataIntegrationThread(station_camera_dataIntegrationThread_list[s]);
|
||
|
||
station_camerathreadlist.append(m_thread); //iothread运行相机,processtask判断相机状态,两边都在用,会不会有问题
|
||
|
||
camerathreadlist.append(m_thread);
|
||
//connect(m_thread, &Logic_camera_thread::mvDisconnect, [=]() {
|
||
// emit mvDisconnect(station_name + "_" + camera_key, m_thread->m_Module.module_ptr->status);//0106 相机断连弹窗
|
||
// });
|
||
}
|
||
|
||
///创建IO线程,通信,即设备信息交互
|
||
if (station_obj.contains("批量读指令"))
|
||
{
|
||
Logic_myiothread* m_IoThread = new Logic_myiothread;
|
||
//传递参数
|
||
m_IoThread->setIOMutex(m_io_mutex, write_data_mutex);
|
||
m_IoThread->setStationKey(station_name);
|
||
//m_IoThread->setStationObject(taskObj);
|
||
m_IoThread->setCommunicationPtr(finstcp_ptr);
|
||
|
||
if (station_trigger_signal_dataIntegrationThread_list.size() > 0)
|
||
m_IoThread->station_trigger_signal_dataIntegrationThread_list = station_trigger_signal_dataIntegrationThread_list;
|
||
|
||
// 设置IOThread的读取指令
|
||
QString readAll_cmd = station_obj.value("批量读指令").toString();
|
||
QString restSignal = station_obj.value("复位信号").toString();
|
||
QString restSignalBack = station_obj.value("复位信号-收到后回复").toString();
|
||
if (!readAll_cmd.isEmpty())
|
||
{
|
||
m_IoThread->setReadAllCmd(readAll_cmd, restSignal, restSignalBack);
|
||
m_IoThread->setTriggerCmd(station_obj);
|
||
}
|
||
|
||
//m_IoThread->setCommunication_Type(Communication_Type);
|
||
//connect(m_IoThread, &Logic_myiothread::SendStationLabel, this, &Logic_processtask::SendStationLabel);
|
||
//m_IoThread->set_dataIntegrationThread_list(station_camera_dataIntegrationThread_list);
|
||
m_IoThread->set_camerathreadlist(station_camerathreadlist);
|
||
m_IoThread->station_task_thread_list = station_task_thread_list;
|
||
if (!readAll_cmd.isEmpty())
|
||
Logic_myiothreadList.append(m_IoThread);
|
||
|
||
}
|
||
}
|
||
|
||
//心跳线程
|
||
QString heartCmd = tcplist_array.first().toObject().value("心跳指令").toString();
|
||
QString restSignal = tcplist_array.first().toObject().value("复位信号").toString();
|
||
QString restSignalBack = tcplist_array.first().toObject().value("复位信号-收到后回复").toString();
|
||
if (!heartCmd.isEmpty() || !restSignal.isEmpty())
|
||
{
|
||
Logic_myiothread* m_IoThread = new Logic_myiothread;
|
||
m_IoThread->setIOMutex(m_io_mutex, write_data_mutex);
|
||
//心跳线程无用
|
||
m_IoThread->setCommunicationPtr(finstcp_ptr);
|
||
//设置心跳指令
|
||
m_IoThread->setheartCmd(heartCmd);
|
||
QString mheartTime = tcplist_array.first().toObject().value("心跳间隔时间").toString();
|
||
//设置时间
|
||
m_IoThread->setheartTime(mheartTime);
|
||
//设置通信类型,"Heart"执行心跳
|
||
m_IoThread->setType("Heart");
|
||
|
||
// 设置IOThread的读取指令
|
||
QString readAll_cmd = tcplist_array.first().toObject().value("批量读指令").toString();
|
||
QString programNum= tcplist_array.first().toObject().value("程序号").toString();
|
||
QString programNumBack = tcplist_array.first().toObject().value("程序号-收到后回复").toString();
|
||
if (!restSignal.isEmpty())
|
||
{
|
||
//m_IoThread->setReadAllCmd(readAll_cmd, emergencystop_signal);
|
||
m_IoThread->setOverAllCmd(readAll_cmd, restSignal, programNum, restSignalBack, programNumBack);
|
||
}
|
||
|
||
Logic_myiothreadList.append(m_IoThread);
|
||
Logic_myiothread_heart = m_IoThread;
|
||
}
|
||
}
|
||
catch (QString ev) {
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
||
return false;
|
||
}
|
||
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " camerathreadlist size:" << camerathreadlist.size();
|
||
return true;
|
||
}
|
||
|
||
// 获取 相处对应的 任务模块名称
|
||
MyModule Logic_processtask::getModule(QString m_key)
|
||
{
|
||
MyModule m_mod;
|
||
for (int i = 0; i < module_list.size(); i++)
|
||
{
|
||
QString key = module_list.at(i).module_key;
|
||
QString moduleName = module_list.at(i).moduleName;
|
||
if (key == m_key)
|
||
return module_list.at(i);
|
||
}
|
||
return m_mod;
|
||
}
|
||
|
||
bool Logic_processtask::startTask()
|
||
{
|
||
if (isStartThread == true)
|
||
return true;
|
||
// init
|
||
if (initTcpModule())
|
||
{
|
||
qDebug() << __FUNCTION__ << __LINE__ << " initTcpModule success!";
|
||
setInitFlag(1);
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << __LINE__ << " initTcpModule failed!";
|
||
setInitFlag(0);
|
||
return false;
|
||
}
|
||
|
||
Sleep(100);
|
||
|
||
// 创建线程
|
||
if (createModuleThread())
|
||
{
|
||
qDebug() << __FUNCTION__ << __LINE__ << " createModuleThread success!";
|
||
setCreateThread(1);
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << __LINE__ << " createAllModuleThread failed!";
|
||
setCreateThread(0);
|
||
return false;
|
||
}
|
||
|
||
Sleep(100);
|
||
|
||
// 设置本地保存文件夹的路径
|
||
saveImageDir = programObj.value("保存图片总文件夹").toString();
|
||
if (!saveImageDir.isEmpty())
|
||
{
|
||
for (int i = 0; i < camerathreadlist.size(); i++)
|
||
{
|
||
camerathreadlist.at(i)->setSaveImage(saveImageDir);
|
||
}
|
||
}
|
||
|
||
|
||
//开启IO线程
|
||
foreach(Logic_myiothread * m_thread, Logic_myiothreadList)
|
||
{
|
||
m_thread->CommunicationProcessLevel = CommunicationProcessLevel;
|
||
if (ifAddCmdHead == false)
|
||
{
|
||
m_thread->CmdHeadShort = "";
|
||
m_thread->CmdHeadString = "";
|
||
}
|
||
m_thread->ifAddCmdHead = ifAddCmdHead;
|
||
qDebug() << "[INFO] " << " start io thread!";
|
||
m_thread->start();
|
||
}
|
||
|
||
// 开启所有相机线程
|
||
foreach(Logic_camera_thread * m_thread, camerathreadlist)
|
||
{
|
||
m_thread->CommunicationProcessLevel = CommunicationProcessLevel;
|
||
if (ifAddCmdHead == false)
|
||
{
|
||
m_thread->CmdHeadShort = "";
|
||
m_thread->CmdHeadString = "";
|
||
}
|
||
m_thread->ifAddCmdHead = ifAddCmdHead;
|
||
if (m_thread->getType().contains("algorithm"))
|
||
{
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " name:" << m_thread->getName();
|
||
//m_thread->start(); // 运行所有线程
|
||
}
|
||
else if (m_thread->getType().contains("camera"))// && !m_thread->getTriggerModel().contains("software"))
|
||
{
|
||
if (CommunicationProcessLevel == 1)
|
||
m_thread->getImgNums = 1;
|
||
m_thread->start();
|
||
}
|
||
}
|
||
|
||
//// 开启黑匣子线程
|
||
//foreach(Logic_hdataintegration* m_thread, dataIntegrationThread_list)
|
||
//{
|
||
// m_thread->start();
|
||
//}
|
||
|
||
// 开启任务线程
|
||
foreach(Logic_taskthread * m_thread, task_thread_list)
|
||
{
|
||
if (ifAddCmdHead == false)
|
||
{
|
||
m_thread->CmdHeadShort = "";
|
||
m_thread->CmdHeadString = "";
|
||
}
|
||
m_thread->CommunicationProcessLevel = CommunicationProcessLevel;
|
||
m_thread->ifAddCmdHead = ifAddCmdHead;
|
||
m_thread->start();
|
||
}
|
||
/*foreach(Logic_mysaveimagethread* savethread, mySaveImageThread_list)
|
||
{
|
||
savethread->start();
|
||
}*/
|
||
|
||
isStartThread = true;
|
||
allfuture1Num = 0;
|
||
future1[allfuture1Num++] = QtConcurrent::run(this, &Logic_processtask::thread_checkAll_Status); //检查所有模块状态
|
||
if (alwaysRun_Logic_hdataintegration.size() > 0)
|
||
future1[allfuture1Num++] = QtConcurrent::run(this, &Logic_processtask::thread_Logic_hdataintegration);
|
||
|
||
static int DeleteDay = -1; //
|
||
if (programObj.value("数据保存天数").toString().toInt() > 0)
|
||
DeleteDay = programObj.value("数据保存天数").toString().toInt();
|
||
static int DeleteLogDay = -1; //日志保存天数
|
||
if (programObj.value("日志保存天数").toString().toInt() > 0)
|
||
DeleteLogDay = programObj.value("日志保存天数").toString().toInt();
|
||
|
||
QDir dir = QApplication::applicationDirPath();
|
||
LogPath = dir.path()+"/log";
|
||
DeleteFileTimer = new QTimer;
|
||
connect(DeleteFileTimer, &QTimer::timeout, this, [=]() {
|
||
//QString tmpSrc = "D:/SrcImage";
|
||
//QString tmpResult = "D:/ResultImage";
|
||
//FindFileForDelete(tmpSrc, DeleteDay);
|
||
//FindFileForDelete(tmpResult, DeleteDay);
|
||
if (deleteingImg == false)
|
||
{
|
||
deleteingImg = true;
|
||
QFuture<bool> myDeleteF = QtConcurrent::run(this, &Logic_processtask::FindFileForDelete, saveImageDir, DeleteDay);
|
||
}
|
||
if (deleteingLog == false)
|
||
{
|
||
deleteingLog = true;
|
||
QFuture<bool> myDeleteF = QtConcurrent::run(this, &Logic_processtask::FindFileForDelete, LogPath, DeleteLogDay);
|
||
}
|
||
|
||
//FindFileForDelete(saveImageDir, DeleteDay);
|
||
});
|
||
DeleteFileTimer->start(1000 * 3600);//1小时
|
||
|
||
|
||
isStartThread = true;
|
||
allfuture1Num = 0;
|
||
future1[allfuture1Num++] = QtConcurrent::run(this, &Logic_processtask::thread_reciveImageData);
|
||
int threadCount = 2 * QThread::idealThreadCount();
|
||
QThreadPool::globalInstance()->setMaxThreadCount(threadCount);
|
||
|
||
return true;
|
||
}
|
||
|
||
bool Logic_processtask::initTcpModule()
|
||
{
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " initTcpModule start!";
|
||
try{
|
||
if (init_flag == 1)
|
||
{
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " Tcp module inited!";
|
||
return true;
|
||
}
|
||
|
||
std::vector<cv::Mat> vec_mat;
|
||
QJsonObject finstcp_obj = tcplist_array.first().toObject();
|
||
QString ip_port = finstcp_obj.value("通信初始化").toString();
|
||
if (ip_port.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << "line:" << __LINE__ << " ip_port is null ! ";
|
||
return false;
|
||
}
|
||
QByteArray mm_byte = ip_port.toUtf8();
|
||
if (!finstcp_ptr->writeData(vec_mat, mm_byte))
|
||
{
|
||
qCritical() << __FUNCTION__ << "line:" << __LINE__ << " finstcp reciveData error!";
|
||
return false;
|
||
}
|
||
|
||
//m_IoThread->setFinsTcpPtr(finstcp_ptr);
|
||
|
||
// 参数初始化 ,传递finstcp_sleeptime,和finstcp_readnum的值到finstcp中进行初始化
|
||
QByteArray init_byte;
|
||
QString finstcp_sleeptime = finstcp_obj.value("通信睡眠时间").toString();
|
||
QString finstcp_readnum = finstcp_obj.value("批量读指令").toString(); //读取包的数量
|
||
QString finstcp_writeType = finstcp_obj.value("写入类型").toString(); // 写入类型 ushort 还是 short
|
||
if ((finstcp_sleeptime.isEmpty() || finstcp_readnum.isEmpty()) && ifAddCmdHead == true)
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " finstcp_sleeptime or finstcp_readnum is null!";
|
||
}
|
||
else
|
||
{
|
||
QStringList list = finstcp_readnum.split("_");
|
||
if (list.size() < 3 && ifAddCmdHead==true)
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " list size is error! size :" << list.size();
|
||
}
|
||
else
|
||
{
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " list:" << list;
|
||
QString initdata="";
|
||
initdata = "sleeptime_" + finstcp_sleeptime + "_readnum_" + list[list.size()-1] + "_writetype_" + finstcp_writeType;
|
||
init_byte = initdata.toLocal8Bit();
|
||
}
|
||
}
|
||
|
||
// 初始化finstcp
|
||
if (finstcp_ptr->initParas(init_byte))
|
||
{
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " finstcp initParas success!";
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << __LINE__ << " finstcp initParas failed!";
|
||
return false;
|
||
}
|
||
|
||
// 写清除信号 20230113
|
||
//initClearSignal();
|
||
|
||
//m_IoThread->start();
|
||
//m_readThread->start();
|
||
}
|
||
catch (QString ev)
|
||
{
|
||
qDebug() << __FUNCTION__ << __LINE__ << " ev:" << ev;
|
||
}
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " initTcpModule success! ";
|
||
return true;
|
||
}
|
||
|
||
void Logic_processtask::initClearSignal()
|
||
{
|
||
std::vector<cv::Mat> vec_mat;
|
||
QString m_signal = "write_short_D3032_3_000";
|
||
QByteArray m_byte = m_signal.toUtf8();
|
||
if (!finstcp_ptr->writeData(vec_mat, m_byte))
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " finstcp_ptr reciveData error! ";
|
||
}
|
||
|
||
m_signal = "write_short_D3082_7_0000000";
|
||
m_byte = m_signal.toUtf8();
|
||
if (!finstcp_ptr->writeData(vec_mat, m_byte))
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " finstcp_ptr reciveData error! ";
|
||
}
|
||
}
|
||
|
||
QJsonArray Logic_processtask::getCameraConfigArray(QString key)
|
||
{
|
||
QJsonArray array;
|
||
|
||
for (auto val = 0; val < m_cameramodulelist.size(); val++)
|
||
{
|
||
QJsonObject obj = m_cameramodulelist.at(val).toObject();
|
||
QString camera_key = obj.value("相机key").toString();
|
||
if (key == camera_key)
|
||
{
|
||
if (!obj.value("相机参数").toArray().isEmpty())
|
||
array = obj.value("相机参数").toArray();
|
||
}
|
||
}
|
||
return array;
|
||
}
|
||
//"相机参数值",封装为3+数据长度+数据内容;
|
||
QByteArray Logic_processtask::initCameraParam(QString key, QJsonArray array)
|
||
{
|
||
if (key.isEmpty())
|
||
return "";
|
||
|
||
std::string sendMsg;
|
||
|
||
// 添加相机名称
|
||
int type = 3; // int
|
||
std::string m_type;
|
||
m_type.insert(0, 4, '1');
|
||
memcpy((void*)m_type.c_str(), (void*)&type, 4);
|
||
sendMsg.append(m_type);
|
||
|
||
// len
|
||
int len = key.length();
|
||
std::string m_len;
|
||
m_len.insert(0, 4, '1');
|
||
memcpy((void*)m_len.c_str(), (void*)&len, 4);
|
||
sendMsg.append(m_len);
|
||
|
||
// 相机名称
|
||
std::string m_key = key.toStdString();
|
||
sendMsg.append(key.toStdString());
|
||
|
||
//添加数据
|
||
for (int i = 0; i < array.size(); i++)
|
||
{
|
||
QJsonObject m_obj = array.at(i).toObject();
|
||
QString value = m_obj.value("相机参数值").toString();
|
||
if (value.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << "line:" << __LINE__ << " value is null!";
|
||
return "";
|
||
}
|
||
|
||
int type = 3; // int
|
||
std::string _type;
|
||
_type.insert(0, 4, '1');
|
||
memcpy((void*)_type.c_str(), (void*)&type, 4);
|
||
sendMsg.append(_type);
|
||
|
||
// len
|
||
int data_len = value.length();
|
||
std::string _len;
|
||
_len.insert(0, 4, '1');
|
||
memcpy((void*)_len.c_str(), (void*)&data_len, 4);
|
||
sendMsg.append(_len);
|
||
|
||
// data
|
||
sendMsg.append(value.toStdString());
|
||
}
|
||
|
||
QByteArray byteArray(sendMsg.c_str(), sendMsg.length());
|
||
return byteArray;
|
||
}
|
||
|
||
//遍历自动删除旧数据
|
||
bool Logic_processtask::FindFileForDelete(const QString& path, int DeleteDay)
|
||
{
|
||
QDir dir(path);
|
||
if (!dir.exists())
|
||
return false;
|
||
dir.setFilter(QDir::Dirs | QDir::Files);
|
||
dir.setSorting(QDir::DirsFirst);
|
||
QFileInfoList list = dir.entryInfoList();
|
||
int i = 0;
|
||
do {
|
||
|
||
QFileInfo fileInfo = list.at(i);
|
||
if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
|
||
{
|
||
i++;
|
||
continue;
|
||
}
|
||
bool bisDir = fileInfo.isDir();
|
||
if (bisDir)
|
||
{
|
||
|
||
//qDebug() << qPrintable(QString("%1 %2 %3").arg(fileInfo.size(), 10)
|
||
// .arg(fileInfo.fileName(), 10).arg(fileInfo.path())) << endl;
|
||
|
||
FindFileForDelete(fileInfo.filePath(), DeleteDay);
|
||
//dir.rmdir(dir.absolutePath());
|
||
}
|
||
else {
|
||
//如果是文件,判断文件日期 目前默认是30天。
|
||
|
||
QDateTime delDateTime = QDateTime::currentDateTime().addDays(-DeleteDay);
|
||
//QString tmpDate = delDateTime.toString("yyyy-MM-dd-hh-mm-ss-zzz");
|
||
qint64 nSecs = delDateTime.secsTo(fileInfo.lastModified()); //birthTime
|
||
QString mfileName=fileInfo.fileName();
|
||
if (nSecs < 0)
|
||
{
|
||
|
||
//qDebug() << qPrintable(QString("%1 %2 %3").arg(fileInfo.size(), 10)
|
||
// .arg(fileInfo.fileName(), 10).arg(fileInfo.path())) << endl;
|
||
//删除30天前的文件
|
||
fileInfo.dir().remove(fileInfo.fileName());
|
||
}
|
||
|
||
}
|
||
i++;
|
||
Sleep(20);
|
||
qApp->processEvents();
|
||
} while (i < list.size());
|
||
if (path == saveImageDir)deleteingImg = false;
|
||
else if(path == LogPath)deleteingLog = false;
|
||
return true;
|
||
|
||
}
|
||
|
||
void Logic_processtask::setLoadFlag(int index)
|
||
{
|
||
load_flag = index;
|
||
}
|
||
|
||
void Logic_processtask::setInitFlag(int index)
|
||
{
|
||
init_flag = index;
|
||
}
|
||
|
||
void Logic_processtask::setRunFlag(int index)
|
||
{
|
||
run_flag = index;
|
||
}
|
||
//退出所有类、释放模块
|
||
bool Logic_processtask::exitTask()
|
||
{
|
||
isStartThread = false;
|
||
for (int i = 0; i < 200; i++)
|
||
{
|
||
Sleep(1);
|
||
// qApp->processEvents();
|
||
}
|
||
|
||
if (ifChecked == false)
|
||
return true;
|
||
|
||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " ---start";
|
||
try{
|
||
//if (m_IoThread)
|
||
//{
|
||
// m_IoThread->setRunFlag(false); // 设置停止运行;
|
||
// m_IoThread->wait();
|
||
// m_IoThread->quit();
|
||
// delete m_IoThread;
|
||
// m_IoThread = nullptr;
|
||
//}
|
||
|
||
for (int i = 0; i < Logic_myiothreadList.size(); i++)
|
||
{
|
||
Logic_myiothreadList.at(i)->setRunFlag(false); // 设置停止运行;
|
||
Sleep(10);
|
||
if (finstcp_ptr->status == true)
|
||
Logic_myiothreadList.at(i)->wait();
|
||
Logic_myiothreadList.at(i)->quit();
|
||
Logic_myiothreadList[i]->deleteLater();
|
||
Logic_myiothreadList[i] = nullptr;
|
||
}
|
||
Logic_myiothreadList.clear();
|
||
|
||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " clear m_IoThread success! ";
|
||
Sleep(100);
|
||
|
||
// 清理线程
|
||
for (int i = 0; i < camerathreadlist.size(); i++)
|
||
{
|
||
camerathreadlist[i]->ifRun = false;
|
||
Sleep(10);
|
||
if (finstcp_ptr->status == true)
|
||
camerathreadlist[i]->wait();
|
||
Sleep(1);
|
||
delete camerathreadlist[i];
|
||
camerathreadlist[i] = nullptr;
|
||
}
|
||
camerathreadlist.clear();
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " clear camerathreadlist success ! ";
|
||
|
||
if (finstcp_ptr)
|
||
{
|
||
destory_finstcp(finstcp_ptr);
|
||
finstcp_ptr = nullptr;
|
||
}
|
||
/*for (int i = 0; i < Communication_ptr.size(); i++)
|
||
{
|
||
if (Communication_ptr[i])
|
||
{
|
||
destory_Communication[i](Communication_ptr[i]);
|
||
}
|
||
}*/
|
||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " clear finstcp_ptr success! ";
|
||
|
||
// 数据处理中心
|
||
for (auto val = 0; val < dataIntegrationThread_list.size(); val++)
|
||
{
|
||
if (dataIntegrationThread_list.at(val))
|
||
{
|
||
delete dataIntegrationThread_list.at(val);
|
||
}
|
||
}
|
||
dataIntegrationThread_list.clear();
|
||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " clear dataIntegrationThread_list success ! ";
|
||
// 任务线程
|
||
for (auto val = 0; val < task_thread_list.size(); val++)
|
||
{
|
||
if (task_thread_list.at(val))
|
||
{
|
||
task_thread_list.at(val)->setRunFlag(false);
|
||
delete task_thread_list.at(val);
|
||
}
|
||
}
|
||
task_thread_list.clear();
|
||
|
||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " clear task_thread_list success ! ";
|
||
/*for (auto val = 0; val < mySaveImageThread_list.size(); val++)
|
||
{
|
||
if (mySaveImageThread_list.at(val))
|
||
{
|
||
mySaveImageThread_list.at(val)->setSaveimageflag(false);
|
||
delete mySaveImageThread_list.at(val);
|
||
}
|
||
}
|
||
mySaveImageThread_list.clear(); */
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " clear mySaveImageThread_list success ! ";
|
||
//// 相机线程
|
||
// for (int i = 0; i < camerathreadlist.size(); i++)
|
||
// {
|
||
// if (threadlist.at(i))
|
||
// {
|
||
// threadlist.at(i)->setRunFlag(false);
|
||
// delete threadlist.at(i);
|
||
// }
|
||
// }
|
||
// threadlist.clear();
|
||
|
||
// 模块清理指针
|
||
for (int i = 0; i < module_list.size(); i++)
|
||
{
|
||
QString nnnn_name = module_list.at(i).moduleName;
|
||
Base* base_ptr = const_cast<Base*>(module_list.at(i).module_ptr);
|
||
module_list.at(i).destory_ptr(base_ptr);
|
||
}
|
||
module_list.clear();
|
||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " clear module_list success ! ";
|
||
|
||
// 清理 station_list
|
||
while (!station_list.isEmpty())
|
||
{
|
||
QString m_key = station_list.firstKey();
|
||
delete station_list.first();
|
||
station_list.remove(m_key);
|
||
}
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " clear station_list success ! ";
|
||
}catch (QString ev){
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
||
return false;
|
||
}
|
||
qDebug() << __FUNCTION__ << "line:" << __LINE__ << " clearTask success! ";
|
||
return true;
|
||
}
|
||
|
||
// 加载所有模块;初始化所有模块
|
||
bool Logic_processtask::loadAllModule()
|
||
{
|
||
if (load_flag == 1)
|
||
{
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " all module loaded! ";
|
||
return true;
|
||
}
|
||
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " loadAllModule start! ";
|
||
|
||
m_savePath = programObj.value("保存图片总文件夹").toString();
|
||
QJsonArray tcpArray = programObj.value("通信列表").toArray();
|
||
QJsonObject finstcpObj = getFinsTcpObject(tcpArray);
|
||
QString finstcp_module = finstcpObj.value("模块名称").toString();
|
||
if (finstcpObj.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " finstcpObj is null ! ";
|
||
return false;
|
||
}
|
||
|
||
// 加载通信模块,通信模块不开线程
|
||
if (!loadModule("finstcp", finstcpObj, ""))
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " load finstcp module failed! ";
|
||
return false;
|
||
}
|
||
|
||
// 加载所有的相机模块和算法模块
|
||
if (!loadCameraAndAlgorithmModule(stationlist_array))
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " loadCameraAndAlgorithmModule is failed!";
|
||
return false;
|
||
}
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " loadAllModule success!";
|
||
|
||
// 初始化所有模块
|
||
if (!initStationsModule())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line: " << __LINE__ << " initStationsModule is error! ";
|
||
return false;
|
||
}
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " initParas module list success!";
|
||
return true;
|
||
}
|
||
|
||
// 加载所有的相机模块和算法模块
|
||
bool Logic_processtask::loadCameraAndAlgorithmModule(QJsonArray stationlist_array)
|
||
{
|
||
for (int i = 0; i < stationlist_array.size(); i++)
|
||
{
|
||
QJsonObject station_obj = stationlist_array.at(i).toObject();
|
||
QString station_key = station_obj.value("工站名").toString();
|
||
|
||
HStationData* stationPtr = new HStationData;
|
||
stationPtr->setStationKey(station_key);
|
||
|
||
// 根据sn区域设置,创建容器
|
||
QJsonArray sn_array = station_obj.value("SN流程列表").toArray();
|
||
QQueue<QString> indexList;
|
||
for (auto sss = 0; sss < sn_array.size(); sss++)
|
||
{
|
||
QJsonObject obj = sn_array.at(sss).toObject();
|
||
QString snOKCmd = obj.value("读SN正常-回复指令").toString();
|
||
QJsonObject clearData = obj.value("清理容器数据").toObject();
|
||
|
||
QString channel_index = clearData.value("通道序号").toString();
|
||
if (!indexList.contains(channel_index) && !channel_index.isEmpty())
|
||
{
|
||
indexList.append(channel_index);
|
||
HSnData* sndata = new HSnData;
|
||
stationPtr->appendSnData(sndata);
|
||
}
|
||
}
|
||
|
||
m_cameramodulelist = station_obj.value("相机模块列表").toArray();
|
||
for (auto val = 0; val < m_cameramodulelist.size(); val++)
|
||
{
|
||
QJsonObject camera_obj = m_cameramodulelist.at(val).toObject();
|
||
QString camera_key = camera_obj.value("相机key").toString();
|
||
QString moduleName = camera_obj.value("模块名称").toString(); // 相机模块名
|
||
QString image_queue_num = camera_obj.value("单通道拍照数").toString();
|
||
QString image_channel_num = camera_obj.value("上料通道数").toString();
|
||
QJsonArray read_sn_format = camera_obj.value("获取SN方式列表").toArray();
|
||
QJsonArray init_array= camera_obj.value("相机参数").toArray();
|
||
QString init_string;
|
||
for (int i = 0; i < init_array.size(); i++)
|
||
{
|
||
if (i > 0)
|
||
init_string += "+";
|
||
QJsonObject init_obj = init_array.at(i).toObject();
|
||
QString cfg_name = init_obj.value("参数名").toString();
|
||
init_string += cfg_name;
|
||
QJsonArray cfg_array = init_obj.value("相机参数值").toArray();
|
||
for (int j = 0; j < cfg_array.size(); j++)
|
||
init_string += "#" + cfg_array[j].toString();
|
||
}
|
||
QString addString = "+是否为标准通信流程#" + programObj.value("是否为标准通信流程").toString()+"+取图超时#"+ camera_obj.value("取图超时").toString();
|
||
//数据格式:相机key+曝光#3000#5000#6000+ip#192.168.0.1
|
||
QByteArray init_Byte = camera_key.toLocal8Bit() + "+" + init_string.toLocal8Bit() + addString.toLocal8Bit();
|
||
// 加载相机模块
|
||
if (!loadModule(camera_key, camera_obj, init_Byte))
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " moduleName:" << moduleName << " load camera module failed! ";
|
||
return false;
|
||
}
|
||
|
||
if (image_channel_num.isEmpty())
|
||
{
|
||
image_channel_num = "1";
|
||
}
|
||
|
||
// 此处创建保存图片数据的容器
|
||
HCameraData* cameraPtr = new HCameraData;
|
||
if (!cameraPtr->initCameraVectorQueue(image_channel_num.toInt(), image_queue_num.toInt()))
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " initCameraVectorQueue is error!";
|
||
if (cameraPtr)
|
||
{
|
||
delete cameraPtr;
|
||
cameraPtr = nullptr;
|
||
}
|
||
return false;
|
||
}
|
||
cameraPtr->setCameraKey(camera_key);
|
||
cameraPtr->setSetSnFormat(read_sn_format);
|
||
stationPtr->addCameraData(cameraPtr);
|
||
}
|
||
//读取"存图设置"
|
||
QJsonArray SaveImgArray = programObj.value("存图设置").toArray();
|
||
QString SaveImgString;
|
||
for (int s = 0; s < SaveImgArray.size(); s++)
|
||
{
|
||
/*"Class图": "不保存",
|
||
"Seg图" : "不保存",
|
||
"原图" : "不保存",
|
||
"界面窗口名称" : "工位1-相机1",
|
||
"相机key" : "offline-camera1",
|
||
"结果图" : "保存"*/
|
||
//if (i > 0)
|
||
SaveImgString += "+";
|
||
QJsonObject init_obj = SaveImgArray.at(s).toObject();
|
||
QString cfg_name = init_obj.value("相机key").toString();
|
||
SaveImgString += cfg_name;
|
||
SaveImgString += "#原图_" + init_obj.value("原图").toString();
|
||
SaveImgString += "#Seg图_" + init_obj.value("Seg图").toString();
|
||
SaveImgString += "#Class图_" + init_obj.value("Class图").toString();
|
||
SaveImgString += "#结果图_" + init_obj.value("结果图").toString();
|
||
}
|
||
|
||
//缺陷名称
|
||
QString defectNames;
|
||
QJsonArray defectNamesArray = programObj.value("缺陷类别名称设置").toArray();
|
||
for (int s = 0; s < defectNamesArray.size(); s++)
|
||
{
|
||
defectNames += "+";
|
||
QJsonObject init_obj = defectNamesArray.at(s).toObject();
|
||
QString cfg_name = init_obj.value("缺陷类别").toString();
|
||
defectNames += "缺陷类别_"+cfg_name;
|
||
defectNames += "#缺陷名称_" + init_obj.value("缺陷名称").toString();
|
||
defectNames += "#是否检测_" + init_obj.value("是否检测").toString();
|
||
}
|
||
|
||
|
||
QJsonArray algorithmlist = station_obj.value("算法列表").toArray();
|
||
for (auto val = 0; val < algorithmlist.size(); val++)
|
||
{
|
||
QJsonObject algorithm_obj = algorithmlist.at(val).toObject(); // 算法模块obj
|
||
QString algorithm_key = algorithm_obj.value("算法名称").toString(); // 算法名称
|
||
QString moduleName = algorithm_obj.value("模块名称").toString(); // 算法模块名
|
||
//为空
|
||
QString algorithm_config = algorithm_obj.value("algorithm_config").toString(); // 算法所需要的配置文件路径
|
||
QJsonArray algorithm_init_array = algorithm_obj.value("算法初始化参数").toArray(); // 基准坐标
|
||
//QJsonArray delete_result_index_array = algorithm_obj.value("delete_result_index").toArray(); // 删除算法结果数据
|
||
if (algorithm_key.isEmpty() || moduleName.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " algorithm_key or moduleName is null! ";
|
||
return false;
|
||
}
|
||
QJsonArray init_array = algorithm_obj.value("算法初始化参数").toArray();
|
||
QString init_string;
|
||
for (int i = 0; i < init_array.size(); i++)
|
||
{
|
||
if (i > 0)
|
||
init_string += "+";
|
||
QJsonObject init_obj = init_array.at(i).toObject();
|
||
QString cfg_name = init_obj.value("算法参数名").toString();
|
||
init_string += cfg_name;
|
||
QJsonArray cfg_array = init_obj.value("算法参数值").toArray();
|
||
for (int j = 0; j < cfg_array.size(); j++)
|
||
init_string += "#" + cfg_array[j].toString();
|
||
}
|
||
//数据格式:算法名称+曝光#3000#5000#6000+ip#192.168.0.1
|
||
QByteArray init_Byte = algorithm_key.toLocal8Bit() + "+" + init_string.toLocal8Bit() + SaveImgString.toLocal8Bit()+ defectNames.toLocal8Bit();
|
||
|
||
//数据格式:3+3+数据长度+数据内容
|
||
//QByteArray init_Byte = encodeAlgorithmData(algorithm_init_array, algorithm_config);
|
||
// 加载算法模块;
|
||
if (!loadModule(algorithm_key, algorithm_obj, init_Byte))
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " camerakey load module! ";
|
||
return false;
|
||
}
|
||
// 此处创建保存算法数据的容器
|
||
HAlgorithmData* algorithmPtr = new HAlgorithmData;
|
||
|
||
algorithmPtr->setAlgorithmKey(algorithm_key);
|
||
|
||
HAlgorithmExecData* algorithmExecPtr = new HAlgorithmExecData();
|
||
algorithmExecPtr->setAlgorithmName(algorithm_key);
|
||
QStringList cameraList;
|
||
if (algorithm_obj.value("算法名称").toString() == "检测")
|
||
{
|
||
//检查: 检测条件总表
|
||
QJsonArray checkALLarray = algorithm_obj.value("检测条件总表").toArray();
|
||
for (int yy = 0; yy < checkALLarray.size(); yy++)
|
||
{
|
||
//检查: 检测条件列表
|
||
QJsonArray checkarrayT = checkALLarray[yy].toObject().value("检测条件列表").toArray();
|
||
for (int cca = 0; cca < checkarrayT.size(); cca++)
|
||
{
|
||
QJsonObject checkobj = checkarrayT[cca].toObject();
|
||
//检查: and或者or列表元素
|
||
QJsonArray checkArray = checkobj.value("and").toArray();
|
||
if (checkArray.isEmpty())
|
||
checkArray = checkobj.value("or").toArray();
|
||
for (int cc = 0; cc < checkArray.size(); cc++)
|
||
{
|
||
QJsonObject checkobjKid = checkArray[cc].toObject();
|
||
//检查: 检测/汇总-触发条件
|
||
QString checkString = checkobjKid.value("检测/汇总-触发条件").toString();
|
||
bool ifmatch = false;
|
||
if (!checkString.isEmpty())
|
||
{
|
||
cameraList.append(checkString);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
algorithmPtr->cameraList = cameraList;
|
||
algorithmExecPtr->cameraList = cameraList;
|
||
|
||
stationPtr->addAlgorithmData(algorithmPtr);
|
||
stationPtr->addAlgorithmExecData(algorithmExecPtr);
|
||
}
|
||
|
||
// 添加站中算法数据
|
||
station_list.insert(station_key, stationPtr);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
//数据格式:3+3+数据长度+数据内容
|
||
QByteArray Logic_processtask::encodeAlgorithmData(QJsonArray array, QString algorithm_config)
|
||
{
|
||
QByteArray byte;
|
||
if (array.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << "line:" << __LINE__ << " array is null! ";
|
||
// return byte;
|
||
}
|
||
|
||
if (algorithm_config.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << "line:" << __LINE__ << " algorithm_config is null! ";
|
||
}
|
||
|
||
std::string sendMsg;
|
||
// 添加配置文件的信息
|
||
|
||
// 添加参数个数
|
||
int type = 3; // int
|
||
std::string m_type;
|
||
m_type.insert(0, 4, '1');
|
||
memcpy((void*)m_type.c_str(), (void*)&type, 4);
|
||
sendMsg.append(m_type);
|
||
|
||
// len
|
||
int len = 4;
|
||
std::string m_len;
|
||
m_len.insert(0, 4, '1');
|
||
memcpy((void*)m_len.c_str(), (void*)&len, 4);
|
||
sendMsg.append(m_len);
|
||
|
||
// num
|
||
int num;
|
||
if (algorithm_config.isEmpty())
|
||
num = array.size();
|
||
else
|
||
num = array.size() + 1; // 添加配置文件的信息
|
||
|
||
std::string m_num;
|
||
m_num.insert(0, 4, '1');
|
||
memcpy((void*)m_num.c_str(), (void*)&num, 4);
|
||
sendMsg.append(m_num);
|
||
|
||
if (!algorithm_config.isEmpty())
|
||
{
|
||
int type = 3; //int
|
||
std::string _type;
|
||
_type.insert(0, 4, '1');
|
||
memcpy((void*)_type.c_str(), (void*)&type, 4);
|
||
sendMsg.append(_type);
|
||
|
||
//len
|
||
int data_len = algorithm_config.length();
|
||
std::string _len;
|
||
_len.insert(0, 4, '1');
|
||
memcpy((void*)_len.c_str(), (void*)&data_len, 4);
|
||
sendMsg.append(_len);
|
||
|
||
//data
|
||
sendMsg.append(algorithm_config.toStdString());
|
||
}
|
||
// 添加数据
|
||
for (int i = 0; i < array.size(); i++)
|
||
{
|
||
QJsonObject m_obj = array.at(i).toObject();
|
||
QString value = m_obj.value("value").toString();
|
||
if (value.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << "line:" << __LINE__ << " value is null!";
|
||
return "";
|
||
}
|
||
|
||
int type = 3; // int
|
||
std::string _type;
|
||
_type.insert(0, 4, '1');
|
||
memcpy((void*)_type.c_str(), (void*)&type, 4);
|
||
sendMsg.append(_type);
|
||
|
||
//len
|
||
int data_len = value.length();
|
||
std::string _len;
|
||
_len.insert(0, 4, '1');
|
||
memcpy((void*)_len.c_str(), (void*)&data_len, 4);
|
||
sendMsg.append(_len);
|
||
|
||
//data
|
||
sendMsg.append(value.toStdString());
|
||
}
|
||
QByteArray byteArray(sendMsg.c_str(), sendMsg.length());
|
||
return byteArray;
|
||
}
|
||
//加载单个模块:通信、相机、算法
|
||
bool Logic_processtask::loadModule(QString name, QJsonObject module_obj, QByteArray initByte)
|
||
{
|
||
QString moduleName = module_obj.value("模块名称").toString();
|
||
QString task_thread_index;// = module_obj.value("算法任务序号").toString();
|
||
try
|
||
{
|
||
//QString module_path = JsonPath + "/plugin/"+moduleName; // 暂时做修改
|
||
QString module_path = JsonPath + "/" + moduleName;
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " module_path:" << module_path;
|
||
|
||
if (module_path.contains("#"))
|
||
{
|
||
QStringList myList = module_path.split("#");
|
||
|
||
if (myList.size() == 2)
|
||
{
|
||
QString motherDllString = myList[0] + ".dll";
|
||
QFileInfo motherDll(motherDllString);
|
||
QFileInfo copyDll(module_path + ".dll");
|
||
if (motherDll.exists())
|
||
{
|
||
bool ifDoCopy = false;
|
||
if (copyDll.exists())
|
||
{
|
||
QString motherDllQDateTimeString = motherDll.lastModified().toString("yyyy-MM-dd-hh-mm-ss-zzz");
|
||
QString copyDllQDateTimeString = copyDll.lastModified().toString("yyyy-MM-dd-hh-mm-ss-zzz");
|
||
if (copyDllQDateTimeString != motherDllQDateTimeString)
|
||
{
|
||
ifDoCopy = true;
|
||
QFile(module_path + ".dll").remove();
|
||
//copyDll.remove();
|
||
Sleep(10);
|
||
}
|
||
else ifDoCopy = false;
|
||
}
|
||
else
|
||
ifDoCopy = true;
|
||
if (ifDoCopy == true)
|
||
{
|
||
QFile::copy(motherDllString, module_path + ".dll");
|
||
QFile::copy(myList[0] + ".pdb", module_path + ".pdb");
|
||
qDebug() << "[INFO] " << "copy dll " << module_path + ".dll";
|
||
}
|
||
}
|
||
}
|
||
//if (myList.size() == 2)
|
||
//{
|
||
// //if (myList[1].toInt() > 0)
|
||
// {
|
||
// //命名格式: 模块名#1/2/3...13/14...;如:Hd_AI_Bian_module#1;按此修改配置文件
|
||
// //注意,模块代码修改后,要删除复制的模块,再运行程序;
|
||
// QString path_model = myList[0];// module_path.mid(0, module_path.size() - 1);
|
||
// QFile dllFile(module_path + ".dll");
|
||
// if (dllFile.exists())
|
||
// {
|
||
// dllFile.remove();
|
||
// Sleep(100);
|
||
// }
|
||
// //if (!QFile::exists(module_path + ".dll") && QFile::exists(path_model + ".dll"))
|
||
// {
|
||
// QFile::copy(path_model + ".dll", module_path + ".dll");
|
||
// //QFile::copy(path_model + ".pdb", module_path + ".pdb");
|
||
// qDebug() << "[INFO] " << "copy dll " << module_path;
|
||
// }
|
||
// }
|
||
//}
|
||
}
|
||
|
||
QLibrary* m_lib = new QLibrary(module_path + ".dll");
|
||
lib_list.append(m_lib);
|
||
bool flag = m_lib->load();
|
||
if (!m_lib->isLoaded())
|
||
{
|
||
qDebug() << __FUNCTION__ << __LINE__ << " Loaded error:" << m_lib->errorString();
|
||
delete m_lib;
|
||
m_lib = nullptr;
|
||
return false;
|
||
}
|
||
|
||
if (name == "finstcp")
|
||
{
|
||
if (flag)
|
||
{
|
||
CreateFunction createFun = (CreateFunction)m_lib->resolve("create");
|
||
ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory");
|
||
if (createFun && releaseFun)
|
||
{
|
||
//Base* Communication_ptr1 = nullptr;
|
||
//Communication_ptr1 = createFun();
|
||
//Communication_ptr1->moduleName = moduleName.toStdString(); //为什么报错,访问冲突??
|
||
finstcp_ptr = createFun();
|
||
destory_finstcp = releaseFun;
|
||
//finstcp_ptr->moduleName = moduleName.toStdString();
|
||
//finstcp_ptr = Communication_ptr1;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (flag)
|
||
{
|
||
CreateFunction createFun = (CreateFunction)m_lib->resolve("create");
|
||
ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory");
|
||
if (createFun && releaseFun)
|
||
{
|
||
MyModule m_module;
|
||
m_module.module_key = name;
|
||
m_module.m_initByte = initByte;
|
||
m_module.moduleName = moduleName;
|
||
m_module.module_ptr = createFun();
|
||
m_module.destory_ptr = releaseFun;
|
||
|
||
if (!task_thread_index.isEmpty())
|
||
m_module.task_thread_index = task_thread_index;
|
||
|
||
module_list.append(m_module);
|
||
}
|
||
else
|
||
{
|
||
delete m_lib;
|
||
m_lib = nullptr;
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}catch (QString event){
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " event:" << event;
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
// 停止任务、清理数据
|
||
bool Logic_processtask::stopTask()
|
||
{
|
||
if (!clearStationData())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line: " << __LINE__ << " clearStationData is error!";
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
// 停止任务
|
||
bool Logic_processtask::clearStationData()
|
||
{
|
||
QList<QString> keyList = station_list.keys();
|
||
for (int i = 0; i < keyList.size(); i++)
|
||
{
|
||
HStationData* stationPtr = station_list.value(keyList.at(i));
|
||
if (stationPtr)
|
||
{
|
||
QStringList cameraList;
|
||
cameraList.append("all");
|
||
for(int i=0;i<10;i++)
|
||
stationPtr->clearData("all", cameraList);
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << " line: " << __LINE__ << " i:" << i << " stationPtr is null!";
|
||
return false;
|
||
}
|
||
}
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " clear Station data succeeded! ";
|
||
return true;
|
||
}
|
||
|
||
bool Logic_processtask::decodePackage(QString name, cv::Mat mtx)
|
||
{
|
||
try
|
||
{
|
||
if (!mtx.empty()){
|
||
//if ((mtx.dataend - mtx.datastart) < mtx.cols * mtx.rows * mtx.channels()) //尝试能否解决mtx内容异常问题
|
||
//{
|
||
// qCritical() << __FUNCTION__ << " line:" << __LINE__ << " mtx.dataend - mtx.datastart err!!:";
|
||
// return false;
|
||
//}
|
||
|
||
QMutexLocker locker(LsendImgStructMutex); // 加锁
|
||
LsendImgStruct mL;
|
||
mL.send_Mat = mtx;
|
||
mL.cameraKey_index = name;
|
||
|
||
Lqueue_sendImgStruct.enqueue(mL);
|
||
}
|
||
}
|
||
catch (QString ev)
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
bool Logic_processtask::getImageData(QQueue< LsendImgStruct>& my_LsendImgStruct)
|
||
{
|
||
QMutexLocker locker(LsendImgStructMutex); // 加锁
|
||
if (Lqueue_sendImgStruct.size() > 0)
|
||
{
|
||
my_LsendImgStruct.append(Lqueue_sendImgStruct);
|
||
Lqueue_sendImgStruct.clear();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
//重新运行
|
||
bool Logic_processtask::restartTask()
|
||
{
|
||
bool myinit = exitTask();
|
||
if (myinit == false)
|
||
return false;
|
||
Sleep(100);
|
||
myinit = initTask();
|
||
if (myinit == false)
|
||
return false;
|
||
Sleep(100);
|
||
return startTask();
|
||
|
||
|
||
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " start restart load Task! ";
|
||
|
||
setInitFlag(0);
|
||
// 1.清理容器数据
|
||
if (!clearStationData())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line: " << __LINE__ << " clearStationData is error!";
|
||
return false;
|
||
}
|
||
|
||
// 2.停止IO线程
|
||
for (int i = 0; i < Logic_myiothreadList.size(); i++)
|
||
{
|
||
if (Logic_myiothreadList[i])
|
||
{
|
||
Logic_myiothreadList[i]->setRunFlag(false); // 停止IO线程
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << " LINE: " << __LINE__ << " m_IoThread is null! ";
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//m_readThread->setRunFlag(false);
|
||
|
||
// 3.重新连接通讯模块
|
||
|
||
// 3.停止相机线程
|
||
for (int i = 0; i < camerathreadlist.size(); i++)
|
||
{
|
||
camerathreadlist.at(i)->setRunFlag(false);
|
||
}
|
||
|
||
// 4.停止任务线程
|
||
for ( auto val = 0; val < task_thread_list.size(); val++ )
|
||
{
|
||
task_thread_list.at(val)->setRunFlag(false);
|
||
}
|
||
|
||
/*for (auto val = 0; val < mySaveImageThread_list.size(); val++)
|
||
{
|
||
mySaveImageThread_list.at(val)->setSaveimageflag(false);
|
||
}*/
|
||
// 6.开始重新初始化所有模块
|
||
if (!initStationsModule())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line: " << __LINE__ << " initAllModule is error! ";
|
||
return false;
|
||
}
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " initStationsModule is success!";
|
||
|
||
// 7.开启任务线程
|
||
foreach(Logic_taskthread * m_thread, task_thread_list)
|
||
{
|
||
m_thread->setRunFlag(true);
|
||
m_thread->start();
|
||
}
|
||
/*foreach(Logic_mysaveimagethread* m_saveImagethread, mySaveImageThread_list)
|
||
{
|
||
m_saveImagethread->setSaveimageflag(true);
|
||
m_saveImagethread->start();
|
||
}*/
|
||
// 8.开启所有相机线程
|
||
foreach(Logic_camera_thread* m_thread, camerathreadlist)
|
||
{
|
||
m_thread->setRunFlag(true);
|
||
if (m_thread->getType().contains("algorithm"))
|
||
{
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " name:" << m_thread->getName();
|
||
// m_thread->start(); // 运行所有线程
|
||
}
|
||
else if (m_thread->getType().contains("camera") )//&& !m_thread->getTriggerModel().contains("software"))
|
||
{
|
||
m_thread->start();
|
||
}
|
||
}
|
||
|
||
|
||
//
|
||
//m_readThread->setRunFlag(true);
|
||
if (initTcpModule())
|
||
{
|
||
qDebug() << __FUNCTION__ << __LINE__ << " initTcpModule success!";
|
||
setInitFlag(1);
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << __LINE__ << " initTcpModule failed!";
|
||
setInitFlag(0);
|
||
return false;
|
||
}
|
||
|
||
// 9.开启IO线程
|
||
//m_IoThread->setRunFlag(true);
|
||
for (int i = 0; i < Logic_myiothreadList.size(); i++)
|
||
{
|
||
if (Logic_myiothreadList[i])
|
||
{
|
||
Logic_myiothreadList[i]->setRunFlag(true); // 开启IO线程
|
||
Logic_myiothreadList[i]->start();
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << " LINE: " << __LINE__ << " m_IoThread is null! ";
|
||
return false;
|
||
}
|
||
}
|
||
|
||
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " restartTask is success!";
|
||
|
||
return true;
|
||
}
|
||
//开始重新初始化所有模块
|
||
bool Logic_processtask::initStationsModule()
|
||
{
|
||
// 初始化所有模块
|
||
for (auto i = 0; i < module_list.size(); i++)
|
||
{
|
||
MyModule m_module = module_list.at(i);
|
||
QString moduleName = m_module.moduleName;
|
||
QString key = m_module.module_key;
|
||
m_module.ConfigArray = getCameraConfigArray(key);
|
||
|
||
//传递初始化参数
|
||
QByteArray initByte = m_module.m_initByte;
|
||
std::vector<cv::Mat> vec_moduleMat;
|
||
//数据格式:3+3+数据长度+数据内容
|
||
//QByteArray m_keyByte = initCameraParam(key, m_module.ConfigArray);
|
||
//统一用initParas进行初始化
|
||
//if (moduleName.contains("Camera"))
|
||
// m_module.module_ptr->writeData(vec_moduleMat, m_keyByte); // 初始化相机
|
||
if (m_module.module_ptr->initParas(initByte))
|
||
{
|
||
qDebug() << __FUNCTION__ << " line: " << __LINE__ << " key: " << key << " initParas success! ";
|
||
}
|
||
else
|
||
{
|
||
qCritical() << __FUNCTION__ << " line: " << __LINE__ << " moduleName: " << m_module.moduleName << " initParas failed! ";
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
void Logic_processtask::thread_checkAll_Status()
|
||
{
|
||
//异常判断;基本不耗时
|
||
int runNum = 0;
|
||
while (isStartThread)
|
||
{
|
||
if (runNum++ % 10 == 0)
|
||
{
|
||
if (finstcp_ptr != nullptr)
|
||
{
|
||
//检查通信指针
|
||
bool status = finstcp_ptr->checkStatus();
|
||
//状态改变
|
||
if (status != finstcp_ptr->status)
|
||
{
|
||
if (status == false)
|
||
{
|
||
finstcp_ptr->status = false;
|
||
}
|
||
else
|
||
{
|
||
finstcp_ptr->status = true;
|
||
}
|
||
//emit CommunicationDisconnect(QString::fromStdString("finstcp_ptr->moduleName"), status);
|
||
emit sendTcpStatus(status);
|
||
}
|
||
}
|
||
|
||
//for (int i = 0; i < Communication_ptr.size(); i++)
|
||
//{
|
||
// bool status = Communication_ptr[i]->checkStatus();
|
||
// //状态改变
|
||
// if (status != Communication_ptr[i]->status)
|
||
// {
|
||
// if (status == false)
|
||
// {
|
||
// Communication_ptr[i]->status = false;
|
||
// }
|
||
// else
|
||
// {
|
||
// Communication_ptr[i]->status = true;
|
||
// }
|
||
// emit CommunicationDisconnect(QString::fromStdString(Communication_ptr[i]->moduleName), status);
|
||
// }
|
||
//}
|
||
|
||
|
||
|
||
//检测相机指针
|
||
//相机线程-原图显示
|
||
for (int i = 0; i < camerathreadlist.size(); i++)
|
||
{
|
||
Logic_camera_thread* m_thread = camerathreadlist[i];
|
||
if (m_thread == nullptr)
|
||
continue;
|
||
bool status = m_thread->m_Module.module_ptr->checkStatus();
|
||
//状态改变
|
||
if (status != m_thread->m_Module.module_ptr->status)
|
||
{
|
||
m_thread->m_Module.module_ptr->status = status;
|
||
emit cameraDisconnect(m_thread->m_stationName + "_" + m_thread->m_name, status);
|
||
}
|
||
}
|
||
}
|
||
Sleep(10);
|
||
qApp->processEvents();
|
||
}
|
||
qApp->processEvents();
|
||
}
|
||
|
||
//检测条件判断;只循环判断
|
||
void Logic_processtask::thread_Logic_hdataintegration()
|
||
{
|
||
while (isStartThread)
|
||
{
|
||
if (isStartThread == false)
|
||
break;
|
||
if (alwaysRun_Logic_hdataintegration.size() < 1)
|
||
return;
|
||
// 开启检测条件判断线程
|
||
for (int i = 0; i < alwaysRun_Logic_hdataintegration.size(); i++)
|
||
{
|
||
alwaysRun_Logic_hdataintegration.at(i)->run();
|
||
}
|
||
Sleep(2);
|
||
//qApp->processEvents();
|
||
}
|
||
}
|
||
|
||
long int checkTimes = 0;
|
||
void Logic_processtask::thread_reciveImageData()
|
||
{
|
||
while (isStartThread)
|
||
{
|
||
if (isStartThread == false)
|
||
break;
|
||
double time_Start = (double)clock();
|
||
reciveImageData();
|
||
double time_End = (double)clock();
|
||
//正常情况:2个任务时,无结果耗时20ms,有结果耗时70ms;图像480*480
|
||
if (time_End - time_Start > 200)
|
||
qDebug() << "[reciveImageData]" << "Time:" << (time_End - time_Start) / 1000.0 << "s";
|
||
Sleep(100);
|
||
checkTimes++;
|
||
}
|
||
}
|
||
|
||
//Q_INVOKABLE 修饰的,超链接函数;Logic_taskthread和Logic_camera_thread可访问
|
||
void Logic_processtask::reciveImageData()
|
||
{
|
||
|
||
try {
|
||
//// 开启检测条件判断线程
|
||
//for (int i = 0; i < dataIntegrationThread_list.size(); i++)
|
||
//{
|
||
// dataIntegrationThread_list.at(i)->run();
|
||
//}
|
||
//Sleep(10);
|
||
//qApp->processEvents();
|
||
//double time_Start = (double)clock();
|
||
//检测线程-结果图显示 ;2个任务时,无结果耗时20ms,有结果耗时70ms
|
||
for (int i = 0; i < task_thread_list.size(); i++)
|
||
{
|
||
Logic_taskthread* myTask = task_thread_list[i];
|
||
if (myTask == nullptr)
|
||
continue;
|
||
//无SN信息的,相机线程返回的图片数据;
|
||
//有相机关键字(SN或userName);
|
||
//从Logic_module_thread相机拍的图,传过来
|
||
if (myTask->dataPack_size() < 1)
|
||
{
|
||
//Sleep(10);
|
||
continue;
|
||
}
|
||
//Sleep(20);
|
||
while (myTask->dataPack_size() >= 1)
|
||
{
|
||
dataPack m_data;
|
||
m_data = myTask->getdataPack();
|
||
|
||
//continue;
|
||
|
||
cv::Mat m_mat;
|
||
m_mat = m_data.mat;
|
||
//相机关键字(SN或userName);
|
||
|
||
QString snInfo = m_data.snInfo;
|
||
QString cameraName = m_data.cameraName; //931实际对应camera_name_index
|
||
QString algorithmName = m_data.algorithmName;
|
||
QString retFlag = m_data.retFlag;
|
||
QString sendNgInfo = m_data.sendNgInfo;
|
||
QByteArray m_byte = m_data.byteArray;
|
||
if (snInfo.isEmpty())
|
||
Sleep(1);
|
||
// 传递图片
|
||
if (retFlag == "camera")
|
||
decodePackage(cameraName, m_mat);
|
||
else if (retFlag == "algorithm")
|
||
{
|
||
if (!m_mat.empty())
|
||
{
|
||
decodePackage(cameraName, m_mat); //转换Mat为QImage*并发送到界面显示
|
||
//发送数据,显示右侧日志
|
||
emit LwriteData(algorithmName, cameraName, snInfo, m_byte);
|
||
}
|
||
else
|
||
//发送数据,显示右侧日志
|
||
emit LwriteData(algorithmName, cameraName, snInfo, m_byte);
|
||
}
|
||
m_mat.release();
|
||
m_data.mat.release();
|
||
m_byte.resize(0);
|
||
}
|
||
|
||
|
||
|
||
////传递图片
|
||
//if (retFlag == "camera")
|
||
// decodePackage(name, m_mat, m_byte);
|
||
//else if (retFlag == "algorithm")
|
||
//{
|
||
// if (!m_mat.empty())
|
||
// decodePackage(name, m_mat, m_byte);
|
||
|
||
// emit sendResultData(name, m_byte); //未使用?
|
||
//}
|
||
//else if (retFlag == "offline")
|
||
// decodePackage(name, m_mat, m_byte);
|
||
|
||
//QJsonDocument dockey = QJsonDocument::fromJson(m_byte);
|
||
//QJsonObject objkey = dockey.object();
|
||
////Logic_hdataintegration里,加数据的key值;
|
||
//QString mykey = objkey.value("result_key").toString();
|
||
|
||
//if (mykey.contains("offline1"))
|
||
//{
|
||
// ;//myres_offline[0].enqueue(m_byte);
|
||
//}
|
||
//if (mykey.contains("offline2"))
|
||
//{
|
||
// ;//myres_offline[1].enqueue(m_byte);
|
||
//}
|
||
|
||
////发送MES
|
||
//for (int i = 0; i < Communication_ptr.size(); i++)
|
||
// if (Communication_Type[i].contains("MES"))
|
||
// {
|
||
// std::vector<cv::Mat> vec_mat;
|
||
// if (!Communication_ptr[i]->writeData(vec_mat, m_byte))
|
||
// {
|
||
// qCritical() << "[TCP Error] " << QString::fromUtf8("结果信号发送失败!");
|
||
// return;
|
||
// }
|
||
// }
|
||
|
||
|
||
//addPack_mutex->unlock();
|
||
}
|
||
|
||
/*double time_End = (double)clock();
|
||
qDebug() << "[结果图显示]" << "Time:" << (time_End - time_Start) / 1000.0 << "s";
|
||
time_Start = (double)clock();*/
|
||
|
||
//清除没有检测算法的相机容器数据;基本不耗时
|
||
if (checkTimes % 10 == 0) //10次轮询,即10*10ms=100ms,判断一次
|
||
{
|
||
for (int i = 0; i < stationlist_array.size(); i++)
|
||
{
|
||
QJsonObject station_obj = stationlist_array.at(i).toObject();
|
||
QString station_name = station_obj.value("station_key").toString();
|
||
|
||
//算法任务数
|
||
int task_thread_num = station_obj.value("task_thread_num").toString().toInt();
|
||
if (task_thread_num == 0)
|
||
{
|
||
// 删除数据
|
||
QJsonArray cameramodulelist = station_obj.value("cameramodulelist").toArray();
|
||
for (auto val = 0; val < cameramodulelist.size(); val++)
|
||
{
|
||
QJsonObject camera_obj = cameramodulelist.at(val).toObject();
|
||
//相机SN或usrID
|
||
QString camera_key = camera_obj.value("camera_key").toString();
|
||
|
||
//获取工站名+相机对应的图片容器类
|
||
HCameraData* camera_ptr = station_list.value(station_name)->getCameraData(camera_key);
|
||
if (camera_ptr)
|
||
{
|
||
camera_ptr->clearData("0");
|
||
camera_ptr->clearData("1");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
////Logic_myiothread_heart;
|
||
//if (Logic_myiothread_heart != nullptr)
|
||
//{
|
||
// if (Logic_myiothread_heart->dataPack_size() > 0)
|
||
// while (Logic_myiothread_heart->dataPack_size() >= 1)
|
||
// {
|
||
// dataPack m_data;
|
||
// m_data = Logic_myiothread_heart->getdataPack();
|
||
|
||
// cv::Mat m_mat;
|
||
// m_mat = m_data.mat.clone();
|
||
// //相机关键字(SN或userName);
|
||
|
||
// QString snInfo = m_data.snInfo;
|
||
// QString cameraName = m_data.cameraName; //931实际对应camera_name_index
|
||
// QString algorithmName = m_data.algorithmName;
|
||
// QString retFlag = m_data.retFlag;
|
||
// QString sendNgInfo = m_data.sendNgInfo;
|
||
// QByteArray m_byte = m_data.byte;
|
||
// // 传递图片
|
||
// if (retFlag == "camera")
|
||
// decodePackage(cameraName, m_mat);
|
||
// else if (retFlag == "algorithm")
|
||
// {
|
||
// if (!m_mat.empty())
|
||
// {
|
||
// decodePackage(cameraName, m_mat); //转换Mat为QImage*并发送到界面显示
|
||
// //发送数据,显示右侧日志
|
||
// emit LwriteData(algorithmName, cameraName, snInfo, m_byte);
|
||
// }
|
||
// else
|
||
// //发送数据,显示右侧日志
|
||
// emit LwriteData(algorithmName, cameraName, snInfo, m_byte);
|
||
// }
|
||
// m_mat.release();
|
||
// m_data.mat.release();
|
||
// m_byte.resize(0);
|
||
// }
|
||
//}
|
||
}
|
||
|
||
|
||
//异常判断;基本不耗时
|
||
{
|
||
//检查通信指针
|
||
//for (int i = 0; i < Communication_ptr.size(); i++)
|
||
//{
|
||
// bool status = Communication_ptr[i]->checkStatus();
|
||
// //状态改变
|
||
// if (status != Communication_ptr[i]->status)
|
||
// {
|
||
// if (status == false)
|
||
// {
|
||
// Communication_ptr[i]->status = false;
|
||
// }
|
||
// else
|
||
// {
|
||
// Communication_ptr[i]->status = true;
|
||
// }
|
||
// emit CommunicationDisconnect(QString::fromStdString(Communication_ptr[i]->moduleName), status);
|
||
// }
|
||
//}
|
||
//for (int i = 0; i < Communication_ptr.size(); i++)
|
||
{
|
||
bool status = finstcp_ptr->checkStatus();
|
||
//状态改变
|
||
if (status != finstcp_ptr->status)
|
||
{
|
||
if (status == false)
|
||
{
|
||
finstcp_ptr->status = false;
|
||
}
|
||
else
|
||
{
|
||
finstcp_ptr->status = true;
|
||
}
|
||
//emit CommunicationDisconnect(QString::fromStdString(finstcp_ptr->moduleName), status);
|
||
emit sendTcpStatus(status);
|
||
}
|
||
}
|
||
|
||
|
||
//检测相机指针
|
||
//相机线程-原图显示
|
||
for (int i = 0; i < camerathreadlist.size(); i++)
|
||
{
|
||
Logic_camera_thread* m_thread = camerathreadlist[i];
|
||
bool status = m_thread->m_Module.module_ptr->checkStatus();
|
||
//状态改变
|
||
if (status != m_thread->m_Module.module_ptr->status)
|
||
{
|
||
m_thread->m_Module.module_ptr->status = status;
|
||
emit cameraDisconnect(m_thread->m_stationName + "_" + m_thread->m_name, status);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (QString err)
|
||
{
|
||
qCritical() << "[Error] " << "show image reeor!";
|
||
}
|
||
|
||
}
|
||
|
||
|
||
bool Logic_processtask::sendPLC(QString cmd)
|
||
{
|
||
QByteArray qa = cmd.toLocal8Bit();
|
||
QMutexLocker locker(write_data_mutex);
|
||
if (!finstcp_ptr)
|
||
return false;
|
||
writeCommondPack.append(qa);//结果指令格式,为,例:write_short_DM3000_1;
|
||
return true;
|
||
}
|
||
|
||
//重新加载配置文件json,仅设置参数;
|
||
bool Logic_processtask::reLoadJsonSetParameter(QStringList moduleKeyList)
|
||
{
|
||
//programObj通过setJsonObject完成设置
|
||
stationlist_array = programObj.value("工站列表").toArray();
|
||
for (int i = 0; i < stationlist_array.size(); i++)
|
||
{
|
||
QJsonObject station_obj = stationlist_array.at(i).toObject();
|
||
QString station_key = station_obj.value("工站名").toString();
|
||
|
||
m_cameramodulelist = station_obj.value("相机模块列表").toArray();
|
||
for (auto val = 0; val < m_cameramodulelist.size(); val++)
|
||
{
|
||
QJsonObject camera_obj = m_cameramodulelist.at(val).toObject();
|
||
QString camera_key = camera_obj.value("相机key").toString();
|
||
QString moduleName = camera_obj.value("模块名称").toString(); // 相机模块名
|
||
QString image_queue_num = camera_obj.value("单通道拍照数").toString();
|
||
QString image_channel_num = camera_obj.value("上料通道数").toString();
|
||
QJsonArray read_sn_format = camera_obj.value("获取SN方式列表").toArray();
|
||
QJsonArray init_array = camera_obj.value("相机参数").toArray();
|
||
QString init_string;
|
||
for (int i = 0; i < init_array.size(); i++)
|
||
{
|
||
if (i > 0)
|
||
init_string += "+";
|
||
QJsonObject init_obj = init_array.at(i).toObject();
|
||
QString cfg_name = init_obj.value("参数名").toString();
|
||
init_string += cfg_name;
|
||
QJsonArray cfg_array = init_obj.value("相机参数值").toArray();
|
||
for (int j = 0; j < cfg_array.size(); j++)
|
||
init_string += "#" + cfg_array[j].toString();
|
||
}
|
||
QString addString = "+是否为标准通信流程#" + programObj.value("是否为标准通信流程").toString() + "+取图超时#" + camera_obj.value("取图超时").toString();
|
||
//数据格式:相机key+曝光#3000#5000#6000+ip#192.168.0.1
|
||
QByteArray init_Byte = camera_key.toLocal8Bit() + "+" + init_string.toLocal8Bit() + addString.toLocal8Bit();
|
||
|
||
//匹配
|
||
if (moduleKeyList.contains(camera_key))
|
||
{
|
||
for (int m = 0; m < module_list.size(); m++)
|
||
{
|
||
if (module_list[m].module_key == camera_key)
|
||
{
|
||
module_list[m].m_initByte = init_Byte;
|
||
QString onlySetParameter = "+只设置参数不重新初始化onlySetParameter";
|
||
init_Byte += onlySetParameter.toLocal8Bit();
|
||
module_list[m].module_ptr->initParas(init_Byte);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//读取"存图设置"
|
||
QJsonArray SaveImgArray = programObj.value("存图设置").toArray();
|
||
QString SaveImgString;
|
||
for (int s = 0; s < SaveImgArray.size(); s++)
|
||
{
|
||
/*"Class图": "不保存",
|
||
"Seg图" : "不保存",
|
||
"原图" : "不保存",
|
||
"界面窗口名称" : "工位1-相机1",
|
||
"相机key" : "offline-camera1",
|
||
"结果图" : "保存"*/
|
||
//if (i > 0)
|
||
SaveImgString += "+";
|
||
QJsonObject init_obj = SaveImgArray.at(s).toObject();
|
||
QString cfg_name = init_obj.value("相机key").toString();
|
||
SaveImgString += cfg_name;
|
||
SaveImgString += "#原图_" + init_obj.value("原图").toString();
|
||
SaveImgString += "#Seg图_" + init_obj.value("Seg图").toString();
|
||
SaveImgString += "#Class图_" + init_obj.value("Class图").toString();
|
||
SaveImgString += "#结果图_" + init_obj.value("结果图").toString();
|
||
}
|
||
|
||
//缺陷名称
|
||
QString defectNames;
|
||
QJsonArray defectNamesArray = programObj.value("缺陷类别名称设置").toArray();
|
||
for (int s = 0; s < defectNamesArray.size(); s++)
|
||
{
|
||
defectNames += "+";
|
||
QJsonObject init_obj = defectNamesArray.at(s).toObject();
|
||
QString cfg_name = init_obj.value("缺陷类别").toString();
|
||
defectNames += "缺陷类别_" + cfg_name;
|
||
defectNames += "#缺陷名称_" + init_obj.value("缺陷名称").toString();
|
||
defectNames += "#是否检测_" + init_obj.value("是否检测").toString();
|
||
}
|
||
|
||
QJsonArray algorithmlist = station_obj.value("算法列表").toArray();
|
||
for (auto val = 0; val < algorithmlist.size(); val++)
|
||
{
|
||
QJsonObject algorithm_obj = algorithmlist.at(val).toObject(); // 算法模块obj
|
||
QString algorithm_key = algorithm_obj.value("算法名称").toString(); // 算法名称
|
||
QString moduleName = algorithm_obj.value("模块名称").toString(); // 算法模块名
|
||
//为空
|
||
QString algorithm_config = algorithm_obj.value("algorithm_config").toString(); // 算法所需要的配置文件路径
|
||
QJsonArray algorithm_init_array = algorithm_obj.value("算法初始化参数").toArray(); // 基准坐标
|
||
//QJsonArray delete_result_index_array = algorithm_obj.value("delete_result_index").toArray(); // 删除算法结果数据
|
||
if (algorithm_key.isEmpty() || moduleName.isEmpty())
|
||
{
|
||
qCritical() << __FUNCTION__ << " line:" << __LINE__ << " algorithm_key or moduleName is null! ";
|
||
return false;
|
||
}
|
||
QJsonArray init_array = algorithm_obj.value("算法初始化参数").toArray();
|
||
QString init_string;
|
||
for (int i = 0; i < init_array.size(); i++)
|
||
{
|
||
if (i > 0)
|
||
init_string += "+";
|
||
QJsonObject init_obj = init_array.at(i).toObject();
|
||
QString cfg_name = init_obj.value("算法参数名").toString();
|
||
init_string += cfg_name;
|
||
QJsonArray cfg_array = init_obj.value("算法参数值").toArray();
|
||
for (int j = 0; j < cfg_array.size(); j++)
|
||
init_string += "#" + cfg_array[j].toString();
|
||
}
|
||
//数据格式:算法名称+曝光#3000#5000#6000+ip#192.168.0.1
|
||
QByteArray init_Byte = algorithm_key.toLocal8Bit() + "+" + init_string.toLocal8Bit() + SaveImgString.toLocal8Bit() + defectNames.toLocal8Bit();
|
||
|
||
//匹配
|
||
if (moduleKeyList.contains(algorithm_key))
|
||
{
|
||
for (int m = 0; m < module_list.size(); m++)
|
||
{
|
||
if (module_list[m].module_key == algorithm_key)
|
||
{
|
||
module_list[m].m_initByte = init_Byte;
|
||
QString onlySetParameter = "+只设置参数不重新初始化onlySetParameter";
|
||
init_Byte += onlySetParameter.toLocal8Bit();
|
||
module_list[m].module_ptr->initParas(init_Byte);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |