林豪 左 2 سال پیش
والد
کامیت
6212615105

+ 1 - 1
DBHelper/DBHelper.csproj

@@ -8,7 +8,7 @@
 
   <ItemGroup>
     <PackageReference Include="FreeRedis" Version="1.0.10" />
-    <PackageReference Include="LogHelper" Version="1.0.0.1" />
+    <PackageReference Include="LogHelper" Version="1.0.0.3" />
     <PackageReference Include="SqlSugarCore" Version="5.1.4.69" />
   </ItemGroup>
 

+ 1 - 1
DBHelper/DbLog.cs

@@ -27,7 +27,7 @@ namespace DBHelper
             {
                 ["DB"] = "Db"
             };
-            LogHelper.LogHelper.Init(this);
+            LogHub.Init(this);
         }
 
         /// <summary>

+ 1 - 1
LogHelper/InfoLog.cs

@@ -31,7 +31,7 @@
             {
                 ["INFO"] = "Info"
             };
-            LogHelper.Init(this);
+            LogHub.Init(this);
         }
 
         /// <summary>

+ 1 - 1
LogHelper/LogHelper.cs → LogHelper/LogHub.cs

@@ -10,7 +10,7 @@ namespace LogHelper
     /// <summary>
     /// 日志帮助类
     /// </summary>
