You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

500 lines
11 KiB
C++

#include "Station.h"
#include "qobject.h"
#include "iCoreCtrl.h"
#include "TrigDetector.h"
#include "QDebug"
#include "QSqliteWheelHubWf.h"
#include "ModelTable.h"
#include "IWfCtrl.h"
#include "WfModel.h"
#include <QLayout>
#include "lpSysLog.h"
#pragma execution_character_set("utf-8")
int Station::s_nTrigWaitTimes = 5;
Station::Station(QSqliteWheelHubWf *p1, ICoreCtrl *p2, IWfCtrl * p3)
: m_pView(NULL),m_pDbWf(p1)
{
m_pCoreCtrl = p2;
m_pWfCtrl = p3;
m_pTrigDetector = new CTrigDetector;
connect(m_pTrigDetector, SIGNAL(sgErrorTrig(int)), this, SLOT(onErrTrig(int)));
connect(this, SIGNAL(sgUpdateLable()), this, SLOT(updateAll()));
connect(this, SIGNAL(sgaddTrigCount(int)), m_pTrigDetector, SLOT(addTrigCount(int)));
connect(this, SIGNAL(sgaddRevCount(int)), m_pTrigDetector, SLOT(addRevCount(int)));
m_pTrigDetector->start();
}
Station::~Station()
{
if (m_pTrigDetector)
{
m_pTrigDetector->stop();
delete m_pTrigDetector;
}
if (m_pView) {
delete m_pView;
}
}
void Station::setCamInfo(int nId, int alg, QString uniqueName, QString showName)
{
this->nId = nId;
this->nAlgorithm = alg;
this->szCamKey = uniqueName;
this->szShow = showName;
}
QString genCurrentTime(QString strFormat = "yyyy-MM-dd hh:mm::ss") {
QDateTime qTime = QDateTime::currentDateTime();
QString strTime = qTime.toString(strFormat);
return strTime;
}
bool Station::addModel(QString strModel)
{
if (m_models.contains(strModel)) {
return false;
}
m_models.append(strModel);
if (m_pView) {
m_pView->resetModel();
m_pView->scroll2Bottom();
}
qDebug() << "add model :" << strModel;
return true;
}
bool Station::delModel(QString strModel)
{
m_models.removeOne(strModel);
if (m_models.size() == 0) {
setCurrentModel(QString());
}
if (m_pView) {
m_pView->resetModel();
}
qDebug() << "del model" << strModel;
return true;
}
int Station::modelCount() {
return m_models.size();
}
QString Station::getModelUnique(const QString &model)
{
return QString::number(nId) + _WF_UNIQUE_SPLIT + szCamKey + _WF_UNIQUE_SPLIT + model;
}
bool Station::updateCurrentModel()
{
//update currrent model
if (!m_models.contains(m_strCurrentModel)) {
m_strCurrentModel = "";
}
if (m_strCurrentModel.isEmpty() && modelCount() != 0) {
m_strCurrentModel = m_models.first();
}
return true;
}
unsigned short Station::swapBigEndian(unsigned short &data)
{
data = ((data & 0x00FF) << 8) + ((data & 0xFF00) >> 8);
return data;
}
void Station::setCurrentModel(QString strModel)
{
if (strModel.isEmpty()) {
qDebug() << "setCurrentModel, model is empty";
return;
}
else {
m_strCurrentModel = strModel;
}
if (m_pView && !m_strCurrentModel.isEmpty()) {
m_pView->resetModel();
}
updateAll();
}
void Station::setCurrentModel(int modelIndex)
{
if (modelCount() != 0 && modelCount() > modelIndex && modelIndex >= 0) {
setCurrentModel(m_models[modelIndex]);
}
}
bool Station::initCurrentModel()
{
if (modelCount() <= 1) {
if (modelCount() == 1) {
setCurrentModel(m_models[0]);
}
else if (modelCount() == 0) {
setCurrentModel("");
}
return true;
}
return false;
}
bool Station::updateCurrentModel2Label()
{
bool bCali = false;
WfModel *pModel = wfModel(m_strCurrentModel);
if (pModel) {
//bCali = pModel->bCaliState;
bCali = IStandard(m_strCurrentModel);
}
QString strText = "<html><head / ><body><p><span style = '%1'>%2%3 __ %4 __%5< / span>< / p>< / body>< / html>";
strText = strText.arg("color:#ff0000;").arg(QObject::tr("工位"))
.arg(nId).arg(m_strCurrentModel == "" ? "?" : m_strCurrentModel)
.arg(bCali == true ? QObject::tr("已标定") : QObject::tr("未标定"));
emit sgShowModeName(nId, strText);
return true;
}
bool Station::updateAll()
{
updateCurrentModel();
updateCurrentModel2Label();
return true;
}
QVariant Station::getVariant()
{
QVariantMap vMap;
vMap.insert("current_model", getModelUnique(m_strCurrentModel));
return vMap;
}
void Station::onErrTrig(int nErrCount)
{
if (nErrCount > s_nTrigWaitTimes * 4) {
qWarning() << "stationid = " << nId << ", error count is bigger than xxx";
}
else if (s_nTrigWaitTimes * 3 == nErrCount) {
qWarning() << "stationid =" << nId << ",nErrCount = " << nErrCount;
QString str = genTimerInfo() + "camera disconnect!";
emit sgPrint2Window(str);
str = genTimerInfo() + "camera count reset, but not trig again!";
emit sgPrint2Window(str);
m_pTrigDetector->correct();
str = genTimerInfo() + "try reopen camera!";
emit sgPrint2Window(str);
m_pCoreCtrl->IStopCamera(szCamKey);
m_pCoreCtrl->ICloseCamera(szCamKey);
if (m_pWfCtrl->IBatchModel()) {
changeImageFolder("");
}
bool bb = m_pCoreCtrl->IOpenCamera(szCamKey);
str = QString("reopen_%1").arg(bb);
emit sgPrint2Window(str);
bool b = m_pCoreCtrl->IStartCamera(szCamKey);
if (b) {
str = genTimerInfo() + "try reopen successful!";
m_pCoreCtrl->ISnapImage(QStringList()<<szCamKey);
}
else {
str = genTimerInfo() + "reopen failed!";
qWarning() << "stationid = " << nId << ", try reopen camera:" << szCamKey << " failed";
}
emit sgPrint2Window(str);
if (m_pWfCtrl->IBatchModel()) {
trigImage();
}
qDebug() << "stationid = " << nId << ", " << str;
}
else if (s_nTrigWaitTimes == nErrCount || s_nTrigWaitTimes * 2 == nErrCount) {
qWarning() << "camera trig failed!";
QString str = genTimerInfo() + "camera trig failed!";
emit sgPrint2Window(str);
str = genTimerInfo() + "try to trig again!";
emit sgPrint2Window(str);
QStringList strList = m_pCoreCtrl->ICameraKeys();
if (strList.contains(szCamKey))
m_pCoreCtrl->ISnapImage(QStringList() << szCamKey);
qWarning() << str;
}
else {
qDebug() << "stationid = " << nId << ", wait for image result " << nErrCount;
}
}
int Station::stationId()
{
return nId;
}
QString Station::model(int index)
{
if (index >= 0 && index < m_models.size()) {
return m_models.at(index);
}
return QString();
}
QStringList Station::modelList()
{
return m_models;
}
void Station::revResult()
{
emit(sgaddRevCount(1));
if (m_pWfCtrl->IBatchModel()) {
}
}
bool Station::trigImage()
{
TP_CAMERA_OPTION optCam;
m_pCoreCtrl->ICameraOptionByKey(szCamKey, optCam);
if (optCam.status != TP_CAMERA_STATUS_OPENED &&
optCam.status != TP_CAMERA_STATUS_STARTED){
if (m_pCoreCtrl->IOpenCamera(szCamKey))
{
bool b = m_pCoreCtrl->IStartCamera(szCamKey);
}
}
//filter
if (!m_pTrigDetector->filterTrig()) {
return false;
}
// check
if (!m_pTrigDetector->isWaitForTrig()) {
return false;
}
QString str = genTimerInfo();
//m_pTrigDetector->addTrigCount();
emit(sgaddTrigCount(1));
str += "trig succussful: " + QString::number(m_pTrigDetector->getTrigCount());
emit sgPrint2Window(str);
qDebug() << str;
QStringList strList = m_pCoreCtrl->ICameraKeys();
if (strList.contains(szCamKey))
{
m_pCoreCtrl->ISnapImage(QStringList() << szCamKey);
SYSLOG_STATUS << QString("●工位%1:相机触发拍照,型号为[%2],型号索引ID为[%3],时间:%4")
.arg(stationId())
.arg(m_strCurrentModel)
.arg(m_CurrentPLCIndex)
.arg(QDateTime::currentDateTime().toString("hh:mm:ss zzz"));
}
return true;
}
bool Station::setView(QWidget *pWgt)
{
m_pView = new ModelView(qobject_cast<QTableView*>(pWgt), this);
return true;
}
bool Station::IStandard(QString strModel)
{
if (!m_pWfCtrl)
return false;
return m_pWfCtrl->IStandard(stationId(), strModel);
}
bool Station::sendResult(double angleValue)
{
if (m_pPort)
{
const int c_nDataNum = 8;
const double c_dMaxAngle = 360.0;
if (angleValue < 0) {
angleValue += c_dMaxAngle;
}
unsigned short data[c_nDataNum] = { 0 };
data[2] = int(angleValue);
double tmp = angleValue - int(angleValue);
tmp *= 1000.0;
data[3] = int(tmp);
SComFrame sendFram;
sendFram.cmd = m_nCmd;
sendFram.data1 = data[0];
sendFram.data2 = data[1];
sendFram.data3 = data[2];
sendFram.data4 = data[3];
sendFram.data5 = data[4];
sendFram.data6 = data[5];
sendFram.data7 = data[6];
sendFram.data8 = data[7];
m_pPort->enquequeData(sendFram);
if (angleValue >= c_dMaxAngle) {
IImageObject::DATA_TO_COMM_HEAD head2;
unsigned short data2[c_nDataNum] = { 0 };
data2[0] = 6 + nId - 1;
data2[1] = 50;
// for (int i = 0; i < c_nDataNum; i++) {
// swapBigEndian(data2[i]);
// }
SComFrame sendFram2;
sendFram2.cmd = 0x48;
sendFram2.data1 = data2[0];
sendFram2.data2 = data2[1];
sendFram2.data3 = data2[2];
sendFram2.data4 = data2[3];
sendFram2.data5 = data2[4];
sendFram2.data6 = data2[5];
sendFram2.data7 = data2[6];
sendFram2.data8 = data2[7];
m_pPort->enquequeData(sendFram2);
}
}
else
{
const int c_nDataNum = 8;
const double c_dMaxAngle = 360.0;
if (angleValue < 0) {
angleValue += c_dMaxAngle;
}
unsigned short data[c_nDataNum] = { 0 };
data[2] = int(angleValue);
double tmp = angleValue - int(angleValue);
tmp *= 1000.0;
data[3] = int(tmp);
// QString strPrint = "data1 = %1, data2 = %2";
// strPrint = strPrint.arg(data[2]).arg(data[3]);
for (int i = 0; i < c_nDataNum; i++) {
swapBigEndian(data[i]);
}
IImageObject::DATA_TO_COMM_HEAD head;
head.cmd = m_nCmd;
QByteArray comArray = m_strComName.toLatin1();
head.commNames = comArray.data();
head.pfCallback = NULL;
head.pContext = this;
if (!m_pCoreCtrl) {
qWarning() << "send result, core ctrl is null";
return false;
}
//m_pCoreCtrl->IDataToComm(head, (char*)data, c_nDataNum * 2);
// send io ctrl
if (angleValue >= c_dMaxAngle) {
IImageObject::DATA_TO_COMM_HEAD head2;
unsigned short data2[c_nDataNum] = { 0 };
head2.cmd = m_nCmd;
head2.commNames = comArray.data();
head2.pContext = NULL;
head2.pfCallback = NULL;
head2.cmd = 0x48;
data2[0] = 6 + nId - 1;
data2[1] = 50;
for (int i = 0; i < c_nDataNum; i++) {
swapBigEndian(data2[i]);
}
//m_pCoreCtrl->IDataToComm(head2, (char*)data2, c_nDataNum * 2);
qDebug() << "send result, angle is bigger than 360";
}
}
return true;
}
QString Station::currentSelectModel()
{
if (m_pView) {
int row = m_pView->currentRow();
if (modelCount() > row) {
return model(row);
}
}
return QString();
}
QString Station::genTimerInfo(QString strPre /*= ""*/, QString strFormat /*= ""*/, QString strEnd /*= ": "*/)
{
QTime dTime = QTime::currentTime();
QString strTime = dTime.toString() + ":" + QString::number(dTime.msec()) + strEnd;
return strTime;
}
QString Station::stationKey()
{
return szCamKey;
}
QString Station::stationShowName()
{
return szShow;
}
WfModel * Station::wfModel(QString str)
{
return m_pWfCtrl->IGetModelInfo(nId, str);
}
bool Station::isWorkingOk()
{
return true;
}
QString Station::modelByPlcCmd(int nIndex)
{
//qWarning() << "modelByPlcCmd, nIndex is :" << nIndex;
for (int i = 0; i < m_models.size(); i++) {
WfModel *pModel = wfModel(m_models.at(i));
if (pModel && pModel->nIndex == nIndex) {
QString strModel =m_models.at(i);
if (currentRunningModel() == strModel)
{
m_CurrentPLCIndex = nIndex;
return strModel;
}
m_CurrentPLCIndex = nIndex;
return strModel;
}
}
m_CurrentPLCIndex = 0;
qWarning() << "modelByPlcCmd, nIndex is invalid; index = " << nIndex;
return QString();
}
bool Station::startBatchTest(QString strBasePath)
{
if (m_pWfCtrl->IBatchModel()) {
return true;
}
return false;
}
bool Station::changeImageFolder(const QString str)
{
TP_CAMERA_OPTION camOpt;
camOpt.uniqueName = szCamKey;
m_pCoreCtrl->ICameraOption(szCamKey, camOpt);
camOpt.folder = str;
m_pCoreCtrl->ISetCameraOption(szCamKey, camOpt);
if (m_pWfCtrl->IBatchModel()) {
}
return true;
}