From 70cdea5f6e57d1ebc29c25ef60f311fadca6ab21 Mon Sep 17 00:00:00 2001
From: wxw <Administrator@DESKTOP-5BIMHQ3>
Date: 星期四, 26 九月 2024 19:10:18 +0800
Subject: [PATCH] Merge branch 'wxw'

---
 Admin.NET/WCS.Application/Hub/PlcHub.cs |  190 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 188 insertions(+), 2 deletions(-)

diff --git a/Admin.NET/WCS.Application/Hub/PlcHub.cs b/Admin.NET/WCS.Application/Hub/PlcHub.cs
index b6dbb86..bec19d3 100644
--- a/Admin.NET/WCS.Application/Hub/PlcHub.cs
+++ b/Admin.NET/WCS.Application/Hub/PlcHub.cs
@@ -1,5 +1,6 @@
 锘縰sing Furion.InstantMessaging;
 using Microsoft.AspNetCore.SignalR;
+using WCS.Application.Entity;
 
 namespace WCS.Application;
 
@@ -10,10 +11,13 @@
 public class PlcHub : Hub<IPlcHub>
 {
     private readonly IHubContext<PlcHub, IPlcHub> _plcHubContext;
-
-    public PlcHub(IHubContext<PlcHub, IPlcHub> plcHubContext)
+    private readonly SysConfigService _sysConfigService;
+    private readonly SqlSugarRepository<WcsCheckTask> _wcsCheckTaskRep;
+    public PlcHub(IHubContext<PlcHub, IPlcHub> plcHubContext, SysConfigService sysConfigService, SqlSugarRepository<WcsCheckTask> wcsCheckTaskRep)
     {
         _plcHubContext = plcHubContext;
+        _sysConfigService = sysConfigService;
+        _wcsCheckTaskRep = wcsCheckTaskRep;
     }
     /// <summary>
     /// 涓嬪彂PLC杩炴帴鐘舵��
@@ -34,4 +38,186 @@
     //{
     //    await _plcHubContext.Clients.All.PublicStationStatus(context);
     //}
+
+    /// <summary>
+    /// 涓嬪彂鏈嶅姟鐘舵��
+    /// </summary>
+    public async Task UpdateService(PLCServiceModel context)
+    {
+        //杩愯鐘舵��
+        if (context.BoRunningState.HasValue)
+        {
+            PLCTaskAction.boRunningState = context.BoRunningState.Value;
+            if (context.BoRunningState.Value)
+                PLCTaskAction.Init();
+            else
+                PLCTaskAction.Stop();
+            await _sysConfigService.UpdateConfigValue("sys_RunningState", context.BoRunningState.Value);
+        }
+        //鑴辨満妯″紡
+        if (context.BoOffline.HasValue)
+        {
+            PLCTaskAction.boOffline = context.BoOffline.Value;
+            await _sysConfigService.UpdateConfigValue("sys_Offline", context.BoOffline.Value);
+        }
+        //鑷埛鏂�
+        if (context.BoRefresh.HasValue)
+        {
+            PLCTaskAction.boRefresh = context.BoRefresh.Value;
+            await _sysConfigService.UpdateConfigValue("sys_Refresh", context.BoRefresh.Value);
+        }
+        //婕旂ず妯″紡
+        if (context.BoDemo.HasValue)
+        {
+            PLCTaskAction.boDemo = context.BoDemo.Value;
+
+            if (PLCTaskAction.boDemo)
+            {
+                //鍒ゆ柇鍒嗘嫞鐮佸灈鏈夋病鏈夌粦瀹氫换鍔�
+                var checkTask = await _wcsCheckTaskRep.Context.Queryable<WcsCheckTask>().Where(w => w.Status == "1" && w.RoboatType == PLCTypeEnum.RobotPalletizer).ToListAsync();
+                if (checkTask.Count > 0)
+                {
+                    PLCTaskAction.boDemo = false;
+                    await _plcHubContext.Clients.All.UpdateService(
+                        new PLCServiceModel()
+                        {
+                            BoRunningState = PLCTaskAction.boRunningState,
+                            BoRefresh = PLCTaskAction.boRefresh,
+                            BoOffline = PLCTaskAction.boOffline,
+                            BoDemo = PLCTaskAction.boDemo,
+                            BoDrumReversal = PLCTaskAction.boDrumReversal,
+                            Error = "鍒嗘嫞鐮佸灈宸茬粦瀹氫换鍔★紝涓嶆敮鎸佸紑鍚紨绀烘ā寮忥紒"
+                        });
+                    return;
+                }
+            }
+            //PLC杩炴帴
+            var plcStackeConn = PLCTaskAction.listPlcConn.First(m => m.PlcIP == "10.18.51.120");
+            if (plcStackeConn.Connected)
+            {
+                //鍐欏叆娴佺▼瀛�
+                var ret = plcStackeConn.SetPlcDBValue(PLCDataTypeEnum.Bit, "DB119", "0", PLCTaskAction.boDemo == true ? "1" : "0");
+                if (ret.IsSucceed)
+                {
+                    if (PLCTaskAction.boDemo)
+                    {
+                        // 鍐欏叆plc鎶撶鍝佺
+                        var rett = plcStackeConn.SetPlcDBValue(PLCDataTypeEnum.Short, "DB111", "0", "1");
+                        if (rett.IsSucceed)
+                        {
+                            await _sysConfigService.UpdateConfigValue("sys_demo", context.BoDemo.Value);
+                        }
+                    }
+                    else 
+                    {
+                        var rett = plcStackeConn.SetPlcDBValue(PLCDataTypeEnum.Short, "DB111", "0", "0");
+                        if (rett.IsSucceed)
+                        {
+                            await _sysConfigService.UpdateConfigValue("sys_demo", context.BoDemo.Value);
+                        }
+                    }                 
+                }
+                else
+                {
+                    PLCTaskAction.boDemo = !PLCTaskAction.boDemo;
+
+                    await _plcHubContext.Clients.All.UpdateService(
+                       new PLCServiceModel()
+                       {
+                           BoRunningState = PLCTaskAction.boRunningState,
+                           BoRefresh = PLCTaskAction.boRefresh,
+                           BoOffline = PLCTaskAction.boOffline,
+                           BoDemo = PLCTaskAction.boDemo,
+                           BoDrumReversal = PLCTaskAction.boDrumReversal,
+                           Error = "涓嶱CL浜や簰澶辫触锛屾搷浣滃け璐ワ紒"
+                       });
+                    return;
+                }
+            }
+            else
+            {
+                PLCTaskAction.boDemo = !PLCTaskAction.boDemo;
+
+                await _plcHubContext.Clients.All.UpdateService(
+                   new PLCServiceModel()
+                   {
+                       BoRunningState = PLCTaskAction.boRunningState,
+                       BoRefresh = PLCTaskAction.boRefresh,
+                       BoOffline = PLCTaskAction.boOffline,
+                       BoDemo = PLCTaskAction.boDemo,
+                       BoDrumReversal = PLCTaskAction.boDrumReversal,
+                       Error = "PCL鏈繛鎺ワ紝鎿嶄綔澶辫触锛�"
+                   });
+                return;
+            }
+        }
+        //婊氱瓛鍙嶈浆
+        if (context.BoDrumReversal.HasValue)
+        {
+            PLCTaskAction.boDrumReversal = context.BoDrumReversal.Value;
+
+            //PLC杩炴帴
+            var plcStackeConn = PLCTaskAction.listPlcConn.First(m => m.PlcIP == "10.18.51.120");
+            if (plcStackeConn.Connected)
+            {
+                //鍐欏叆娴佺▼瀛�
+                var ret = plcStackeConn.SetPlcDBValue(PLCDataTypeEnum.Bit, "DB119", "0.1", PLCTaskAction.boDemo == true ? "1" : "0");
+                if (ret.IsSucceed)
+                {
+                    await _sysConfigService.UpdateConfigValue("sys_DrumReversal", context.BoDrumReversal.Value);
+                }
+                else
+                {
+                    PLCTaskAction.boDrumReversal = !PLCTaskAction.boDrumReversal;
+
+                    await _plcHubContext.Clients.All.UpdateService(
+                       new PLCServiceModel()
+                       {
+                           BoRunningState = PLCTaskAction.boRunningState,
+                           BoRefresh = PLCTaskAction.boRefresh,
+                           BoOffline = PLCTaskAction.boOffline,
+                           BoDemo = PLCTaskAction.boDemo,
+                           BoDrumReversal = PLCTaskAction.boDrumReversal,
+                           Error = "涓嶱CL浜や簰澶辫触锛屾搷浣滃け璐ワ紒"
+                       });
+                    return;
+                }
+            }
+            else
+            {
+                PLCTaskAction.boDrumReversal = !PLCTaskAction.boDrumReversal;
+
+                await _plcHubContext.Clients.All.UpdateService(
+                   new PLCServiceModel()
+                   {
+                       BoRunningState = PLCTaskAction.boRunningState,
+                       BoRefresh = PLCTaskAction.boRefresh,
+                       BoOffline = PLCTaskAction.boOffline,
+                       BoDemo = PLCTaskAction.boDemo,
+                       BoDrumReversal = PLCTaskAction.boDrumReversal,
+                       Error = "PCL鏈繛鎺ワ紝鎿嶄綔澶辫触锛�"
+                   });
+                return;
+            }
+        }
+        await _plcHubContext.Clients.All.UpdateService(new PLCServiceModel() { BoRunningState = PLCTaskAction.boRunningState, BoRefresh = PLCTaskAction.boRefresh, BoOffline = PLCTaskAction.boOffline, BoDemo = PLCTaskAction.boDemo, BoDrumReversal = PLCTaskAction.boDrumReversal });
+    }
 }
+public class PLCServiceModel
+{
+    public bool? BoRunningState { get; set; }
+
+
+    public bool? BoOffline { get; set; }
+
+
+    public bool? BoRefresh { get; set; }
+
+    public bool? BoDemo { get; set; }
+
+    public bool? BoDrumReversal { get; set; }
+    /// <summary>
+    /// 閿欒淇℃伅
+    /// </summary>
+    public string Error { get; set; }
+}
\ No newline at end of file

--
Gitblit v1.8.0