350 lines
9.6 KiB
C++
350 lines
9.6 KiB
C++
#include "channelmanager.h"
|
|
|
|
ChannelManager *ChannelManager::m_instance = nullptr;
|
|
|
|
ChannelManager::ChannelManager(QObject *parent) : QObject(parent)
|
|
{
|
|
initializeChannelCapabilities();
|
|
|
|
// Initialize all channels as available
|
|
for (int i = INPUT_CHANNEL_1; i <= OUTPUT_CHANNEL_2; ++i)
|
|
{
|
|
ChannelId channelId = static_cast<ChannelId>(i);
|
|
ChannelStatus status;
|
|
status.occupied = false;
|
|
status.operationDetails.operation = NONE;
|
|
status.operationDetails.description = QString();
|
|
status.operationDetails.connectionType = TWO_WIRE;
|
|
m_channelStatus[channelId] = status;
|
|
}
|
|
}
|
|
|
|
ChannelManager *ChannelManager::instance()
|
|
{
|
|
if (!m_instance)
|
|
{
|
|
m_instance = new ChannelManager();
|
|
}
|
|
return m_instance;
|
|
}
|
|
|
|
void ChannelManager::initializeChannelCapabilities()
|
|
{
|
|
// 输入通道1: 支持最多四线连接(白-红-灰-黑),可进行所有测量功能
|
|
ChannelCapability inputCh1;
|
|
inputCh1.type = INPUT_CHANNEL;
|
|
inputCh1.maxWires = 4;
|
|
inputCh1.wireColors = QStringList() << "白" << "红" << "灰" << "黑";
|
|
inputCh1.supportedConnections = QList<ConnectionType>()
|
|
<< TWO_WIRE << THREE_WIRE << FOUR_WIRE;
|
|
inputCh1.supportedOperations = QList<OperationType>()
|
|
<< VOLTAGE_MEASUREMENT << MV_MEASUREMENT
|
|
<< MA_MEASUREMENT << AC_MEASUREMENT
|
|
<< RESISTANCE_MEASUREMENT << FREQUENCY_MEASUREMENT
|
|
<< SWITCH_MEASUREMENT << INSULATION_MEASUREMENT;
|
|
m_channelCapabilities[INPUT_CHANNEL_1] = inputCh1;
|
|
|
|
// 输入通道2: 支持最多两线连接(白-灰),仅支持二线制测量
|
|
ChannelCapability inputCh2;
|
|
inputCh2.type = INPUT_CHANNEL;
|
|
inputCh2.maxWires = 2;
|
|
inputCh2.wireColors = QStringList() << "白" << "灰";
|
|
inputCh2.supportedConnections = QList<ConnectionType>() << TWO_WIRE;
|
|
inputCh2.supportedOperations = QList<OperationType>()
|
|
<< VOLTAGE_MEASUREMENT << MV_MEASUREMENT
|
|
<< MA_MEASUREMENT << AC_MEASUREMENT
|
|
<< RESISTANCE_MEASUREMENT << FREQUENCY_MEASUREMENT
|
|
<< SWITCH_MEASUREMENT;
|
|
m_channelCapabilities[INPUT_CHANNEL_2] = inputCh2;
|
|
|
|
// 输出通道1: 支持最多四线连接(用于电阻模拟)
|
|
ChannelCapability outputCh1;
|
|
outputCh1.type = OUTPUT_CHANNEL;
|
|
outputCh1.maxWires = 4;
|
|
outputCh1.wireColors = QStringList() << "白" << "红" << "灰" << "黑";
|
|
outputCh1.supportedConnections = QList<ConnectionType>()
|
|
<< TWO_WIRE << THREE_WIRE << FOUR_WIRE;
|
|
outputCh1.supportedOperations = QList<OperationType>()
|
|
<< VOLTAGE_OUTPUT << MV_OUTPUT << MA_OUTPUT
|
|
<< WAVEFORM_OUTPUT << RESISTANCE_SIMULATION;
|
|
m_channelCapabilities[OUTPUT_CHANNEL_1] = outputCh1;
|
|
|
|
// 输出通道2: 支持两线连接
|
|
ChannelCapability outputCh2;
|
|
outputCh2.type = OUTPUT_CHANNEL;
|
|
outputCh2.maxWires = 2;
|
|
outputCh2.wireColors = QStringList() << "白" << "灰";
|
|
outputCh2.supportedConnections = QList<ConnectionType>() << TWO_WIRE;
|
|
outputCh2.supportedOperations = QList<OperationType>()
|
|
<< VOLTAGE_OUTPUT << MV_OUTPUT << MA_OUTPUT
|
|
<< WAVEFORM_OUTPUT;
|
|
m_channelCapabilities[OUTPUT_CHANNEL_2] = outputCh2;
|
|
}
|
|
|
|
bool ChannelManager::isChannelAvailable(ChannelId channel) const
|
|
{
|
|
return !m_channelStatus.value(channel).occupied;
|
|
}
|
|
|
|
bool ChannelManager::canChannelSupport(ChannelId channel, OperationType operation,
|
|
ConnectionType connection) const
|
|
{
|
|
if (!m_channelCapabilities.contains(channel))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const ChannelCapability &capability = m_channelCapabilities[channel];
|
|
|
|
// 检查连接类型支持
|
|
if (!capability.supportedConnections.contains(connection))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// 检查操作类型支持
|
|
if (operation != NONE && !capability.supportedOperations.contains(operation))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// 检查操作类型与通道类型的匹配
|
|
if (isInputChannelOperation(operation))
|
|
{
|
|
return capability.type == INPUT_CHANNEL;
|
|
}
|
|
else if (isOutputChannelOperation(operation))
|
|
{
|
|
return capability.type == OUTPUT_CHANNEL;
|
|
}
|
|
else if (operation == NONE)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ChannelManager::occupyChannel(ChannelId channel, OperationType operation,
|
|
const QString &description,
|
|
ConnectionType connectionType)
|
|
{
|
|
if (!isChannelAvailable(channel))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!canChannelSupport(channel, operation, connectionType))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
ChannelStatus &status = m_channelStatus[channel];
|
|
status.occupied = true;
|
|
status.operationDetails.operation = operation;
|
|
status.operationDetails.description = description;
|
|
status.operationDetails.connectionType = connectionType;
|
|
status.occupiedTime = QDateTime::currentDateTime();
|
|
|
|
emit channelStatusChanged(channel, operation, description);
|
|
return true;
|
|
}
|
|
|
|
void ChannelManager::releaseChannel(ChannelId channel)
|
|
{
|
|
if (m_channelStatus.contains(channel))
|
|
{
|
|
ChannelStatus &status = m_channelStatus[channel];
|
|
status.occupied = false;
|
|
status.operationDetails.operation = NONE;
|
|
status.operationDetails.description.clear();
|
|
status.operationDetails.connectionType = TWO_WIRE;
|
|
status.occupiedTime = QDateTime();
|
|
|
|
emit channelReleased(channel);
|
|
emit channelStatusChanged(channel, NONE, QString());
|
|
}
|
|
}
|
|
|
|
ChannelManager::OperationType ChannelManager::getChannelOperation(ChannelId channel) const
|
|
{
|
|
return m_channelStatus.value(channel).operationDetails.operation;
|
|
}
|
|
|
|
QString ChannelManager::getChannelDescription(ChannelId channel) const
|
|
{
|
|
return m_channelStatus.value(channel).operationDetails.description;
|
|
}
|
|
|
|
ChannelManager::OperationDetails
|
|
ChannelManager::getChannelOperationDetails(ChannelId channel) const
|
|
{
|
|
return m_channelStatus.value(channel).operationDetails;
|
|
}
|
|
|
|
QList<ChannelManager::OperationType>
|
|
ChannelManager::getSupportedOperations(ChannelId channel) const
|
|
{
|
|
return m_channelCapabilities.value(channel).supportedOperations;
|
|
}
|
|
|
|
ChannelManager::ChannelCapability
|
|
ChannelManager::getChannelCapability(ChannelId channel) const
|
|
{
|
|
return m_channelCapabilities.value(channel);
|
|
}
|
|
|
|
QList<ChannelManager::ChannelId>
|
|
ChannelManager::getAvailableChannels(ChannelType type) const
|
|
{
|
|
QList<ChannelId> availableChannels;
|
|
|
|
for (auto it = m_channelCapabilities.constBegin();
|
|
it != m_channelCapabilities.constEnd(); ++it)
|
|
{
|
|
if (it.value().type == type && isChannelAvailable(it.key()))
|
|
{
|
|
availableChannels.append(it.key());
|
|
}
|
|
}
|
|
|
|
return availableChannels;
|
|
}
|
|
|
|
QString ChannelManager::getOperationDisplayName(OperationType operation) const
|
|
{
|
|
switch (operation)
|
|
{
|
|
case NONE:
|
|
return "空闲";
|
|
// 输入通道操作
|
|
case VOLTAGE_MEASUREMENT:
|
|
return "电压测量";
|
|
case MV_MEASUREMENT:
|
|
return "mV测量";
|
|
case MA_MEASUREMENT:
|
|
return "mA测量";
|
|
case AC_MEASUREMENT:
|
|
return "交流测量";
|
|
case RESISTANCE_MEASUREMENT:
|
|
return "电阻测量";
|
|
case FREQUENCY_MEASUREMENT:
|
|
return "频率测量";
|
|
case SWITCH_MEASUREMENT:
|
|
return "开关量测量";
|
|
case INSULATION_MEASUREMENT:
|
|
return "绝缘电阻测量";
|
|
// 输出通道操作
|
|
case VOLTAGE_OUTPUT:
|
|
return "电压输出";
|
|
case MV_OUTPUT:
|
|
return "mV输出";
|
|
case MA_OUTPUT:
|
|
return "mA输出";
|
|
case WAVEFORM_OUTPUT:
|
|
return "波形输出";
|
|
case RESISTANCE_SIMULATION:
|
|
return "电阻模拟";
|
|
}
|
|
return "未知";
|
|
}
|
|
|
|
QString ChannelManager::getConnectionTypeDisplayName(ConnectionType connection) const
|
|
{
|
|
switch (connection)
|
|
{
|
|
case TWO_WIRE:
|
|
return "二线制";
|
|
case THREE_WIRE:
|
|
return "三线制";
|
|
case FOUR_WIRE:
|
|
return "四线制";
|
|
}
|
|
return "未知";
|
|
}
|
|
|
|
QString ChannelManager::getDetailedOperationDescription(
|
|
const OperationDetails &details) const
|
|
{
|
|
QString baseDescription = getOperationDisplayName(details.operation);
|
|
|
|
if (details.operation == RESISTANCE_MEASUREMENT ||
|
|
details.operation == RESISTANCE_SIMULATION)
|
|
{
|
|
baseDescription +=
|
|
QString("(%1)").arg(getConnectionTypeDisplayName(details.connectionType));
|
|
}
|
|
|
|
if (!details.description.isEmpty())
|
|
{
|
|
baseDescription += QString(" - %1").arg(details.description);
|
|
}
|
|
|
|
return baseDescription;
|
|
}
|
|
|
|
bool ChannelManager::isInputChannelOperation(OperationType operation) const
|
|
{
|
|
switch (operation)
|
|
{
|
|
case VOLTAGE_MEASUREMENT:
|
|
case MV_MEASUREMENT:
|
|
case MA_MEASUREMENT:
|
|
case AC_MEASUREMENT:
|
|
case RESISTANCE_MEASUREMENT:
|
|
case FREQUENCY_MEASUREMENT:
|
|
case SWITCH_MEASUREMENT:
|
|
case INSULATION_MEASUREMENT:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool ChannelManager::isOutputChannelOperation(OperationType operation) const
|
|
{
|
|
switch (operation)
|
|
{
|
|
case VOLTAGE_OUTPUT:
|
|
case MV_OUTPUT:
|
|
case MA_OUTPUT:
|
|
case WAVEFORM_OUTPUT:
|
|
case RESISTANCE_SIMULATION:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
QString ChannelManager::getChannelDisplayName(ChannelId channel) const
|
|
{
|
|
switch (channel)
|
|
{
|
|
case INPUT_CHANNEL_1:
|
|
return "输入通道1";
|
|
case INPUT_CHANNEL_2:
|
|
return "输入通道2";
|
|
case OUTPUT_CHANNEL_1:
|
|
return "输出通道1";
|
|
case OUTPUT_CHANNEL_2:
|
|
return "输出通道2";
|
|
}
|
|
return "未知通道";
|
|
}
|
|
|
|
QList<ChannelManager::ChannelId>
|
|
ChannelManager::getChannelsWithOperation(OperationType operation) const
|
|
{
|
|
QList<ChannelId> channels;
|
|
|
|
for (auto it = m_channelStatus.constBegin(); it != m_channelStatus.constEnd(); ++it)
|
|
{
|
|
if (it.value().operationDetails.operation == operation)
|
|
{
|
|
channels.append(it.key());
|
|
}
|
|
}
|
|
|
|
return channels;
|
|
}
|