ScanPoint.cs 7.93 KB
using HHECS.Bll;
using HHECS.Model.BllModel;
using HHECS.Model.Common;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.OPC;
using HHECS.Scan;
using System.Collections.Generic;
using System.Linq;

namespace HHECS.EquipmentExcute
{
    public class ScanPoint : StationExcute
    {
        public override BllResult Excute(List<Equipment> equipments, OPCHelp plc)
        {
            List<Equipment> stations = equipments.Where(a => a.EquipmentType.Id == EquipmentType.Id).ToList();
            foreach (var station in stations)
            {
                InnerExcute(station, plc);
            }
            return BllResultFactory.Sucess();
        }

        private void InnerExcute(Equipment station, OPCHelp plc)
        {
            //如果没有请求扫码,就清除掉已读码状态
            if (station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestScan").Value == "False" && station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyScan").Value == "1")
            {
                ClearScanRequest(station, plc);
            }

            //如果有请求扫码
            if (station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestScan").Value == "True")
            {
                //扫描条码
                if (Pepperl.socketClient[station.Name].Connected() == false)
                {
                    Pepperl.socketClient[station.Name].connect();
                }
                //读取条码并且写入PLC
                var barcode = Pepperl.socketClient[station.Name].getBarcode();
                if (string.IsNullOrWhiteSpace(barcode) || barcode.ToUpper() == "FAIL" || barcode.Length != 6)
                {
                    Logger.Log($"站台{station.Name}获取条码失败!", LogLevel.Error);
                    return;
                }

                var prop = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WSCReplyBarcode");
                prop.Value = barcode;
                var sendResult = plc.WriteAddress(new List<EquipmentProp> { prop });
                if (sendResult.Success)
                {
                    Logger.Log($"站台{station.Name}对应的条码{barcode}写入成功!", LogLevel.Success);
                }
                else
                {
                    Logger.Log($"站台{station.Name}对应的条码{barcode}写入失败!", LogLevel.Error);
                    return;
                }

                //获取任务
                TaskEntity task = null;
                var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where containerCode = '{barcode}' and lastStatus < {TaskEntityStatus.任务完成.GetIndexInt()}");
                if (taskResult.Success == false)
                {
                    var containerType = "DX";
                    if (station.Code == "ProductScan")
                    {
                        containerType = "MZ";
                    } 
                    BllResult<TaskEntity> airflareResult = AppSession.Bll.ReceiptAirflare<TaskEntity>(barcode, containerType, AppSession.Client, AppSession.Urls);
                    if (airflareResult.Success)
                    {
                        List<TaskEntity> data = new List<TaskEntity>() { airflareResult.Data };
                        taskResult = BllResultFactory<List<TaskEntity>>.Sucess(data);
                    }
                    else
                    {
                        Logger.Log($"站台{station.Name}根据条码{barcode}生成空托盘入库任务失败:{airflareResult.Msg}!", LogLevel.Error);
                        return;
                    }
                }
                if (taskResult.Data.Count > 1)
                {
                    Logger.Log($"站台{station.Name}根据条码{barcode}查找到了多条任务,请检查数据", LogLevel.Error);
                    return;
                }
                else
                {
                    task = taskResult.Data[0];
                    if (task.Type == TaskType.整盘出库.GetIndexInt() || task.Type == TaskType.空容器出库.GetIndexInt())
                    {
                        Logger.Log($"站台{station.Name}不接受整盘出库和空托盘出库任务", LogLevel.Error);
                        return;
                    }
                    //检测是否有去向库位,没有则请求
                    if (string.IsNullOrWhiteSpace(task.DestinationLocation))
                    {
                        var result = AppSession.Bll.GetDestinationLocation<string>(task.Id, 0, AppSession.Client, AppSession.Urls);
                        if (result.Success)
                        {
                            task.DestinationLocation = result.Data;
                            var a = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                            if (a.Success)
                            {
                                Logger.Log($"站台{station.Name},任务{task.Id},获取去向地址:{result.Data}成功", LogLevel.Success);
                            }
                            else
                            {
                                Logger.Log($"站台{station.Name},任务{task.Id},获取去向地址:{result.Data}失败", LogLevel.Success);
                            }
                        }
                        else
                        {
                            Logger.Log($"站台:{station.Name},任务{task.Id},未能获取到去向地址:{result.Msg}", LogLevel.Error);
                        }
                    }
                    int tempStatus = task.FirstStatus; //记录原始状态以便回滚
                    //更新任务状态到60
                    task.FirstStatus = TaskEntityStatus.拣选台回库.GetIndexInt();
                    task.LastStatus = TaskEntityStatus.拣选台回库.GetIndexInt();
                    var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                    if (temp.Success)
                    {
                        //给PLC确认读码完成
                        prop = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyScan");
                        prop.Value = "1";
                        sendResult = plc.WriteAddress(new List<EquipmentProp> { prop });
                        if (sendResult.Success)
                        {
                            Logger.Log($"站台{station.Name}请求读码确认成功!", LogLevel.Success);
                            AppSession.Bll.SendLED(station, task, AppSession.LEDExcute);
                        }
                        else
                        {
                            Logger.Log($"站台{station.Name}请求读码确认失败!", LogLevel.Error);
                            task.FirstStatus = tempStatus;
                            task.LastStatus = tempStatus;
                            AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                        }
                    }
                    else
                    {
                        Logger.Log($"站台{station.Name},响应请求时更新任务{task.Id}状态失败。详情:{temp.Msg}", LogLevel.Error);
                        return;
                    }
                }

            }


        }

        private void ClearScanRequest(Equipment station, OPCHelp plc)
        {
            //给PLC确认读码完成
            var prop = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyScan");
            prop.Value = "0";
            var sendResult = plc.WriteAddress(new List<EquipmentProp> { prop });
            if (sendResult.Success)
            {
                Logger.Log($"站台{station.Name}读码状态清除成功!", LogLevel.Success);
            }
            else
            {
                Logger.Log($"站台{station.Name}读码状态清除成功!", LogLevel.Error);
                return;
            }
        }
    }
}