using System.Text.Json; using Microsoft.AspNetCore.Mvc; using SlnMesnac.Config; using SlnMesnac.Model.dto; using SlnMesnac.Plc; using SlnMesnac.Repository.service; namespace SlnMesnac.Controllers; /// /// ingredientsChenck 投料校验 /// [Route("api/[controller]")] [ApiController] public class IngCheckController { private ILogger _logger; private AppConfig _appConfig; private IMcsBinToMaterService _mcsBinToMaterService; private IWmsTaskOutService _wmsTaskOutService; private PlcPool _plcPool; /// /// /// /// public IngCheckController(ILogger logger, IMcsBinToMaterService mcsBinToMaterService, AppConfig appConfig, IWmsTaskOutService wmsTaskOutService, PlcPool plcPool) { _logger = logger; _mcsBinToMaterService = mcsBinToMaterService; _appConfig = appConfig; _wmsTaskOutService = wmsTaskOutService; _plcPool = plcPool; } /// /// 上辅机投料校验 /// /// /// [HttpPost("BigMaterial")] public ApiResponse BigMaterialCheck(IngredientInfo? ingredientInfo) { var result = new ApiResponse(); try { if (ingredientInfo != null) { _logger.LogInformation($"上辅机验证参数:{JsonSerializer.Serialize(ingredientInfo)}"); var mcsInfo = _mcsBinToMaterService.GetMcsBinToMaterByBinNo(1, ingredientInfo.CanNumber); if (mcsInfo != null) { var wmsInfo = _wmsTaskOutService.GetWmsTaskOutBySerialNum(ingredientInfo.MaterialBarcode); if (mcsInfo.MatCode.Contains(wmsInfo.MaterNo)) { result.SetSuccess($"上辅机校验成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); if (ingredientInfo.IsOpen == 1) { var openFlag = _plcPool.GetPlcByKey("mcs").writeInt32ByAddress(_appConfig.mcsOpenAddr,ingredientInfo.CanNumber); if (openFlag) { _logger.LogInformation($"上辅机开仓指令下发成功!"); } else { _logger.LogError("上辅机开仓指令下发失败"); } } //保存数据 } else { result.SetFailure($"上辅机校验失败,物料不匹配,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } } else { result.SetFailure($"上辅机校验失败,根据料罐获取对应信息为空,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } } else { result.SetSuccess($"上辅机校验失败,缺少必要参数,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } } catch (Exception e) { result.SetException(e,"上辅机投料校验异常"); _logger.LogError($"上辅机投料校验异常:{e.Message}"); } return result; } /// /// 小料投料校验 /// /// /// [HttpPost("SmallMaterial")] public ApiResponse SmallMaterialCheck(IngredientInfo? ingredientInfo) { var result = new ApiResponse(); try { if (ingredientInfo != null) { _logger.LogInformation($"小料验证参数:{JsonSerializer.Serialize(ingredientInfo)}"); result.SetSuccess($"小料校验成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } else { result.SetSuccess($"小料校验失败,缺少必要参数,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } } catch (Exception e) { result.SetException(e,"小料投料校验异常"); _logger.LogError($"小料投料校验异常:{e.Message}"); } return result; } /// /// 下发开仓指令 /// /// /// /// private bool SendOpenInstructions(string plcType, string address, int canNumber) { bool result = false; try { result = _plcPool.GetPlcByKey(plcType).writeInt32ByAddress(address,canNumber); } catch (Exception e) { _logger.LogError($"开仓指令下发异常:{e.Message}"); } return result; } }