zhangchengsong
12 小时以前 f59f3b24814387573b5bca9a9b6c832a3720ee7e
ÏîÄ¿´úÂë/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/WMSTaskService.cs
@@ -16,7 +16,9 @@
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using AutoMapper;
using AutoMapper.Internal;
using Castle.Components.DictionaryAdapter.Xml;
using HslCommunication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.OpenApi.Any;
@@ -84,7 +86,7 @@
                if (transfer.to == null || string.IsNullOrEmpty(transfer.to.code)) return apiResponse.ErrorResponse("目标位置不能为空");
                if (transfer.device == null || string.IsNullOrEmpty(transfer.device.uuid)) return apiResponse.ErrorResponse("设备标识不能为空");
                Dt_Task setask = BaseDal.QueryData(x => x.WMStaskid == transfer.barCode).FirstOrDefault();
                Dt_Task? setask = BaseDal.QueryData(x => x.WMStaskid == transfer.barCode).FirstOrDefault();
                if (setask != null) return apiResponse.ErrorResponse($"WCS已有当前任务,不可重复下发,托盘编号:{transfer.barCode}");
@@ -158,23 +160,44 @@
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                //IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1002");
                //if (device == null) return apiResponse.ErrorResponse("WCS未能获取到库口实例");
                //CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                Dictionary<string, int> portStatusDict = new Dictionary<string, int>();
                foreach (string DeStateName in datas)
                {
                    //暂时数据
                    portStatusDict[DeStateName] = 1;
                    string DeciceCodes = "1003";
                    if (DeStateName.StartsWith("C")) DeciceCodes = "1004";
                    /*DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == DeStateName && x.DeviceProParamName == "StationFree");
                    if (deviceProDTO == null) return apiResponse.ErrorResponse($"WCS未找到库口:{DeStateName},对应的协议");
                    conveyorLine.Communicator.Read<bool>(deviceProDTO.DeviceProAddress);
                    bool portStatus = conveyorLine.Communicator.Read<bool>(deviceProDTO.DeviceProAddress);
                    int statusValue = portStatus ? 1 : 0;
                    portStatusDict[DeStateName] = statusValue;*/
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeciceCodes);
                    if (device == null) return apiResponse.ErrorResponse("WCS未能获取到库口实例");
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    //是否可进出
                    DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == DeStateName && x.DeviceProParamName == "PermitHandShake");
                    //是否有货
                    DeviceProDTO? StationNumProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == DeStateName && x.DeviceProParamName == "StationNum");
                    if (deviceProDTO == null || StationNumProDTO ==null) return apiResponse.ErrorResponse($"WCS未找到库口:{DeStateName},对应的协议");
                    int statusValue = 0;
                    //1允许取  2允许放
                    int portStatus = conveyorLine.Communicator.Read<ushort>(deviceProDTO.DeviceProAddress);
                    //是否有货
                    int StationStatus = conveyorLine.Communicator.Read<ushort>(StationNumProDTO.DeviceProAddress);
                    if(DeStateName== "G01" || DeStateName == "G06" || DeStateName == "G07")
                    {
                        //有货,可进信号
                        statusValue = (portStatus == 1 && StationStatus == 1) ? 1 : (portStatus == 2 && StationStatus == 0) ?0: (portStatus == 0) ? 0 : 0;
                    }
                    else
                    {
                        statusValue = StationStatus == 1 ? 0 : 1;
                    }
                    portStatusDict[DeStateName] = statusValue;
                }
                return apiResponse.SuccessResponse(portStatusDict);
