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);
Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
//ExLogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {ExLogInfo}";
if (DeviceProcessLogNotify != null) DeviceProcessLogNotify(info);
}
}
#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
}
}