DIOS.Dev.MSGMould.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // CCOS.Dev.FPDDeviceMould.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "CCOS.Dev.MSGMould.hpp"
  5. #include "MessageInfo.tlh"
  6. #include "MessageInfo.tli"
  7. //#include "Base64.h"
  8. namespace CCOS::Dev::Detail
  9. {
  10. MSGUnit::MSGUnit(std::shared_ptr <CCOS::Dev::IOEventCenter> center, std::string DevInstance)
  11. {
  12. m_DevInstance = DevInstance;
  13. m_Eventcenter = center;
  14. }
  15. MSGUnit:: ~MSGUnit()
  16. {
  17. }
  18. std::string MSGUnit::JSGet()
  19. {
  20. return m_ErrorList;
  21. }
  22. std::string MSGUnit::GetKey()
  23. {
  24. return Key;
  25. }
  26. std::wstring mb2wc(const char* mbstr)
  27. {
  28. std::wstring strVal;
  29. int size = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, NULL, 0);
  30. wchar_t* wcstr = new wchar_t[size + 1];
  31. if (wcstr)
  32. {
  33. memset(wcstr, 0, size * sizeof(wchar_t));
  34. int ret = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, wcstr, size);
  35. if (ret != 0) // MultiByteToWideChar returns 0 if it does not succeed.
  36. {
  37. strVal = wcstr;
  38. }
  39. delete[] wcstr;
  40. wcstr = NULL;
  41. }
  42. return strVal;
  43. }
  44. int MSGUnit::AddMessage(const char* Code, int& Level, const char* ResInfo, int nMessageType, int nSenderID)
  45. {
  46. if (Code == 0 || (string)ResInfo == "")
  47. {
  48. return 1;
  49. }
  50. //string strBase64;
  51. //wstring wResUTF = mb2wc(ResInfo);
  52. //CBase64::Encode((const unsigned char*)wResUTF.c_str(), (unsigned long)wResUTF.size() * sizeof(wchar_t), strBase64);
  53. ResDataObject tempErrorList;
  54. if (m_ErrorList != "")
  55. {
  56. tempErrorList.decode(m_ErrorList.c_str());
  57. }
  58. MessageInfo info;
  59. info.CodeID = Code;
  60. info.Type = nMessageType;
  61. info.Level = Level;
  62. info.Resouceinfo = ResInfo;
  63. info.Description = "";
  64. int ret = 1;
  65. for (size_t i = 0; i < tempErrorList.size(); i++)
  66. {
  67. string strCodekey = tempErrorList.GetKey(i);
  68. string strtype = tempErrorList[strCodekey.c_str()]["Type"];
  69. if (strCodekey == (string)Code && atoi(strtype.c_str()) == nMessageType)
  70. {//Same Code:%s with MessageType:%d already Exist
  71. ret = 0;
  72. break;
  73. }
  74. }
  75. if (ret == 1)
  76. {
  77. ResDataObject /*ResNotify,*/ ErrorInfo, tempInfo;
  78. info.GetResDataObject(tempInfo);
  79. if (0 != nSenderID)
  80. {
  81. string strSenderID = std::to_string(nSenderID);
  82. tempInfo.add("SenderId", strSenderID.c_str());
  83. }
  84. if (nMessageType == ERRORTYPE)//只有错误会增加到错误列表中,警告通知上层即可
  85. {
  86. tempErrorList.add(Code, tempInfo);
  87. m_ErrorList = tempErrorList.encode();
  88. }
  89. ErrorInfo.add(Code, tempInfo);
  90. //ResNotify.add(m_DevInstance.c_str(), ErrorInfo); //ResNotify.add(Code, ErrorInfo);//for json format unique key when multi-error in list //ResNotify.add(m_DevInstance.c_str(), ErrorInfo);
  91. //m_Eventcenter->OnNotify(2, "ErrorList", ResNotify.encode());
  92. m_Eventcenter->OnNotify(2, "ErrorList", ErrorInfo.encode());
  93. }
  94. //put log here
  95. return 2;
  96. }
  97. int MSGUnit::DelMessage(const char* Code, int &Level, const char* ResInfo, int nMessageType)
  98. {
  99. ResDataObject tempErrorList;
  100. if (m_ErrorList != "")
  101. {
  102. tempErrorList.decode(m_ErrorList.c_str());
  103. }
  104. else
  105. {
  106. return 0;
  107. }
  108. ResDataObject /*ResNotify,*/ ErrorInfo, tempInfo;
  109. MessageInfo info;
  110. info.CodeID = Code;
  111. info.Type = nMessageType;
  112. info.Level = Level;
  113. info.Resouceinfo = ResInfo;
  114. info.Description = "";
  115. info.GetResDataObject(tempInfo);
  116. ErrorInfo.add(Code, tempInfo);
  117. //ResNotify.add(m_DevInstance.c_str(), ErrorInfo); //ResNotify.add(Code, ErrorInfo);//for json format unique key when multi-error in list //
  118. bool m_bClearAll = false;
  119. if ((string)Code == "0" || (string)Code == "")
  120. {
  121. m_bClearAll = true;
  122. }
  123. if (m_bClearAll)
  124. {
  125. m_ErrorList ="";
  126. //m_Eventcenter->OnNotify(3, "ErrorList", ResNotify.encode());
  127. m_Eventcenter->OnNotify(3, "ErrorList", ErrorInfo.encode());
  128. }
  129. else
  130. {
  131. int ret = 1;
  132. for (size_t i = 0; i < tempErrorList.size(); i++)
  133. {
  134. string strCodekey = tempErrorList.GetKey(i);
  135. string strtype = tempErrorList[strCodekey.c_str()]["Type"];
  136. if (strCodekey == (string)Code && atoi(strtype.c_str()) == nMessageType)
  137. {
  138. ret = 0;
  139. break;
  140. }
  141. }
  142. if (ret == 0)
  143. {
  144. tempErrorList.eraseAllOf(Code);
  145. m_ErrorList = tempErrorList.encode();
  146. //m_Eventcenter->OnNotify(3, "ErrorList", ResNotify.encode());
  147. m_Eventcenter->OnNotify(3, "ErrorList", ErrorInfo.encode());
  148. }
  149. }
  150. return 2;
  151. }
  152. int MSGUnit::AddWarnMessage(const char* Code, int& Level, const char* ResInfo, int nSenderID)
  153. {
  154. return AddMessage(Code, Level, ResInfo, WARNTYPE, nSenderID);
  155. }
  156. int MSGUnit::DelWarnMessage(const char* Code, int& Level, const char* ResInfo)
  157. {
  158. ResDataObject /*ResNotify,*/ ErrorInfo, tempInfo;
  159. MessageInfo info;
  160. info.CodeID = Code;
  161. info.Type = WARNTYPE;
  162. info.Level = Level;
  163. info.Resouceinfo = ResInfo;
  164. info.Description = "";
  165. info.GetResDataObject(tempInfo);
  166. ErrorInfo.add(Code, tempInfo);
  167. //ResNotify.add(m_DevInstance.c_str(), ErrorInfo);
  168. m_Eventcenter->OnNotify(3, "ErrorList", ErrorInfo.encode());
  169. return 2;
  170. }
  171. int MSGUnit::AddErrorMessage(const char* Code, int& Level, const char* ResInfo, int nSenderID)
  172. {
  173. return AddMessage(Code, Level, ResInfo, ERRORTYPE, nSenderID);
  174. }
  175. int MSGUnit::DelErrorMessage(const char* Code, int& Level, const char* ResInfo)
  176. {
  177. return DelMessage(Code, Level, ResInfo, ERRORTYPE);
  178. }
  179. }