using Autofac.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Common.PLCEnum; using WIDESEAWCS_Core; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.BaseServices; using WIDESEAWCS_DTO.BasicInfo; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; namespace WIDESEAWCS_BasicInfoService { public class ScanStationService : ServiceBase>, IScanStationService { private readonly IFormulaService _formulaService; private readonly IFormulaDetailService _formulaDetailService; public ScanStationService(IRepository BaseDal, IFormulaService formulaService, IFormulaDetailService formulaDetailService ) : base(BaseDal) { _formulaService = formulaService; _formulaDetailService = formulaDetailService; } public IRepository Repository => BaseDal; /// /// 启动PLC /// /// /// /// public WebResponseContent StartPLC(bool isStop) { try { OtherDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "主控PLC") as OtherDevice; if (device == null) throw new Exception("未找到主控PLC设备信息"); if (!device.IsConnected) throw new Exception($"主控PLC设备通讯异常"); if (isStop) { device.SetValue(W_PLCDBName.wboolAutoStart, false); //暂停信号 device.SetValue(W_PLCDBName.wboolAutoPause, false); return WebResponseContent.Instance.OK(); } var Heart = device.GetValue(R_PLCDBName.rboolHeart); var EMG = device.GetValue(R_PLCDBName.rboolEMG); var OnlineExecuting = device.GetValue(R_PLCDBName.rboolOnlineExecuting); var Error = device.GetValue(R_PLCDBName.rboolError); if (Heart && !EMG && OnlineExecuting && !Error) { device.SetValue(W_PLCDBName.wboolAutoStart, true); //暂停信号 device.SetValue(W_PLCDBName.wboolAutoPause, false); return WebResponseContent.Instance.OK(); } return WebResponseContent.Instance.Error("设备不是在线状态"); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } /// /// 暂停/恢复PLC(双向控制) /// /// 是否为暂停操作:true=暂停,false=恢复 /// 统一格式的响应结果 public WebResponseContent PausePLC(bool isPause) { try { OtherDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "主控PLC") as OtherDevice; if (device == null) throw new Exception("未找到主控PLC设备信息"); if (!device.IsConnected) throw new Exception($"主控PLC设备通讯异常"); var Heart = device.GetValue(R_PLCDBName.rboolHeart); var EMG = device.GetValue(R_PLCDBName.rboolEMG); var Error = device.GetValue(R_PLCDBName.rboolError); if(!device.GetValue(W_PLCDBName.wboolAutoStart)) { return WebResponseContent.Instance.Error("设备未启动"); } if (!Heart || EMG || Error) { return WebResponseContent.Instance.Error("设备状态异常,无法执行暂停/恢复操作"); } device.SetValue(W_PLCDBName.wboolAutoPause, isPause); return WebResponseContent.Instance.OK(); } catch (Exception ex) { return WebResponseContent.Instance.Error(ex.Message); } } /// /// 返回信号 /// /// /// public WebResponseContent GetSignalStates() { OtherDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "主控PLC") as OtherDevice; if (device == null) throw new Exception("未找到主控PLC设备信息"); if (!device.IsConnected) throw new Exception($"主控PLC设备通讯异常"); //获取信号灯状态 var Heart = device.GetValue(R_PLCDBName.rboolHeart); var EMG = device.GetValue(R_PLCDBName.rboolEMG); var AutoExecuting = device.GetValue(R_PLCDBName.rboolAutoExecuting); var OnlineExecuting = device.GetValue(R_PLCDBName.rboolOnlineExecuting); var Error = device.GetValue(R_PLCDBName.rboolError); //获取启动暂停状态 var IsStarted = device.GetValue(W_PLCDBName.wboolAutoStart); var IsPaused = device.GetValue(W_PLCDBName.wboolAutoPause); //返回对象 var responseData = new { // 信号灯状态数组(保持原有顺序) signalStates = new bool[] { Heart, EMG, AutoExecuting, OnlineExecuting, Error }, // PLC启动/暂停状态对象 plcStatus = new { isStarted = IsStarted, isPaused = IsPaused } }; return WebResponseContent.Instance.OK(data: responseData); } /// /// 获取成品信息 /// /// public WebResponseContent GetLeftInitialData() { try { Dt_ScanStation dt_ScanStation = BaseDal.QueryFirst(x => x.StationCode == "001"); if (dt_ScanStation == null) { return WebResponseContent.Instance.Error("未找到工位编码为001的工位信息"); } Dt_Formula dt_Formula = _formulaService.Repository.QueryFirst(x => x.ProductCode == dt_ScanStation.StationEndProduct); if (dt_Formula == null) { return WebResponseContent.Instance.Error($"未找到成品编号【{dt_ScanStation.StationEndProduct}】对应的配方信息"); } List dt_FormulaDetails = _formulaDetailService.Repository.QueryData(x => x.FormulaId == dt_Formula.Id); var responseData = new { finishedProductId = dt_Formula.Id, finishedProduct = dt_Formula.ProductCode, leftPartCodes = new List(), leftPartIds = new List(), leftPartChecked = new List() }; foreach (var detail in dt_FormulaDetails.Take(10)) { responseData.leftPartCodes.Add(detail.ComponentCode ?? ""); responseData.leftPartIds.Add(detail.Id); responseData.leftPartChecked.Add(detail.IsScanned); } int needFillCount = 10 - responseData.leftPartCodes.Count; for (int i = 0; i < needFillCount; i++) { responseData.leftPartCodes.Add(""); responseData.leftPartIds.Add(0); responseData.leftPartChecked.Add(0); } return WebResponseContent.Instance.OK("获取左侧初始数据成功", responseData); } catch (Exception ex) { return WebResponseContent.Instance.Error($"获取左侧初始数据失败,请稍后重试【{ex.Message}】"); } } /// /// 更新是否扫码 /// /// /// public WebResponseContent UpdatePartScannedStatus(UpdatePartScannedStatusRequest updatePartScannedStatusRequest) { try { Dt_FormulaDetail dt_FormulaDetail = _formulaDetailService.Repository.QueryFirst(x => x.Id == updatePartScannedStatusRequest.Id); dt_FormulaDetail.IsScanned = updatePartScannedStatusRequest.IsScanned; bool flag = _formulaDetailService.Repository.UpdateData(dt_FormulaDetail); if (flag) { return WebResponseContent.Instance.OK(); } return WebResponseContent.Instance.Error("更新错误"); } catch (Exception ex) { return WebResponseContent.Instance.Error("获取数据失败"); } } } }