终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MessageLog.cs 5.5 KiB

há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //using BPASmartClient.Peripheral;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Collections.Concurrent;
  10. namespace BPASmartClient.Message
  11. {
  12. public class MessageLog
  13. {
  14. private volatile static MessageLog _Instance;
  15. public static MessageLog GetInstance => _Instance ?? (_Instance = new MessageLog());
  16. private MessageLog() { }
  17. #region 普通消息日志
  18. /// <summary>
  19. /// 日志显示委托
  20. /// </summary>
  21. public Action<string> InfoNotify { get; set; }
  22. public Action<string> DebugLog { get; set; }
  23. public string DebugLogInfo { get; set; } = string.Empty;
  24. /// <summary>
  25. /// 日志信息
  26. /// </summary>
  27. public string LogInfo { get; set; } = string.Empty;
  28. /// <summary>
  29. /// 普通日志输出
  30. /// </summary>
  31. /// <param name="info"></param>
  32. public void Show(string info)
  33. {
  34. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  35. LogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {LogInfo}";
  36. if (InfoNotify != null) InfoNotify(info);
  37. }
  38. public void ShowDebugLog(string info)
  39. {
  40. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  41. DebugLogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {DebugLogInfo}";
  42. DebugLog?.Invoke(info);
  43. }
  44. #endregion
  45. #region 异常消息日志
  46. /// <summary>
  47. /// 异常日志委托
  48. /// </summary>
  49. public Action<string> ExInfoNotify { get; set; }
  50. /// <summary>
  51. /// 异常日志信息
  52. /// </summary>
  53. public string ExLogInfo { get; set; } = string.Empty;
  54. /// <summary>
  55. /// 异常日志输出
  56. /// </summary>
  57. /// <param name="info"></param>
  58. public void ShowEx(string info)
  59. {
  60. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  61. ExLogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {ExLogInfo}";
  62. if (ExInfoNotify != null) ExInfoNotify(info);
  63. }
  64. #endregion
  65. #region 设备过程日志
  66. /// <summary>
  67. /// 设备过程日志委托
  68. /// </summary>
  69. public Action<string, string> DeviceProcessLogNotify { get; set; }
  70. /// <summary>
  71. /// 设备日志信息字典
  72. /// </summary>
  73. public ConcurrentDictionary<string, string> DPLogInfo = new ConcurrentDictionary<string, string>();
  74. /// <summary>
  75. /// 设备日志输出
  76. /// </summary>
  77. /// <param name="info"></param>
  78. public void DeviceProcessLogShow(string id, string info)
  79. {
  80. if (!DPLogInfo.ContainsKey(id))
  81. DPLogInfo.TryAdd(id, info);
  82. else
  83. DPLogInfo[id] = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {DPLogInfo[id]}";
  84. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  85. DeviceProcessLogNotify?.Invoke(id, info);
  86. }
  87. #endregion
  88. #region 设备告警日志
  89. /// <summary>
  90. /// 设备告警日志委托
  91. /// </summary>
  92. public Action<string, string> DeviceAlarmLogNotify { get; set; }
  93. public Action AlarmLogNotify { get; set; }
  94. /// <summary>
  95. /// 设备告警日志委托字典
  96. /// </summary>
  97. public ConcurrentDictionary<string, string> DPAlarmInfo = new ConcurrentDictionary<string, string>();
  98. /// <summary>
  99. /// 设备告警日志输出
  100. /// </summary>
  101. /// <param name="info"></param>
  102. public void AddDeviceAlarmLogShow(string id, string info)
  103. {
  104. DPAlarmInfo[info] = id;
  105. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  106. if (DeviceAlarmLogNotify != null) DeviceAlarmLogNotify(id, info);
  107. if (AlarmLogNotify != null) AlarmLogNotify();
  108. }
  109. /// <summary>
  110. /// 设备告警日志输出
  111. /// </summary>
  112. /// <param name="info"></param>
  113. public void DeleteDeviceAlarmLogShow(string id, string info)
  114. {
  115. if (DPAlarmInfo.ContainsKey(id))
  116. {
  117. string mes = string.Empty;
  118. DPAlarmInfo.Remove(id, out mes);
  119. if (AlarmLogNotify != null) AlarmLogNotify();
  120. }
  121. }
  122. #endregion
  123. #region 查找设备ID
  124. /// <summary>
  125. /// 查询设备ID
  126. /// </summary>
  127. /// <returns></returns>
  128. public object ReturnDeviceID()
  129. {
  130. object DeviceId = null;
  131. //StackTrace trace = new StackTrace();
  132. //for (int i = 1; i < 10; i++)
  133. //{
  134. // Type type = trace.GetFrame(i).GetMethod().ReflectedType;
  135. // try
  136. // {
  137. // object obj = Activator.CreateInstance(type);
  138. // if (obj is IPeripheral)
  139. // {
  140. // IPeripheral peripheral = obj as IPeripheral;
  141. // DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null);
  142. // }
  143. // if (DeviceId != null) break;
  144. // }
  145. // catch (Exception ex)
  146. // {
  147. // }
  148. //}
  149. return DeviceId;
  150. }
  151. #endregion
  152. }
  153. }