From aeb58bd0b1acfb74fba9bfa83279e88acba2a859 Mon Sep 17 00:00:00 2001 From: hehaibing-1996 Date: Tue, 15 Apr 2025 13:38:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E7=81=AF=E4=BA=A4=E4=BA=92=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E4=B8=8E=E6=B6=B2=E6=99=B6=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WCS.BLL/HardWare/SingleLightShelf.cs | 393 +++++++++++++++++- WCS.BLL/Services/Service/MXL4Service.cs | 4 +- .../Controllers/SingleLightController.cs | 45 +- 3 files changed, 425 insertions(+), 17 deletions(-) diff --git a/WCS.BLL/HardWare/SingleLightShelf.cs b/WCS.BLL/HardWare/SingleLightShelf.cs index b9d0abf..ff14c1b 100644 --- a/WCS.BLL/HardWare/SingleLightShelf.cs +++ b/WCS.BLL/HardWare/SingleLightShelf.cs @@ -1,21 +1,29 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; +using TouchSocket.Sockets; +using WCS.BLL.Config; using WCS.BLL.DbModels; using WCS.BLL.DbModels.Task; using WCS.BLL.Manager; +using WCS.BLL.Tool; using WCS.DAL.Db; using WCS.DAL.DbModels; using WCS.Model; +using WCS.Model.ApiModel.MKYBackgroundThread; +using WCS.Model.ApiModel.SingleLight; using static WCS.BLL.HardWare.WarningLight; +using static WCS.BLL.Tool.Helper; namespace WCS.BLL.HardWare { public class SingleLightShelf : IShelfBase { - public SingleLightShelf(ShelfInfo shelfInfo) + public SingleLightShelf(ShelfInfo shelfInfo) { ShelfId = shelfInfo.Id; ShelfCode = shelfInfo.ShelfCode; @@ -24,9 +32,249 @@ namespace WCS.BLL.HardWare SetCurrentMode(TaskModeEnum.待机模式); ClientIp = shelfInfo.ClientIp; LightId = shelfInfo.LightId; + IsService = shelfInfo.IsService; + ClientIp = shelfInfo.ClientIp; + + if (ClientIp.Contains("127")) + { + ; + } ShelfTypeName = shelfInfo.ShelfTypeName; + + //初始化Module + Logs.Write($"[初始化单灯货架]初始化模组{ShelfCode}"); + var task = Task.Run(() => + { + var modules = DbHelp.db.Queryable().Where(t => t.ShelfId == ShelfId).ToList(); + foreach (var module in modules) + { + MXL4Modules.Add(new MXL4ShelfModule() + { + ModuleId = module.Id, + ModuleCode = module.ModuleCode, + BoardId = module.BoardId, + IsEnable = module.IsEnable, + CurrentMode = TaskModeEnum.待机模式, + + IsNeedRefresh = false, + }); + } + ModulesStr = string.Join(";", MXL4Modules.Select(t => t.ModuleCode)); + + ModuleIds = MXL4Modules.Select(t => t.BoardId).ToList(); + }); + + Logs.Write($"[初始化单灯货架]初始化单灯线程{ShelfCode}"); + //初始化单灯线程 + //一个货架存在一个线程去刷新数据 + Task.Run(async () => + { + //等待模组加载完毕再开始发送数据进行刷新 + Task.WaitAll(task); + while (true) + { + try + { + await Task.Delay(200); + //未完成首次连接时 暂不发送 + if (IsService) + { + if (TcpCleint == null) + { + await Task.Delay(2000); + continue; + } + if (TcpCleint.IsFirstConnected == false) + { + await Task.Delay(2000); + continue; + } + } + else + { + if (ClientIp.Contains("127")) + { + ; + } + var tcpClient = TCPClientManager.Service.GetClients().Where(t => t.IP + ":" + t.Port.ToString() == ClientIp).FirstOrDefault(); + if (tcpClient == null) + { + await Task.Delay(2000); + continue; + } + } + + //查询属于本货架的任务 + var currentShelfTasks = DbHelp.db.Queryable() + .Where(t => t.ShelfId == this.ShelfId) + .Where(t => t.IsSended == false || t.IsCancel == true) + .OrderBy(t => t.CreateTime) + .ToList(); + + //获取因为任务需要刷新的module + var haveTaskModules = currentShelfTasks.Where(t => t.IsCancel == true || t.IsSended == false) + .Select(t => t.ModuleId) + .Distinct() + .ToList(); + MXL4Modules.ForEach(t => + { + if (haveTaskModules.Contains(t.ModuleId)) + { + t.IsNeedRefresh = true; + } + }); + + //查询是否有需要刷新的 + var needRefreshModules = MXL4Modules.Where(t => t.IsNeedRefresh) + .ToList(); + foreach (var module in needRefreshModules) + { + Logs.Write($"[初始化单灯货架]存在需要刷新的{ShelfCode}"); + try + { + //查询当前是否有任务 + var currentTasks = currentShelfTasks.Where(t => t.ModuleId == module.ModuleId) + .OrderBy(t => t.CreateTime) + .ToList(); + #region 取消任务的逻辑 存在取消的任务 先删除任务 + var cancelTasks = currentTasks.Where(t => t.IsCancel == true) + .ToList(); + if (cancelTasks != null && cancelTasks.Count > 0) + { + Logs.Write($"[单灯后台刷新线程]当前有任务需要取消!"); + //获取该库位还有无任务 + var currentModuleTask = DbHelp.db.Queryable() + .Where(t => t.ShelfId == this.ShelfId) + .Where(t => t.ModuleId == module.ModuleId) + .Where(t => t.IsSended == true && t.IsCancel == false) + .ToList(); + //当前库位无任务 关闭库位灯 + if (!currentModuleTask.Any()) + { + Logs.Write($"[单灯后台刷新线程]当前库位{module.ModuleCode},无任务,关闭库位灯!"); + + var storeCodes = new List(); + storeCodes.Add(module.ModuleCode); + var shelfModel = new SingleLightShelfModel(); + //先不操作报警灯 + shelfModel.WarningLightMode = -1; + shelfModel.WarningLightColor = 0; + shelfModel.WarningBoardId = 0; + + shelfModel.ClientIp = ClientIp; + shelfModel.StoreList = new List(); + shelfModel.StoreList.Add(new SingleLightStoreModel() + { + BoardId = module.BoardId, + LightColor = 0, + LightMode = 0, + }); + var response = SingleLightControl(shelfModel, storeCodes); + if (response != null && response.Code == 200) + { + //删除取消的这个任务 + DbHelp.db.Deleteable(cancelTasks).ExecuteCommand(); + //删除被取消的任务 + await Task.Delay(20); + module.IsNeedRefresh = false; + + + } + } + else + { + Logs.Write($"[单灯后台刷新线程]当前库位{module.ModuleCode},还存在任务,不关闭库位灯!"); + + module.IsNeedRefresh = false; + continue; + } + } + #endregion + } + catch (Exception ex) + { + Logs.Write($"【后台发送线程】遇到异常{ex.Message},{ex.StackTrace}"); + } + + } + + //刷新报警灯 + if (needRefreshModules != null && needRefreshModules.Count > 0) + { + var currentTasks = DbHelp.db.Queryable() + .Where(t => t.ShelfId == this.ShelfId) + .ToList(); + if (currentTasks != null && currentTasks.Count > 0) + { + var firstCurrentTask = currentTasks[0]; + //开报警灯 开大灯 + var moduleCodes = currentTasks.Select(t => t.ModuleCode) + .Distinct() + .ToList(); + var shelfModel = new SingleLightShelfModel(); + //报警灯长亮 + shelfModel.WarningLightMode = 1; + shelfModel.WarningLightColor = (int)firstCurrentTask.ButtonColor; + shelfModel.WarningBoardId = shelfInfo.LightId; + shelfModel.ClientIp = ClientIp; + shelfModel.StoreList = new List(); + currentTasks.ForEach(task => + { + var module = MXL4Modules.Where(t => t.ModuleCode == task.ModuleCode).First(); + if (module != null) + { + shelfModel.StoreList.Add(new SingleLightStoreModel() + { + BoardId = module.BoardId, + LightColor = (int)task.ButtonColor, + LightMode = 1, + }); + } + }); + + var response = SingleLightControl(shelfModel, moduleCodes); + //亮了灯的更新为已发送 + if (response != null && response.Code == 200) + { + Logs.Write($"[单灯后台刷新线程]当前灯继续亮!"); + + MXL4Modules.ForEach(t => t.IsNeedRefresh = false); + currentTasks.ForEach(t => t.IsSended = true); + DbHelp.db.Updateable(currentTasks).UpdateColumns(ct => new { ct.IsSended }).ExecuteCommand(); + } + } + else + { + //关报警灯 小灯也全关 + var shelfModel = new SingleLightShelfModel(); + //报警灯关闭 + shelfModel.WarningLightMode = 0; + shelfModel.WarningLightColor = 0; + shelfModel.WarningBoardId = shelfInfo.LightId; + shelfModel.ClientIp = ClientIp; + shelfModel.StoreList = new List(); + var response = SingleLightControl(shelfModel, new List()); + if (response != null && response.Code == 200) + { + Logs.Write($"[单灯后台刷新线程]当前灯不继续亮!"); + MXL4Modules.ForEach(t => t.IsNeedRefresh = false); + } + } + } + + } + catch (Exception ex) + { + Logs.Write($"【后台发送线程】遇到异常{ex.Message},{ex.StackTrace}"); + } + await Task.Delay(1500); + } + }); } + + public bool IsService { get; set; } + public string ShelfTypeName { get; set; } public int ShelfId { get; set; } public string ShelfCode { get; set; } @@ -51,8 +299,8 @@ namespace WCS.BLL.HardWare public TCPClient TcpCleint { get { return TCPClientManager.GetTCPClientByIPHost(ClientIp); } } - public List MXL4Modules { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - + public List MXL4Modules { get; set; } = new List(); + public bool ConfirmStocktakingSingle(int BoardId, int LightNumber) { @@ -67,33 +315,33 @@ namespace WCS.BLL.HardWare public void GoInOutstore(List MatDetails, OutOrder outOrder, string OperateUser) { - + } public void GoInStocktaking() { - + } public void GoInStocktaking(List MatDetails, StockTakingOrder outOrder) { - + } public void GoOutInstore() { //找到已亮灯的 灭灯 - + } public void GoOutOutstore() { - + } public void GoOutStocktaking() { - + } public void Reset() @@ -103,17 +351,136 @@ namespace WCS.BLL.HardWare public void SetCurrentMode(TaskModeEnum mode) { - + } public void ShelfCheck() { - + } public void Warning() { - + + } + + + public ResponseCommon SingleLightControl(SingleLightShelfModel request, List storeCodes) + { + try + { + var shelf = DbHelp.db.Queryable().Where(t => t.Id == ShelfId) + .First(); + var client = new + { + ClientIp = shelf.ClientIp, + IsService = shelf.IsService, + }; + + List singleLightShelfModels = new List(); + singleLightShelfModels.Add(request); + + var sendData = Helper.SingleLightControl(singleLightShelfModels); + if (client.IsService) + { + TCPClient tcpClient = TCPClientManager.GetTCPClientByIPHost(client.ClientIp); + if (tcpClient != null) + { + try + { + tcpClient.Send(sendData); + Logs.Write("【单灯单独控制】发送指令" + BitConverter.ToString(sendData)); + } + catch (Exception ex) + { + Logs.Write($"【单灯单独控制】{client}以下指令发送中遇到异常{ex.Message}" + BitConverter.ToString(sendData)); + } + } + else + { + Logs.Write($"【单灯单独控制】{client}未连接,以下指令未能成功发送" + BitConverter.ToString(sendData)); + } + } + else + { + var tcpClient = TCPClientManager.Service.GetClients().Where(t => t.IP + ":" + t.Port.ToString() == ClientIp).FirstOrDefault(); + if (tcpClient != null) + { + try + { + tcpClient.Send(sendData, 0, sendData.Length); + Logs.Write("【单灯单独控制】发送指令" + BitConverter.ToString(sendData)); + } + catch (Exception ex) + { + Logs.Write($"【单灯单独控制】{client}以下指令发送中遇到异常{ex.Message}" + BitConverter.ToString(sendData)); + return new ResponseCommon() + { + Code = 201, + Message = $"【单灯单独控制】{client}指令发送中遇到异常{ex.Message}", + }; + } + } + else + { + Logs.Write($"【单灯单独控制】{client}未连接,以下指令未能成功发送" + BitConverter.ToString(sendData)); + return new ResponseCommon() + { + Code = 201, + Message = $"【单灯单独控制】{client}未连接,以下指令未能成功发送", + }; + } + } + + var stores = DbHelp.db.Queryable() + .Where(t => storeCodes.Contains(t.StoreCode)) + .ToList(); + //更新数据库灯的状态 + stores.ForEach(t => + { + t.LightMode = request.WarningLightMode == -1 ? 0 : request.WarningLightMode; + t.ColorMode = request.WarningLightColor; + }); + DbHelp.db.Updateable(stores) + .UpdateColumns(t => new { t.LightMode, t.ColorMode }) + .ExecuteCommand(); + + //返回成功 + return new ResponseCommon() + { + Code = 200, + Message = "success", + }; + } + catch (Exception ex) + { + + //操作失败 + return new ResponseCommon() + { + Code = 300, + Message = $"操作失败:{ex.Message}", + }; + } + } + + public string ExtractUntilSecondDashCorrect(string input) + { + int firstDashIndex = input.IndexOf('-'); + if (firstDashIndex == -1) + { + return string.Empty; // 如果没有找到第一个'-',则返回空字符串 + } + + int secondDashIndex = input.IndexOf('-', firstDashIndex + 1); + if (secondDashIndex == -1) + { + return input; + } + else + { + return input.Substring(0, secondDashIndex); + } } } } diff --git a/WCS.BLL/Services/Service/MXL4Service.cs b/WCS.BLL/Services/Service/MXL4Service.cs index d09defd..8336666 100644 --- a/WCS.BLL/Services/Service/MXL4Service.cs +++ b/WCS.BLL/Services/Service/MXL4Service.cs @@ -42,7 +42,7 @@ namespace WCS.BLL.Services.Service .ToList(); var moduleInfo = DbHelp.db.Queryable() .LeftJoin((si, sti) => si.ShelfTypeId == sti.Id) - .Where((si, sti) => sti.ShelfTypeName == "液晶标签货架") + .Where((si, sti) => sti.ShelfTypeName == "液晶标签货架" || sti.ShelfTypeName == "信息化货架") .Where((si, sti) => storeCodeList.Contains(si.ModuleCode)) .Select((si, sti) => si) .ToList(); @@ -270,7 +270,7 @@ namespace WCS.BLL.Services.Service var needCancelTasks = DbHelp.db.Queryable() .Where(t => request.OrderNumber == request.OrderNumber) - .WhereIF(!string.IsNullOrEmpty(request.MatCode), t => t.MatCode == request.MatCode) + .WhereIF(!string.IsNullOrEmpty(request.MatCode), t => t.ItemNo == request.MatCode) .ToList(); needCancelTasks.ForEach(t => t.IsCancel = true); //获取当前 diff --git a/WCS.WebApi/Controllers/SingleLightController.cs b/WCS.WebApi/Controllers/SingleLightController.cs index 8d6bd9b..7c9d494 100644 --- a/WCS.WebApi/Controllers/SingleLightController.cs +++ b/WCS.WebApi/Controllers/SingleLightController.cs @@ -113,7 +113,9 @@ namespace WCS.WebApi.Controllers .OrderBy(t => t.ShelfCode) .ToList(); var storesToSave = new List(); - foreach (var shelf in shelfInfo) + var modulesToSave = new List(); + + foreach (var shelf in shelfInfo) { if (shelf.ShelfCode == "C07-4") ; @@ -126,6 +128,8 @@ namespace WCS.WebApi.Controllers //奇数行 灯Id是最后一个灯 for (int i = shelf.Columncounts; i > 0; i--) { + + var storeInfo = new StoreInfo() { StoreCode = shelf.ShelfCode + "-" + i.ToString(), @@ -144,9 +148,27 @@ namespace WCS.WebApi.Controllers LightMode = 0, ColorMode = 0, GroupName = shelf.GroupName, - Area = shelf.ShelfCode.Substring(0,1), + Area = shelf.ShelfCode.Substring(0, 1), }; storesToSave.Add(storeInfo); + + var moduleInfo = new ModuleInfo() + { + ModuleCode = shelf.ShelfCode + "-" + i.ToString(), + ShelfTypeId = shelf.ShelfTypeId, + ShelfId = shelf.Id, + ShelfCode = shelf.ShelfCode, + BoardId = storeInfo.BoardId, + LightCount = 1, + CleintIp = shelf.ClientIp, + GroupName = shelf.GroupName, + R = storeInfo.R, + C = storeInfo.C, + Bigshelfcode = "", + IsEnable = true, + CurrentMode = BLL.DbModels.Task.TaskModeEnum.待机模式, + }; + modulesToSave.Add(moduleInfo); } shelf.LightId = ++id; @@ -184,12 +206,31 @@ namespace WCS.WebApi.Controllers Area = shelf.ShelfCode.Substring(0, 1), }; storesToSave.Add(storeInfo); + + var moduleInfo = new ModuleInfo() + { + ModuleCode = shelf.ShelfCode + "-" + i.ToString(), + ShelfTypeId = shelf.ShelfTypeId, + ShelfId = shelf.Id, + ShelfCode = shelf.ShelfCode, + BoardId = storeInfo.BoardId, + LightCount = 1, + CleintIp = shelf.ClientIp, + GroupName = shelf.GroupName, + R = storeInfo.R, + C = storeInfo.C, + Bigshelfcode = "", + IsEnable = true, + CurrentMode = BLL.DbModels.Task.TaskModeEnum.待机模式, + }; + modulesToSave.Add(moduleInfo); } } } DbHelp.db.Insertable(storesToSave).ExecuteCommand(); + DbHelp.db.Insertable(modulesToSave).ExecuteCommand(); DbHelp.db.Updateable(shelfInfo).ExecuteCommand(); return new ResponseCommon()