Browse Source

更新代码

zhaoyaxiong 1 year ago
parent
commit
6d833e7a4f
22 changed files with 644 additions and 16 deletions
  1. 14 1
      TFT_PC_FrameWork/TFT_BizService/IService/IAgvTaskService.cs
  2. 0 1
      TFT_PC_FrameWork/TFT_BizService/IService/IMesLoadDataService.cs
  3. 0 1
      TFT_PC_FrameWork/TFT_BizService/IService/IMesUnLoadDataService.cs
  4. 21 1
      TFT_PC_FrameWork/TFT_BizService/IService/ISysRealTimeRunService.cs
  5. 28 1
      TFT_PC_FrameWork/TFT_BizService/Service/AgvTaskService.cs
  6. 37 0
      TFT_PC_FrameWork/TFT_BizService/Service/AutoMapper/AutoMapperProvider.cs
  7. 1 0
      TFT_PC_FrameWork/TFT_BizService/Service/MesLoadDataService.cs
  8. 2 1
      TFT_PC_FrameWork/TFT_BizService/Service/MesUnLoadDataService.cs
  9. 278 1
      TFT_PC_FrameWork/TFT_BizService/Service/SysRealTimeRunService.cs
  10. 2 0
      TFT_PC_FrameWork/TFT_BizService/TFT_BizService.csproj
  11. 175 0
      TFT_PC_FrameWork/TFT_Common/MqttClientHelper.cs
  12. 1 0
      TFT_PC_FrameWork/TFT_Common/TFT_Common.csproj
  13. 12 2
      TFT_PC_FrameWork/TFT_Model/Entities/BasLoc.cs
  14. 1 1
      TFT_PC_FrameWork/TFT_Model/Entities/BasPlc.cs
  15. 1 1
      TFT_PC_FrameWork/TFT_Model/Entities/BasPlcAddress.cs
  16. 45 1
      TFT_PC_FrameWork/TFT_Model/Entities/BasPlcItem.cs
  17. 1 1
      TFT_PC_FrameWork/TFT_Model/Entities/BizAgvTask.cs
  18. 1 1
      TFT_PC_FrameWork/TFT_Model/Entities/MesUnloadCst.cs
  19. 2 0
      TFT_PC_FrameWork/TFT_PC_FrameWork/ViewModels/MainWindowViewModel.cs
  20. 8 1
      TFT_PC_FrameWork/TFT_PC_FrameWork/ViewModels/MesUnLoadDataViewModel.cs
  21. 13 0
      TFT_PC_FrameWork/TFT_PC_FrameWork/ViewModels/SysRealTimeRunViewModel.cs
  22. 1 1
      TFT_PC_FrameWork/TFT_PC_FrameWork/Views/AgvTask.xaml.cs

+ 14 - 1
TFT_PC_FrameWork/TFT_BizService/IService/IAgvTaskService.cs

@@ -9,6 +9,19 @@ namespace TFT_BizService
 {
     public interface IAgvTaskService
     {
-        
+        public List<BizAgvTask> AgvTasks { get; set; }
+        /// <summary>
+        /// 监控AGV与PLC的交互:1、PLC给AGV的信息2、AGV给PLC的信息
+        /// </summary>
+        public void AgvMonitor();
+
+        /// <summary>
+        /// 检查status为0的任务,并下发任务到agv
+        /// </summary>
+        public void AgvTaskRun();
+        /// <summary>
+        /// 强制完成AGV任务
+        /// </summary>
+        public void TaskComplete();
     }
 }

+ 0 - 1
TFT_PC_FrameWork/TFT_BizService/IService/IMesLoadDataService.cs

@@ -9,6 +9,5 @@ namespace TFT_BizService
 {
     public interface IMesLoadDataService
     {
-        
     }
 }

+ 0 - 1
TFT_PC_FrameWork/TFT_BizService/IService/IMesUnLoadDataService.cs

@@ -9,6 +9,5 @@ namespace TFT_BizService
 {
     public interface IMesUnLoadDataService
     {
-        
     }
 }

+ 21 - 1
TFT_PC_FrameWork/TFT_BizService/IService/ISysRealTimeRunService.cs

@@ -9,6 +9,26 @@ namespace TFT_BizService
 {
     public interface ISysRealTimeRunService
     {
-        
+        List<BasLoc> BasLocList { get; set; }
+        List<BasPlcItem> BasPlcItemList { get; set; }
+        /// <summary>
+        /// 下料机上空卡
+        /// </summary>
+        public void LoadEmpty();
+        /// <summary>
+        /// 上料机上满卡
+        /// </summary>
+        public void LoadFull();
+        /// <summary>
+        /// 上料机下空卡
+        /// </summary>
+        public void UnloadEmpty();
+        /// <summary>
+        /// 下料机下满卡
+        /// </summary>
+        public void UnloadFull();
+
+        public void TaskRun();
+        public void TaskStop();
     }
 }

