终端一体化运控平台
Você não pode selecionar mais de 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.
 
 
 

170 linhas
5.4 KiB

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