-    public static class LogHelper
+    public static class LogHub
     {
         /// <summary>
         /// 日志仓库

+ 4 - 1
ServiceCenter/ServiceCenter.cs

@@ -2,7 +2,10 @@
 
 namespace ServiceCenter
 {
-    public static class ServiceCenter
+    /// <summary>
+    ///  服务中心
+    /// </summary>
+    public static class ServiceHub
     {
         #region 配置中心
 

+ 6 - 3
ServiceCenter/ServiceCenter.csproj

@@ -8,10 +8,13 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="DBHelper" Version="1.0.0.1" />
-    <PackageReference Include="LogHelper" Version="1.0.0.2" />
+    <PackageReference Include="DBHelper" Version="1.0.0.2" />
     <PackageReference Include="PlcSiemens" Version="1.0.0.2" />
-    <PackageReference Include="WCS.Core" Version="1.0.0.3" />
+    <PackageReference Include="WCS.Core" Version="1.0.0.4" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\WCS.Entity\WCS.Entity.csproj" />
   </ItemGroup>
 
 </Project>

+ 2 - 2
WCS.Entity.Protocol/WCS.Entity.Protocol.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <TargetFramework>netstandard2.1</TargetFramework>
@@ -7,7 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="WCS.Entity" Version="1.0.0.2" />
+    <PackageReference Include="WCS.Entity" Version="1.0.0.4" />
   </ItemGroup>
 
 </Project>

+ 1 - 1
WCS.Entity/WCS_PathDtl.cs

@@ -2,7 +2,7 @@
 
 namespace WCS.Entity
 {
-    internal class WCS_PathDtl
+    public class WCS_PathDtl
     {
         /// <summary>
         /// 起始地址设备号

+ 25 - 0
WCS.Service/PLCAccessors/PLCAccessorsCreater.cs

@@ -0,0 +1,25 @@
+using WCS.Core;
+using PLCType = WCS.Core.PLCType;
+
+namespace WCS.Service.PLCAccessors
+{
+    /// <summary>
+    ///  PLC访问器创建者
+    /// </summary>
+    public class PLCAccessorsCreater : IPLCAccessorCreater
+    {
+        /// <summary>
+        ///  创建PLC访问器
+        /// </summary>
+        /// <param name="data">PLC信息</param>
+        /// <returns></returns>
+        /// <exception cref="Exception"> </exception>
+        public IPLCAccessor Create(PLCInfo data)
+        {
+            if (data.Type == PLCType.Siemens)
+                return new SiemensS7PLC(data.IP, data.Port, data.Rack, data.Slot);
+            else
+                throw new Exception("不支持此PLC");
+        }
+    }
+}

+ 50 - 0
WCS.Service/PLCAccessors/SiemensS7PLC.cs

@@ -0,0 +1,50 @@
+using PlcSiemens.O;
+using PlcSiemens.Protocol.Common;
+using ServiceCenter;
+using ServiceCenter.Virtual_PLC;
+using WCS.Core;
+
+namespace WCS.Service.PLCAccessors
+{
+    public class SiemensS7PLC : IPLCAccessor
+    {
+        private SimenssPlc plc;
+
+        public SiemensS7PLC(string ip, int port, int rack, int slot)
+        {
+            plc = new SimenssPlc(ip, rack, slot);
+            plc.Connect();
+        }
+
+        public byte[] ReadBytes(ushort db, ushort address, ushort length)
+        {
+            if (ServiceHub.Any(SystemMode.虚拟plc))
+            {
+                return PlcData.Read(new PLCData { IP = plc.IP, DB = db, Length = length, DataLength = length });
+            }
+
+            if (!plc.Connected)
+                plc.Connect();
+            var res = plc.ReadArea(AreaType.DB, db, address, length, DataType.Byte);
+            if (res == null)
+                throw new Exception("读取DB块数据失败");
+            return res.Data;
+        }
+
+        public void WriteBytes(ushort db, ushort address, byte[] data)
+        {
+            if (ServiceHub.Any(SystemMode.虚拟plc))
+            {
+                PlcData.Write(new PLCData { IP = plc.IP, DB = db }, address, data);
+            }
+            else
+            {
+                if (!plc.Connected)
+                    plc.Connect();
+                var res = plc.WriteArea(AreaType.DB, db, address, (ushort)data.Length, DataType.Byte, data);
+                if (!res)
+                    throw new Exception("写入DB块数据失败");
+            }
+        }
+    }
+}

+ 21 - 0
WCS.Service/ProtocolProxy.cs

@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WCS.Core;
+
+namespace WCS.Service
+{
+    public class ProtocolProxy : ProtocolProxyBase
+    {
+        public ProtocolProxy(Device dev, ProtocolInfo info, Type protocolType) : base(dev, info, protocolType)
+        {
+        }
+
+        protected override void DataChanged()
+        {
+            //Console.WriteLine($"{Device.Code} Changed");
+        }
+    }
+}

+ 39 - 0
WCS.Service/Systems/DataCollectionSysyem.cs

@@ -0,0 +1,39 @@
+using System.ComponentModel;
+using WCS.Core;
+using WCS.Entity.Protocol.Station;
+using WCS.Service.Worlds;
+using WCS.WorkEngineering.Extensions;
+
+namespace WCS.Service.Systems
+{
+    /// <summary>
+    ///  数据采集系统
+    /// </summary>
+    //[BelongTo(typeof(DataCollectionWorld))]
+    [Description("数据采集系统")]
+    public class DataCollectionSysyem : ServiceSystem<bool, bool>
+    {
+        public List<Device<IStation520, IStation521>> ConvList;
+
+        public DataCollectionSysyem()
+        {
+            ConvList = World.Devices.Where(v => v.IsConv()).Select(v => new Device<IStation520, IStation521>(v)).ToList();
+        }
+
+        protected override bool Do(bool obj)
+        {
+            //Db.Do(db =>
+            //{
+            //    var plcData = new WCS_PlcData()
+            //    {
+            //        UPDATETIME = DateTime.Now,
+            //        WAREHOUSE = "111",
+            //        CONTENT = JsonConvert.SerializeObject(ConvList),
+            //    };
+            //    db.Default.Insertable<WCS_PlcData>(plcData).ExecuteCommand(); ;
+            //});
+            Console.WriteLine("11111");
+            return true;
+        }
+    }
+}

+ 0 - 2
WCS.Service/WCS.Service.csproj

@@ -12,8 +12,6 @@
     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
     <PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />
     <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.0" />
-    <PackageReference Include="ServiceCenter" Version="1.0.0.1" />
-    <PackageReference Include="WCS.Entity" Version="1.0.4" />
   </ItemGroup>
 
   <ItemGroup>

+ 251 - 4
WCS.Service/Worker.cs

@@ -1,21 +1,268 @@
+using DBHelper;
+using DBHelper.Redis;
+using LogHelper;
+using Newtonsoft.Json;
+using ServiceCenter;
+using ServiceCenter.Virtual_PLC;
+using SqlSugar;
+using System.Text;
+using WCS.Core;
+using WCS.Entity;
+using WCS.Entity.Protocol;
+using WCS.Entity.Protocol.BCR;
+using WCS.Entity.Protocol.Station;
+
 namespace WCS.Service
 {
+    /// <summary>
+    /// 工作服务
+    /// </summary>
     public class Worker : BackgroundService
     {
+        /// <summary>
+        /// 记录器
+        /// </summary>
         private readonly ILogger<Worker> _logger;
 
+        /// <summary>
+        /// 构造函数
+        /// </summary>
+        /// <param name="logger">记录器</param>
         public Worker(ILogger<Worker> logger)
         {
             _logger = logger;
         }
 
+        public static readonly string WcsDlog = "WCSDlog";
+        public static readonly string Wcsdb = "WCSDB";
+
+        /// <summary>
+        ///  执行
+        /// </summary>
+        /// <param name="stoppingToken">停止令牌</param>
+        /// <returns></returns>
         protected override async Task ExecuteAsync(CancellationToken stoppingToken)
         {
-            while (!stoppingToken.IsCancellationRequested)
+            if (stoppingToken.IsCancellationRequested)
+                return;
+
+            #region 启用日志
+
+            var logConfigText = RedisHub.Default.Check("LogConfigText") ?? throw new Exception("请在Redis中配置log4net相关内容");
+            var logConfig = JsonConvert.DeserializeObject<LogConfig>(logConfigText);
+            LogHub.SetConfigInfo(logConfig!);
+
+            #endregion 启用日志
+
+            _logger.LogInformation("WCS开始启动");
+            Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
+            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
+            Configs.StringEncoding = Encoding.UTF8;
+
+            #region 初始化数据库连接
+
+            var dbConnectionStrings = RedisHub.Default.Check("DbConnectionStrings") ?? throw new Exception("请在Redis中配置数据库连接相关内容");
+            ServiceHub.DbConnectionStrings = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(dbConnectionStrings);
+            if (ServiceHub.DbConnectionStrings != null)
             {
-                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
-                await Task.Delay(1000, stoppingToken);
+                if (ServiceHub.DbConnectionStrings.All(v => v.Key != Wcsdb)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库连接字符串");
+                if (ServiceHub.DbConnectionStrings.All(v => v.Key == Wcsdb && !v.IsDefault)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库为默认数据库");
+                if (ServiceHub.DbConnectionStrings.All(v => v.Key != WcsDlog)) throw new Exception("请在DbConnectionStrings中配置WCS日志数据库连接字符串");
             }
+
+            foreach (var connectionString in ServiceHub.DbConnectionStrings!)
+            {
+                Db.CreateContext(new ConnectionConfig()
+                {
+                    ConnectionString = connectionString.ConnectionString,
+                    DbType = connectionString.DbType
+                }, connectionString.Key);
+                if (connectionString.IsDefault) Db.SetDefaultDbContextType(connectionString.Key);
+
+                switch (connectionString.Key)
+                {
+                    case "WCSDB"://WCS基本数据库
+                        Db.Do(db =>
+                        {
+                            db.Default.CodeFirst.InitTables(typeof(WCS_PLC));
+                            db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
+                            db.Default.CodeFirst.InitTables(typeof(WCS_DEVICEHdr));
+                            db.Default.CodeFirst.InitTables(typeof(WCS_PathHdr));
+                            db.Default.CodeFirst.InitTables(typeof(WCS_PathDtl));
+                            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_PlcData));
+                            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_BCR80));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_RGV520));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_RGV521));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_RGV523));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_SRM520));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_SRM521));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_SRM537));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_Station520));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_Station521));
+                            //db.Default.CodeFirst.InitTables(typeof(WCS_Station523));
+                        });
+                        break;
+
+                    case "WCSDlog"://WCS日志数据库
+                        Db.Do(db =>
+                        {
+                            //TODO:DbMaintenance.CreateDatabase()并没起到作用,如果没有对应的数据库的话任然需要手动新建一个
+                            db.Context(WcsDlog).DbMaintenance.CreateDatabase();
+                            db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_BCR80));
+                            //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV520));
+                            //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV521));
+                            //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV523));
+                            //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_SRM520));
+                            //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_SRM521));
+                            //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_SRM537));
+                            db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_Station520));
+                            db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_Station521));
+                            db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_Station523));
+                            db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_Station91));
+                            //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS.Entity.PlcRawData));
+                        });
+                        break;
+
+                    default: //其他库
+                        break;
+                };
+            };
+
+            #endregion 初始化数据库连接
+
+            #region 创建虚拟PLC
+
+            var isOpenVirtualPlc = RedisHub.Default.Check("isOpenVirtualPLC") ?? throw new Exception("请在Redsi中配置是否启用虚拟PLC");
+            if (isOpenVirtualPlc == "1")
+            {
+                var plcDataConnectionString = RedisHub.Default.Check("plcDataConnectionString") ?? throw new Exception("请在Redsi中配置虚拟PLC使用的Redis连接字符串");
+
+                //从现有结构解析出需要的结构
+                var list = new List<PLCData>();
+                Db.Do(db =>
+                {
+                    var dataBlocks = db.Default.Queryable<WCS_DATABLOCK>().Includes(v => v.PLC).ToList();
+                    list.AddRange(dataBlocks.Select(dataBlock => new PLCData()
+                    {
+                        IP = dataBlock.PLC.IP,
+                        DB = dataBlock.NO,
+                        Length = dataBlock.LENGTH,
+                        DataLength = dataBlock.DATALENGTH,
+                    }));
+                });
+                PlcData.Init(plcDataConnectionString).InitPlcData(list);
+
+                ServiceHub.AddSystemMode(SystemMode.虚拟plc);
+            }
+
+            #endregion 创建虚拟PLC
+
+            //日志发布事件s
+            Configs.PublishEvent += () =>
+            {
+                //WMS.UploadDevInfo();
+                //ProtocolProxy.Do();
+            };
+            //异常上抛
+            Configs.UploadException = (d, s) =>
+            {
+                //if (s == "接口调用中") return;
+                //if (ProtocolProxy.AllDatas.ContainsKey(d))
+                //{
+                //    ProtocolProxy.AllDatas[d].Info = s;
+                //    //ProtocolProxy.AllDatas[d].Frame = LogicHandler.Frame;
+                //}
+                //WMS.TaskException(d, s);
+            };
+            //创建PLC访问器
+            Configs.PLCAccessorCreater = new PLCAccessors.PLCAccessorsCreater();
+            try
+            {
+                Db.Do(db =>
+                {
+                    //获取所有DB块读写协议
+                    var dbProtocols = db.Default.Queryable<WCS_DEVICEPROTOCOL>().Includes(v => v.DB, p => p.PLC).ToList();
+                    foreach (var dbProtocol in dbProtocols)
+                    {
+                        if (dbProtocol.DEVICECODE.Contains("SRM"))
+                        {
+                        }
+                        else if (dbProtocol.DEVICECODE.Contains("RGV"))
+                        {
+                        }
+                        else if (dbProtocol.DEVICECODE.Contains("BCR"))
+                        {
+                        }
+                        else
+                        {
+                            if (dbProtocol.DB.CODE.Contains("520"))
+                            {
+                                Add<IStation520>(dbProtocol.DEVICECODE, dbProtocol.POSITION, (ushort)dbProtocol.DB.NO, dbProtocol.DB.PLC.IP);
+                            }
+                            else if (dbProtocol.DB.CODE.Contains("521"))
+                            {
+                                Add<IStation521>(dbProtocol.DEVICECODE, dbProtocol.POSITION, (ushort)dbProtocol.DB.NO, dbProtocol.DB.PLC.IP);
+                            }
+                            else if (dbProtocol.DB.CODE.Contains("523"))
+                            {
+                                Add<IStation522>(dbProtocol.DEVICECODE, dbProtocol.POSITION, (ushort)dbProtocol.DB.NO, dbProtocol.DB.PLC.IP);
+                            }
+                            else
+                            {
+                                continue;
+                            }
+                        }
+                    }
+                });
+
+                #region 唤醒所有的世界
+
+                World.StartAll();
+
+                #endregion 唤醒所有的世界
+
+                _logger.LogInformation("WCS启动成功");
+            }
+            catch (Exception ex)
+            {
+                _logger.LogError("WCS启动失败{0}", ex.Message);
+            }
+        }
+
+        /// <summary>
+        ///  添加协议
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="code"> </param>
+        /// <param name="position"> </param>
+        /// <param name="db"></param>
+        /// <param name="ip"></param>
+        public static void Add<T>(string code, int position, ushort db, string ip)
+        {
+            var info = new ProtocolInfo
+            {
+                Position = position,
+                DBInfo = new DBInfo
+                {
+                    No = db,
+                    PLCInfo = new PLCInfo
+                    {
+                        IP = ip,
+                        Port = 102,
+                        Rack = 0,
+                        Slot = 1,
+                        Type = Core.PLCType.Siemens
+                    }
+                }
+            };
+            Protocols<T>.Add(code, info);
         }
     }
