Selaa lähdekoodia

重构代码并添加数据采集功能

- 在 `DataAcquisition.cs` 中引入 `DataAcquisitionSolution.DTO` 命名空间,添加 `PlcInfos` 列表,修改 `Init` 方法为 `PlcInfosAdd` 方法。
- 在 `DevDbConfig.cs` 和 `DevInterval.cs` 中删除并重新定义类,移动到 `DataAcquisitionSolution.DTO` 命名空间。
- 在 `WorkStart.cs` 中的 `InitializeDeviceInfo` 方法中添加 `PlcInfosAdd` 调用。
- 在 `Worker.cs` 中注释掉初始化 PLC 访问器的代码,添加 `BusinessCenter.DataCollectionLoop` 方法调用。
- 在 `appsettings.Development.json` 和 `appsettings.json` 中添加日志配置和 Redis 连接字符串。
- 新增 `BusinessCenter` 类及其 `DataCollectionLoop` 和 `Do` 方法。
- 新增 `DbInfoDto` 结构体,`PlcDbInfo` 类,和 `PlcInfo` 类用于存储相关信息。
林豪 左 7 kuukautta sitten
vanhempi
commit
56902284ee

+ 75 - 0
DataAcquisitionSolution/BusinessCenter.cs

@@ -0,0 +1,75 @@
+using System.Diagnostics;
+using DataAcquisitionSolution.DTO;
+using DataAcquisitionSolution.Entity;
+using MessagePack;
+using PlcSiemens.Core.Extension;
+using ServiceCenter.Redis;
+
+namespace DataAcquisitionSolution;
+
+/// <summary>
+///     业务中心
+/// </summary>
+public static class BusinessCenter
+{
+    public static string RedisDataKey = "DataAcquisition";
+
+    /// <summary>
+    ///     数据采集
+    /// </summary>
+    public static void DataCollectionLoop()
+    {
+        while (true)
+            Do(500, () =>
+            {
+                var dataTime = DateTime.Now;
+                Parallel.ForEach(DataAcquisition.PlcInfos.GroupBy(x => x.PlcInfo.IP), plc =>
+                {
+                    var plcInfo = new PlcInfo
+                    {
+                        Ip = plc.Key,
+                        CreateTime = dataTime
+                    };
+                    var plcDbInfos = new List<PlcDbInfo>();
+                    Parallel.ForEach(plc, db =>
+                    {
+                        var data = db.Accessor?.ReadBytes(db.No, 0, db.Length);
+                        plcDbInfos.Add(new PlcDbInfo(db.No, data));
+                    });
+                    plcInfo.Content = MessagePackSerializer.Serialize(plcDbInfos);
+                    RedisHub.Default.RPush(RedisDataKey, plcInfo);
+                });
+            });
+    }
+
+    /// <summary>
+    ///     数据处理
+    /// </summary>
+    public static void DataStorageLoop()
+    {
+    }
+
+
+    /// <summary>
+    ///     执行事务
+    /// </summary>
+    /// <param name="act"></param>
+    /// <param name="maxTime">最大时间(毫秒)</param>
+    /// <exception cref="Exception"></exception>
+    public static void Do(int maxTime, Action act)
+    {
+        try
+        {
+            var sw = new Stopwatch();
+            sw.Start();
+            act(); //执行委托
+            sw.Stop();
+            var time = maxTime - sw.ElapsedMilliseconds.ToInt();
+            if (time > 0) Thread.Sleep(time);
+        }
+        catch (Exception ex)
+        {
+            Console.WriteLine(ex.Message);
+        }
+    }
+}

+ 32 - 0
DataAcquisitionSolution/DTO/DBInfo.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WCS.Core;
+
+namespace DataAcquisitionSolution.DTO
+{
+    /// <summary>
+    ///  DB块信息
+    /// </summary>
+    public struct DbInfoDto()
+    {
+        /// <summary>
+        ///  PLC信息
+        /// </summary>
+        public PLCInfo PlcInfo { get; set; } = default;
+
+        /// <summary>
+        /// DB块
+        /// </summary>
+        public ushort No { get; set; } = 0;
+
+        /// <summary>
+        ///  总长度
+        /// </summary>
+        public ushort Length { get; set; } = 0;
+
+        public IPLCAccessor? Accessor { get; set; } = null;
+    }
+}

+ 1 - 1
DataAcquisitionSolution/DevDbConfig.cs → DataAcquisitionSolution/DTO/DevDbConfig.cs

