205 lines
6.2 KiB
C++
205 lines
6.2 KiB
C++
#include "VrError.h"
|
||
#include "WorkpieceSplicePresenter.h"
|
||
#include "VrLog.h"
|
||
|
||
WorkpieceSplicePresenter::WorkpieceSplicePresenter(QObject *parent)
|
||
: BasePresenter(parent)
|
||
, m_pConfig(nullptr)
|
||
, m_pMarkReceiver(nullptr)
|
||
, m_binocularMarkPort(0)
|
||
, m_bMarkConnected(false)
|
||
, m_pMarkReconnectTimer(new QTimer(this))
|
||
{
|
||
connect(m_pMarkReconnectTimer, &QTimer::timeout, this, &WorkpieceSplicePresenter::onMarkReconnectTimeout);
|
||
}
|
||
|
||
WorkpieceSplicePresenter::~WorkpieceSplicePresenter()
|
||
{
|
||
if (m_pMarkReconnectTimer) {
|
||
m_pMarkReconnectTimer->stop();
|
||
}
|
||
if (m_pMarkReceiver) {
|
||
m_pMarkReceiver->Disconnect();
|
||
delete m_pMarkReceiver;
|
||
m_pMarkReceiver = nullptr;
|
||
}
|
||
if (m_pConfig) {
|
||
delete m_pConfig;
|
||
m_pConfig = nullptr;
|
||
}
|
||
}
|
||
|
||
// ============ 实现 BasePresenter 纯虚函数 ============
|
||
|
||
int WorkpieceSplicePresenter::InitApp()
|
||
{
|
||
LOG_INFO("WorkpieceSplicePresenter::InitApp()\n");
|
||
SetWorkStatus(WorkStatus::InitIng);
|
||
|
||
int ret = InitConfig();
|
||
if (ret != SUCCESS) {
|
||
LOG_ERR("初始化配置失败: %d\n", ret);
|
||
SetWorkStatus(WorkStatus::Error);
|
||
return ret;
|
||
}
|
||
|
||
ret = InitBinocularMarkReceiver();
|
||
if (ret != SUCCESS) {
|
||
LOG_ERR("初始化BinocularMarkReceiver失败: %d\n", ret);
|
||
m_pMarkReconnectTimer->start(5000);
|
||
}
|
||
|
||
SetWorkStatus(WorkStatus::Ready);
|
||
LOG_INFO("WorkpieceSplicePresenter初始化完成\n");
|
||
return SUCCESS;
|
||
}
|
||
|
||
int WorkpieceSplicePresenter::InitAlgoParams()
|
||
{
|
||
// WorkpieceSplicePresenter 不使用激光相机算法,返回成功
|
||
LOG_INFO("WorkpieceSplicePresenter::InitAlgoParams() - 不使用激光相机算法\n");
|
||
return SUCCESS;
|
||
}
|
||
|
||
int WorkpieceSplicePresenter::ProcessAlgoDetection(std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& detectionDataCache)
|
||
{
|
||
// WorkpieceSplicePresenter 不使用激光相机算法检测
|
||
LOG_INFO("WorkpieceSplicePresenter::ProcessAlgoDetection() - 不使用激光相机算法检测\n");
|
||
return SUCCESS;
|
||
}
|
||
|
||
EVzResultDataType WorkpieceSplicePresenter::GetDetectionDataType()
|
||
{
|
||
// 返回默认数据类型,实际上不会被使用
|
||
return keResultDataType_Position;
|
||
}
|
||
|
||
void WorkpieceSplicePresenter::OnCameraStatusChanged(int cameraIndex, bool isConnected)
|
||
{
|
||
// WorkpieceSplicePresenter 使用 BinocularMark,不使用激光相机
|
||
// 这里可以留空或记录日志
|
||
LOG_DEBUG("WorkpieceSplicePresenter::OnCameraStatusChanged() - 相机%d 状态: %s\n",
|
||
cameraIndex, isConnected ? "已连接" : "断开");
|
||
}
|
||
|
||
// ============ 重写 BasePresenter 虚函数 ============
|
||
|
||
void WorkpieceSplicePresenter::OnWorkStatusChanged(WorkStatus status)
|
||
{
|
||
auto callback = GetStatusCallback<IYWorkpieceSpliceStatus>();
|
||
if (callback) {
|
||
callback->OnWorkStatusChanged(status);
|
||
}
|
||
}
|
||
|
||
void WorkpieceSplicePresenter::OnStatusUpdate(const std::string& statusMessage)
|
||
{
|
||
auto callback = GetStatusCallback<IYWorkpieceSpliceStatus>();
|
||
if (callback) {
|
||
callback->OnStatusUpdate(statusMessage);
|
||
}
|
||
}
|
||
|
||
// ============ 重写 BasePresenter 的检测方法 ============
|
||
|
||
int WorkpieceSplicePresenter::StartDetection(int cameraIndex, bool isAuto)
|
||
{
|
||
LOG_INFO("WorkpieceSplicePresenter::StartDetection()\n");
|
||
// WorkpieceSplicePresenter 的检测由 BinocularMark 回调触发,不需要主动开始
|
||
SetWorkStatus(WorkStatus::Working);
|
||
return SUCCESS;
|
||
}
|
||
|
||
int WorkpieceSplicePresenter::StopDetection()
|
||
{
|
||
LOG_INFO("WorkpieceSplicePresenter::StopDetection()\n");
|
||
SetWorkStatus(WorkStatus::Ready);
|
||
return SUCCESS;
|
||
}
|
||
|
||
// ============ 私有方法实现 ============
|
||
|
||
int WorkpieceSplicePresenter::InitConfig()
|
||
{
|
||
if (!IVrConfig::CreateInstance((IVrConfig**)&m_pConfig)) {
|
||
LOG_ERR("创建配置管理器失败\n");
|
||
return ERR_CODE(APP_ERR_EXEC);
|
||
}
|
||
|
||
std::string configPath = "./config/config.xml";
|
||
int ret = m_pConfig->LoadConfig(configPath, m_configResult);
|
||
if (ret != SUCCESS) {
|
||
LOG_ERR("加载配置文件失败: %s\n", configPath.c_str());
|
||
return -2;
|
||
}
|
||
|
||
// 从 binocularMarkConfig 获取 BinocularMark 连接信息
|
||
m_binocularMarkIp = m_configResult.binocularMarkConfig.serverIP;
|
||
m_binocularMarkPort = m_configResult.binocularMarkConfig.serverPort;
|
||
|
||
return SUCCESS;
|
||
}
|
||
|
||
int WorkpieceSplicePresenter::InitBinocularMarkReceiver()
|
||
{
|
||
int ret = IBinocularMarkReceiver::CreateInstance(&m_pMarkReceiver);
|
||
if (ret != SUCCESS) {
|
||
return ERR_CODE(APP_ERR_EXEC);
|
||
}
|
||
|
||
m_pMarkReceiver->SetMarkResultCallback([this](const std::vector<VrMark3D>& marks, int64_t timestamp, int errorCode) {
|
||
this->OnMarkResult(marks, timestamp, errorCode);
|
||
});
|
||
m_pMarkReceiver->SetEventCallback([this](ReceiverEventType eventType, const std::string& errorMsg) {
|
||
this->OnMarkConnectionChanged(eventType == ReceiverEventType::CONNECTED);
|
||
});
|
||
|
||
return ConnectToBinocularMark();
|
||
}
|
||
|
||
void WorkpieceSplicePresenter::OnMarkResult(const std::vector<VrMark3D>& marks, qint64 timestamp, int errorCode)
|
||
{
|
||
LOG_INFO("收到Mark结果,数量: %zu, 错误码: %d\n", marks.size(), errorCode);
|
||
|
||
WorkpieceSpliceResult result;
|
||
result.marks = marks;
|
||
result.timestamp = timestamp;
|
||
result.errorCode = errorCode;
|
||
result.success = (errorCode == 0);
|
||
|
||
auto callback = GetStatusCallback<IYWorkpieceSpliceStatus>();
|
||
if (callback) {
|
||
callback->OnMarkResult(result);
|
||
}
|
||
}
|
||
|
||
void WorkpieceSplicePresenter::OnMarkConnectionChanged(bool connected)
|
||
{
|
||
m_bMarkConnected = connected;
|
||
|
||
auto callback = GetStatusCallback<IYWorkpieceSpliceStatus>();
|
||
if (callback) {
|
||
callback->OnBinocularMarkConnectionChanged(connected);
|
||
}
|
||
|
||
if (connected) {
|
||
m_pMarkReconnectTimer->stop();
|
||
OnStatusUpdate("BinocularMarkApp已连接");
|
||
} else {
|
||
m_pMarkReconnectTimer->start(5000);
|
||
OnStatusUpdate("BinocularMarkApp连接断开");
|
||
}
|
||
}
|
||
|
||
int WorkpieceSplicePresenter::ConnectToBinocularMark()
|
||
{
|
||
LOG_INFO("连接BinocularMarkApp: %s:%d\n", m_binocularMarkIp.c_str(), m_binocularMarkPort);
|
||
return m_pMarkReceiver->Connect(m_binocularMarkIp, m_binocularMarkPort);
|
||
}
|
||
|
||
void WorkpieceSplicePresenter::onMarkReconnectTimeout()
|
||
{
|
||
LOG_INFO("尝试重连BinocularMarkApp\n");
|
||
ConnectToBinocularMark();
|
||
}
|