-}
+}

+ 32 - 0
WCS.Service/Worlds/DataCollectionWorld.cs

@@ -0,0 +1,32 @@
+using DBHelper;
+using Newtonsoft.Json;
+using System.ComponentModel;
+using WCS.Core;
+using WCS.Entity;
+using WCS.Service.Systems;
+
+namespace WCS.Service.Worlds
+{
+    /// <summary>
+    ///  数据采集世界,
+    ///  该世界不进行任何的交互处理,仅进行硬件数据采集
+    /// </summary>
+    [Description("数据采集世界")]
+    public class DataCollectionWorld : World
+    { /// <summary>
+      /// 构造函数
+      /// </summary>
+        public DataCollectionWorld()
+        {
+            Interval = 50;
+        }
+
+        protected override void AfterUpdate()
+        {
+           
+            //var a = Ltc.GetSystem<DataCollectionSysyem>();
+            //var v = a.Invoke(true);
+            // Ltc.GetSystem<DataCollectionSysyem>().Invoke(true);
+        }
+    }
+}

+ 1 - 1
WCS.Service/appsettings.json

@@ -7,6 +7,6 @@
     }
   },
   "ConnectionStrings": {
-    "Redis": "127.0.0.1,database=0,prefix=Aging:"
+    "Redis": "127.0.0.1,database=0,prefix=Sorting:"
   }
 }