@@ -1,4 +1,4 @@
-namespace DataAcquisitionSolution;
+namespace DataAcquisitionSolution.DTO;
 
 public class DevDbConfig<T>
 {

+ 1 - 1
DataAcquisitionSolution/DevInterval.cs → DataAcquisitionSolution/DTO/DevInterval.cs

@@ -1,4 +1,4 @@
-namespace DataAcquisitionSolution;
+namespace DataAcquisitionSolution.DTO;
 
 public class DevInterval<T>(T s, T e)
 {

+ 22 - 0
DataAcquisitionSolution/DTO/PlcDbInfo.cs

@@ -0,0 +1,22 @@
+using System.Runtime.Serialization;
+
+namespace DataAcquisitionSolution.DTO;
+
+/// <summary>
+///     DB块的数据
+/// </summary>
+[DataContract]
+public class PlcDbInfo(ushort db, byte[]? content)
+{
+    /// <summary>
+    ///     DB号
+    /// </summary>
+    [DataMember(Order = 0)]
+    public ushort Db { get; set; } = db;
+
+    /// <summary>
+    ///     内容
+    /// </summary>
+    [DataMember(Order = 1)]
+    public byte[]? Content { get; set; } = content;
+}

+ 28 - 17
DataAcquisitionSolution/DataAcquisition.cs

@@ -1,29 +1,40 @@
-using WCS.Core;
+using DataAcquisitionSolution.DTO;
+using WCS.Core;
 
 namespace DataAcquisitionSolution;
 
+/// <summary>
+///     数据采集
+/// </summary>
 public static class DataAcquisition
 {
-    public static List<ProtocolInfo> ProtocolInfos = new();
+    /// <summary>
+    ///     PLC信息
+    /// </summary>
+    public static List<DbInfoDto> PlcInfos = new();
 
-    public static void Init(int position, ushort db, string ip)
+    /// <summary>
+    ///     PLC信息添加
+    /// </summary>
+    /// <param name="db">DB块名称</param>
+    /// <param name="length"></param>
+    /// <param name="ip">IP地址</param>
+    public static void PlcInfosAdd(ushort db, ushort length, string ip)
     {
-        var info = new ProtocolInfo
+        var info = new DbInfoDto
         {
-            Position = position,
-            DBInfo = new DBInfo
+            No = db,
+            PlcInfo = new PLCInfo
             {
-                No = db,
-                PLCInfo = new PLCInfo
-                {
-                    IP = ip,
-                    Port = 102,
-                    Rack = 0,
-                    Slot = 1,
-                    Type = PLCType.Siemens
-                }
-            }
+                IP = ip,
+                Port = 102,
+                Rack = 0,
+                Slot = 1,
+                Type = PLCType.Siemens
+            },
+            Length = length
         };
-        ProtocolInfos.Add(info);
+        info.Accessor = Configs.PLCAccessorCreater?.Create(info.PlcInfo);
+        PlcInfos.Add(info);
     }
 }

+ 28 - 0
DataAcquisitionSolution/Entity/PlcInfo.cs

@@ -0,0 +1,28 @@
+using System.Runtime.Serialization;
+
+namespace DataAcquisitionSolution.Entity;
+
+/// <summary>
+///     PLC信息
+/// </summary>
+[DataContract]
+public class PlcInfo
+{
+    /// <summary>
+    ///     IP
+    /// </summary>
+    [DataMember(Order = 0)]
+    public string Ip { get; set; } = string.Empty;
+
+    /// <summary>
+    ///     内容
+    /// </summary>
+    [DataMember(Order = 1)]
+    public byte[] Content { get; set; } 
+
+    /// <summary>
+    ///     处理时间
+    /// </summary>
+    [DataMember(Order = 2)]
+    public DateTime CreateTime { get; set; } = DateTime.Now;
+}

+ 5 - 0
DataAcquisitionSolution/WorkStart.cs

@@ -18,6 +18,11 @@ public static class WorkStart
     /// </summary>
     public static void InitializeDeviceInfo()
     {
+        DataAcquisition.PlcInfosAdd(520, 688, "10.30.37.166");
+        DataAcquisition.PlcInfosAdd(521, 784, "10.30.37.166");
+        DataAcquisition.PlcInfosAdd(523, 8484, "10.30.37.166");
+        DataAcquisition.PlcInfosAdd(524, 11936, "10.30.37.166");
+
     }
 
     /// <summary>

+ 15 - 14
DataAcquisitionSolution/Worker.cs

@@ -141,27 +141,28 @@ public class Worker : BackgroundService
 
         #endregion 初始化设备信息
 
-        #region 初始化PLC访问器及PLC读取协议
-
         //创建PLC访问器
         Configs.PLCAccessorCreater = new PlcAccessorsCreater();
 
-        try
-        {
-            #region 唤醒所有的世界
+        BusinessCenter.DataCollectionLoop();
 
-            World.StartAll();
+        //#region 初始化PLC访问器及PLC读取协议
+        //try
+        //{
+        //    #region 唤醒所有的世界
 
-            #endregion 唤醒所有的世界
+        //    World.StartAll();
 
-            _logger.LogInformation("WCS启动成功");
-        }
-        catch (Exception ex)
-        {
-            _logger.LogError("WCS启动失败{0}", ex.Message);
-        }
+        //    #endregion 唤醒所有的世界
+
+        //    _logger.LogInformation("WCS启动成功");
+        //}
+        //catch (Exception ex)
+        //{
+        //    _logger.LogError("WCS启动失败{0}", ex.Message);
+        //}
 
-        #endregion 初始化PLC访问器及PLC读取协议
+        //#endregion 初始化PLC访问器及PLC读取协议
 
         LogHub.init();
         return Task.CompletedTask;

+ 8 - 0
DataAcquisitionSolution/appsettings.Development.json

@@ -0,0 +1,8 @@
+{
+  "Logging": {
+    "LogLevel": {
+      "Default": "Information",
+      "Microsoft.Hosting.Lifetime": "Information"
+    }
+  }
+}

+ 14 - 0
DataAcquisitionSolution/appsettings.json

@@ -0,0 +1,14 @@
+{
+  "Logging": {
+    "LogLevel": {
+      "Default": "Information",
+      "Microsoft": "Warning",
+      "Microsoft.Hosting.Lifetime": "Information"
+    }
+  },
+  "ConnectionStrings": {
+    //"Redis": "127.0.0.1:6379,database=0,prefix=DataAcquisitionSolution:"
+    "Redis": "10.30.37.2,database=0,prefix=DataAcquisitionSolution:"
+    //"WareHouses": "FJ1|FJ2|FJ3"
+  }
+}