BindableCommunicationBase.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Runtime.CompilerServices;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using TeamAAS.Communication.Interfaces;
  8. namespace TeamAAS.Communication.Base
  9. {
  10. public class DebugLogEntry
  11. {
  12. [JsonIgnore] public DateTime Time { get; set; }
  13. [JsonIgnore] public string Message { get; set; }
  14. public override string ToString() => $"[{Time:HH:mm:ss}] {Message}";
  15. }
  16. [Serializable]
  17. /// <summary>
  18. /// 通讯设备基类:提供 INotifyPropertyChanged + SetProperty + Id/Index/Name/TypeKey 默认实现。
  19. /// 设备实例本身就是配置对象(PropertyGrid 直接绑定),不再需要 Info 子类。
  20. /// </summary>
  21. public abstract class BindableCommunicationBase : ICommunication, INotifyPropertyChanged
  22. {
  23. [field:NonSerialized]
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. private Guid _id = Guid.NewGuid();
  26. [Browsable(false)]
  27. public Guid Id { get { return _id; } set { SetProperty(ref _id, value); } }
  28. private int _index;
  29. [Browsable(false)]
  30. public int Index { get { return _index; } set { SetProperty(ref _index, value); } }
  31. private string _name;
  32. [Category("I.基础配置"), DisplayName("1.设备名称"), Description("设置设备名称")]
  33. public string Name { get { return _name; } set { SetProperty(ref _name, value); } }
  34. [Browsable(false)]
  35. public virtual string TypeKey => GetType().FullName;
  36. [Browsable(false)]
  37. public abstract string EndpointUrl { get; set; }
  38. [Category("I.基础配置"), DisplayName("2.是否已连接"), Description("是否已连接")]
  39. [ReadOnly(true)]
  40. public abstract bool IsConnected { get; }
  41. public abstract event Action<object, bool> ConnectChangedEvent;
  42. public abstract event Action<object, string> DataReceivedEvent;
  43. [JsonIgnore, Browsable(false)]
  44. public ObservableCollection<DebugLogEntry> DebugLog { get; } = new ObservableCollection<DebugLogEntry>();
  45. public void AppendLog(string message)
  46. {
  47. DebugLog.Add(new DebugLogEntry { Time = DateTime.Now, Message = message });
  48. while (DebugLog.Count > 50) DebugLog.RemoveAt(0);
  49. }
  50. public abstract void Connect();
  51. public abstract System.Threading.Tasks.Task ConnectAsync();
  52. public abstract void Disconnect();
  53. public abstract object ReadValue(string address);
  54. public abstract System.Threading.Tasks.Task<object> ReadValueAsync(string address);
  55. public abstract void WriteValue(string address, object value);
  56. public abstract System.Threading.Tasks.Task WriteValueAsync(string address, object value);
  57. public abstract void Dispose();
  58. protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  59. {
  60. if (Equals(field, value)) return false;
  61. field = value;
  62. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  63. return true;
  64. }
  65. protected void Notify([CallerMemberName] string propertyName = null)
  66. {
  67. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  68. }
  69. }
  70. }