//using BPASmartClient.Peripheral; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Collections.Concurrent; namespace BPASmartClient.Message { public class MessageLog { private volatile static MessageLog _Instance; public static MessageLog GetInstance => _Instance ?? (_Instance = new MessageLog()); private MessageLog() { } #region 普通消息日志 /// /// 日志显示委托 /// public Action InfoNotify { get; set; } /// /// 日志信息 /// public string LogInfo { get; set; } = string.Empty; /// /// 普通日志输出 /// /// public void Show(string info) { Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}"); LogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {LogInfo}"; if (InfoNotify != null) InfoNotify(info); } #endregion #region 异常消息日志 /// /// 异常日志委托 /// public Action ExInfoNotify { get; set; } /// /// 异常日志信息 /// public string ExLogInfo { get; set; } = string.Empty; /// /// 异常日志输出 /// /// public void ShowEx(string info) { Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}"); ExLogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {ExLogInfo}"; if (ExInfoNotify != null) ExInfoNotify(info); } #endregion #region 设备过程日志 /// /// 设备过程日志委托 /// public Action DeviceProcessLogNotify { get; set; } /// /// 设备日志信息字典 /// public ConcurrentDictionary DPLogInfo = new ConcurrentDictionary(); /// /// 设备日志输出 /// /// public void DeviceProcessLogShow(string id, string info) { if (!DPLogInfo.ContainsKey(id)) DPLogInfo.TryAdd(id, info); else DPLogInfo[id] = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {DPLogInfo[id]}"; Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}"); DeviceProcessLogNotify?.Invoke(id, info); } #endregion #region 设备告警日志 /// /// 设备告警日志委托 /// public Action DeviceAlarmLogNotify { get; set; } public Action AlarmLogNotify { get; set; } /// /// 设备告警日志委托字典 /// public ConcurrentDictionary DPAlarmInfo = new ConcurrentDictionary(); /// /// 设备告警日志输出 /// /// public void AddDeviceAlarmLogShow(string id, string info) { DPAlarmInfo[info] = id; Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}"); if (DeviceAlarmLogNotify != null) DeviceAlarmLogNotify(id, info); if (AlarmLogNotify != null) AlarmLogNotify(); } /// /// 设备告警日志输出 /// /// public void DeleteDeviceAlarmLogShow(string id, string info) { if (DPAlarmInfo.ContainsKey(id)) { string mes = string.Empty; DPAlarmInfo.Remove(id, out mes); if (AlarmLogNotify != null) AlarmLogNotify(); } } #endregion #region 查找设备ID /// /// 查询设备ID /// /// public object ReturnDeviceID() { object DeviceId = null; //StackTrace trace = new StackTrace(); //for (int i = 1; i < 10; i++) //{ // Type type = trace.GetFrame(i).GetMethod().ReflectedType; // try // { // object obj = Activator.CreateInstance(type); // if (obj is IPeripheral) // { // IPeripheral peripheral = obj as IPeripheral; // DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null); // } // if (DeviceId != null) break; // } // catch (Exception ex) // { // } //} return DeviceId; } #endregion } }