From 5af0a043ffbbe444f7e43b6bf52a9e94c74283f2 Mon Sep 17 00:00:00 2001 From: bklLiudl <673013083@qq.com> Date: 星期三, 11 九月 2024 08:43:43 +0800 Subject: [PATCH] 跺机交互流程 --- Admin.NET/WCS.Application/PLC/PLCTaskAction.cs | 278 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 229 insertions(+), 49 deletions(-) diff --git a/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs b/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs index 9576ac4..c301944 100644 --- a/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs +++ b/Admin.NET/WCS.Application/PLC/PLCTaskAction.cs @@ -2,6 +2,7 @@ using DocumentFormat.OpenXml.Drawing; using Furion.Logging; using Microsoft.AspNetCore.SignalR; +using System; namespace WCS.Application; public static class PLCTaskAction @@ -13,9 +14,12 @@ //鑷埛鏂� public static bool boRefresh = false; + public static List<PlcPositionInfo> listPositionInfo = new List<PlcPositionInfo>(); + 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 = App.GetService<IHubContext<PlcHub, IPlcHub>>(); + private static readonly SysConfigService _sysConfigService = App.GetService<SysConfigService>(); private static List<WcsPlc> listPlc = new List<WcsPlc>(); private static List<WcsDevice> listPlcDevice = new List<WcsDevice>(); @@ -29,48 +33,64 @@ { get { return listPlcUtil; } } + // 浜や簰鐐归泦鍚� + public static List<WcsDevice> plcDevices + { + get { return listPlcDevice; } + } + public static event EventHandler DeviceValueChangeEvent; static PLCTaskAction() { //璁㈤槄浜嬩欢 DeviceValueChangeEvent += PLCService.OnChangeEvent; + boRunningState = _sysConfigService.GetConfigValue<bool>("sys_RunningState").Result; + boOffline = _sysConfigService.GetConfigValue<bool>("sys_Offline").Result; + boRefresh = _sysConfigService.GetConfigValue<bool>("sys_Refresh").Result; } /// <summary> /// 鍒濆鍖朠LC杩炴帴 /// </summary> public static void Init() { - if (listPlcUtil.Count != 0) + cts.Cancel(); + 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<WcsPosition>().ToList(); + listAlarmInfo = _db.Queryable<WcsAlarmInfo>().ToList(); + //绛夊緟鍑犵閽燂紝鎶婂凡鏈夌嚎绋嬪彇娑堟帀鍐嶈繛鎺� + //Thread.Sleep(5000); + foreach (var modPlcUtil in listPlcUtil) { - cts.Cancel(); - 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<WcsPosition>().ToList(); - listAlarmInfo = _db.Queryable<WcsAlarmInfo>().ToList(); - //绛夊緟鍑犵閽燂紝鎶婂凡鏈夌嚎绋嬪彇娑堟帀鍐嶈繛鎺� - Thread.Sleep(5000); - foreach (var modPlcUtil in listPlcUtil) - { - modPlcUtil.Close(); - } - listPlcUtil.Clear(); + modPlcUtil.Close(); } + listPlcUtil.Clear(); foreach (var modPlc in listPlc) { var plc = new PLCUtil(modPlc); listPlcUtil.Add(plc); } - cts = new CancellationTokenSource(); - boRunningState = true; - StartRead(); - ConnectionStatus(); - StartWatchAlarm(); + _plcHubContext.Clients.All.UpdateService(new PLCServiceModel() + { + BoRunningState = boRunningState, + BoOffline = boOffline, + BoRefresh = boRefresh + }); + if (boRunningState) + { + cts = new CancellationTokenSource(); + StartRead(); + ConnectionStatus(); + StartWatchAlarm(); + StartWatchPosition(); + } } /// <summary> /// 寮�鍚鍙杙lc绾跨▼ /// </summary> public static void StartRead() { + Console.WriteLine("寮�鍚鍙杙lc绾跨▼"); foreach (var modPlc in listPlc) { Task.Run(() => @@ -94,16 +114,33 @@ var modPlcUtil = listPlcUtil.FirstOrDefault(s => s != null && s.PlcId == modPlc.Id); if (modPlcUtil == null) { + Console.WriteLine($"杩炴帴plc{modPlc.IP}"); modPlcUtil = new PLCUtil(modPlc); listPlcUtil.Add(modPlcUtil); + if (modPlcUtil.Connected) + Console.WriteLine($"杩炴帴plc{modPlc.IP}鎴愬姛"); } - var listDevice = listPlcDevice.Where(s => s.PlcId == _modplc.Id && s.DeviceType == DeviceTypeEnum.Business).ToList(); + if (!modPlcUtil.Connected) + { + Thread.Sleep(10000); + modPlcUtil.Open(); + continue; + } + var listDevice = listPlcDevice + .Where(s => s.PlcId == _modplc.Id && s.DeviceType == DeviceTypeEnum.Business) + .WhereIF(modPlc.Type == PLCTypeEnum.ConveyorLine, s => s.Level == DeviceLevelEnum.Station) + .WhereIF(modPlc.Type == PLCTypeEnum.StackingMachine, s => s.Level == DeviceLevelEnum.DB) + .ToList(); + //寰幆璇昏澶� - foreach (var modDevice in listDevice.Where(s => s.Level == DeviceLevelEnum.DB)) + foreach (var modDevice in listDevice) { var (result, value) = modPlcUtil.GetPlcDBValue(modDevice.PosType, modDevice.DbNumber, modDevice.PlcPos); + //if (!result.IsSucceed) + // Console.WriteLine($"{modPlc.IP}閿欒{modDevice.DbNumber}.{modDevice.PlcPos}锛�"+result.Err); if (result.IsSucceed) { + //Console.WriteLine($"{modPlc.IP}璇诲彇{modDevice.DbNumber}.{modDevice.PlcPos}鐨勫�间负锛歿value}"); //鏃犳祦绋嬭烦鍑� if (value == 0) continue; @@ -114,18 +151,19 @@ dto.listStation = listPlcStation.Where(s => s.DeviceId == modDevice.Id).ToList(); dto.listDevice = listDevice.Where(s => s.StationNum == modDevice.StationNum && s.Level == DeviceLevelEnum.Station).ToList(); //杩欓噷瑙﹀彂鍊煎彉鏇翠簨浠� - //DeviceValueChangeEvent?.Invoke(dto, EventArgs.Empty); + DeviceValueChangeEvent?.Invoke(dto, EventArgs.Empty); } - else + else if (!modPlcUtil.Connected) { - //鍒犻櫎褰撳墠杩炴帴 涓嬩竴娆″惊鐜噸鏂拌繛鎺� - Console.WriteLine("杩炴帴鏂紑..."); - modPlcUtil.Close(); - listPlcUtil.Remove(modPlcUtil); + //Console.WriteLine($"杩炴帴鏂紑...{modPlc.IP}"); + //閲嶆柊鎵撳紑杩炴帴 + modPlcUtil.Open(); + //if (modPlcUtil.Connected) + // Console.WriteLine($"閲嶈繛鎴愬姛...{modPlc.IP}"); } } - Thread.Sleep(3000); + Thread.Sleep(10000); } catch (OperationCanceledException) { @@ -133,7 +171,7 @@ } catch (Exception ex) { - Log.Error(ex.Message, ex); + Log.Error("璇诲彇PLC绾跨▼鍙戠敓寮傚父", ex); } } }, cts.Token); @@ -149,9 +187,9 @@ { try { + Console.WriteLine("杩炴帴鐘舵�佺嚎绋�"); while (true) { - //鍙栨秷绾跨▼ 鍏抽棴PLC杩炴帴 if (cts.Token.IsCancellationRequested) { @@ -171,16 +209,22 @@ modPlc.IsConn = false; else modPlc.IsConn = modPlcUtil.Connected; - if (sysCacheService.ExistKey("PLCCONN" + modPlc.Id)) + if (sysCacheService.ExistKey("PLCCONN:" + modPlc.Id)) { - var cachePlc = sysCacheService.Get<WcsPlc>("PLCCONN" + modPlc.Id); + var cachePlc = sysCacheService.Get<WcsPlc>("PLCCONN:" + modPlc.Id); if (cachePlc.IsConn != modPlc.IsConn) { //杩炴帴鐘舵�佸彉鏇� 閫氱煡鍓嶇 + Console.WriteLine($"閫氱煡鍓嶇鍙樻洿{modPlc.Text} {modPlc.IsConn}"); _plcHubContext.Clients.All.PublicPlcConn(modPlc); } + sysCacheService.Set("PLCCONN:" + modPlc.Id, modPlc); } - sysCacheService.Set("PLCCONN" + modPlc.Id, modPlc); + else + { + sysCacheService.Set("PLCCONN:" + modPlc.Id, modPlc); + _plcHubContext.Clients.All.PublicPlcConn(modPlc); + } } Thread.Sleep(1000); } @@ -192,10 +236,10 @@ } catch (Exception ex) { - Log.Error(ex.Message, ex); + Log.Error("杩炴帴鐘舵�佺嚎绋嬪彂鐢熷紓甯�", ex); } - }); + }, cts.Token); } @@ -228,41 +272,177 @@ } foreach (var modUtil in listPlcUtil) { - //鎶ヨ鐐逛綅 - var listAlarm = listAlarmInfo.Where(s => s.PlcIP == modUtil.PlcId.ToString()); - foreach (var modAlarm in listAlarm) + try { - (var result, var value) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, modAlarm.AlarmCode, ""); - if (result.IsSucceed) + //鎶ヨ鐐逛綅 + var listAlarm = listAlarmInfo.Where(s => s.PlcIP == modUtil.PlcIP.ToString()).ToList(); + Console.WriteLine("鎶ヨ鐐逛綅鏁帮細" + listAlarm.Count); + var listaddress = new Dictionary<string, PLCDataTypeEnum>(); + foreach (var modAlarm in listAlarm) { - //鍋囪涓嶄负0灏辩畻鎶ヨ - if (value != 0) + listaddress.Add(modAlarm.AlarmCode, PLCDataTypeEnum.Bit); + } + Console.WriteLine($"璇诲彇鐐逛綅{listaddress.Count}涓�"); + var result = modUtil.GetPlcBatchDBValue(listaddress); + Console.WriteLine("err" + result.Err); + var listPulish = new List<WcsAlarmInfo>(); + //娴嬭瘯鎶ヨ + //foreach (var modAlarm in listAlarm) + //{ + // result.Value.Add(modAlarm.AlarmCode, true); + //} + foreach (var item in result.Value) + { + var value = Convert.ToBoolean(item.Value); + Console.WriteLine(item.Key + "+" + value); + var modAlarm = listAlarm.FirstOrDefault(s => s.AlarmCode == item.Key); + if (value && modAlarm.Status == YesNoEnum.N) { //淇敼鎶ヨ鐘舵�佸悓鏃惰褰曟姤璀︽棩蹇� modAlarm.AlarmTime = DateTime.Now; modAlarm.Status = YesNoEnum.Y; - _db.Updateable(modAlarm).ExecuteCommand(); - WcsAlarmLog modLog = modAlarm.Adapt<WcsAlarmLog>(); - _db.Insertable(modLog).ExecuteCommand(); - //涓嬪彂鎶ヨ鐘舵�� - _plcHubContext.Clients.All.PublicAlarm(modAlarm.Adapt<WcsAlarmInfoOutput>()); + listPulish.Add(modAlarm); } //鍙栨秷鎶ヨ - else if (modAlarm.Status == YesNoEnum.Y) + else if (value && modAlarm.Status == YesNoEnum.Y) { modAlarm.AlarmTime = null; modAlarm.Status = YesNoEnum.N; - _db.Updateable(modAlarm).ExecuteCommand(); + listPulish.Add(modAlarm); } } + if (listPulish.Count > 0) + { + _db.Updateable(listPulish).ExecuteCommand(); + var listLog = new List<WcsAlarmLog>(); + foreach (var mod in listPulish.Where(s => s.Status == YesNoEnum.Y)) + { + WcsAlarmLog modLog = mod.Adapt<WcsAlarmLog>(); + modLog.Id = 0; + listLog.Add(modLog); + } + _db.Insertable(listLog).ExecuteCommand(); + _plcHubContext.Clients.All.PublicAlarm(listPulish); + } + } + catch (Exception ex) + { + Log.Error("鎶ヨ鐩戞帶鍙戠敓寮傚父", ex); } } - Thread.Sleep(1000); + Thread.Sleep(10000); } }, cts.Token); } /// <summary> + /// 寮�鍚綅缃洃鎺� + /// </summary> + public static void StartWatchPosition() + { + Console.WriteLine("寮�鍚綅缃洃鎺�"); + Task.Run(() => + { + while (true) + { + //鍙栨秷绾跨▼ 鍏抽棴PLC杩炴帴 + if (cts.Token.IsCancellationRequested) + { + foreach (var modPlcUtil in listPlcUtil) + { + if (modPlcUtil != null && modPlcUtil.Connected) + modPlcUtil.Close(); + } + break; + //throw new OperationCanceledException(); + } + try + { + foreach (var modPlcUtil in listPlcUtil) + { + if (modPlcUtil == null) + continue; + var listDevice = listPlcDevice.Where(s => s.PlcId == modPlcUtil.PlcId && s.DeviceType == DeviceTypeEnum.Show).ToList(); + var modPlc = listPlc.FirstOrDefault(s => s.Id == modPlcUtil.PlcId); var listaddress = new Dictionary<string, PLCDataTypeEnum>(); + foreach (var modDevice in listDevice) + { + listaddress.Add(modDevice.DbNumber + "." + modDevice.PlcPos, modDevice.PosType); + } + //鎵归噺璇� + var result = modPlcUtil.GetPlcBatchDBValue(listaddress); + //娴嬭瘯鐢� + //foreach (var modDevice in listDevice) + //{ + // if (modPlc.Type == PLCTypeEnum.StackingMachine) + // result.Value.Add(modDevice.DbNumber + "." + modDevice.PlcPos, new Random().Next(790000)); + // else if (modPlc.Type == PLCTypeEnum.ConveyorLine) + // result.Value.Add(modDevice.DbNumber + "." + modDevice.PlcPos, new Random().Next(2) == 0); + //} + foreach (var item in result.Value) + { + var modDevice = listDevice.FirstOrDefault(s => item.Key == (s.DbNumber + "." + s.PlcPos)); + if (modDevice == null) + continue; + switch (modPlc.Type) + { + case PLCTypeEnum.StackingMachine: + { + var value = Convert.ToInt32(item.Value); + var height = Math.Round(value / 790000d * 200).ToInt(); + if (modDevice.BoxHeight != height) + { + modDevice.BoxHeight = Math.Round(value / 790000d * 200).ToInt(); + var modInfo = new PlcPositionInfo() + { + Type = modPlc.Type, + StationNum = modDevice.StationNum, + BoxHeight = modDevice.BoxHeight + }; + _plcHubContext.Clients.All.PublicPosition(modInfo); + UpdatePosition(modInfo); + } + } + break; + case PLCTypeEnum.ConveyorLine: + { + var value = Convert.ToBoolean(item.Value); + if (value != modDevice.BoHaveItem) + { + modDevice.BoHaveItem = value; + //涓嬪彂鐘舵�� + var modInfo = new PlcPositionInfo() { Type = modPlc.Type, StationNum = modDevice.StationNum, BoHaveItem = value }; + _plcHubContext.Clients.All.PublicPosition(modInfo); + UpdatePosition(modInfo); + } + } + break; + default: + break; + } + } + } + Thread.Sleep(1000); + } + catch (Exception ex) + { + Log.Error("浣嶇疆鐩戞帶鍙戠敓寮傚父", ex); + } + } + }); + } + private static void UpdatePosition(PlcPositionInfo modInfo) + { + var modTemp = listPositionInfo.FirstOrDefault(s => s.StationNum == modInfo.StationNum && modInfo.Type == s.Type); + if (modTemp == null) + { + listPositionInfo.Add(modInfo); + } + else + { + modTemp.BoHaveItem = modInfo.BoHaveItem; + } + } + /// <summary> /// 鍋滄鏈嶅姟 /// </summary> public static void Stop() -- Gitblit v1.8.0