patrick
2019-12-10 1c4426810c71eead57084be8a18ade8d314dd8c4
src/Bro.Device.Common/Base/DeviceBase.cs
@@ -14,6 +14,7 @@
//using BroCComn.PubSub;
using static Bro.Common.Helper.EnumHelper;
using System.ComponentModel;
using System.IO;
namespace Bro.Common.Base
{
@@ -28,7 +29,6 @@
        [JsonIgnore]
        public Action<EnumHelper.DeviceState> OnDeviceStateChanged { get; set; }
        #endregion
        int RetryTime = 3;
        /// <summary>
@@ -168,8 +168,6 @@
        [JsonIgnore]
        public Dictionary<DeviceInputMethodAttribute, MethodInfo> InputMethods { get; set; } = new Dictionary<DeviceInputMethodAttribute, MethodInfo>();
        //public event DeviceStateChangedDelegate OnDeviceStateChanged;
@@ -393,7 +391,7 @@
            {
                string currentStateStr = CurrentStateToBe.GetEnumDescription();
                string stateToBeStr = stateToBe.GetEnumDescription();
                throw new ProcessException($"{InitialConfig.Name}设备的当前状态为{currentStateStr},无法切换至{stateToBeStr}", null);
                throw new ProcessException($"{InitialConfig.Name}设备的当前状态为{currentStateStr},无法切换至{stateToBeStr}");
            }
            CurrentState = EnumHelper.DeviceState.TBD;
@@ -417,7 +415,7 @@
                    }
                    else
                    {
                        throw new ProcessException($"{InitialConfig.Name}设备操作超时", null);
                        throw new ProcessException($"{InitialConfig.Name}设备操作超时");
                    }
                }
@@ -489,7 +487,7 @@
                        //this.CurrentState = EnumHelper.DeviceState.DSExcept;
                        //this.CurrentStateToBe = EnumHelper.DeviceState.DSExcept;
                        throw new ProcessException($"设备{InitialConfig.Name}的{CurrentStateToBe.GetEnumDescription()}操作重复3次失败", ex);
                        throw new ProcessException($"设备{InitialConfig.Name}的{CurrentStateToBe.GetEnumDescription()}操作重复3次失败", ExceptionLevel.Warning, ex);
                    }
                }
            }
@@ -515,5 +513,40 @@
            CurrentUserId = obj;
        }
        #endregion
        #region 日志处理
        object logObject = new object();
        public virtual async void LogAsync(DateTime dt, string prefix, string msg)
        {
            await Task.Run(() =>
            {
                if (InitialConfig.IsEnableLog)
                {
                    OnLog?.Invoke(dt, this, prefix + "\t" + msg);
                    if (string.IsNullOrWhiteSpace(InitialConfig.LogPath) || !Path.IsPathRooted(InitialConfig.LogPath))
                    {
                        return;
                    }
                    if (!Directory.Exists(InitialConfig.LogPath))
                    {
                        Directory.CreateDirectory(InitialConfig.LogPath);
                    }
                    string filePath = Path.Combine(InitialConfig.LogPath, $"Log_{Name}_{DateTime.Now.ToString("yyyyMMdd")}.txt");
                    lock (logObject)
                    {
                        using (StreamWriter writer = new StreamWriter(filePath, true, Encoding.UTF8))
                        {
                            writer.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}\t{prefix}\t{msg}");
                            writer.Flush();
                            writer.Close();
                        }
                    }
                }
            });
        }
        #endregion
    }
}