@@ -187,7 +210,7 @@
        //获取光幕状态
        public ApiResponse getSafeStatus(string[] datas)
        public ApiResponse getSafeStatus(string[]  datas)
        {
            WriteLog.Write_Log("获取光幕状态接口", "获取光幕状态信息", "调取参数", setCurtain);
            ApiResponse apiResponse = new ApiResponse();
@@ -206,47 +229,134 @@
                return apiResponse.ErrorResponse($"WCS任务添加错误,原因:{ex.Message}");
            }
        }
        //设置光幕
        /// <summary>
        /// è®¾ç½®å…‰å¹•
        /// </summary>
        /// <param name="setCurtain"></param>
        /// <returns></returns>
        public ApiResponse setCurtain(SetCurtainRequest setCurtain)
        {
            //获取WMS调取的参数
            WriteLog.Write_Log("设置光幕接口", "设置光幕信息", "调取参数", setCurtain);
            if (setCurtain == null || setCurtain.Datas == null || setCurtain.Datas.Length == 0)
            {
                return new ApiResponse().ErrorResponse("请求参数或库口列表不能为空");
            }
            if (string.IsNullOrWhiteSpace(setCurtain.Operate))
            {
                return new ApiResponse().ErrorResponse("操作类型(Operate)不能为空");
            }
            WriteLog.Write_Log("设置光幕接口", "设置光幕接口信息", $"库口名称:【{string.Join(",", setCurtain.Datas)}】,申请状态:【{setCurtain.Operate}】");
            ApiResponse apiResponse = new ApiResponse();
            bool allSuccess = true;
            var specialPorts = new[] { "G01", "G06", "G07" };
            try
            {
                return apiResponse.SuccessResponse();
                foreach (string portName in setCurtain.Datas)
                {
                    if (string.IsNullOrWhiteSpace(portName)) { allSuccess = false; continue; }
                    string deviceCode = portName.StartsWith("C") ? "1004" : "1003";
                        CommonConveyorLine? conveyorLine = Storage.Devices.FirstOrDefault(x => x.DeviceCode == deviceCode) as CommonConveyorLine;
                        if (conveyorLine == null) { allSuccess = false; continue; }
                        DeviceProDTO? handShakeDto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == portName && x.DeviceProParamName == "HandShake");
                        DeviceProDTO? requestDto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == portName && x.DeviceProParamName == "Request");
                        if (handShakeDto == null) { allSuccess = false; continue; }
                        bool writeResult = false;
                        //StringComparison.OrdinalIgnoreCase是兼容大小写
                        if (setCurtain.Operate.Equals("OPEN", StringComparison.OrdinalIgnoreCase))
                        {
                            if (specialPorts.Contains(portName))
                                writeResult = conveyorLine.Communicator.Write<short>(requestDto.DeviceProAddress, 0);
                            else
                                writeResult = conveyorLine.Communicator.Write<short>(handShakeDto.DeviceProAddress, 0);
                        }
                        else if (setCurtain.Operate.Equals("CLOSE", StringComparison.OrdinalIgnoreCase))
                        {
                            if (specialPorts.Contains(portName))
                                writeResult = conveyorLine.Communicator.Write<short>(handShakeDto.DeviceProAddress, 1) && conveyorLine.Communicator.Write<short>(requestDto.DeviceProAddress, 1);
                            else
                                writeResult = conveyorLine.Communicator.Write<short>(handShakeDto.DeviceProAddress, 1);
                        }
                        if (!writeResult) allSuccess = false;
                    }
                return allSuccess ? apiResponse.SuccessResponse() : apiResponse.ErrorResponse("部分/全部库口写入PLC信号失败");
            }
            catch (Exception ex)
            {
                return apiResponse.ErrorResponse($"WCS任务添加错误,原因:{ex.Message}");
                WriteLog.Write_Log("设置光幕接口异常", "错误信息", ex.ToString());
                return apiResponse.ErrorResponse($"WCS设置光幕错误,原因:{ex.Message}");
            }
        }
        //agv进出状态反馈
       #endregion
        /// <summary>
        /// agv进出入反馈
        /// </summary>
        /// <param name="agvUpdateRequest"></param>
        /// <returns></returns>
        public ApiResponse getAGVStatus(AgvUpdateRequest agvUpdateRequest)
        {
            //获取WMS调取的参数
            WriteLog.Write_Log("agv进出状态反馈接口", "agv进出状态信息", "调取参数", setCurtain);
            if (agvUpdateRequest == null) return new ApiResponse().ErrorResponse("请求参数不能为空");
            if (string.IsNullOrWhiteSpace(agvUpdateRequest.warehousenumber)) return new ApiResponse().ErrorResponse("库口编号不能为空");
            WriteLog.Write_Log("agv进出状态反馈接口", "agv进出状态信息", $"库口名称:【{agvUpdateRequest.warehousenumber}】,申请状态:【{agvUpdateRequest.agvstatus}】");
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                string deviceCode = agvUpdateRequest.warehousenumber.StartsWith("C") ? "1004" : "1003";
                    CommonConveyorLine? conveyorLine = Storage.Devices.FirstOrDefault(x => x.DeviceCode == deviceCode) as CommonConveyorLine;
                    if (conveyorLine == null) return apiResponse.ErrorResponse("WCS未能获取到库口实例");
                return apiResponse.SuccessResponse();
                    DeviceProDTO? handShake = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == agvUpdateRequest.warehousenumber && x.DeviceProParamName == "HandShake");
                    if (handShake == null) return apiResponse.ErrorResponse($"WCS未找到库口:{agvUpdateRequest.warehousenumber},对应的协议");
                    DeviceProDTO? request = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == agvUpdateRequest.warehousenumber && x.DeviceProParamName == "Request");
                    if (agvUpdateRequest.agvstatus == 1)
                    {
                        bool writeHandShake = conveyorLine.Communicator.Write<short>(handShake.DeviceProAddress, 1);
                        if (!writeHandShake) return apiResponse.ErrorResponse("写入plc进入信号失败");
                        if (request != null) conveyorLine.Communicator.Write<short>(request.DeviceProAddress, 1);
                        var specialPorts = new[] { "G01", "G06", "G07" };
                        if (specialPorts.Contains(agvUpdateRequest.warehousenumber))
                        {
                            DeviceProDTO? permitHandShake = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == agvUpdateRequest.warehousenumber && x.DeviceProParamName == "PermitHandShake");
                            DeviceProDTO? stationNum = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == agvUpdateRequest.warehousenumber && x.DeviceProParamName == "StationNum");
                            if (permitHandShake == null || stationNum == null) return apiResponse.ErrorResponse($"WCS未找到库口:{agvUpdateRequest.warehousenumber},对应的协议");
                            int portStatus = conveyorLine.Communicator.Read<ushort>(permitHandShake.DeviceProAddress);
                            int stationStatus = conveyorLine.Communicator.Read<ushort>(stationNum.DeviceProAddress);
                            bool canEnter = (portStatus == 1 && stationStatus == 1) || (portStatus == 2 && stationStatus == 0);
                            if (!canEnter)
                            {
                                if (request != null) conveyorLine.Communicator.Write<short>(request.DeviceProAddress, 0);
                                return apiResponse.ErrorResponse("读取plc不可进入信息");
                            }
                        }
                        return apiResponse.SuccessResponse();
                    }
                    else if (agvUpdateRequest.agvstatus == 2)
                    {
                        bool clearHandShake = conveyorLine.Communicator.Write<short>(handShake.DeviceProAddress, 0);
                        if (!clearHandShake) return apiResponse.ErrorResponse("写入plc清除信号失败");
                        if (request != null) conveyorLine.Communicator.Write<short>(request.DeviceProAddress, 0);
                        return apiResponse.SuccessResponse();
                    }
                    else
                    {
                        return apiResponse.ErrorResponse($"agv对应的申请错误,字段:agvstatus,申请的值:{agvUpdateRequest.agvstatus}");
                    }
            }
            catch (Exception ex)
            {
                return apiResponse.ErrorResponse($"WCS任务添加错误,原因:{ex.Message}");
                return apiResponse.ErrorResponse($"WCS AGV状态错误,原因:{ex.Message}");
            }
        }
        #endregion
        #region è°ƒå–上游接口
        public CommandResult taskreturn(int taskId, string taskType, string invType, string psd)
