#pragma once #include #include #include #include #include #include #include #include #include #include "json/json.h" #include #include #include #include "TaskRunThread.h" #include #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* 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 m_vectorLogJson; // 读写锁,保证线程安全 mutable QReadWriteLock m_lock; QString strAllDataFile; public: //设备状态 QMap m_mapSystemStatus; // QMap> m_mapEqpmtFault; QMap m_mapEqpmtType; TaskRunThread* pRunThread; };