| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #pragma once
- #include <QObject>
- #include <QObject>
- #include <QHash>
- #include <QVariant>
- #include <QMutex>
- #include <QMutexLocker>
- #include <QReadWriteLock>
- #include <mutex>
- #include <QSettings>
- #include "json/json.h"
- #include <QMap>
- #include <QList>
- #include <QDateTime>
- #include "TaskRunThread.h"
- #include <QTextCodec>
- #ifdef Q_OS_WIN
- #define GBK2UTF8 DataManager::gbkString2utf8
- #define G2U(x) QString::fromStdString(DataManager::gbkString2utf8(x))
- #else
- #define GBK2UTF8
- #define G2U(x) QString(x)
- #endif
- #define READ_INI_STR(key, default_value) (settings.contains(key) ? settings.value(key).toString() : default_value)
- #define READ_INI_INT(key, default_value) (settings.contains(key) ? settings.value(key).toInt() : default_value)
- enum SignalDataType
- {
- CourseData = 0,
- HttpLogList = 1,
- };
- typedef struct tag_SaveJsonLog
- {
- std::string m_jsonRequest; //记录发送的json
- std::string m_jsonResponse; //记录返回的json
- bool m_bStatus;
- std::string m_nAPIID;
- bool m_serverData;
- tag_SaveJsonLog(std::string strRequest, std::string strResponse, std::string nAPIID, bool bStatus,bool bserverData=false)
- {
- m_jsonRequest = strRequest;
- m_jsonResponse = strResponse;
- m_nAPIID = nAPIID;
- m_bStatus = bStatus;
- m_serverData =bserverData;
- }
- }LogJson;
- struct SystemData
- {
- int nSystemID;
- int nSysType;
- QString strSysType;
- QString strSysName;
- int nStatus;
- };
- struct EqpmtFault
- {
- int nFaultID;
- QDateTime FaultTime;
- };
- class DataManager : public QObject
- {
- Q_OBJECT
- public:
- // 获取单例实例
- static DataManager& instance();
- // 清空所有数据
- static void destroyInstance();
- //公用操作函数
- //utf8与string互相转换
- static std::string string_To_UTF8(const std::string& str);
- static std::string UTF8_to_string(const std::string& str);
- //json格式化
- static bool JsonToStyled(const std::string& str, std::string& strRetrun);
- static bool JsonToStyled(Json::Value& jValue, std::string& strRetrun);
- //HTTP日志
- void AddHttpLog(LogJson& pNewLog);
- //清空HTTP日志
- void ClearHttpLog();
- QList<LogJson>* GetLogJson() {
- return &m_vectorLogJson;
- };
- bool addEqpmFault(std::string strEqpmt, int nEqpmtType, int nFaultID);
- bool DeleteEqpmtFault(std::string strEqpmt, int nFaultID);
- void clearEqpmtFault();
- void InitTestFileData();
- QString getTestFileData(){return strAllDataFile;}
- static std::string utf8String2gbk(const char* utf8str)
- {
- QTextCodec* codec = QTextCodec::codecForName("GBK"); // 获取GBK编码器
- QByteArray gbkBytes = codec->fromUnicode(utf8str); // 将UTF-8字符串转换为GBK编码的字节数组
- std::string str = gbkBytes.data();
- return str;
- }
- static std::string gbkString2utf8(const char* gbk8str)
- {
- QTextCodec* codec = QTextCodec::codecForName("GBK"); // 获取GBK编码器
- QString strS = codec->toUnicode(gbk8str); // 将UTF-8字符串转换为GBK编码的字节数组
- std::string str = strS.toUtf8().data();
- return str;
- }
- //子系统状态更新
- void setEqpmtStatus(int nSysID,int nStatus);
- signals:
- // 数据改变信号
- void dataChanged(const int key);
- private:
- explicit DataManager(QObject* parent = nullptr);
- ~DataManager() = default;
- // 禁止拷贝和赋值
- DataManager(const DataManager&) = delete;
- DataManager& operator=(const DataManager&) = delete;
- static DataManager* g_GlobalSingle;
- static std::mutex m_Mutex;
- // 数据存储
- QList<LogJson> m_vectorLogJson;
- // 读写锁,保证线程安全
- mutable QReadWriteLock m_lock;
- QString strAllDataFile;
- public:
- //设备状态
- QMap<int, SystemData> m_mapSystemStatus;
- //
- QMap<std::string, QList<EqpmtFault>> m_mapEqpmtFault;
- QMap<std::string, int> m_mapEqpmtType;
-
- TaskRunThread* pRunThread;
- };
|