2026-08-18 09:40:43 +08:00
|
|
|
#include "configmanager.h"
|
|
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QStringList>
|
2026-10-27 22:16:55 +08:00
|
|
|
#include <QSet>
|
|
|
|
|
|
|
|
|
|
bool VisionConfig::isValid(QString *error) const
|
|
|
|
|
{
|
|
|
|
|
QStringList errors;
|
2026-10-28 15:19:18 +08:00
|
|
|
if (!contractConfirmed || !photoPoseReadyConfirmed) {
|
|
|
|
|
errors.append("Photo-position PLC contract is not confirmed");
|
2026-10-27 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
if (ipcName.trimmed().isEmpty() || startupTimeoutMs <= 0
|
2026-10-28 15:19:18 +08:00
|
|
|
|| initializeTimeoutMs <= 0 || requestTimeoutMs <= 5000) {
|
2026-10-27 22:16:55 +08:00
|
|
|
errors.append("Vision IPC name or timeouts are invalid");
|
|
|
|
|
}
|
|
|
|
|
if (photoPoseR < 0 || photoPoseR > 32756 || photoPoseR % 2
|
|
|
|
|
|| graspPoseR < 0 || graspPoseR > 32756 || graspPoseR % 2
|
|
|
|
|
|| qAbs(photoPoseR - graspPoseR) < 12) {
|
|
|
|
|
errors.append("Vision REAL blocks must be distinct, even, 12-word R ranges");
|
|
|
|
|
}
|
2026-10-29 17:20:14 +08:00
|
|
|
const int coils[] = {photoMoveCoil, photoMoveCoilFeedback, comparisonPickCoil};
|
2026-10-27 22:16:55 +08:00
|
|
|
QSet<int> unique;
|
|
|
|
|
for (int coil : coils) {
|
|
|
|
|
if (coil < 0 || coil > 65535 || unique.contains(coil)) {
|
|
|
|
|
errors.append("Vision coil addresses must be configured and distinct");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
unique.insert(coil);
|
|
|
|
|
}
|
|
|
|
|
for (auto it = trayMap.constBegin(); it != trayMap.constEnd(); ++it) {
|
|
|
|
|
if (it.key() < 1 || it.value() < 1 || it.value() > 99) {
|
|
|
|
|
errors.append("Vision tray mapping is invalid");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = errors.join("; ");
|
|
|
|
|
}
|
|
|
|
|
return errors.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VisionConfig::visionTrayNo(int sspTrayNo) const
|
|
|
|
|
{
|
|
|
|
|
return trayMap.value(sspTrayNo, -1);
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
|
|
|
|
ConfigManager &ConfigManager::instance()
|
|
|
|
|
{
|
|
|
|
|
static ConfigManager manager;
|
|
|
|
|
return manager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConfigManager::ConfigManager()
|
|
|
|
|
: m_databaseConfig(DbConfig::defaultConfig()),
|
|
|
|
|
m_modbusConfig(ModbusConfig::defaultConfig()),
|
2026-08-24 17:28:59 +08:00
|
|
|
m_simulationMode(false),
|
|
|
|
|
m_productionEnabled(true)
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConfigManager::load(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
m_filePath = filePath.isEmpty() ? defaultFilePath() : filePath;
|
|
|
|
|
|
|
|
|
|
QSettings settings(m_filePath, QSettings::IniFormat);
|
|
|
|
|
m_databaseConfig.driverName = settings.value("database/driver", m_databaseConfig.driverName).toString();
|
|
|
|
|
m_databaseConfig.hostName = settings.value("database/host", m_databaseConfig.hostName).toString();
|
|
|
|
|
m_databaseConfig.port = settings.value("database/port", m_databaseConfig.port).toInt();
|
|
|
|
|
m_databaseConfig.databaseName = settings.value("database/name", m_databaseConfig.databaseName).toString();
|
|
|
|
|
m_databaseConfig.userName = settings.value("database/user", m_databaseConfig.userName).toString();
|
|
|
|
|
m_databaseConfig.password = settings.value("database/password", m_databaseConfig.password).toString();
|
|
|
|
|
m_databaseConfig.connectionName = settings.value("database/connection", m_databaseConfig.connectionName).toString();
|
|
|
|
|
|
|
|
|
|
m_modbusConfig.host = settings.value("modbus/host", m_modbusConfig.host).toString();
|
|
|
|
|
m_modbusConfig.port = settings.value("modbus/port", m_modbusConfig.port).toInt();
|
|
|
|
|
m_modbusConfig.serverAddress = settings.value("modbus/serverAddress", m_modbusConfig.serverAddress).toInt();
|
|
|
|
|
m_modbusConfig.timeoutMs = settings.value("modbus/timeoutMs", m_modbusConfig.timeoutMs).toInt();
|
|
|
|
|
m_modbusConfig.retryCount = settings.value("modbus/retryCount", m_modbusConfig.retryCount).toInt();
|
|
|
|
|
m_modbusConfig.pollIntervalMs = settings.value("modbus/pollIntervalMs", m_modbusConfig.pollIntervalMs).toInt();
|
|
|
|
|
m_modbusConfig.feedbackTimeoutMs = settings.value("modbus/feedbackTimeoutMs", m_modbusConfig.feedbackTimeoutMs).toInt();
|
|
|
|
|
m_modbusConfig.reconnectDelayMs = settings.value("modbus/reconnectDelayMs", m_modbusConfig.reconnectDelayMs).toInt();
|
|
|
|
|
m_modbusConfig.reconnectAttempts = settings.value("modbus/reconnectAttempts", m_modbusConfig.reconnectAttempts).toInt();
|
|
|
|
|
|
|
|
|
|
ModbusAddressMap &addresses = m_modbusConfig.addressMap;
|
|
|
|
|
addresses.clearAlarmCommand = settings.value("modbus/clearAlarmCommand", addresses.clearAlarmCommand).toInt();
|
|
|
|
|
addresses.plcReady = settings.value("modbus/plcReady", addresses.plcReady).toInt();
|
|
|
|
|
addresses.robotReady = settings.value("modbus/robotReady", addresses.robotReady).toInt();
|
|
|
|
|
addresses.safetyDoorClosed = settings.value("modbus/safetyDoorClosed", addresses.safetyDoorClosed).toInt();
|
|
|
|
|
addresses.emergencyStop = settings.value("modbus/emergencyStop", addresses.emergencyStop).toInt();
|
2026-08-24 17:28:59 +08:00
|
|
|
addresses.inspectionMachineReady =
|
|
|
|
|
settings.value("modbus/inspectionMachineReady", addresses.inspectionMachineReady).toInt();
|
|
|
|
|
addresses.visionReady =
|
|
|
|
|
settings.value("modbus/visionReady", addresses.visionReady).toInt();
|
|
|
|
|
addresses.robotControlCabinetReady =
|
|
|
|
|
settings.value("modbus/robotControlCabinetReady", addresses.robotControlCabinetReady).toInt();
|
|
|
|
|
addresses.mainControlCabinetReady =
|
|
|
|
|
settings.value("modbus/mainControlCabinetReady", addresses.mainControlCabinetReady).toInt();
|
|
|
|
|
addresses.safetyDoor1Closed =
|
|
|
|
|
settings.value("modbus/safetyDoor1Closed", addresses.safetyDoor1Closed).toInt();
|
|
|
|
|
addresses.safetyDoor2Closed =
|
|
|
|
|
settings.value("modbus/safetyDoor2Closed", addresses.safetyDoor2Closed).toInt();
|
|
|
|
|
addresses.safetyDoor3Closed =
|
|
|
|
|
settings.value("modbus/safetyDoor3Closed", addresses.safetyDoor3Closed).toInt();
|
|
|
|
|
addresses.activeAlarm =
|
|
|
|
|
settings.value("modbus/activeAlarm", addresses.activeAlarm).toInt();
|
2026-08-18 09:40:43 +08:00
|
|
|
addresses.currentStep = settings.value("modbus/currentStep", addresses.currentStep).toInt();
|
|
|
|
|
addresses.checkResult = settings.value("modbus/checkResult", addresses.checkResult).toInt();
|
|
|
|
|
addresses.errorCode = settings.value("modbus/errorCode", addresses.errorCode).toInt();
|
2026-08-20 11:22:55 +08:00
|
|
|
addresses.rackPickCommand =
|
|
|
|
|
settings.value("modbus/rackPickCommand", addresses.rackPickCommand).toInt();
|
|
|
|
|
addresses.frontLoadCommand =
|
|
|
|
|
settings.value("modbus/frontLoadCommand", addresses.frontLoadCommand).toInt();
|
|
|
|
|
addresses.inspectionCommand =
|
|
|
|
|
settings.value("modbus/inspectionCommand", addresses.inspectionCommand).toInt();
|
|
|
|
|
addresses.frontUnloadCommand =
|
|
|
|
|
settings.value("modbus/frontUnloadCommand", addresses.frontUnloadCommand).toInt();
|
|
|
|
|
addresses.flipCommand =
|
|
|
|
|
settings.value("modbus/flipCommand", addresses.flipCommand).toInt();
|
|
|
|
|
addresses.backLoadCommand =
|
|
|
|
|
settings.value("modbus/backLoadCommand", addresses.backLoadCommand).toInt();
|
|
|
|
|
addresses.backUnloadCommand =
|
|
|
|
|
settings.value("modbus/backUnloadCommand", addresses.backUnloadCommand).toInt();
|
|
|
|
|
addresses.rackPlaceCommand =
|
|
|
|
|
settings.value("modbus/rackPlaceCommand", addresses.rackPlaceCommand).toInt();
|
|
|
|
|
addresses.rackPickFeedback =
|
|
|
|
|
settings.value("modbus/rackPickFeedback", addresses.rackPickFeedback).toInt();
|
|
|
|
|
addresses.frontLoadFeedback =
|
|
|
|
|
settings.value("modbus/frontLoadFeedback", addresses.frontLoadFeedback).toInt();
|
|
|
|
|
addresses.inspectionFeedback =
|
|
|
|
|
settings.value("modbus/inspectionFeedback", addresses.inspectionFeedback).toInt();
|
|
|
|
|
addresses.frontUnloadFeedback =
|
|
|
|
|
settings.value("modbus/frontUnloadFeedback", addresses.frontUnloadFeedback).toInt();
|
|
|
|
|
addresses.flipFeedback =
|
|
|
|
|
settings.value("modbus/flipFeedback", addresses.flipFeedback).toInt();
|
|
|
|
|
addresses.backLoadFeedback =
|
|
|
|
|
settings.value("modbus/backLoadFeedback", addresses.backLoadFeedback).toInt();
|
|
|
|
|
addresses.backUnloadFeedback =
|
|
|
|
|
settings.value("modbus/backUnloadFeedback", addresses.backUnloadFeedback).toInt();
|
|
|
|
|
addresses.rackPlaceFeedback =
|
|
|
|
|
settings.value("modbus/rackPlaceFeedback", addresses.rackPlaceFeedback).toInt();
|
|
|
|
|
addresses.inspectionMode =
|
|
|
|
|
settings.value("modbus/inspectionMode", addresses.inspectionMode).toInt();
|
|
|
|
|
addresses.trayNumber =
|
|
|
|
|
settings.value("modbus/trayNumber", addresses.trayNumber).toInt();
|
|
|
|
|
addresses.slotNumber =
|
|
|
|
|
settings.value("modbus/slotNumber", addresses.slotNumber).toInt();
|
2026-10-30 16:43:24 +08:00
|
|
|
addresses.slotRowNumber =
|
|
|
|
|
settings.value("modbus/slotRowNumber", addresses.slotRowNumber).toInt();
|
|
|
|
|
addresses.slotColumNumber =
|
|
|
|
|
settings.value("modbus/slotColumNumber", addresses.slotColumNumber).toInt();
|
2026-08-18 09:40:43 +08:00
|
|
|
|
|
|
|
|
m_productionConfig.loadStep =
|
|
|
|
|
settings.value("production/loadStep", m_productionConfig.loadStep).toInt();
|
|
|
|
|
m_productionConfig.frontCheckStep =
|
|
|
|
|
settings.value("production/frontCheckStep", m_productionConfig.frontCheckStep).toInt();
|
|
|
|
|
m_productionConfig.backCheckStep =
|
|
|
|
|
settings.value("production/backCheckStep", m_productionConfig.backCheckStep).toInt();
|
|
|
|
|
m_productionConfig.unloadStep =
|
|
|
|
|
settings.value("production/unloadStep", m_productionConfig.unloadStep).toInt();
|
|
|
|
|
m_productionConfig.cycleCompleteStep =
|
|
|
|
|
settings.value("production/cycleCompleteStep",
|
|
|
|
|
m_productionConfig.cycleCompleteStep).toInt();
|
|
|
|
|
m_productionConfig.okResult =
|
|
|
|
|
settings.value("production/okResult", m_productionConfig.okResult).toInt();
|
|
|
|
|
m_productionConfig.ngResult =
|
|
|
|
|
settings.value("production/ngResult", m_productionConfig.ngResult).toInt();
|
|
|
|
|
m_productionConfig.errorResult =
|
|
|
|
|
settings.value("production/errorResult", m_productionConfig.errorResult).toInt();
|
|
|
|
|
m_productionConfig.simulationResult =
|
|
|
|
|
settings.value("production/simulationResult",
|
|
|
|
|
m_productionConfig.simulationResult).toInt();
|
|
|
|
|
m_productionConfig.timeoutWarningMs =
|
|
|
|
|
settings.value("production/timeoutWarningMs",
|
|
|
|
|
m_productionConfig.timeoutWarningMs).toInt();
|
|
|
|
|
m_productionConfig.stateTimeoutMs =
|
|
|
|
|
settings.value("production/stateTimeoutMs",
|
|
|
|
|
m_productionConfig.stateTimeoutMs).toInt();
|
|
|
|
|
m_productionConfig.maxRecoveryRetries =
|
|
|
|
|
settings.value("production/maxRecoveryRetries",
|
|
|
|
|
m_productionConfig.maxRecoveryRetries).toInt();
|
|
|
|
|
m_productionConfig.simulationStepMs =
|
|
|
|
|
settings.value("production/simulationStepMs",
|
|
|
|
|
m_productionConfig.simulationStepMs).toInt();
|
2026-08-23 09:41:44 +08:00
|
|
|
m_productionConfig.usePlcErrorCode =
|
|
|
|
|
settings.value("production/usePlcErrorCode",
|
|
|
|
|
m_productionConfig.usePlcErrorCode).toBool();
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-10-27 22:16:55 +08:00
|
|
|
// Reload from safe defaults so removing a key cannot retain an old contract.
|
|
|
|
|
m_visionConfig = VisionConfig();
|
|
|
|
|
m_visionConfig.enabled = settings.value("vision/enabled", false).toBool();
|
|
|
|
|
m_visionConfig.contractConfirmed = settings.value("vision/contractConfirmed", false).toBool();
|
|
|
|
|
m_visionConfig.photoPoseReadyConfirmed = settings.value("vision/photoPoseReadyConfirmed", false).toBool();
|
|
|
|
|
m_visionConfig.executablePath = settings.value("vision/executablePath").toString();
|
|
|
|
|
m_visionConfig.ipcName = settings.value("vision/ipcName", m_visionConfig.ipcName).toString();
|
|
|
|
|
m_visionConfig.startupTimeoutMs = settings.value("vision/startupTimeoutMs", m_visionConfig.startupTimeoutMs).toInt();
|
|
|
|
|
m_visionConfig.initializeTimeoutMs = settings.value("vision/initializeTimeoutMs", m_visionConfig.initializeTimeoutMs).toInt();
|
|
|
|
|
m_visionConfig.requestTimeoutMs = settings.value("vision/requestTimeoutMs", m_visionConfig.requestTimeoutMs).toInt();
|
2026-10-29 17:20:14 +08:00
|
|
|
// PLC settings belong to modbus; retain vision keys for existing installations.
|
|
|
|
|
m_visionConfig.photoPoseR = settings.value("modbus/photoPoseR", settings.value("vision/photoPoseR", 40)).toInt();
|
|
|
|
|
m_visionConfig.graspPoseR = settings.value("modbus/graspPoseR", settings.value("vision/graspPoseR", 60)).toInt();
|
|
|
|
|
m_visionConfig.lowWordFirst = settings.value("modbus/lowWordFirst", settings.value("vision/lowWordFirst", true)).toBool();
|
|
|
|
|
m_visionConfig.photoMoveCoil = settings.value("modbus/photoMoveCoil", settings.value("vision/photoMoveCoil", 850)).toInt();
|
|
|
|
|
m_visionConfig.photoMoveCoilFeedback = settings.value(
|
|
|
|
|
"modbus/photoMoveCoilFeedback",
|
|
|
|
|
settings.value("modbus/photoArrivedCoil",
|
|
|
|
|
settings.value("vision/photoMoveCoilFeedback",
|
|
|
|
|
settings.value("vision/photoArrivedCoil", 851)))).toInt();
|
|
|
|
|
m_visionConfig.comparisonPickCoil = settings.value("modbus/comparisonPickCoil", settings.value("vision/comparisonPickCoil", 852)).toInt();
|
2026-10-27 22:16:55 +08:00
|
|
|
settings.beginGroup("visionTrayMap");
|
|
|
|
|
for (const QString &key : settings.childKeys()) {
|
|
|
|
|
bool validKey = false;
|
|
|
|
|
const int tray = key.toInt(&validKey);
|
|
|
|
|
m_visionConfig.trayMap.insert(validKey ? tray : -1, settings.value(key).toInt());
|
|
|
|
|
}
|
|
|
|
|
settings.endGroup();
|
|
|
|
|
|
2026-08-18 09:40:43 +08:00
|
|
|
m_simulationMode = settings.value("system/simulation", m_simulationMode).toBool();
|
|
|
|
|
m_productionEnabled = settings.value("system/productionEnabled", m_productionEnabled).toBool();
|
|
|
|
|
return settings.status() == QSettings::NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ConfigManager::filePath() const
|
|
|
|
|
{
|
|
|
|
|
return m_filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DbConfig ConfigManager::databaseConfig() const
|
|
|
|
|
{
|
|
|
|
|
return m_databaseConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ModbusConfig ConfigManager::modbusConfig() const
|
|
|
|
|
{
|
|
|
|
|
return m_modbusConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProductionConfig ConfigManager::productionConfig() const
|
|
|
|
|
{
|
|
|
|
|
return m_productionConfig;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-27 22:16:55 +08:00
|
|
|
VisionConfig ConfigManager::visionConfig() const
|
|
|
|
|
{
|
|
|
|
|
return m_visionConfig;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-18 09:40:43 +08:00
|
|
|
bool ConfigManager::simulationMode() const
|
|
|
|
|
{
|
|
|
|
|
return m_simulationMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConfigManager::productionEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return m_productionEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConfigManager::validate(QString *error) const
|
|
|
|
|
{
|
|
|
|
|
QStringList errors;
|
2026-10-27 22:16:55 +08:00
|
|
|
if (m_visionConfig.enabled) {
|
|
|
|
|
QString visionError;
|
|
|
|
|
if (!m_visionConfig.isValid(&visionError)) {
|
|
|
|
|
errors.append(visionError);
|
|
|
|
|
}
|
|
|
|
|
if (m_modbusConfig.serverAddress == 0) {
|
|
|
|
|
errors.append("Vision PLC writes cannot use Modbus broadcast address 0");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
if (m_modbusConfig.host.trimmed().isEmpty()) {
|
|
|
|
|
errors.append("Modbus host is empty");
|
|
|
|
|
}
|
|
|
|
|
if (m_modbusConfig.port <= 0 || m_modbusConfig.port > 65535) {
|
|
|
|
|
errors.append("Modbus port is invalid");
|
|
|
|
|
}
|
|
|
|
|
if (m_modbusConfig.serverAddress < 0 || m_modbusConfig.serverAddress > 255) {
|
|
|
|
|
errors.append("Modbus server address is invalid");
|
|
|
|
|
}
|
|
|
|
|
if (m_modbusConfig.timeoutMs <= 0) {
|
|
|
|
|
errors.append("Modbus timeoutMs must be greater than 0");
|
|
|
|
|
}
|
|
|
|
|
if (m_modbusConfig.pollIntervalMs <= 0) {
|
|
|
|
|
errors.append("Modbus pollIntervalMs must be greater than 0");
|
|
|
|
|
}
|
2026-08-20 11:22:55 +08:00
|
|
|
if (m_modbusConfig.feedbackTimeoutMs <= 0
|
2026-08-18 09:40:43 +08:00
|
|
|
|| m_modbusConfig.reconnectDelayMs < 0
|
|
|
|
|
|| m_modbusConfig.reconnectAttempts < 0) {
|
|
|
|
|
errors.append("Modbus timing or retry settings are invalid");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString addressError;
|
|
|
|
|
if (!m_modbusConfig.addressMap.isValid(&addressError)) {
|
|
|
|
|
errors.append(addressError);
|
|
|
|
|
}
|
|
|
|
|
if (m_productionEnabled && m_simulationMode) {
|
|
|
|
|
errors.append("productionEnabled cannot be true while simulation is enabled");
|
|
|
|
|
}
|
|
|
|
|
if (m_productionConfig.timeoutWarningMs <= 0
|
|
|
|
|
|| m_productionConfig.stateTimeoutMs <= 0
|
|
|
|
|
|| m_productionConfig.timeoutWarningMs >= m_productionConfig.stateTimeoutMs
|
|
|
|
|
|| m_productionConfig.maxRecoveryRetries < 0
|
|
|
|
|
|| m_productionConfig.simulationStepMs <= 0) {
|
|
|
|
|
errors.append("Production timing settings are invalid");
|
|
|
|
|
}
|
|
|
|
|
if (m_productionConfig.okResult == m_productionConfig.ngResult
|
|
|
|
|
|| m_productionConfig.okResult == m_productionConfig.errorResult
|
|
|
|
|
|| m_productionConfig.ngResult == m_productionConfig.errorResult) {
|
|
|
|
|
errors.append("Production result values must be different");
|
|
|
|
|
}
|
|
|
|
|
if (m_productionConfig.simulationResult != m_productionConfig.okResult
|
|
|
|
|
&& m_productionConfig.simulationResult != m_productionConfig.ngResult) {
|
|
|
|
|
errors.append("Simulation result must be OK or NG");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = errors.join("; ");
|
|
|
|
|
}
|
|
|
|
|
return errors.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ConfigManager::defaultFilePath() const
|
|
|
|
|
{
|
|
|
|
|
return QDir(QCoreApplication::applicationDirPath()).filePath("config.ini");
|
|
|
|
|
}
|