终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

167 lignes
5.1 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. DeviceProcessLogNotify?.Invoke(id, info);
  78. }
  79. #endregion
  80. #region 设备告警日志
  81. /// <summary>
  82. /// 设备告警日志委托
  83. /// </summary>
  84. public Action<string, string> DeviceAlarmLogNotify { get; set; }
  85. public Action AlarmLogNotify { get; set; }
  86. /// <summary>
  87. /// 设备告警日志委托字典
  88. /// </summary>
  89. public ConcurrentDictionary<string, string> DPAlarmInfo = new ConcurrentDictionary<string, string>();
  90. /// <summary>
  91. /// 设备告警日志输出
  92. /// </summary>
  93. /// <param name="info"></param>
  94. public void AddDeviceAlarmLogShow(string id, string info)
  95. {
  96. DPAlarmInfo[info] = id;
  97. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  98. if (DeviceAlarmLogNotify != null) DeviceAlarmLogNotify(id, info);
  99. if (AlarmLogNotify != null) AlarmLogNotify();
  100. }
  101. /// <summary>
  102. /// 设备告警日志输出
  103. /// </summary>
  104. /// <param name="info"></param>
  105. public void DeleteDeviceAlarmLogShow(string id, string info)
  106. {
  107. if (DPAlarmInfo.ContainsKey(id))
  108. {
  109. string mes = string.Empty;
  110. DPAlarmInfo.Remove(id, out mes);
  111. if (AlarmLogNotify != null) AlarmLogNotify();
  112. }
  113. }
  114. #endregion
  115. #region 查找设备ID
  116. /// <summary>
  117. /// 查询设备ID
  118. /// </summary>
  119. /// <returns></returns>
  120. public object ReturnDeviceID()
  121. {
  122. object DeviceId = null;
  123. //StackTrace trace = new StackTrace();
  124. //for (int i = 1; i < 10; i++)
  125. //{
  126. // Type type = trace.GetFrame(i).GetMethod().ReflectedType;
  127. // try
  128. // {
  129. // object obj = Activator.CreateInstance(type);
  130. // if (obj is IPeripheral)
  131. // {
  132. // IPeripheral peripheral = obj as IPeripheral;
  133. // DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null);
  134. // }
  135. // if (DeviceId != null) break;
  136. // }
  137. // catch (Exception ex)
  138. // {
  139. // }
  140. //}
  141. return DeviceId;
  142. }
  143. #endregion
  144. }
  145. }