Add DeviceConnectionManager class to record serial port scan.

This commit is contained in:
sawinglogz 2020-06-06 16:53:47 -04:00
parent cd29593280
commit efbb967b5c
4 changed files with 113 additions and 9 deletions

View File

@ -8,6 +8,7 @@
#include "deviceconnection.h"
#include <QtSerialPort/QSerialPortInfo>
#include <QDateTime>
#include <QDebug>
@ -17,6 +18,76 @@ static QString hex(int i)
}
// MARK: -
inline DeviceConnectionManager & DeviceConnectionManager::getInstance()
{
static DeviceConnectionManager instance;
return instance;
}
DeviceConnectionManager::DeviceConnectionManager()
: m_record(nullptr), m_replay(nullptr)
{
}
void DeviceConnectionManager::Record(QXmlStreamWriter* stream)
{
getInstance().m_record = stream;
}
void DeviceConnectionManager::Replay(QXmlStreamReader* stream)
{
getInstance().m_replay = stream;
}
void DeviceConnectionManager::startEvent(const QString & event)
{
if (m_record) {
QDateTime now = QDateTime::currentDateTime();
now = now.toOffsetFromUtc(now.offsetFromUtc()); // force display of UTC offset
#if QT_VERSION < QT_VERSION_CHECK(5,9,0)
// TODO: Can we please deprecate support for Qt older than 5.9?
QString timestamp = now.toString(Qt::ISODate);
#else
QString timestamp = now.toString(Qt::ISODateWithMs);
#endif
m_record->writeStartElement(event);
m_record->writeAttribute("time", timestamp);
}
}
#define RECORD(x) if (m_record) { *m_record << (x); }
void DeviceConnectionManager::endEvent()
{
if (m_record) {
m_record->writeEndElement();
}
}
QList<SerialPortInfo> DeviceConnectionManager::getAvailablePorts()
{
QList<SerialPortInfo> out;
startEvent("getAvailablePorts");
if (m_replay) {
// TODO
} else {
for (auto & info : QSerialPortInfo::availablePorts()) {
out.append(SerialPortInfo(info));
}
}
for (auto & portInfo : out) {
//qDebug().noquote() << portInfo;
RECORD(portInfo);
}
endEvent();
return out;
}
// MARK: -
SerialPortInfo::SerialPortInfo(const QSerialPortInfo & other)
@ -48,17 +119,10 @@ SerialPortInfo::SerialPortInfo(const QString & data)
xml >> *this;
}
// TODO: This is a temporary wrapper until we begin refactoring.
QList<SerialPortInfo> SerialPortInfo::availablePorts()
{
// TODO: internal state when in record or playback mode
QList<SerialPortInfo> out;
for (auto & info : QSerialPortInfo::availablePorts()) {
SerialPortInfo portInfo(info);
qDebug().noquote() << portInfo;
out.append(portInfo);
}
return out;
return DeviceConnectionManager::getInstance().getAvailablePorts();
}
QXmlStreamWriter & operator<<(QXmlStreamWriter & xml, const SerialPortInfo & info)

View File

@ -18,6 +18,29 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
class DeviceConnectionManager : public QObject
{
Q_OBJECT
private:
DeviceConnectionManager();
QXmlStreamWriter* m_record;
QXmlStreamReader* m_replay;
void startEvent(const QString & event);
void endEvent();
public:
static DeviceConnectionManager & getInstance();
QList<class SerialPortInfo> getAvailablePorts();
// TODO: method to start a polling thread that maintains the list of ports
// TODO: emit signal when new port is detected
static void Record(QXmlStreamWriter* stream);
static void Replay(QXmlStreamReader* stream);
};
// TODO: This class may eventually be internal to a DeviceConnection class,
// but for now it is used to provide support for recording and playback of
// serial port connections before refactoring.
@ -56,6 +79,8 @@ public:
protected:
SerialPortInfo(const class QSerialPortInfo & other);
QHash<QString,QVariant> m_info;
friend class DeviceConnectionManager;
};
#endif // DEVICECONNECTION_H

View File

@ -50,3 +50,17 @@ void DeviceConnectionTests::testSerialPortInfoSerialization()
serialized = info3;
Q_ASSERT(serialized == tag3);
}
void DeviceConnectionTests::testSerialPortScanning()
{
QString string;
QXmlStreamWriter xml(&string);
xml.setAutoFormatting(true);
DeviceConnectionManager::Record(&xml);
SerialPortInfo::availablePorts();
SerialPortInfo::availablePorts();
DeviceConnectionManager::Record(nullptr);
qDebug().noquote() << string;
}

View File

@ -13,5 +13,6 @@ class DeviceConnectionTests : public QObject
Q_OBJECT
private slots:
void testSerialPortInfoSerialization();
void testSerialPortScanning();
};
DECLARE_TEST(DeviceConnectionTests)