955 lines
30 KiB
C++
955 lines
30 KiB
C++
#include "deviceservice.h"
|
|
|
|
#include "../config/configmanager.h"
|
|
#include "../modbus/modbustcpmanager.h"
|
|
|
|
#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(QObject *parent)
|
|
: QObject(parent)
|
|
, m_pollTimer(new QTimer(this))
|
|
, m_modbus(new ModbusTcpManager(
|
|
ConfigManager::instance().modbusConfig(), this))
|
|
, m_statusRequestPending(false)
|
|
, m_controlRequestPending(false)
|
|
, m_productionActionActive(false)
|
|
, m_productionActionBusy(false)
|
|
, m_productionCommandAddress(-1)
|
|
, m_productionFeedbackAddress(-1)
|
|
, m_terminationActive(false)
|
|
, m_terminationSignalIndex(0)
|
|
, m_controlCommandAddress(-1)
|
|
, m_controlFeedbackAddress(-1)
|
|
{
|
|
connect(m_pollTimer, &QTimer::timeout,
|
|
this, &DeviceService::pollStatus);
|
|
}
|
|
|
|
void DeviceService::startPolling()
|
|
{
|
|
if (m_terminationActive) {
|
|
return;
|
|
}
|
|
|
|
if (m_pollTimer->isActive()) {
|
|
return;
|
|
}
|
|
|
|
const int interval = qMax(100, m_modbus->config().pollIntervalMs);
|
|
m_pollTimer->start(interval);
|
|
pollStatus();
|
|
}
|
|
|
|
void DeviceService::stopPolling()
|
|
{
|
|
m_pollTimer->stop();
|
|
}
|
|
|
|
bool DeviceService::isPolling() const
|
|
{
|
|
return m_pollTimer->isActive();
|
|
}
|
|
|
|
void DeviceService::requestStatus(const StatusCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
DeviceStatus status;
|
|
callback(false, status, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
const DeviceStatus status = simulatedStatus();
|
|
emit statusChanged(status);
|
|
if (callback) {
|
|
callback(true, status, QString());
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (m_statusRequestPending) {
|
|
if (callback) {
|
|
m_statusCallbacks.append(callback);
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_statusRequestPending = true;
|
|
m_statusCallbacks.clear();
|
|
if (callback) {
|
|
m_statusCallbacks.append(callback);
|
|
}
|
|
DeviceStatus status;
|
|
status.plcConnected = true;
|
|
readStatusStep(0, status);
|
|
}
|
|
|
|
void DeviceService::finishStatusRequest(bool success,
|
|
const DeviceStatus &status,
|
|
const QString &error)
|
|
{
|
|
DeviceStatus finalStatus = status;
|
|
finalStatus.statusValid = success;
|
|
if (success) {
|
|
finalStatus.safetyDoorClosed = finalStatus.safetyDoorClosed
|
|
&& finalStatus.safetyDoor1Closed
|
|
&& finalStatus.safetyDoor2Closed
|
|
&& finalStatus.safetyDoor3Closed;
|
|
}
|
|
|
|
m_statusRequestPending = false;
|
|
const QList<StatusCallback> callbacks = m_statusCallbacks;
|
|
m_statusCallbacks.clear();
|
|
emit statusChanged(finalStatus);
|
|
if (!success && !error.isEmpty()) {
|
|
emit communicationError(error);
|
|
}
|
|
for (const StatusCallback &callback : callbacks) {
|
|
if (callback) {
|
|
callback(success, finalStatus, error);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DeviceService::readStatusStep(int index, const DeviceStatus &status)
|
|
{
|
|
const bool useErrorCode =
|
|
ConfigManager::instance().productionConfig().usePlcErrorCode;
|
|
if (index >= (useErrorCode ? 15 : 14)) {
|
|
finishStatusRequest(true, status, QString());
|
|
return;
|
|
}
|
|
|
|
auto failed = [this](const QString &error) {
|
|
DeviceStatus status;
|
|
status.plcConnected = false;
|
|
finishStatusRequest(
|
|
false, status,
|
|
error.isEmpty() ? "Failed to read PLC status" : error);
|
|
};
|
|
|
|
auto next = [this, index, status, failed](
|
|
bool success, quint16 value, const QString &error) {
|
|
if (!success) {
|
|
failed(error);
|
|
return;
|
|
}
|
|
|
|
DeviceStatus nextStatus = status;
|
|
switch (index) {
|
|
case 0: nextStatus.plcReady = value != 0; break;
|
|
case 1: nextStatus.robotReady = value != 0; break;
|
|
case 2: nextStatus.safetyDoorClosed = value != 0; break;
|
|
case 3: nextStatus.emergencyStop = value != 0; break;
|
|
case 4: nextStatus.inspectionMachineReady = value != 0; break;
|
|
case 5: nextStatus.visionReady = value != 0; break;
|
|
case 6: nextStatus.robotControlCabinetReady = value != 0; break;
|
|
case 7: nextStatus.mainControlCabinetReady = value != 0; break;
|
|
case 8: nextStatus.safetyDoor1Closed = value != 0; break;
|
|
case 9: nextStatus.safetyDoor2Closed = value != 0; break;
|
|
case 10: nextStatus.safetyDoor3Closed = value != 0; break;
|
|
case 11: nextStatus.activeAlarm = value != 0; break;
|
|
case 12: nextStatus.currentStep = value; break;
|
|
case 13: nextStatus.checkResult = value; break;
|
|
case 14: nextStatus.errorCode = value; break;
|
|
default: break;
|
|
}
|
|
readStatusStep(index + 1, nextStatus);
|
|
};
|
|
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
switch (index) {
|
|
case 0:
|
|
m_modbus->readCoil(addresses.plcReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 1:
|
|
m_modbus->readCoil(addresses.robotReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 2:
|
|
m_modbus->readCoil(addresses.safetyDoorClosed,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 3:
|
|
m_modbus->readCoil(addresses.emergencyStop,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 4:
|
|
m_modbus->readCoil(addresses.inspectionMachineReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 5:
|
|
m_modbus->readCoil(addresses.visionReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 6:
|
|
m_modbus->readCoil(addresses.robotControlCabinetReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 7:
|
|
m_modbus->readCoil(addresses.mainControlCabinetReady,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 8:
|
|
m_modbus->readCoil(addresses.safetyDoor1Closed,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 9:
|
|
m_modbus->readCoil(addresses.safetyDoor2Closed,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 10:
|
|
m_modbus->readCoil(addresses.safetyDoor3Closed,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 11:
|
|
m_modbus->readCoil(addresses.activeAlarm,
|
|
[next](bool ok, bool value, const QString &error) {
|
|
next(ok, value ? 1 : 0, error);
|
|
});
|
|
break;
|
|
case 12:
|
|
m_modbus->readHoldingRegister(addresses.currentStep, next);
|
|
break;
|
|
case 13:
|
|
m_modbus->readHoldingRegister(addresses.checkResult, next);
|
|
break;
|
|
case 14:
|
|
if (useErrorCode) {
|
|
m_modbus->readHoldingRegister(addresses.errorCode, next);
|
|
} else {
|
|
finishStatusRequest(true, status, QString());
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
void DeviceService::pollStatus()
|
|
{
|
|
if (m_terminationActive || m_statusRequestPending || m_controlRequestPending
|
|
|| m_productionActionBusy) {
|
|
return;
|
|
}
|
|
|
|
requestStatus([this](bool success, const DeviceStatus &, const QString &error) {
|
|
if (!success && !error.isEmpty()) {
|
|
emit communicationError(error);
|
|
}
|
|
});
|
|
}
|
|
|
|
DeviceStatus DeviceService::simulatedStatus() const
|
|
{
|
|
DeviceStatus status;
|
|
status.statusValid = true;
|
|
status.plcConnected = true;
|
|
status.plcReady = true;
|
|
status.robotReady = true;
|
|
status.safetyDoorClosed = true;
|
|
status.emergencyStop = false;
|
|
status.inspectionMachineReady = true;
|
|
status.visionReady = true;
|
|
status.robotControlCabinetReady = true;
|
|
status.mainControlCabinetReady = true;
|
|
status.safetyDoor1Closed = true;
|
|
status.safetyDoor2Closed = true;
|
|
status.safetyDoor3Closed = true;
|
|
status.activeAlarm = false;
|
|
status.currentStep = 0;
|
|
status.checkResult = 0;
|
|
return status;
|
|
}
|
|
|
|
void DeviceService::start(const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (callback) {
|
|
callback(false, "Production control is disabled by configuration");
|
|
}
|
|
return;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
executeCommand(addresses.startCommand, addresses.startFeedback, callback);
|
|
}
|
|
|
|
void DeviceService::pause(const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (callback) {
|
|
callback(false, "Production control is disabled by configuration");
|
|
}
|
|
return;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
executeCommand(addresses.pauseCommand, addresses.pauseFeedback, callback);
|
|
}
|
|
|
|
void DeviceService::reset(const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (callback) {
|
|
callback(false, "Production control is disabled by configuration");
|
|
}
|
|
return;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
executeCommand(addresses.resetCommand, addresses.resetFeedback, callback);
|
|
}
|
|
|
|
void DeviceService::clearAlarm(const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (callback) {
|
|
callback(false, "Production control is disabled by configuration");
|
|
}
|
|
return;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
executeCommand(addresses.clearAlarmCommand,
|
|
addresses.clearAlarmFeedback, callback);
|
|
}
|
|
|
|
void DeviceService::setInspectionMode(const QString &mode,
|
|
const OperationCallback &callback)
|
|
{
|
|
if (mode != "BEFORE_PLATING" && mode != "AFTER_PLATING") {
|
|
if (callback) {
|
|
callback(false,
|
|
"Inspection mode must be BEFORE_PLATING or AFTER_PLATING");
|
|
}
|
|
return;
|
|
}
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (callback) {
|
|
callback(false, "Production control is disabled by configuration");
|
|
}
|
|
return;
|
|
}
|
|
if (m_controlRequestPending || m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(false, "Another PLC operation is already active");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_controlRequestPending = true;
|
|
const bool afterPlating = mode == "AFTER_PLATING";
|
|
m_modbus->writeCoil(
|
|
m_modbus->config().addressMap.inspectionMode, afterPlating,
|
|
[this, callback](bool success, const QString &error) {
|
|
m_controlRequestPending = false;
|
|
if (callback) {
|
|
callback(success, error);
|
|
}
|
|
});
|
|
}
|
|
|
|
void DeviceService::terminate(const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is already in progress");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_terminationActive = true;
|
|
m_terminationCallback = callback;
|
|
m_terminationSignals.clear();
|
|
m_terminationSignalIndex = 0;
|
|
m_pollTimer->stop();
|
|
|
|
const QList<QPair<int, int>> currentSignals = QList<QPair<int, int>>()
|
|
<< qMakePair(m_controlCommandAddress, m_controlFeedbackAddress)
|
|
<< qMakePair(m_productionCommandAddress,
|
|
m_productionFeedbackAddress);
|
|
for (const QPair<int, int> &signalPair : currentSignals) {
|
|
if (signalPair.first >= 0 && signalPair.second >= 0
|
|
&& !m_terminationSignals.contains(signalPair)) {
|
|
m_terminationSignals.append(signalPair);
|
|
}
|
|
}
|
|
|
|
m_modbus->abortAll("PLC termination requested");
|
|
m_statusRequestPending = false;
|
|
m_controlRequestPending = false;
|
|
m_productionActionActive = false;
|
|
m_productionActionBusy = false;
|
|
if (ConfigManager::instance().simulationMode()
|
|
|| !ConfigManager::instance().productionEnabled()) {
|
|
finishTermination(true, QString());
|
|
return;
|
|
}
|
|
clearNextTerminationSignal();
|
|
}
|
|
|
|
void DeviceService::clearNextTerminationSignal()
|
|
{
|
|
if (!m_terminationActive) {
|
|
return;
|
|
}
|
|
if (m_terminationSignalIndex >= m_terminationSignals.size()) {
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
executeCommand(
|
|
addresses.pauseCommand, addresses.pauseFeedback,
|
|
[this](bool success, const QString &error) {
|
|
if (!success) {
|
|
finishTermination(false, error);
|
|
return;
|
|
}
|
|
finishTermination(true, QString());
|
|
}, true);
|
|
return;
|
|
}
|
|
|
|
const QPair<int, int> signalPair =
|
|
m_terminationSignals.at(m_terminationSignalIndex++);
|
|
clearSignals(
|
|
signalPair.first, signalPair.second,
|
|
[this](bool success, const QString &error) {
|
|
if (!success) {
|
|
finishTermination(false, error);
|
|
return;
|
|
}
|
|
clearNextTerminationSignal();
|
|
});
|
|
}
|
|
|
|
void DeviceService::finishTermination(bool success, const QString &error)
|
|
{
|
|
if (!m_terminationActive) {
|
|
return;
|
|
}
|
|
|
|
const OperationCallback callback = m_terminationCallback;
|
|
m_terminationCallback = OperationCallback();
|
|
m_terminationActive = false;
|
|
m_terminationSignals.clear();
|
|
m_terminationSignalIndex = 0;
|
|
m_controlRequestPending = false;
|
|
m_productionActionActive = false;
|
|
m_productionActionBusy = false;
|
|
if (success) {
|
|
m_controlCommandAddress = -1;
|
|
m_controlFeedbackAddress = -1;
|
|
m_productionCommandAddress = -1;
|
|
m_productionFeedbackAddress = -1;
|
|
}
|
|
if (callback) {
|
|
callback(success, error);
|
|
}
|
|
}
|
|
|
|
void DeviceService::executeCommand(int commandAddress, int feedbackAddress,
|
|
const OperationCallback &callback,
|
|
bool allowDuringTermination)
|
|
{
|
|
if (m_terminationActive && !allowDuringTermination) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
if (m_controlRequestPending || m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(false, "Another PLC operation is already active");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_controlRequestPending = true;
|
|
m_controlCommandAddress = commandAddress;
|
|
m_controlFeedbackAddress = feedbackAddress;
|
|
writeCommandAndWaitFeedback(commandAddress, feedbackAddress,
|
|
[this, commandAddress, feedbackAddress,
|
|
callback](bool success,
|
|
const QString &error) {
|
|
m_controlRequestPending = false;
|
|
if (success
|
|
&& m_controlCommandAddress
|
|
== commandAddress
|
|
&& m_controlFeedbackAddress
|
|
== feedbackAddress) {
|
|
m_controlCommandAddress = -1;
|
|
m_controlFeedbackAddress = -1;
|
|
}
|
|
if (callback) {
|
|
callback(success, error);
|
|
}
|
|
}, allowDuringTermination);
|
|
}
|
|
|
|
void DeviceService::writeCommandAndWaitFeedback(
|
|
int commandAddress, int feedbackAddress,
|
|
const OperationCallback &callback, bool allowDuringTermination)
|
|
{
|
|
if (m_terminationActive && !allowDuringTermination) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
m_modbus->writeCoil(
|
|
commandAddress, true,
|
|
[this, commandAddress, feedbackAddress, callback,
|
|
allowDuringTermination](bool success,
|
|
const QString &error) {
|
|
if (!success) {
|
|
if (callback) {
|
|
callback(false, error);
|
|
}
|
|
return;
|
|
}
|
|
pollFeedback(commandAddress, feedbackAddress, callback,
|
|
allowDuringTermination);
|
|
});
|
|
}
|
|
|
|
void DeviceService::pollFeedback(int commandAddress, int feedbackAddress,
|
|
const OperationCallback &callback,
|
|
bool allowDuringTermination)
|
|
{
|
|
if (m_terminationActive && !allowDuringTermination) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
m_modbus->readCoil(
|
|
feedbackAddress,
|
|
[this, commandAddress, feedbackAddress, callback,
|
|
allowDuringTermination](
|
|
bool success, bool feedback,
|
|
const QString &error) {
|
|
if (!success) {
|
|
if (callback) {
|
|
callback(false, error);
|
|
}
|
|
return;
|
|
}
|
|
if (!feedback) {
|
|
QTimer::singleShot(
|
|
FeedbackPollIntervalMs, this,
|
|
[this, commandAddress, feedbackAddress, callback,
|
|
allowDuringTermination]() {
|
|
pollFeedback(commandAddress, feedbackAddress, callback,
|
|
allowDuringTermination);
|
|
});
|
|
return;
|
|
}
|
|
|
|
clearSignals(commandAddress, feedbackAddress,
|
|
[callback](bool cleared, const QString &clearError) {
|
|
if (callback) {
|
|
callback(cleared, clearError);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
void DeviceService::beginProductionAction(
|
|
ProductionAction action, int trayNo, int slotNo,
|
|
const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (!ConfigManager::instance().productionEnabled()) {
|
|
if (callback) {
|
|
callback(false, "Production control is disabled by configuration");
|
|
}
|
|
return;
|
|
}
|
|
if (m_productionActionActive || m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(false, "A PLC production action is already active");
|
|
}
|
|
return;
|
|
}
|
|
if (trayNo <= 0 || slotNo < 1 || slotNo > 49) {
|
|
if (callback) {
|
|
callback(false, "PLC tray or slot position is invalid");
|
|
}
|
|
return;
|
|
}
|
|
|
|
const ModbusAddressMap &addresses = m_modbus->config().addressMap;
|
|
if (!actionAddresses(action, addresses,
|
|
&m_productionCommandAddress,
|
|
&m_productionFeedbackAddress)) {
|
|
if (callback) {
|
|
callback(false, "Unknown PLC production action");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_productionActionBusy = true;
|
|
m_modbus->readCoil(
|
|
m_productionFeedbackAddress,
|
|
[this, action, trayNo, slotNo, addresses, callback](
|
|
bool success, bool feedback, const QString &error) {
|
|
if (!success) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(false, error);
|
|
}
|
|
return;
|
|
}
|
|
if (feedback) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(false,
|
|
"PLC feedback is still active before a new action");
|
|
}
|
|
return;
|
|
}
|
|
|
|
auto sendCommand =
|
|
[this, callback](bool success, const QString &error) {
|
|
if (!success) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(false, error);
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_productionActionActive = true;
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
};
|
|
|
|
auto writeCommand = [this, sendCommand](bool success,
|
|
const QString &error) {
|
|
if (!success) {
|
|
sendCommand(false, error);
|
|
return;
|
|
}
|
|
m_modbus->writeCoil(
|
|
m_productionCommandAddress, true, sendCommand);
|
|
};
|
|
|
|
if (action == RackPick || action == RackPlace) {
|
|
m_modbus->writeHoldingRegister(
|
|
addresses.trayNumber,
|
|
static_cast<quint16>(trayNo),
|
|
[this, addresses, slotNo, writeCommand](
|
|
bool traySuccess, const QString &trayError) {
|
|
if (!traySuccess) {
|
|
writeCommand(false, trayError);
|
|
return;
|
|
}
|
|
m_modbus->writeHoldingRegister(
|
|
addresses.slotNumber,
|
|
static_cast<quint16>(slotNo),
|
|
writeCommand);
|
|
});
|
|
return;
|
|
}
|
|
|
|
writeCommand(true, QString());
|
|
});
|
|
}
|
|
|
|
void DeviceService::pollProductionAction(const ActionCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
if (ConfigManager::instance().simulationMode()) {
|
|
if (callback) {
|
|
callback(true, true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(true, false, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (!m_productionActionActive) {
|
|
if (callback) {
|
|
callback(false, false, "No PLC production action is active");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_productionActionBusy = true;
|
|
m_modbus->readCoil(
|
|
m_productionFeedbackAddress,
|
|
[this, callback](bool success, bool feedback,
|
|
const QString &error) {
|
|
if (!success) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(false, false, error);
|
|
}
|
|
return;
|
|
}
|
|
if (!feedback) {
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(true, false, QString());
|
|
}
|
|
return;
|
|
}
|
|
|
|
clearActionSignals(
|
|
[this, callback](bool cleared, const QString &clearError) {
|
|
m_productionActionBusy = false;
|
|
if (cleared) {
|
|
m_productionActionActive = false;
|
|
m_productionCommandAddress = -1;
|
|
m_productionFeedbackAddress = -1;
|
|
if (callback) {
|
|
callback(true, true, QString());
|
|
}
|
|
} else if (callback) {
|
|
callback(false, false, clearError);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
void DeviceService::cancelProductionAction(const OperationCallback &callback)
|
|
{
|
|
if (m_terminationActive) {
|
|
if (callback) {
|
|
callback(false, "PLC termination is in progress");
|
|
}
|
|
return;
|
|
}
|
|
if (ConfigManager::instance().simulationMode()
|
|
|| (!m_productionActionActive && !m_productionActionBusy)) {
|
|
m_productionActionActive = false;
|
|
m_productionActionBusy = false;
|
|
if (callback) {
|
|
callback(true, QString());
|
|
}
|
|
return;
|
|
}
|
|
if (m_productionActionBusy) {
|
|
if (callback) {
|
|
callback(false, "PLC production action is busy");
|
|
}
|
|
return;
|
|
}
|
|
|
|
m_productionActionBusy = true;
|
|
clearActionSignals(
|
|
[this, callback](bool cleared, const QString &error) {
|
|
m_productionActionBusy = false;
|
|
if (cleared) {
|
|
m_productionActionActive = false;
|
|
m_productionCommandAddress = -1;
|
|
m_productionFeedbackAddress = -1;
|
|
}
|
|
if (callback) {
|
|
callback(cleared, error);
|
|
}
|
|
});
|
|
}
|
|
|
|
void DeviceService::clearActionSignals(const OperationCallback &callback)
|
|
{
|
|
if (m_productionCommandAddress < 0
|
|
|| m_productionFeedbackAddress < 0) {
|
|
if (callback) {
|
|
callback(false, "PLC production action addresses are invalid");
|
|
}
|
|
return;
|
|
}
|
|
|
|
clearSignals(m_productionCommandAddress, m_productionFeedbackAddress,
|
|
callback);
|
|
}
|
|
|
|
void DeviceService::clearSignals(int commandAddress, int feedbackAddress,
|
|
const OperationCallback &callback)
|
|
{
|
|
m_modbus->writeCoil(
|
|
commandAddress, false,
|
|
[this, feedbackAddress, callback](bool commandCleared,
|
|
const QString &commandError) {
|
|
m_modbus->writeCoil(
|
|
feedbackAddress, false,
|
|
[callback, commandCleared, commandError](
|
|
bool feedbackCleared, const QString &feedbackError) {
|
|
if (callback) {
|
|
callback(commandCleared && feedbackCleared,
|
|
commandCleared ? feedbackError
|
|
: commandError);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
bool DeviceService::productionActionActive() const
|
|
{
|
|
return m_productionActionActive;
|
|
}
|
|
|
|
bool DeviceService::productionActionBusy() const
|
|
{
|
|
return m_productionActionBusy || m_modbus->isBusy();
|
|
}
|