Browse Source

虚拟PLC

林豪 左 3 years ago
parent
commit
0e22ae854a

+ 1 - 0
Projects/永冠OPP/WCS.Service/WCS.Service.csproj

@@ -44,6 +44,7 @@
   <ItemGroup>
     <ProjectReference Include="..\..\..\DBHelper-SqlSugar\DBHelper-SqlSugar.csproj" />
     <ProjectReference Include="..\..\..\Logs\Logs.csproj" />
+    <ProjectReference Include="..\..\..\Virtual_PLC\Virtual_PLC.csproj" />
     <ProjectReference Include="..\..\..\WCS.Entity\WCS.Entity.csproj" />
     <ProjectReference Include="..\WCS.Entity.Protocol\WCS.Entity.Protocol.csproj" />
   </ItemGroup>

+ 8 - 35
Projects/永冠OPP/WCS.Service/Worker.cs

@@ -12,6 +12,7 @@ using System.Reflection;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
+using Virtual_PLC;
 using WCS.Core;
 using WCS.Core.DataTrans;
 using WCS.Entity;
@@ -49,42 +50,14 @@ namespace WCS.Service
             Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
             Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
             Configs.StringEncoding = Encoding.UTF8;
-            Db.CreateContext(new ConnectionConfig()
+            var data = new PLCData()
             {
-                ConnectionString = AppSettings.Config.GetConnectionString("WCSDB"),
-                DbType = DbType.MySql
-            }, "WCSDB");
-            Db.SetDefaultDbContextType("WCSDB");
-            Db.Do(db =>
-            {
-                //TODO:DbMaintenance.CreateDatabase()并没起到作用,如果没有对应的数据库的话任然需要手动新建一个
-                db.Default.DbMaintenance.CreateDatabase();
-                db.Default.CodeFirst.InitTables(typeof(WCS_CMD));
-                db.Default.CodeFirst.InitTables(typeof(WCS_PLC));
-                db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
-                db.Default.CodeFirst.InitTables(typeof(WCS_DEVICE));
-                db.Default.CodeFirst.InitTables(typeof(WCS_PATH));
-                db.Default.CodeFirst.InitTables(typeof(WCS_PATHPOINT));
-                db.Default.CodeFirst.InitTables(typeof(WCS_ROUTE));
-                db.Default.CodeFirst.InitTables(typeof(WCS_TASK));
-                db.Default.CodeFirst.InitTables(typeof(WCS_TASK_OLD));
-                db.Default.CodeFirst.InitTables(typeof(WCS_EXCEPTION));
-                db.Default.CodeFirst.InitTables(typeof(WCS_SystemConfig));
-                db.Default.CodeFirst.InitTables(typeof(WCS_AGVTask));
-                db.Default.CodeFirst.InitTables(typeof(WCS_DEVICEPROTOCOL));
-                db.Default.CodeFirst.InitTables(typeof(WCS_GROUPMEMBER));
-                db.Default.CodeFirst.InitTables(typeof(WCS_MAPPINGENTRY));
-                db.Default.CodeFirst.InitTables(typeof(WCS_USERS));
-                db.Default.CodeFirst.InitTables(typeof(WCS_StatusLog));
-                db.Default.CodeFirst.InitTables(typeof(WCS_BCR80));
-                db.Default.CodeFirst.InitTables(typeof(WCS_RGV520));
-                //db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
-                //db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
-                //db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
-                //db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
-                //db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
-                //db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
-            });
+                IP = "192.168.3.1",
+                DB = "520",
+                Length = 1320,
+                DataLength = 22,
+            };
+            PlcData.Init("127.0.0.1,database=0").InitPlcData(data);
 
             //日志发布事件s
             Configs.PublishEvent += () =>

+ 129 - 0
Virtual_PLC/PlcData.cs