@@ -259,17 +369,20 @@
                taskInfo1.taskType = taskType;
                taskInfo1.invType = invType;
                taskInfo1.psd = "akjfapjfpadaoif";
                commandResult = HttpHelper.Post<CommandResult>(urlWMStaskreturn, taskInfo1, "任务状态回调");
                WriteLog.Write_Log("同步给上游反馈任务完成", "任务信息", $"任务号:{taskId}", $"调取参数:{taskInfo1.ToJson()},返回参数:{commandResult.ToJson()}");
                WriteLog.Write_Log("同步给上游反馈任务完成", "任务信息", $"任务号:【{taskId}】", $"调取参数:【{taskInfo1.ToJsonString()}】\n返回参数:{commandResult.ToJsonString()}】");
                return commandResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine("调取上游接口错误:原因:"+ex.Message);
                commandResult.status = "error";
                commandResult.result = $"WCS接口错误,原因:{ex.Message}";
                string resujos = $"WCS接口错误,原因:{ex.Message}";
                commandResult.result = resujos.ToJsonString();
                WriteLog.Write_Log("同步给上游反馈任务完成", "任务信息", $"任务号:{taskId}", $"WCS接口错误,原因:{ex.Message}");
                WriteLog.Write_Log("同步给上游反馈任务完成", "任务信息", $"任务号:【{taskId}】", $"需要上传WMS信息内容:【{commandResult.ToJsonString()}】\nWCS接口错误,原因:{ex.Message}");
                return commandResult;
            }
            
@@ -307,7 +420,9 @@
            catch (Exception ex)
            {
                commandResult.status = "error";
                commandResult.result = $"WCS接口错误,原因:{ex.Message}";
                string resujos = $"WCS接口错误,原因:{ex.Message}";
                commandResult.result = resujos.ToJsonString();
                WriteLog.Write_Log("同步给上游设备故障信息", "设备故障信息", $"设备id:{devuuid},失败", $"WCS接口错误,原因:{ex.Message}");
                return commandResult;
@@ -335,7 +450,9 @@
            catch (Exception ex)
            {
                commandResult.status = "error";
                commandResult.result = $"WCS接口错误,原因:{ex.Message}";
                string resujos = $"WCS接口错误,原因:{ex.Message}";
                commandResult.result = resujos.ToJsonString();
                WriteLog.Write_Log("同步给上游提升到位信息", "信息内容", $"错误信息", $"WCS接口错误,原因:{ex.Message}");
                return commandResult;
            }