+ 16 - 0
WCS.WorkEngineering/Extensions/DeviceExtension.cs

@@ -0,0 +1,16 @@
+using WCS.Core;
+using WCS.Entity.Protocol.Station;
+
+namespace WCS.WorkEngineering.Extensions
+{
+    /// <summary>
+    ///  设备扩展
+    /// </summary>
+    public static class DeviceExtension
+    {
+        public static bool IsConv(this Device source)
+        {
+            return source.HasProtocol(typeof(IStation521)) || source.HasProtocol(typeof(IStation520)) || source.HasProtocol(typeof(IStation522));
+        }
+    }
+}

+ 6 - 2
WCS.WorkEngineering/WCS.WorkEngineering.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <TargetFramework>net7.0</TargetFramework>
@@ -8,7 +8,11 @@
 
   <ItemGroup>
     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
-    <PackageReference Include="ServiceCenter" Version="1.0.0.1" />
+    <PackageReference Include="ServiceCenter" Version="1.0.0.6" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\WCS.Entity.Protocol\WCS.Entity.Protocol.csproj" />
   </ItemGroup>
 
 </Project>

+ 1 - 1
WCS.WorkEngineering/worlds/MainWorld.cs

@@ -5,7 +5,7 @@ using System.Text;
 using System.Threading.Tasks;
 using WCS.Core;
 
-namespace WCS.WorkEngineering.worlds
+namespace WCS.WorkEngineering.Worlds
 {
     /// <summary>
     /// 主世界,所有的系统(交互点)默认在该世界下执行。