|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <QMutex>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QSerialPort>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QThread>
|
|
|
|
|
#include "device/Device.h"
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
class DeviceManager
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static DeviceManager *GetInstance() {
|
|
|
|
|
if(!m_pInstance)
|
|
|
|
|
{
|
|
|
|
|
m_mutex.lock();
|
|
|
|
|
if(!m_pInstance)
|
|
|
|
|
{
|
|
|
|
|
m_pInstance = new DeviceManager();
|
|
|
|
|
}
|
|
|
|
|
m_mutex.unlock();
|
|
|
|
|
}
|
|
|
|
|
return m_pInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeviceManager(const DeviceManager&)= delete;
|
|
|
|
|
DeviceManager& operator=(const DeviceManager&)= delete;
|
|
|
|
|
|
|
|
|
|
void AddDevice(QString deviceName, Device *device);
|
|
|
|
|
void RemoveDevice(QString deviceName);
|
|
|
|
|
void RemoveAllDevices();
|
|
|
|
|
Device *GetDevice(QString deviceName);
|
|
|
|
|
|
|
|
|
|
Device *GetCurrDevice();
|
|
|
|
|
|
|
|
|
|
void setCurrDeviceName(std::string currDeviceName) { m_currDeviceName = currDeviceName; }
|
|
|
|
|
void setCurrBaudRate(std::string currBaudRate) { m_currBaudRate = currBaudRate; }
|
|
|
|
|
void setCurrDataBits(std::string currDataBits) { m_currDataBits = currDataBits; }
|
|
|
|
|
void setCurrStopBits(std::string currStopBits) { m_currStopBits = currStopBits; }
|
|
|
|
|
void setCurrParity(std::string currParity) { m_currParity = currParity; }
|
|
|
|
|
|
|
|
|
|
std::string getCurrDeviceName() { return m_currDeviceName; }
|
|
|
|
|
std::string getCurrBaudRate() { return m_currBaudRate; }
|
|
|
|
|
std::string getCurrDataBits() { return m_currDataBits; }
|
|
|
|
|
std::string getCurrStopBits() { return m_currStopBits; }
|
|
|
|
|
std::string getCurrParity() { return m_currParity; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
DeviceManager() {};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static DeviceManager *m_pInstance;
|
|
|
|
|
static QMutex m_mutex;
|
|
|
|
|
|
|
|
|
|
QMap<QString, Device *> m_deviceMap;
|
|
|
|
|
|
|
|
|
|
std::string m_currDeviceName;
|
|
|
|
|
std::string m_currBaudRate;
|
|
|
|
|
std::string m_currDataBits;
|
|
|
|
|
std::string m_currStopBits;
|
|
|
|
|
std::string m_currParity;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// 垃圾回收类,它的唯一工作就是在析构函数中删除DeviceFactory的实例
|
|
|
|
|
class Garbo
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
~Garbo()
|
|
|
|
|
{
|
|
|
|
|
if (DeviceManager::m_pInstance)
|
|
|
|
|
{
|
|
|
|
|
// 销毁设备管理单例实例
|
|
|
|
|
delete DeviceManager::m_pInstance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// 定义一个静态成员,在程序结束时,系统会调用它的析构函数
|
|
|
|
|
static Garbo m_Garbo;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|