Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sensor detector usability #897

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qml/DeviceManagerViewer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ PingPopup {
Label {
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
text: connection.typeToString() + " " + connection.createConfString()
text: connection.typeToString() + " " + connection.argsAsConst()[0]
tcanabrava marked this conversation as resolved.
Show resolved Hide resolved
}

StatusIndicator {
Expand Down
49 changes: 44 additions & 5 deletions src/devicemanager/devicemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ DeviceManager::DeviceManager()
void DeviceManager::append(const LinkConfiguration& linkConf, const QString& deviceName, const QString& detectorName)
{
for (int i {0}; i < _sensors[Connection].size(); i++) {
auto vectorLinkConf = _sensors[Connection][i].value<QSharedPointer<LinkConfiguration>>().get();
if (*vectorLinkConf == linkConf) {
const auto vectorLinkConf = _sensors[Connection][i].value<QSharedPointer<LinkConfiguration>>();
if (!vectorLinkConf.isNull() && *vectorLinkConf == linkConf) {
qCDebug(DEVICEMANAGER) << "Connection configuration already exist for:" << _sensors[Name][i] << linkConf
<< linkConf.argsAsConst();
_sensors[Available][i] = true;
Expand All @@ -50,6 +50,7 @@ void DeviceManager::append(const LinkConfiguration& linkConf, const QString& dev
_sensors[Connected].append(false);
_sensors[DetectorName].append(detectorName);
_sensors[Name].append(deviceName);
_sensors[UnavailableCounter].append(0);

const auto& indexRow = index(line);
endInsertRows();
Expand Down Expand Up @@ -151,19 +152,57 @@ void DeviceManager::updateAvailableConnections(
const QVector<LinkConfiguration>& availableLinkConfigurations, const QString& detector)
{
qCDebug(DEVICEMANAGER) << "Available devices:" << availableLinkConfigurations;
// Make all connections unavailable by default

for (int i {0}; i < _sensors[Available].size(); i++) {
auto linkConf = _sensors[Connection][i].value<QSharedPointer<LinkConfiguration>>();
const auto linkConf = _sensors[Connection][i].value<QSharedPointer<LinkConfiguration>>();
if (linkConf->isSimulation() || _sensors[DetectorName][i] != detector) {
continue;
}

// Make all connections unavailable by default
_sensors[Available][i] = false;
const auto indexRow = index(i);
emit dataChanged(indexRow, indexRow, _roles);
}

// Check if the configuration already exists for a sensor
// Serial ports does not support multiple devices connected
// Some sensors, like Ping1D, can fail to answer a ABR procedure
for (const auto& link : availableLinkConfigurations) {
append(link, PingHelper::nameFromDeviceType(link.deviceType()), detector);
const bool sameSerialDevice = std::any_of(
_sensors[Connection].cbegin(), _sensors[Connection].cend(), [&link](const QVariant& variantLink) {
tcanabrava marked this conversation as resolved.
Show resolved Hide resolved
const auto sensorLink = variantLink.value<QSharedPointer<LinkConfiguration>>();
qCDebug(DEVICEMANAGER) << "Device" << *sensorLink
<< "already already provided by a different connection:" << link;
return link.serialPort() == sensorLink->serialPort() && link != *sensorLink;
});

if (!sameSerialDevice) {
append(link, PingHelper::nameFromDeviceType(link.deviceType()), detector);
}
}

// We'll let the link to fail the communication attempt "a max number of fails" before making it unavailable
// This is necessary to avoid any problem related to automatic baud rates problem from the sensor side.
static const int maxNumberOfFails = 3;
for (int i {0}; i < _sensors[Available].size(); i++) {
const auto linkConf = _sensors[Connection][i].value<QSharedPointer<LinkConfiguration>>();
if (linkConf->isSimulation()) {
continue;
}

const auto indexRow = index(i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicit types when it's ipossible to guess the type by reading the code?

Copy link
Member Author

@patrickelectric patrickelectric Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indexRow = index(i) appears to be pretty clear that is an index value. We should also not care what type it's, since it's an argument for dataChanged.
Besides that, this happens in others parts of the code.


// The sensor was detected, we can remove unavailable counter
if (_sensors[Available][i].toBool()) {
tcanabrava marked this conversation as resolved.
Show resolved Hide resolved
_sensors[UnavailableCounter][i] = 0;
tcanabrava marked this conversation as resolved.
Show resolved Hide resolved
emit dataChanged(indexRow, indexRow, _roles);
continue;
}
_sensors[Available][i] = _sensors[UnavailableCounter][i].toInt() < maxNumberOfFails;
_sensors[UnavailableCounter][i] = _sensors[UnavailableCounter][i].toInt() + 1;

emit dataChanged(indexRow, indexRow, _roles);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/devicemanager/devicemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ class DeviceManager : public QAbstractListModel {
Connection,
Name,
DetectorName,
UnavailableCounter,
};
QHash<int, QByteArray> _roleNames {
{Available, "available"},
{Connected, "connected"},
{Connection, "connection"},
{Name, "name"},
{DetectorName, "detectorName"},
{UnavailableCounter, "unavailableCounter"},
};

QSharedPointer<Sensor> _primarySensor;
Expand Down
2 changes: 2 additions & 0 deletions src/link/linkconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ bool operator==(const LinkConfiguration& first, const LinkConfiguration& second)
&& (firstLinkconf.deviceType == secondLinkconf.deviceType);
}

bool operator!=(const LinkConfiguration& first, const LinkConfiguration& second) { return !(first == second); }

QDebug operator<<(QDebug d, const LinkConfiguration& other)
{
QString text(QStringLiteral("LinkConfiguration{Name: %1, Sensor: %2, LinkType: %3, Arguments: (%4)}"));
Expand Down
1 change: 1 addition & 0 deletions src/link/linkconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ class LinkConfiguration : public QObject {
};

bool operator==(const LinkConfiguration& first, const LinkConfiguration& second);
bool operator!=(const LinkConfiguration& first, const LinkConfiguration& second);
QDebug operator<<(QDebug d, const LinkConfiguration& other);
QDataStream& operator<<(QDataStream& out, const LinkConfiguration linkConfiguration);
QDataStream& operator>>(QDataStream& in, LinkConfiguration& linkConfiguration);
Expand Down
11 changes: 4 additions & 7 deletions src/sensor/protocoldetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void ProtocolDetector::doScan()
// Scan until something is connected
while (_active) {
auto linksConf = updateLinkConfigurations(_linkConfigs);
qCDebug(PING_PROTOCOL_PROTOCOLDETECTOR) << "Looking for devices in:" << linksConf;
for (LinkConfiguration& tryLinkConf : linksConf) {
if (!_active) {
break;
Expand Down Expand Up @@ -116,13 +117,9 @@ QVector<LinkConfiguration> ProtocolDetector::updateLinkConfigurations(QVector<Li
continue;
}

// Add valid port and baudrate
// Ping360 can't handle 9600 requests with 115200 request in a sort time priod
// TODO: Fix Ping360 is not possible, we should drop 9600 checks if 115200 returns fine
for (auto baud : {115200, 9600}) {
auto config = {portInfo.portName(), QString::number(baud)};
tempConfigs.append({LinkType::Serial, config, QString("Detector serial link")});
}
// By default, all sensors should deal with 115200 auto baudrate detection
auto config = {portInfo.portName(), QString::number(115200)};
tempConfigs.append({LinkType::Serial, config, QString("Detector serial link")});
}
return linkConfig + tempConfigs;
}
Expand Down