+ 28 - 1
TFT_PC_FrameWork/TFT_BizService/Service/AgvTaskService.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using TFT_DbHelper;
 using TFT_Model.Entities;
@@ -13,11 +14,37 @@ namespace TFT_BizService
     /// AGV任务管理 业务类
     /// </summary>
     [AutoInject(typeof(IAgvTaskService), InjectTypeEnum.Singleton, false)]
-    public  class AgvTaskService : IAgvTaskService
+    public class AgvTaskService : IAgvTaskService
     {
+        private readonly CancellationTokenSource tokenSource = new();
+        private readonly List<Task> taskList = new();
+        public List<BizAgvTask> AgvTasks { get; set; }
         public AgvTaskService()
         {
 
         }
+
+
+        public void AgvMonitor()
+        {
+            taskList.Add(Task.Factory.StartNew(() => { 
+                
+            }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
+        }
+
+        public void AgvTaskRun()
+        {
+            taskList.Add(Task.Factory.StartNew(() => {
+                while (true)
+                {
+                    AgvTasks = DbHelper.Db.Queryable<BizAgvTask>().ToList();
+                }
+            }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
+        }
+
+        public void TaskComplete()
+        {
+            throw new NotImplementedException();
+        }
     }
 }

+ 37 - 0
TFT_PC_FrameWork/TFT_BizService/Service/AutoMapper/AutoMapperProvider.cs

@@ -0,0 +1,37 @@
+using AutoMapper;
+using Prism.Ioc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using TFT_Model.Other;
+
+namespace TFT_PC_FrameWork.AutoMapper
+{
+    public interface IAutoMapperProvider
+    {
+        IMapper GetMapper();
+    }
+    [AutoInject(typeof(IAutoMapperProvider), InjectTypeEnum.Singleton, false)]
+    public class AutoMapperProvider : IAutoMapperProvider
+    {
+        private readonly MapperConfiguration _configuration;
+
+        public AutoMapperProvider(IContainerProvider container)
+        {
+            _configuration = new MapperConfiguration(configure =>
+            {
+                configure.ConstructServicesUsing(container.Resolve);
+
+                //扫描profile文件
+                configure.AddMaps(AppDomain.CurrentDomain.GetAssemblies());
+            });
+        }
+
+        public IMapper GetMapper()
+        {
+            return _configuration.CreateMapper();
+        }
+    }
+}

+ 1 - 0
TFT_PC_FrameWork/TFT_BizService/Service/MesLoadDataService.cs

@@ -19,5 +19,6 @@ namespace TFT_BizService
         {
 
         }
+
     }
 }

+ 2 - 1
TFT_PC_FrameWork/TFT_BizService/Service/MesUnLoadDataService.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using TFT_DbHelper;
 using TFT_Model.Entities;
@@ -13,7 +14,7 @@ namespace TFT_BizService
     /// MES下料数据 业务类
     /// </summary>
     [AutoInject(typeof(IMesUnLoadDataService), InjectTypeEnum.Singleton, false)]
-    public  class MesUnLoadDataService : IMesUnLoadDataService
+    public class MesUnLoadDataService : IMesUnLoadDataService
     {
         public MesUnLoadDataService()
         {

+ 278 - 1
TFT_PC_FrameWork/TFT_BizService/Service/SysRealTimeRunService.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using TFT_DbHelper;
 using TFT_Model.Entities;
@@ -13,11 +14,287 @@ namespace TFT_BizService
     /// 系统实时运行 业务类
     /// </summary>
     [AutoInject(typeof(ISysRealTimeRunService), InjectTypeEnum.Singleton, false)]
-    public  class SysRealTimeRunService : ISysRealTimeRunService
+    public class SysRealTimeRunService : ISysRealTimeRunService
     {
+        private readonly CancellationTokenSource tokenSource = new();
+        private readonly List<Task> taskList = new();
+        public List<BasLoc> BasLocList { get; set; }
+        public List<BasPlcItem> BasPlcItemList { get; set; }
+
+
         public SysRealTimeRunService()
         {
 
         }
+
+        #region 上下料
+        public void LoadFull()
+        {
+            taskList.Add(Task.Factory.StartNew(() =>
+            {
+                while (true)
+                {
+                    //从bas_loc表中,查找站台类型为上料机的站台
+                    List<BasLoc> basLocs = BasLocList.Where(x => x.LocType == 1).ToList();
+                    //遍历所有上料机站台,根据bas_plc_item表中每个站台的上料信号处理是否上料。
+                    foreach (BasLoc basLoc_ShangLiaoJi in basLocs)
+                    {
+                        BasPlcItem basPlcItem = BasPlcItemList.First(x => x.LocNo == basLoc_ShangLiaoJi.LocNo && x.PlcItemCode == PlcItemCode.RequestMoveIn);
+                        BasPlcItem _basPlcItem = BasPlcItemList.First(x => x.LocNo == basLoc_ShangLiaoJi.LocNo && x.PlcItemCode == PlcItemCode.MoveInCstFulled);
+                        //读取上料信号,basPlcItem && !_basPlcItem
+                        bool isUnload = true;//读取PLC
+                                             //上料信号为true
+                        if (isUnload)
+                        {
+                            //MES在线 条件:状态为初始,添加状态是MES,站台号是上料机站台号
+                            MesUnloadCst unloadCst = DbHelper.Db.Queryable<MesUnloadCst>().First(x => x.Status == 0 && x.AddType == 0 && x.LocationNo == basLoc_ShangLiaoJi.LocNo);
+
+                            if (unloadCst != null)
+                            {
+                                //更改MES上料任务状态
+                                unloadCst.Status = 11;
+                                DbHelper.Db.Updateable(unloadCst).ExecuteCommand();
+                                //查找缓存架 (无工单) 有满卡 agv任务id为空 启用
+                                BasLoc basLoc_HuanCunJia = BasLocList.First(x => x.LocType == 0
+                                                                       //&& string.IsNullOrEmpty(x.WorkOrder)
+                                                                       && x.CstType == 2
+                                                                       && x.AgvTaskId == null
+                                                                       && x.UsedFlag == 1);
+                                if (basLoc_HuanCunJia != null)
+                                {
+                                    //生成AGV任务,起点缓存架,终点为上料机
+                                    //插入biz_agv_task数据
+                                    BizAgvTask bizAgvTask = new()
+                                    {
+                                        TaskType = 0,
+                                        TakeLocNo = basLoc_HuanCunJia.LocNo,
+                                        PutLocNo = basLoc_ShangLiaoJi.LocNo,
+                                        CstType = 1,
+                                        Status = 0,
+                                        CreateBy = "admin",
+                                        CreateTime = DateTime.Now,
+                                        UpdateBy = "admin",
+                                        UpdateTime = DateTime.Now,
+                                    };
+                                    DbHelper.Db.Insertable(bizAgvTask).ExecuteCommand();
+                                    //更新上料任务状态
+                                    unloadCst.Status = 22;
+                                    DbHelper.Db.Updateable(unloadCst).ExecuteCommand();
+                                }
+                            }
+                        }
+                    }
+
+                }
+            }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
+        }
+        public void UnloadFull()
+        {
+            taskList.Add(Task.Factory.StartNew(() =>
+            {
+                while (true)
+                {
+                    //从bas_loc表中,查找站台类型为下料机的站台
+                    List<BasLoc> basLocs = BasLocList.Where(x => x.LocType == 2).ToList();
+                    //遍历所有下料机站台,根据bas_plc_item表中每个站台的下料信号处理是否下料。
+                    foreach (BasLoc basLoc_XiaLiaoJi in basLocs)
+                    {
+                        BasPlcItem basPlcItem = BasPlcItemList.First(x => x.LocNo == basLoc_XiaLiaoJi.LocNo && x.PlcItemCode == PlcItemCode.RequestMoveOut);
+                        BasPlcItem _basPlcItem = BasPlcItemList.First(x => x.LocNo == basLoc_XiaLiaoJi.LocNo && x.PlcItemCode == PlcItemCode.MoveOutCstFulled);
+                        //读取下料信号,basPlcItem && _basPlcItem
+                        bool isUnload = true;//读取PLC
+                                             //下料信号为true
+                        if (isUnload)
+                        {
+                            //MES在线 条件:状态为初始,添加状态是MES,站台号是basLoc站台号
+                            MesUnloadCst unloadCst = DbHelper.Db.Queryable<MesUnloadCst>().First(x => x.Status == 0 && x.AddType == 0 && x.LocationNo == basLoc_XiaLiaoJi.LocNo);
+
+                            if (unloadCst != null)
+                            {
+                                //更改MES下料任务状态
+                                unloadCst.Status = 11;
+                                DbHelper.Db.Updateable(unloadCst).ExecuteCommand();
+                                //查找缓存架 无工单 无卡 agv任务id为空
+                                BasLoc basLoc_HuanCunJia = BasLocList.First(x => x.LocType == 0
+                                                                       && string.IsNullOrEmpty(x.WorkOrder)
+                                                                       && x.CstType == 0
+                                                                       && x.AgvTaskId == null
+                                                                       && x.UsedFlag == 1);
+                                if (basLoc_HuanCunJia != null)
+                                {
+                                    //生成AGV任务,起点为下料机,终点为缓存架
+                                    //插入biz_agv_task数据
+                                    BizAgvTask bizAgvTask = new()
+                                    {
+                                        TaskType = 1,
+                                        TakeLocNo = basLoc_XiaLiaoJi.LocNo,
+                                        PutLocNo = basLoc_HuanCunJia.LocNo,
+                                        CstType = 1,
+                                        Status = 0,
+                                        CreateBy = "admin",
+                                        CreateTime = DateTime.Now,
+                                        UpdateBy = "admin",
+                                        UpdateTime = DateTime.Now,
+                                    };
+                                    DbHelper.Db.Insertable(bizAgvTask).ExecuteCommand();
+                                    //更新MES下料任务状态
+                                    unloadCst.Status = 22;
+                                    DbHelper.Db.Updateable(unloadCst).ExecuteCommand();
+                                }
+                            }
+                        }
+                    }
+
+                }
+            }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
+        }
+        public void UnloadEmpty()
+        {
+            taskList.Add(Task.Factory.StartNew(() =>
+            {
+                while (true)
+                {
+                    //从bas_loc表中,查找站台类型为上料机的站台
+                    List<BasLoc> basLocs = BasLocList.Where(x => x.LocType == 1).ToList();
+                    //遍历所有上料机站台,根据bas_plc_item表中每个站台的下料信号处理是否下料。
+                    foreach (BasLoc basLoc_ShangLiaoJi in basLocs)
+                    {
+                        BasPlcItem basPlcItem = BasPlcItemList.First(x => x.LocNo == basLoc_ShangLiaoJi.LocNo && x.PlcItemCode == PlcItemCode.RequestMoveOut);
+                        BasPlcItem _basPlcItem = BasPlcItemList.First(x => x.LocNo == basLoc_ShangLiaoJi.LocNo && x.PlcItemCode == PlcItemCode.MoveOutCstFulled);
+                        //读取下料信号,basPlcItem && _basPlcItem
+                        bool isUnload = true;//读取PLC
+                                             //下料信号为true
+                        if (isUnload)
+                        {
+                            //MES在线 条件:状态为初始,添加状态是MES,站台号是上料机站台号
+                            MesUnloadCst unloadCst = DbHelper.Db.Queryable<MesUnloadCst>().First(x => x.Status == 0 && x.AddType == 0 && x.LocationNo == basLoc_ShangLiaoJi.LocNo);
+
+                            if (unloadCst != null)
+                            {
+                                //更改MES下料任务状态
+                                unloadCst.Status = 11;
+                                DbHelper.Db.Updateable(unloadCst).ExecuteCommand();
+                                //查找缓存架 无工单 无卡 agv任务id为空 启用
+                                BasLoc basLoc_HuanCunJia = BasLocList.First(x => x.LocType == 0
+                                                                                 && string.IsNullOrEmpty(x.WorkOrder)
+                                                                                 && x.CstType == 0
+                                                                                 && x.AgvTaskId == null
+                                                                                 && x.UsedFlag == 1);
+                                if (basLoc_HuanCunJia != null)
+                                {
+                                    //生成AGV任务,起点为上料机,终点为缓存架
+                                    //插入biz_agv_task数据
+                                    BizAgvTask bizAgvTask = new()
+                                    {
+                                        TaskType = 2,
+                                        TakeLocNo = basLoc_ShangLiaoJi.LocNo,
+                                        PutLocNo = basLoc_HuanCunJia.LocNo,
+                                        CstType = 0,
+                                        Status = 0,
+                                        CreateBy = "admin",
+                                        CreateTime = DateTime.Now,
+                                        UpdateBy = "admin",
+                                        UpdateTime = DateTime.Now,
+                                    };
+                                    DbHelper.Db.Insertable(bizAgvTask).ExecuteCommand();
+                                    //更新MES下料任务状态
+                                    unloadCst.Status = 22;
+                                    DbHelper.Db.Updateable(unloadCst).ExecuteCommand();
+                                }
+                            }
+                        }
+                    }
+
+                }
+            }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
+        }
+        public void LoadEmpty()
+        {
+            taskList.Add(Task.Factory.StartNew(() =>
+            {
+                while (true)
+                {
+                    //从bas_loc表中,查找站台类型为下料机的站台
+                    List<BasLoc> basLocs = BasLocList.Where(x => x.LocType == 2).ToList();
+                    //遍历所有下料机站台,根据bas_plc_item表中每个站台的上料信号处理是否上料。
+                    foreach (BasLoc basLoc_XiaLiaoJi in basLocs)
+                    {
+                        BasPlcItem basPlcItem = BasPlcItemList.First(x => x.LocNo == basLoc_XiaLiaoJi.LocNo && x.PlcItemCode == PlcItemCode.RequestMoveIn);
+                        BasPlcItem _basPlcItem = BasPlcItemList.First(x => x.LocNo == basLoc_XiaLiaoJi.LocNo && x.PlcItemCode == PlcItemCode.MoveInCstFulled);
+                        //读取信号,basPlcItem && !_basPlcItem
+                        bool isUnload = true;//读取PLC
+                                             //信号为true
+                        if (isUnload)
+                        {
+                            //MES在线 条件:状态为初始,添加状态是MES,站台号是下料机站台号
+                            MesUnloadCst unloadCst = DbHelper.Db.Queryable<MesUnloadCst>().First(x => x.Status == 0 && x.AddType == 0 && x.LocationNo == basLoc_XiaLiaoJi.LocNo);
+
+                            if (unloadCst != null)
+                            {
+                                //更改MES上料任务状态
+                                unloadCst.Status = 11;
+                                DbHelper.Db.Updateable(unloadCst).ExecuteCommand();
+                                //查找缓存架 无工单 有空卡 agv任务id为空 启用
+                                BasLoc basLoc_HuanCunJia = BasLocList.First(x => x.LocType == 0
+                                                                       && string.IsNullOrEmpty(x.WorkOrder)
+                                                                       && x.CstType == 1
+                                                                       && x.AgvTaskId == null
+                                                                       && x.UsedFlag == 1);
+                                if (basLoc_HuanCunJia != null)
+                                {
+                                    //生成AGV任务,起点为缓存架,终点为下料机
+                                    //插入biz_agv_task数据
+                                    BizAgvTask bizAgvTask = new()
+                                    {
+                                        TaskType = 3,
+                                        TakeLocNo = basLoc_HuanCunJia.LocNo,
+                                        PutLocNo = basLoc_XiaLiaoJi.LocNo,
+                                        CstType = 0,
+                                        Status = 0,
+                                        CreateBy = "admin",
+                                        CreateTime = DateTime.Now,
+                                        UpdateBy = "admin",
+                                        UpdateTime = DateTime.Now,
+                                    };
+                                    DbHelper.Db.Insertable(bizAgvTask).ExecuteCommand();
+                                    //更新上料任务状态
+                                    unloadCst.Status = 22;
+                                    DbHelper.Db.Updateable(unloadCst).ExecuteCommand();
+                                }
+                            }
+                        }
+                    }
+
+                }
+            }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
+        }
+
+        public void TaskRun()
+        {
+            LoadFull();
+            UnloadFull();
+            UnloadEmpty();
+            LoadEmpty();
+        }
+
+        public void TaskStop()
+        {
+            if (taskList.Count > 0)
+            {
+                tokenSource?.Cancel();
+                foreach (Task item in taskList)
+                {
+                    while (!item.IsCompleted)
+                    {
+
+                    }
+                    item.Dispose();
+                }
+                taskList.Clear();
+                tokenSource?.Dispose();
+            }
+        }
+
+        #endregion
     }
 }

+ 2 - 0
TFT_PC_FrameWork/TFT_BizService/TFT_BizService.csproj

@@ -7,7 +7,9 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="AutoMapper" Version="12.0.1" />
     <PackageReference Include="Mapster" Version="7.4.0" />
+    <PackageReference Include="Prism.Core" Version="8.1.97" />
   </ItemGroup>
 
   <ItemGroup>

+ 175 - 0
TFT_PC_FrameWork/TFT_Common/MqttClientHelper.cs

@@ -0,0 +1,175 @@
+using MQTTnet;
+using MQTTnet.Client;
+using MQTTnet.Server;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace TFT_Common
+{
+    public static class MqttClientHelper
+    {
+        public static string Id { get; set; }
+        public static string Ip { get; set; }
+        public static string UserName { get; set; }
+        public static string Password { get; set; }
+        public static int Port { get; set; }
+
+        public static Queue<MqttApplicationMessage> MessageQueue { get; set; } = new();
+
+        public static IMqttClient Client { get; set; }
+
+        public static void Init(string ip,int port)
+        {
+            Id = $"Id{DateTime.Now:YYMMDDHHmmss}";
+            Ip = ip ;
+            Port = port ;
+            MessageQueue.Clear();
+            Client = new MqttFactory().CreateMqttClient();
+            Client.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync;
+            Client.ConnectedAsync += MqttClient_ConnectedAsync;
+            Client.DisconnectedAsync += MqttClient_DisconnectedAsync;
+        }
+        public static async Task ConnectMqttServerAsync()
+        {
+            // Create a new MQTT client.
+
+            if (Client == null)
+            {
+                var factory = new MqttFactory();
+                Client = factory.CreateMqttClient();
+
+                Client.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync;
+                Client.ConnectedAsync += MqttClient_ConnectedAsync;
+                Client.DisconnectedAsync += MqttClient_DisconnectedAsync;
+            }
+
+            //非托管客户端
+            try
+            {
+                //Create TCP based options using the builder.
+                var options = new MqttClientOptionsBuilder()
+                    .WithClientId(Id)
+                    .WithTcpServer(Ip, Port)
+                    //.WithCredentials(txtUsername.Text, txtPsw.Text)
+                    //.WithTls()//服务器端没有启用加密协议,这里用tls的会提示协议异常
+                    .WithCleanSession()
+                .Build();
+                await Client.ConnectAsync(options);
+            }
+            catch (Exception ex)
+            {
+            }
+
+            //托管客户端
+            try
+            {
+                //// Setup and start a managed MQTT client.
+                //var options = new ManagedMqttClientOptionsBuilder()
+                //    .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
+                //    .WithClientOptions(new MqttClientOptionsBuilder()
+                //        .WithClientId("Client_managed")
+                //        .WithTcpServer("192.168.88.3", 8223)
+                //        .WithTls()
+                //        .Build())
+                //    .Build();
+
+                //var mqttClient = new MqttFactory().CreateManagedMqttClient();
+                //await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("my/topic").Build());
+                //await mqttClient.StartAsync(options);
+            }
+            catch (Exception)
+            {
+
+            }
+        }
+
+        public static async Task Publish(string topic, string Message)
+        {
+            if (string.IsNullOrEmpty(topic))
+            {
+                MessageBox.Show("发布主题不能为空!");
+                return;
+            }
+
+            var message = new MqttApplicationMessageBuilder()
+                .WithTopic(topic)
+                .WithPayload(Message)
+                .WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce)
+                .WithRetainFlag(true)
+                .Build();
+
+            await Client.PublishAsync(message);
+        }
+
+        public static async Task Subscribe(string topic)
+        {
+            if (string.IsNullOrEmpty(topic))
+            {
+                MessageBox.Show("订阅主题不能为空!");
+                return;
+            }
+
+            if (!Client.IsConnected)
+            {
+                MessageBox.Show("MQTT客户端尚未连接!");
+                return;
+            }
+
+            // Subscribe to a topic
+            await Client.SubscribeAsync(new MqttTopicFilterBuilder()
+                .WithTopic(topic)
+                .WithAtMostOnceQoS()
+                .Build()
+                );
+        }
+
+
+
+        private static async Task MqttClient_DisconnectedAsync(MqttClientDisconnectedEventArgs arg)
+        {
+            //提示连接断开
+
+            //重新连接
+            if (!Client.IsConnected)
+            {
+                var options = new MqttClientOptionsBuilder()
+                    .WithClientId(Id)
+                    .WithTcpServer(Ip, Port)
+                    .WithCredentials(UserName, Password)
+                    //.WithTls()
+                    .WithCleanSession()
+                    .Build();
+                await Task.Delay(TimeSpan.FromSeconds(5));
+                try
+                {
+                    await Client.ConnectAsync(options);
+                }
+                catch (Exception err)
+                {
+                }
+            }
+            else
+            {
+
+            }
+
+        }
+
+        private static Task MqttClient_ConnectedAsync(MqttClientConnectedEventArgs arg)
+        {
+            return Task.CompletedTask;
+        }
+
+        private static Task MqttClient_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
+        {
+            MessageQueue.Enqueue(arg.ApplicationMessage);
+            return Task.CompletedTask;
+        }
+
+
+    }
+}

+ 1 - 0
TFT_PC_FrameWork/TFT_Common/TFT_Common.csproj

@@ -7,6 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="MQTTnet" Version="4.3.1.873" />
     <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
   </ItemGroup>
 

+ 12 - 2
TFT_PC_FrameWork/TFT_Model/Entities/BasLoc.cs

@@ -30,7 +30,7 @@ namespace TFT_Model.Entities
         [SugarColumn(ColumnName = "loc_name")]
         public string LocName { get; set; }
         /// <summary>
-        /// 站台类型。0:缓存;1:上料机站台;2:下料机站台; 
+        /// 站台类型。0:缓存;1:上料机站台;2:下料机站台; 
         ///</summary>
         [SugarColumn(ColumnName = "loc_type")]
         public int LocType { get; set; }
@@ -50,6 +50,16 @@ namespace TFT_Model.Entities
         [SugarColumn(ColumnName = "cst_type")]
         public int CstType { get; set; }
         /// <summary>
+        /// AGV任务ID。 如果有数据代表该缓存位正在被占用,不能被分配;如果没有数据代表该缓存位没有被占用,可以被分配。
+        ///</summary>
+        [SugarColumn(ColumnName = "agv_task_id")]
+        public int? AgvTaskId { get; set; }
+        /// <summary>
+        /// 使用标识。0:禁用;1:启用。默认启用。
+        /// </summary>
+        [SugarColumn(ColumnName = "used_flag")]
+        public int UsedFlag { get; set; }
+        /// <summary>
         /// 创建者 
         ///</summary>
         [SugarColumn(ColumnName = "create_by")]
@@ -58,7 +68,7 @@ namespace TFT_Model.Entities
         /// 创建时间 
         ///</summary>
         [SugarColumn(ColumnName = "create_time")]
-        public DateTime? CreateTime { get; set; }
+        public DateTime CreateTime { get; set; }
         /// <summary>
         /// 更新者 
         ///</summary>

+ 1 - 1
TFT_PC_FrameWork/TFT_Model/Entities/BasPlc.cs

@@ -53,7 +53,7 @@ namespace TFT_Model.Entities
         /// 创建时间 
         ///</summary>
         [SugarColumn(ColumnName = "create_time")]
-        public DateTime? CreateTime { get; set; }
+        public DateTime CreateTime { get; set; }
         /// <summary>
         /// 更新者 
         ///</summary>

+ 1 - 1
TFT_PC_FrameWork/TFT_Model/Entities/BasPlcAddress.cs

@@ -58,7 +58,7 @@ namespace TFT_Model.Entities
         /// 创建时间 
         ///</summary>
         [SugarColumn(ColumnName = "create_time")]
-        public DateTime? CreateTime { get; set; }
+        public DateTime CreateTime { get; set; }
         /// <summary>
         /// 更新者 
         ///</summary>

+ 45 - 1
TFT_PC_FrameWork/TFT_Model/Entities/BasPlcItem.cs

@@ -84,7 +84,7 @@ namespace TFT_Model.Entities
         /// 创建时间 
         ///</summary>
         [SugarColumn(ColumnName = "create_time")]
-        public DateTime? CreateTime { get; set; }
+        public DateTime CreateTime { get; set; }
         /// <summary>
         /// 更新者 
         ///</summary>
@@ -101,4 +101,48 @@ namespace TFT_Model.Entities
         [SugarColumn(ColumnName = "remark")]
         public string Remark { get; set; }
     }
+
+    public static class PlcItemCode
+    {
+        /// <summary>
+        /// 准备好搬出
+        /// </summary>
+        public const string ReadyMoveOut = "ReadyMoveOut";
+        /// <summary>
+        /// 请求搬出
+        /// </summary>
+        public const string RequestMoveOut = "RequestMoveOut";
+        /// <summary>
+        /// 搬出中
+        /// </summary>
+        public const string MovingOut = "MovingOut";
+        /// <summary>
+        /// 搬出完成
+        /// </summary>
+        public const string MoveOutFinished = "MoveOutFinished";
+        /// <summary>
+        /// 满卡
+        /// </summary>
+        public const string MoveOutCstFulled = "MoveOutCstFulled";
+        /// <summary>
+        /// 准备好搬入
+        /// </summary>
+        public const string ReadyMoveIn = "ReadyMoveIn";
+        /// <summary>
+        /// 请求搬入
+        /// </summary>
+        public const string RequestMoveIn = "RequestMoveIn";
+        /// <summary>
+        /// 搬入中
+        /// </summary>
+        public const string MovingIn = "MovingIn";
+        /// <summary>
+        /// 搬入完成
+        /// </summary>
+        public const string MoveInFinished = "MoveInFinished";
+        /// <summary>
+        /// 满卡
+        /// </summary>
+        public const string MoveInCstFulled = "MoveInCstFulled";
+    }
 }

+ 1 - 1
TFT_PC_FrameWork/TFT_Model/Entities/BizAgvTask.cs

@@ -63,7 +63,7 @@ namespace TFT_Model.Entities
         /// 创建时间 
         ///</summary>
         [SugarColumn(ColumnName = "create_time")]
-        public DateTime? CreateTime { get; set; }
+        public DateTime CreateTime { get; set; }
         /// <summary>
         /// 更新者 
         ///</summary>

+ 1 - 1
TFT_PC_FrameWork/TFT_Model/Entities/MesUnloadCst.cs

@@ -68,7 +68,7 @@ namespace TFT_Model.Entities
         /// 创建时间 
         ///</summary>
         [SugarColumn(ColumnName = "create_time")]
-        public DateTime? CreateTime { get; set; }
+        public DateTime CreateTime { get; set; }
         /// <summary>
         /// 更新者 
         ///</summary>

+ 2 - 0
TFT_PC_FrameWork/TFT_PC_FrameWork/ViewModels/MainWindowViewModel.cs

@@ -6,6 +6,7 @@ using Prism.Regions;
 using System;
 using System.Collections.Generic;
 using System.IO;
+using TFT_Model.Entities;
 using TFT_Model.Other;
 using TFT_PC_FrameWork.AutoMapper;
 using TFT_PC_FrameWork.Views;
@@ -27,6 +28,7 @@ namespace TFT_PC_FrameWork.ViewModels
             set { SetProperty(ref _title, value); }
         }
 
+
         public MainWindowViewModel(IRegionManager regionManager,ILogger logger, IMapper mapper)
         {
             this._regionManager = regionManager;

+ 8 - 1
TFT_PC_FrameWork/TFT_PC_FrameWork/ViewModels/MesUnLoadDataViewModel.cs

@@ -10,6 +10,8 @@ using System.Text;
 using System.Threading.Tasks;
 using TFT_Model.Other;
 using TFT_PC_FrameWork.Views;
+using TFT_Model.Entities;
+using TFT_DbHelper;
 
 namespace TFT_PC_FrameWork.ViewModels
 {
@@ -20,7 +22,12 @@ namespace TFT_PC_FrameWork.ViewModels
         private readonly IRegionManager _regionManager;
         private readonly IMapper _mapper;
         private readonly IMesUnLoadDataService _mesUnLoadDataService;
-        public MesUnLoadDataViewModel(IRegionManager regionManager, ILogger logger, IMapper mapper, IMesUnLoadDataService mesUnLoadDataService)
+
+
+        public MesUnLoadDataViewModel(IRegionManager regionManager,
+                                      ILogger logger,
+                                      IMapper mapper,
+                                      IMesUnLoadDataService mesUnLoadDataService)
         {
             _regionManager = regionManager;
             _logger = logger;

+ 13 - 0
TFT_PC_FrameWork/TFT_PC_FrameWork/ViewModels/SysRealTimeRunViewModel.cs

@@ -10,6 +10,9 @@ using System.Text;
 using System.Threading.Tasks;
 using TFT_Model.Other;
 using TFT_PC_FrameWork.Views;
+using TFT_Model.Entities;
+using TFT_DbHelper;
+using TFT_Common;
 
 namespace TFT_PC_FrameWork.ViewModels
 {
@@ -20,12 +23,22 @@ namespace TFT_PC_FrameWork.ViewModels
         private readonly IRegionManager _regionManager;
         private readonly IMapper _mapper;
         private readonly ISysRealTimeRunService _sysRealTimeRunService;
+
+
         public SysRealTimeRunViewModel(IRegionManager regionManager, ILogger logger, IMapper mapper, ISysRealTimeRunService sysRealTimeRunService)
         {
             _regionManager = regionManager;
             _logger = logger;
             _mapper = mapper;
             _sysRealTimeRunService = sysRealTimeRunService;
+
+            _sysRealTimeRunService.BasLocList = DbHelper.Db.Queryable<BasLoc>().ToList();
+            _sysRealTimeRunService.BasPlcItemList = DbHelper.Db.Queryable<BasPlcItem>().ToList();
+
+            MqttClientHelper.Init("127.0.0.1",8222);
+            MqttClientHelper.ConnectMqttServerAsync();
+            Task.Delay(2000).Wait();
+            MqttClientHelper.Subscribe("Hello:");
         }
 
     }

+ 1 - 1
TFT_PC_FrameWork/TFT_PC_FrameWork/Views/AgvTask.xaml.cs

@@ -16,7 +16,7 @@ using System.Windows.Shapes;
 namespace TFT_PC_FrameWork.Views
 {
     /// <summary>
-    /// AgvTask.xaml 的交互逻辑
+    /// AgvTaskRun.xaml 的交互逻辑
     /// </summary>
     public partial class AgvTask : UserControl
     {