@@ -0,0 +1,129 @@
+using FreeRedis;
+using System.Collections.Generic;
+
+namespace Virtual_PLC
+{
+    /// <summary>
+    /// plc数据
+    /// </summary>
+    public class PlcData
+    {
+        private static RedisClient Redis { get; set; }
+
+        /// <summary>
+        /// redis 链接字符串
+        /// </summary>
+        /// <param name="redisClient">Redis链接字符串</param>
+        public PlcData(string redisClient)
+        {
+            Redis = new RedisClient(redisClient);
+        }
+
+        /// <summary>
+        /// redis 链接字符串
+        /// </summary>
+        /// <param name="redisClient"></param>
+        /// <returns></returns>
+        public static PlcData Init(string redisClient)
+        {
+            return new PlcData(redisClient);
+        }
+
+        /// <summary>
+        /// 初始化PLC数据
+        /// </summary>
+        /// <param name="pLCData">一个PLC</param>
+        public void InitPlcData(PLCData pLCData)
+        {
+            //用总长度除以数据长度,再以每断数据的起始位置、IP、DB组成Key
+            var mun = pLCData.Length / pLCData.DataLength;
+            int addstart = 0;
+
+            for (int i = 0; i < mun; i++)
+            {
+                var key = $"{pLCData.IP}:{pLCData.DB}:{addstart}";
+                if (Redis.Exists(key)) continue;
+                Redis.Set(key, new byte[pLCData.DataLength]);
+                addstart = addstart + pLCData.DataLength;
+            }
+        }
+
+        /// <summary>
+        /// 初始化PLC数据
+        /// </summary>
+        /// <param name="pLCDatas">多个PLC</param>
+        public void InitPlcData(List<PLCData> pLCDatas)
+        {
+            pLCDatas.ForEach(v =>
+            {
+                InitPlcData(v);
+            });
+        }
+
+        /// <summary>
+        /// 按照DB读取
+        /// </summary>
+        /// <param name="pLCData"></param>
+        /// <returns></returns>
+        public static byte[] Read(PLCData pLCData)
+        {
+            byte[] data = new byte[pLCData.Length];
+            //用总长度除以数据长度,再以每断数据的起始位置、IP、DB组成Key
+            var mun = pLCData.Length / pLCData.DataLength;
+            int addstart = 0;
+
+            for (int i = 0; i < mun; i++)
+            {
+                var a = Redis.Get<byte[]>($"{pLCData.IP}:{pLCData.DB}:{addstart}");
+                a.CopyTo(data, addstart);
+                addstart = addstart + pLCData.DataLength;
+            }
+            return data;
+        }
+
+        /// <summary>
+        /// 按照长度读取
+        /// </summary>
+        /// <param name="pLCData"></param>
+        /// <param name="startLength">起始长度</param>
+        /// <returns></returns>
+        public static byte[] Read(PLCData pLCData, int startLength)
+        {
+            return System.Text.Encoding.Default.GetBytes(Redis.Get($"{pLCData.IP}:{pLCData.DB}:{startLength}"));
+        }
+
+        /// <summary>
+        ///
+        /// </summary>
+        public static void Write(PLCData pLCData, int startLength, byte[] bytes)
+        {
+            Redis.Set($"{pLCData.IP}:{pLCData.DB}:{startLength}", bytes);
+        }
+    }
+
+    /// <summary>
+    /// PLC数据结构
+    /// </summary>
+    public class PLCData
+    {
+        /// <summary>
+        /// IP
+        /// </summary>
+        public string IP { get; set; }
+
+        /// <summary>
+        /// DB
+        /// </summary>
+        public string DB { get; set; }
+
+        /// <summary>
+        /// 总长度
+        /// </summary>
+        public int Length { get; set; }
+
+        /// <summary>
+        /// 数据长度
+        /// </summary>
+        public int DataLength { get; set; }
+    }
+}

+ 12 - 0
Virtual_PLC/Virtual_PLC.csproj

@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netstandard2.1</TargetFramework>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="FreeRedis" Version="0.5.9" />
+  </ItemGroup>
+
+</Project>

+ 7 - 0
WCS Pedestal.sln

@@ -34,6 +34,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DBHelper-SqlSugar", "DBHelp
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SqlSugar", "..\..\..\..\开源\Src\Asp.NetCore2\SqlSugar\SqlSugar.csproj", "{9993C299-72BF-4F5A-AEAB-F1B41DF01FBC}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Virtual_PLC", "Virtual_PLC\Virtual_PLC.csproj", "{FFE12700-0A7F-474B-8B0B-ABC209C79383}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -80,6 +82,10 @@ Global
 		{9993C299-72BF-4F5A-AEAB-F1B41DF01FBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9993C299-72BF-4F5A-AEAB-F1B41DF01FBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{9993C299-72BF-4F5A-AEAB-F1B41DF01FBC}.Release|Any CPU.Build.0 = Release|Any CPU
+		{FFE12700-0A7F-474B-8B0B-ABC209C79383}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{FFE12700-0A7F-474B-8B0B-ABC209C79383}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{FFE12700-0A7F-474B-8B0B-ABC209C79383}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{FFE12700-0A7F-474B-8B0B-ABC209C79383}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -96,6 +102,7 @@ Global
 		{8F6C4A79-98EA-4019-B72A-29290328185A} = {2C6BCFE4-581D-4BC8-91EC-BD9FA91B9605}
 		{A3E602C7-597C-469F-A065-BBA5901FFE2C} = {2C6BCFE4-581D-4BC8-91EC-BD9FA91B9605}
 		{9993C299-72BF-4F5A-AEAB-F1B41DF01FBC} = {2C6BCFE4-581D-4BC8-91EC-BD9FA91B9605}
+		{FFE12700-0A7F-474B-8B0B-ABC209C79383} = {2C6BCFE4-581D-4BC8-91EC-BD9FA91B9605}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {75D30B04-ADD6-4FC6-8D7E-FAD45B731BB4}