DataManager.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #include <QObject>
  3. #include <QObject>
  4. #include <QHash>
  5. #include <QVariant>
  6. #include <QMutex>
  7. #include <QMutexLocker>
  8. #include <QReadWriteLock>
  9. #include <mutex>
  10. #include <QSettings>
  11. #include "json/json.h"
  12. #include <QMap>
  13. #include <QList>
  14. #include <QDateTime>
  15. #include "TaskRunThread.h"
  16. #include <QTextCodec>
  17. #ifdef Q_OS_WIN
  18. #define GBK2UTF8 DataManager::gbkString2utf8
  19. #define G2U(x) QString::fromStdString(DataManager::gbkString2utf8(x))
  20. #else
  21. #define GBK2UTF8
  22. #define G2U(x) QString(x)
  23. #endif
  24. #define READ_INI_STR(key, default_value) (settings.contains(key) ? settings.value(key).toString() : default_value)
  25. #define READ_INI_INT(key, default_value) (settings.contains(key) ? settings.value(key).toInt() : default_value)
  26. enum SignalDataType
  27. {
  28. CourseData = 0,
  29. HttpLogList = 1,
  30. };
  31. typedef struct tag_SaveJsonLog
  32. {
  33. std::string m_jsonRequest; //记录发送的json
  34. std::string m_jsonResponse; //记录返回的json
  35. bool m_bStatus;
  36. std::string m_nAPIID;
  37. bool m_serverData;
  38. tag_SaveJsonLog(std::string strRequest, std::string strResponse, std::string nAPIID, bool bStatus,bool bserverData=false)
  39. {
  40. m_jsonRequest = strRequest;
  41. m_jsonResponse = strResponse;
  42. m_nAPIID = nAPIID;
  43. m_bStatus = bStatus;
  44. m_serverData =bserverData;
  45. }
  46. }LogJson;
  47. struct SystemData
  48. {
  49. int nSystemID;
  50. int nSysType;
  51. QString strSysType;
  52. QString strSysName;
  53. int nStatus;
  54. };
  55. struct EqpmtFault
  56. {
  57. int nFaultID;
  58. QDateTime FaultTime;
  59. };
  60. class DataManager : public QObject
  61. {
  62. Q_OBJECT
  63. public:
  64. // 获取单例实例
  65. static DataManager& instance();
  66. // 清空所有数据
  67. static void destroyInstance();
  68. //公用操作函数
  69. //utf8与string互相转换
  70. static std::string string_To_UTF8(const std::string& str);
  71. static std::string UTF8_to_string(const std::string& str);
  72. //json格式化
  73. static bool JsonToStyled(const std::string& str, std::string& strRetrun);
  74. static bool JsonToStyled(Json::Value& jValue, std::string& strRetrun);
  75. //HTTP日志
  76. void AddHttpLog(LogJson& pNewLog);
  77. //清空HTTP日志
  78. void ClearHttpLog();
  79. QList<LogJson>* GetLogJson() {
  80. return &m_vectorLogJson;
  81. };
  82. bool addEqpmFault(std::string strEqpmt, int nEqpmtType, int nFaultID);
  83. bool DeleteEqpmtFault(std::string strEqpmt, int nFaultID);
  84. void clearEqpmtFault();
  85. void InitTestFileData();
  86. QString getTestFileData(){return strAllDataFile;}
  87. static std::string utf8String2gbk(const char* utf8str)
  88. {
  89. QTextCodec* codec = QTextCodec::codecForName("GBK"); // 获取GBK编码器
  90. QByteArray gbkBytes = codec->fromUnicode(utf8str); // 将UTF-8字符串转换为GBK编码的字节数组
  91. std::string str = gbkBytes.data();
  92. return str;
  93. }
  94. static std::string gbkString2utf8(const char* gbk8str)
  95. {
  96. QTextCodec* codec = QTextCodec::codecForName("GBK"); // 获取GBK编码器
  97. QString strS = codec->toUnicode(gbk8str); // 将UTF-8字符串转换为GBK编码的字节数组
  98. std::string str = strS.toUtf8().data();
  99. return str;
  100. }
  101. //子系统状态更新
  102. void setEqpmtStatus(int nSysID,int nStatus);
  103. signals:
  104. // 数据改变信号
  105. void dataChanged(const int key);
  106. private:
  107. explicit DataManager(QObject* parent = nullptr);
  108. ~DataManager() = default;
  109. // 禁止拷贝和赋值
  110. DataManager(const DataManager&) = delete;
  111. DataManager& operator=(const DataManager&) = delete;
  112. static DataManager* g_GlobalSingle;
  113. static std::mutex m_Mutex;
  114. // 数据存储
  115. QList<LogJson> m_vectorLogJson;
  116. // 读写锁,保证线程安全
  117. mutable QReadWriteLock m_lock;
  118. QString strAllDataFile;
  119. public:
  120. //设备状态
  121. QMap<int, SystemData> m_mapSystemStatus;
  122. //
  123. QMap<std::string, QList<EqpmtFault>> m_mapEqpmtFault;
  124. QMap<std::string, int> m_mapEqpmtType;
  125. TaskRunThread* pRunThread;
  126. };