设计模式

 /// <summary>
 /// PLC处理器
 /// </summary>
 public interface IPlcHandler
 {
     void Request(IPlcContext context);
 }

 /// <summary>
 /// PLC的数据上下文
 /// </summary>
 public interface IPlcContext
 {
     PlcRequest Request { get; }

     PlcResponse Response { get; }

     //PLC处理器
     IPlcHandler Handler { get; }

     //请求参数
     IDictionary<string, object?> Parameters { get; }
 }

 public class PlcRequest: PlcBase
 {
     //目标IP
     public string Ip {  get; set; }

     //保持连接
     public bool KeepAlive { get; set; }

     public FastData[] DefaultData { get; set; }
 }

 public class PlcResponse: PlcBase
 {
     public PlcStatusCode StatusCode { get; set; } = PlcStatusCode.Ok;

     //状态描述
     public string? StatusDescription { get; set; }
 }

 //里面封装了一些简单的经常会用到的,公共区数据,
 public class FastData
 {
     public DateTime DateTime { get; set; }
     public string Name { get; set; }
     public string Value { get; set; }
     public string Type { get; set; }
     public string TypeCode { get; set; }

     public void SaveData()
     {

     }
 }

 /// <summary>
 /// PLC和上位机通信的状态码
 /// </summary>
 public enum PlcStatusCode
 {
     Ok=200,
     Error=404,
     Fail=500,
 }

 //PLC的基类
 public abstract class PlcBase
 {
     public int ContentLength { get; set; } = -1;
     public string? ContentType { get; set; }

     public IDictionary<string, string> Headers { get; set; } = new NullableDictionary<string, string>(StringComparer.OrdinalIgnoreCase);

     public string this[string key]
     {
         get
         {
             return Headers[key] ?? "";
         }
         set
         {
             Headers[key] = value;
         }
     }
 }
posted @ 2024-01-23 20:32  孤沉  阅读(8)  评论(0编辑  收藏  举报