From f2e462661672913e224f2925165485b3270743ff Mon Sep 17 00:00:00 2001 From: quowingwang Date: Mon, 19 Jan 2026 15:41:58 +0800 Subject: [PATCH] =?UTF-8?q?PLC=E9=80=9A=E8=AE=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PLC/Controllers/PlcController.cs | 4 +-- .../PLC/Model/Dto/PlcProductionDataDto.cs | 2 ++ RIZO.Admin.WebApi/PLC/Model/PlcConfig.cs | 4 +-- .../PLC/Model/PlcProductionData.cs | 12 +++++++ .../PLC/Service/PlcHostedService.cs | 4 +-- RIZO.Admin.WebApi/PLC/Service/PlcService.cs | 35 +++++++++++++++++-- 6 files changed, 52 insertions(+), 9 deletions(-) diff --git a/RIZO.Admin.WebApi/PLC/Controllers/PlcController.cs b/RIZO.Admin.WebApi/PLC/Controllers/PlcController.cs index 017c8db..7fb9c25 100644 --- a/RIZO.Admin.WebApi/PLC/Controllers/PlcController.cs +++ b/RIZO.Admin.WebApi/PLC/Controllers/PlcController.cs @@ -38,7 +38,7 @@ namespace RIZO.Admin.WebApi.PLC.Controllers /// 批量测试所有PLC的连接、读写功能(核心接口) /// /// 批量测试结果 - [HttpPost("batch-test")] + [HttpGet("batch-test")] [Log(Title = "PLC测试", BusinessType = BusinessType.OTHER)] public async Task BatchTestAllPlc() { @@ -58,7 +58,7 @@ namespace RIZO.Admin.WebApi.PLC.Controllers /// /// 单个PLC配置参数 /// 单PLC测试结果 - [HttpPost("single-test")] + [HttpGet("single-test")] [ActionPermissionFilter(Permission = "business:plc:singletest")] [Log(Title = "PLC测试", BusinessType = BusinessType.OTHER)] public async Task TestSinglePlc([FromBody] PlcConfig config) diff --git a/RIZO.Admin.WebApi/PLC/Model/Dto/PlcProductionDataDto.cs b/RIZO.Admin.WebApi/PLC/Model/Dto/PlcProductionDataDto.cs index f474512..f8e9813 100644 --- a/RIZO.Admin.WebApi/PLC/Model/Dto/PlcProductionDataDto.cs +++ b/RIZO.Admin.WebApi/PLC/Model/Dto/PlcProductionDataDto.cs @@ -44,6 +44,8 @@ namespace RIZO.Admin.WebApi.PLC.Model.Dto public string ReworkFlag { get; set; } public int? ProductionCycle { get; set; } + public int? AutoManual { get; set; } + public int? RunStatus { get; set; } public string Remark { get; set; } diff --git a/RIZO.Admin.WebApi/PLC/Model/PlcConfig.cs b/RIZO.Admin.WebApi/PLC/Model/PlcConfig.cs index d8740b7..a0281bb 100644 --- a/RIZO.Admin.WebApi/PLC/Model/PlcConfig.cs +++ b/RIZO.Admin.WebApi/PLC/Model/PlcConfig.cs @@ -10,8 +10,8 @@ public class GlobalPlcConfig { - public int ConnectTimeout { get; set; } = 5000; - public int ReadWriteTimeout { get; set; } = 5000; + public int ConnectTimeout { get; set; } = 2000; + public int ReadWriteTimeout { get; set; } = 2000; } // PLC测试结果实体 diff --git a/RIZO.Admin.WebApi/PLC/Model/PlcProductionData.cs b/RIZO.Admin.WebApi/PLC/Model/PlcProductionData.cs index 3e605ee..bbb5234 100644 --- a/RIZO.Admin.WebApi/PLC/Model/PlcProductionData.cs +++ b/RIZO.Admin.WebApi/PLC/Model/PlcProductionData.cs @@ -93,6 +93,18 @@ namespace RIZO.Admin.WebApi.PLC.Model [SugarColumn(ColumnName = "production_cycle")] public int? ProductionCycle { get; set; } + /// + /// 设备自动手动:0-自动,1-手动 + /// + [SugarColumn(ColumnName = "automanual")] + public int? AutoManual { get; set; } + + /// + /// 运行状态:1正常,2异常 + /// + [SugarColumn(ColumnName = "runstatus")] + public int? RunStatus { get; set; } + /// /// 备注 /// diff --git a/RIZO.Admin.WebApi/PLC/Service/PlcHostedService.cs b/RIZO.Admin.WebApi/PLC/Service/PlcHostedService.cs index 5e8d950..58d2181 100644 --- a/RIZO.Admin.WebApi/PLC/Service/PlcHostedService.cs +++ b/RIZO.Admin.WebApi/PLC/Service/PlcHostedService.cs @@ -44,12 +44,10 @@ namespace RIZO.Admin.WebApi.PLC.Service public PlcHostedService( ILogger logger, - PlcService plcService, - IOptions> plcConfigs) + PlcService plcService) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _plcService = plcService ?? throw new ArgumentNullException(nameof(plcService)); - _plcConfigs = plcConfigs?.Value ?? new List(); //初始化plcConfigs _plcConfigs = initPlcConfigs(_plcConfigs); diff --git a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs index 8e69900..4c8d93e 100644 --- a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs +++ b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs @@ -1,4 +1,5 @@ // 统一引入所有必要命名空间 +using MDM.Services.Plant; using Microsoft.Extensions.Options; using RIZO.Admin.WebApi.PLC.Model; using RIZO.Common; @@ -26,6 +27,7 @@ namespace RIZO.Admin.WebApi.PLC.Service private readonly GlobalPlcConfig _globalConfig; private PlcProductionDataService plcProductionDataService = new PlcProductionDataService(); + private PlantWorkstationService plantWorkstationService = new PlantWorkstationService(); // PLC地址映射(严格匹配业务地址清单) private readonly Dictionary _plcStringMap = new() @@ -56,10 +58,11 @@ namespace RIZO.Admin.WebApi.PLC.Service /// /// 所有PLC的连接配置 /// PLC全局超时配置 - public PlcService(IOptions> plcConfigs, IOptions globalConfig) + public PlcService(IOptions globalConfig) { - _plcConfigs = plcConfigs?.Value ?? throw new ArgumentNullException(nameof(plcConfigs), "PLC配置不能为空"); _globalConfig = globalConfig?.Value ?? throw new ArgumentNullException(nameof(globalConfig), "PLC全局配置不能为空"); + //初始化plcConfigs + _plcConfigs = initPlcConfigs(_plcConfigs); } #endregion @@ -113,6 +116,10 @@ namespace RIZO.Admin.WebApi.PLC.Service QualificationFlag = (await ReadPlcIntAsync(plc, _plcIntMap["QualificationFlag"])).ToString(), //返工标志(0正常,1返工) ReworkFlag = (await ReadPlcIntAsync(plc, _plcIntMap["ReworkFlag"])).ToString(), + //设备自动手动:0-自动,1-手动 + AutoManual = (await ReadPlcIntAsync(plc, _plcIntMap["AutoManual"])), + //运行状态:1正常,2异常 + RunStatus = (await ReadPlcIntAsync(plc, _plcIntMap["AutoManual"])), //生产节拍秒 ProductionCycle = await ReadPlcIntAsync(plc, _plcIntMap["ProductionCycle"]) }; @@ -608,5 +615,29 @@ namespace RIZO.Admin.WebApi.PLC.Service Dispose(false); } #endregion + + private List initPlcConfigs(List result) + { + var defaultResult = result ?? new List(); + try + { + List query = plantWorkstationService.Queryable() + .Where(it => it.Status == 1) + .Select(it => new PlcConfig + { + PlcName = it.WorkstationCode, + Ip = it.PlcIP, + Rack = (short)it.Rack, // 直接强制转换(it.Rack是int,非空) + Slot = (short)it.Slot // 同理,it.Slot是int,非空 + }) + .ToList(); + return query.Count > 0 ? query : defaultResult; + } + catch (Exception ex) + { + Console.WriteLine($"初始化PLC配置异常:{ex.Message}"); + return defaultResult; + } + } } } \ No newline at end of file