425 lines
12 KiB
C++
425 lines
12 KiB
C++
#include "deviceservice.h"
|
|
|
|
#include "../config/configmanager.h"
|
|
#include "../modbus/modbustcpmanager.h"
|
|
|
|
#include <QElapsedTimer>
|
|
#include <QThread>
|
|
#include <QTimer>
|
|
|
|
namespace {
|
|
|
|
const int FeedbackPollIntervalMs = 50;
|
|
|
|
bool actionAddresses(DeviceService::ProductionAction action,
|
|
const ModbusAddressMap &addresses,
|
|
int *commandAddress, int *feedbackAddress)
|
|
{
|
|
if (!commandAddress || !feedbackAddress) {
|
|
return false;
|
|
}
|
|
|
|
switch (action) {
|
|
case DeviceService::RackPick:
|
|
*commandAddress = addresses.rackPickCommand;
|
|
*feedbackAddress = addresses.rackPickFeedback;
|
|
return true;
|
|
case DeviceService::FrontLoad:
|
|
*commandAddress = addresses.frontLoadCommand;
|
|
*feedbackAddress = addresses.frontLoadFeedback;
|
|
return true;
|
|
case DeviceService::Inspect:
|
|
*commandAddress = addresses.inspectionCommand;
|
|
*feedbackAddress = addresses.inspectionFeedback;
|
|
return true;
|
|
case DeviceService::FrontUnload:
|
|
*commandAddress = addresses.frontUnloadCommand;
|
|
*feedbackAddress = addresses.frontUnloadFeedback;
|
|
return true;
|
|
case DeviceService::Flip:
|
|
*commandAddress = addresses.flipCommand;
|
|
*feedbackAddress = addresses.flipFeedback;
|
|
return true;
|
|
case DeviceService::BackLoad:
|
|
*commandAddress = addresses.backLoadCommand;
|
|
*feedbackAddress = addresses.backLoadFeedback;
|
|
return true;
|
|
case DeviceService::BackUnload:
|
|
*commandAddress = addresses.backUnloadCommand;
|
|
*feedbackAddress = addresses.backUnloadFeedback;
|
|
return true;
|
|
case DeviceService::RackPlace:
|
|
*commandAddress = addresses.rackPlaceCommand;
|
|
*feedbackAddress = addresses.rackPlaceFeedback;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
DeviceService &DeviceService::instance()
|
|
{
|
|
static DeviceService service;
|
|
return service;
|
|
}
|
|
|
|
DeviceService::DeviceService(QObject *parent)
|
|
: QObject(parent)
|
|
, m_pollTimer(new QTimer(this))
|
|
{
|
|
connect(m_pollTimer, &QTimer::timeout, this, &DeviceService::pollStatus);
|
|
}
|
|
|
|
void DeviceService::startPolling()
|
|
{
|
|
if (m_pollTimer->isActive()) {
|
|
return;
|
|
}
|
|
|
|
const int interval = qMax(100, ModbusTcpManager::instance().config().pollIntervalMs);
|
|
m_pollTimer->start(interval);
|
|
pollStatus();
|
|
}
|
|
|
|
void DeviceService::stopPolling()
|
|
{
|
|
m_pollTimer->stop();
|
|
}
|
|
|
|
bool DeviceService::isPolling() const
|
|
{
|
|
return m_pollTimer->isActive();
|
|
}
|
|
|
|
bool DeviceService::readStatus(DeviceStatus *status, QString *error)
|
|
{
|
|
if (!status) {
|
|
if (error) {
|
|
*error = "Device status pointer is null";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
*status = simulatedStatus();
|
|
return true;
|
|
}
|
|
|
|
QString configError;
|
|
if (!ConfigManager::instance().validate(&configError)) {
|
|
if (error) {
|
|
*error = configError;
|
|
}
|
|
status->plcConnected = false;
|
|
return false;
|
|
}
|
|
|
|
const int attempts = qMax(1, ModbusTcpManager::instance().config().reconnectAttempts + 1);
|
|
QString lastError;
|
|
for (int attempt = 0; attempt < attempts; ++attempt) {
|
|
if (attempt > 0) {
|
|
QThread::msleep(qMax(0, ModbusTcpManager::instance().config().reconnectDelayMs));
|
|
}
|
|
|
|
if (!connectWithRetry(&lastError)) {
|
|
continue;
|
|
}
|
|
|
|
if (readStatusOnce(status, &lastError)) {
|
|
return true;
|
|
}
|
|
|
|
ModbusTcpManager::instance().disconnectDevice();
|
|
}
|
|
|
|
status->plcConnected = false;
|
|
if (error) {
|
|
*error = lastError.isEmpty() ? "Failed to read PLC status" : lastError;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void DeviceService::pollStatus()
|
|
{
|
|
DeviceStatus status;
|
|
QString error;
|
|
if (readStatus(&status, &error)) {
|
|
emit statusChanged(status);
|
|
return;
|
|
}
|
|
|
|
status.plcConnected = false;
|
|
emit statusChanged(status);
|
|
if (!error.isEmpty()) {
|
|
emit communicationError(error);
|
|
}
|
|
}
|
|
|
|
DeviceStatus DeviceService::simulatedStatus() const
|
|
{
|
|
DeviceStatus status;
|
|
status.plcConnected = true;
|
|
status.plcReady = true;
|
|
status.robotReady = true;
|
|
status.safetyDoorClosed = true;
|
|
status.emergencyStop = false;
|
|
status.currentStep = 0;
|
|
status.checkResult = 0;
|
|
return status;
|
|
}
|
|
|
|
bool DeviceService::start(QString *error)
|
|
{
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
return true;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (error) {
|
|
*error = "Production control is disabled by configuration";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = ModbusTcpManager::instance().config().addressMap;
|
|
return executeCommand(addresses.startCommand, addresses.startFeedback, error);
|
|
}
|
|
|
|
bool DeviceService::pause(QString *error)
|
|
{
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
return true;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (error) {
|
|
*error = "Production control is disabled by configuration";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = ModbusTcpManager::instance().config().addressMap;
|
|
return executeCommand(addresses.pauseCommand, addresses.pauseFeedback, error);
|
|
}
|
|
|
|
bool DeviceService::reset(QString *error)
|
|
{
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
return true;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (error) {
|
|
*error = "Production control is disabled by configuration";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = ModbusTcpManager::instance().config().addressMap;
|
|
return executeCommand(addresses.resetCommand, addresses.resetFeedback, error);
|
|
}
|
|
|
|
bool DeviceService::clearAlarm(QString *error)
|
|
{
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
return true;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (error) {
|
|
*error = "Production control is disabled by configuration";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = ModbusTcpManager::instance().config().addressMap;
|
|
return executeCommand(addresses.clearAlarmCommand, addresses.clearAlarmFeedback, error);
|
|
}
|
|
|
|
bool DeviceService::setInspectionMode(const QString &mode, QString *error)
|
|
{
|
|
if (mode != "BEFORE_PLATING" && mode != "AFTER_PLATING") {
|
|
if (error) {
|
|
*error = "Inspection mode must be BEFORE_PLATING or AFTER_PLATING";
|
|
}
|
|
return false;
|
|
}
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
return true;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (error) {
|
|
*error = "Production control is disabled by configuration";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const bool afterPlating = mode == "AFTER_PLATING";
|
|
const ModbusAddressMap &addresses = ModbusTcpManager::instance().config().addressMap;
|
|
if (!ModbusTcpManager::instance().writeCoil(
|
|
addresses.inspectionMode, afterPlating, error)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool DeviceService::executeProductionAction(ProductionAction action,
|
|
int trayNo, int slotNo,
|
|
QString *error)
|
|
{
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
return true;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (error) {
|
|
*error = "Production control is disabled by configuration";
|
|
}
|
|
return false;
|
|
}
|
|
if (trayNo <= 0 || slotNo < 1 || slotNo > 49) {
|
|
if (error) {
|
|
*error = "PLC tray or slot position is invalid";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = ModbusTcpManager::instance().config().addressMap;
|
|
if (action == RackPick || action == RackPlace) {
|
|
ModbusTcpManager &modbus = ModbusTcpManager::instance();
|
|
if (!modbus.writeHoldingRegister(
|
|
addresses.trayNumber, static_cast<quint16>(trayNo), error)
|
|
|| !modbus.writeHoldingRegister(
|
|
addresses.slotNumber, static_cast<quint16>(slotNo), error)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
int commandAddress = -1;
|
|
int feedbackAddress = -1;
|
|
if (!actionAddresses(action, addresses, &commandAddress, &feedbackAddress)) {
|
|
if (error) {
|
|
*error = "Unknown PLC production action";
|
|
}
|
|
return false;
|
|
}
|
|
return executeCommand(commandAddress, feedbackAddress, error);
|
|
}
|
|
|
|
bool DeviceService::connectWithRetry(QString *error)
|
|
{
|
|
ModbusTcpManager &modbus = ModbusTcpManager::instance();
|
|
const ModbusConfig config = modbus.config();
|
|
const int attempts = qMax(1, config.reconnectAttempts + 1);
|
|
QString lastError;
|
|
|
|
for (int attempt = 0; attempt < attempts; ++attempt) {
|
|
if (attempt > 0) {
|
|
QThread::msleep(qMax(0, config.reconnectDelayMs));
|
|
}
|
|
|
|
if (modbus.connectDevice(&lastError)) {
|
|
return true;
|
|
}
|
|
modbus.disconnectDevice();
|
|
}
|
|
|
|
if (error) {
|
|
*error = lastError.isEmpty() ? "Failed to connect to PLC" : lastError;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool DeviceService::readStatusOnce(DeviceStatus *status, QString *error)
|
|
{
|
|
ModbusTcpManager &modbus = ModbusTcpManager::instance();
|
|
const ModbusAddressMap &addresses = modbus.config().addressMap;
|
|
|
|
status->plcConnected = true;
|
|
if (!modbus.readCoil(addresses.plcReady, &status->plcReady, error)
|
|
|| !modbus.readCoil(addresses.robotReady, &status->robotReady, error)
|
|
|| !modbus.readCoil(addresses.safetyDoorClosed, &status->safetyDoorClosed, error)
|
|
|| !modbus.readCoil(addresses.emergencyStop, &status->emergencyStop, error)
|
|
|| !modbus.readHoldingRegister(addresses.currentStep, &status->currentStep, error)
|
|
|| !modbus.readHoldingRegister(addresses.checkResult, &status->checkResult, error)
|
|
|| !modbus.readHoldingRegister(addresses.errorCode, &status->errorCode, error)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool DeviceService::executeCommand(int commandAddress, int feedbackAddress, QString *error)
|
|
{
|
|
QString configError;
|
|
if (!ConfigManager::instance().validate(&configError)) {
|
|
if (error) {
|
|
*error = configError;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const ModbusConfig config = ModbusTcpManager::instance().config();
|
|
const int attempts = qMax(1, config.reconnectAttempts + 1);
|
|
QString lastError;
|
|
|
|
for (int attempt = 0; attempt < attempts; ++attempt) {
|
|
if (attempt > 0) {
|
|
QThread::msleep(qMax(0, config.reconnectDelayMs));
|
|
}
|
|
|
|
if (!connectWithRetry(&lastError)) {
|
|
continue;
|
|
}
|
|
|
|
ModbusTcpManager &modbus = ModbusTcpManager::instance();
|
|
if (!modbus.writeCoil(commandAddress, true, &lastError)) {
|
|
modbus.disconnectDevice();
|
|
continue;
|
|
}
|
|
|
|
if (!waitForFeedback(feedbackAddress, &lastError)) {
|
|
modbus.writeCoil(commandAddress, false);
|
|
modbus.disconnectDevice();
|
|
continue;
|
|
}
|
|
|
|
if (!modbus.writeCoil(commandAddress, false, &lastError)) {
|
|
modbus.disconnectDevice();
|
|
continue;
|
|
}
|
|
if (!modbus.writeCoil(feedbackAddress, false, &lastError)) {
|
|
modbus.disconnectDevice();
|
|
continue;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
if (error) {
|
|
*error = lastError.isEmpty() ? "PLC command failed" : lastError;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool DeviceService::waitForFeedback(int address, QString *error)
|
|
{
|
|
ModbusTcpManager &modbus = ModbusTcpManager::instance();
|
|
// const int timeoutMs = qMax(1, modbus.config().feedbackTimeoutMs);
|
|
// QElapsedTimer timer;
|
|
// timer.start();
|
|
|
|
//while (timer.elapsed() < timeoutMs) {
|
|
while (true) {
|
|
bool feedback = false;
|
|
if (!modbus.readCoil(address, &feedback, error)) {
|
|
return false;
|
|
}
|
|
if (feedback) {
|
|
return true;
|
|
}
|
|
QThread::msleep(FeedbackPollIntervalMs);
|
|
}
|
|
|
|
if (error) {
|
|
*error = QString("PLC feedback timeout at address %1").arg(address);
|
|
}
|
|
return false;
|
|
}
|