From 66da0f6808fa8496a3216c94973a552389ab96ba Mon Sep 17 00:00:00 2001 From: hwh <332078369@qq.com> Date: 星期一, 02 九月 2024 13:27:25 +0800 Subject: [PATCH] 解决冲突 --- Admin.NET/WCS.Application/PLC/PLCTaskAction.cs | 89 +++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 82 insertions(+), 7 deletions(-) diff --git a/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs b/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs index a6d6fb2..ffa4e97 100644 --- a/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs +++ b/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs @@ -1,15 +1,31 @@ 锘縰sing Admin.NET.Core.Service; +using Furion.Logging; +using Microsoft.AspNetCore.SignalR; namespace WCS.Application; public static class PLCTaskAction { + //鏈嶅姟杩愯鐘舵�� + public static bool boRunningState = false; + //鑴辨満妯″紡 + public static bool boOffline = false; + //鑷埛鏂� + public static bool boRefresh = false; + private static readonly ISqlSugarClient _db = SqlSugarSetup.ITenant.GetConnectionScope(SqlSugarConst.MainConfigId); + private static readonly SysCacheService sysCacheService = App.GetRequiredService<SysCacheService>(); + private static readonly IHubContext<PlcHub, IPlcHub> _plcHubContext; private static List<WcsPlc> listPlc; private static List<WcsDevice> listPlcDevice; - private static List<WcsStation> listPlcStation; + private static List<WcsPosition> listPlcStation; private static List<PLCUtil> listPlcUtil = new List<PLCUtil>(); + //瀵瑰鍏竷杩炴帴鐘舵�� + public static List<PLCUtil> listPlcConn + { + get { return listPlcUtil; } + } private static CancellationTokenSource cts;//鍙栨秷绾跨▼鏍囪瘑 public static event EventHandler DeviceValueChangeEvent; @@ -20,7 +36,7 @@ listPlc = _db.Queryable<WcsPlc>().ToList(); listPlcDevice = _db.Queryable<WcsDevice>().ToList(); - listPlcStation = _db.Queryable<WcsStation>().ToList(); + listPlcStation = _db.Queryable<WcsPosition>().ToList(); } /// <summary> /// 鍒濆鍖朠LC杩炴帴 @@ -30,9 +46,9 @@ if (listPlcUtil.Count != 0) { cts.Cancel(); - listPlc = _db.Queryable<WcsPlc>().ToList(); + listPlc = _db.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.StackingMachine || s.Type == PLCTypeEnum.ConveyorLine || s.Type == PLCTypeEnum.BoxConveyorLine).ToList(); listPlcDevice = _db.Queryable<WcsDevice>().ToList(); - listPlcStation = _db.Queryable<WcsStation>().ToList(); + listPlcStation = _db.Queryable<WcsPosition>().ToList(); //绛夊緟鍑犵閽燂紝鎶婂凡鏈夌嚎绋嬪彇娑堟帀鍐嶈繛鎺� Thread.Sleep(5000); foreach (var modPlcUtil in listPlcUtil) @@ -47,6 +63,7 @@ listPlcUtil.Add(plc); } cts = new CancellationTokenSource(); + boRunningState = true; StartRead(); } /// <summary> @@ -72,7 +89,7 @@ } try { - var modPlcUtil = listPlcUtil.FirstOrDefault(s => s.PlcId == modPlc.Id); + var modPlcUtil = listPlcUtil.FirstOrDefault(s => s != null && s.PlcId == modPlc.Id); if (modPlcUtil == null) { modPlcUtil = new PLCUtil(modPlc); @@ -82,7 +99,7 @@ //寰幆璇昏澶� foreach (var modDevice in listDevice.Where(s => s.Level == DeviceLevelEnum.DB)) { - var (result, value) = modPlcUtil.GetPlcDBValue(modDevice.PosType.Value, modDevice.DbNumber, modDevice.PlcPos); + var (result, value) = modPlcUtil.GetPlcDBValue(modDevice.PosType, modDevice.DbNumber, modDevice.PlcPos); if (result.IsSucceed) { //鏃犳祦绋嬭烦鍑� @@ -108,13 +125,70 @@ Thread.Sleep(3000); } + catch (OperationCanceledException) + { + Console.WriteLine("涓绾跨▼"); + } catch (Exception ex) { - + Log.Error(ex.Message, ex); } } }, cts.Token); } + } + + /// <summary> + /// 杩炴帴鐘舵�佺嚎绋� + /// </summary> + public static void ConnectionStatus() + { + Task.Run(() => + { + try + { + //鍙栨秷绾跨▼ 鍏抽棴PLC杩炴帴 + if (cts.Token.IsCancellationRequested) + { + foreach (var modPlcUtil in listPlcUtil) + { + modPlcUtil.Close(); + } + throw new OperationCanceledException(); + } + //鑾峰彇姣忎釜PLC杩炴帴鐘舵�� + foreach (var modPlc in listPlc) + { + var modPlcUtil = listPlcUtil.FirstOrDefault(s => s.PlcId == modPlc.Id); + if (modPlcUtil == null) + modPlc.IsConn = false; + else + modPlc.IsConn = modPlcUtil.Connected; + if (sysCacheService.ExistKey("PLCCONN" + modPlc.Id)) + { + var cachePlc = sysCacheService.Get<WcsPlc>("PLCCONN" + modPlc.Id); + if (cachePlc.IsConn != modPlc.IsConn) + { + //杩炴帴鐘舵�佸彉鏇� 閫氱煡鍓嶇 + _plcHubContext.Clients.All.PublicPlcConn(modPlc); + } + } + sysCacheService.Set("PLCCONN" + modPlc.Id, modPlc); + } + Thread.Sleep(3000); + } + catch (OperationCanceledException) + { + sysCacheService.RemoveByPrefixKey("PLCCONN"); + Console.WriteLine("涓绾跨▼"); + } + catch (Exception ex) + { + Log.Error(ex.Message, ex); + } + + }); + } /// <summary> /// 鍋滄鏈嶅姟 @@ -122,5 +196,6 @@ public static void Stop() { cts.Cancel(); + boRunningState = false; } } \ No newline at end of file -- Gitblit v1.8.0