1
huangxiaoqiang
3 天以前 ec9c9add38591d7c7a2708ac43ed6b3fce1a4063
ÏîÄ¿´úÂë/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/DeviceInfoService.cs
@@ -16,31 +16,44 @@
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using AutoMapper;
using HslCommunication;
using Newtonsoft.Json;
using SixLabors.ImageSharp.ColorSpaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Log;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.StackerHandTask;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_Model.BasicInfo;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DeviceEnum;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using static WIDESEAWCS_QuartzJob.Service.DeviceInfoService;
namespace WIDESEAWCS_QuartzJob.Service
{
    public class DeviceInfoService : ServiceBase<Dt_DeviceInfo, IDeviceInfoRepository>, IDeviceInfoService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly IMapper _mapper;
        public DeviceInfoService(IDeviceInfoRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IMapper mapper) : base(BaseDal)
        public DeviceInfoService(IDeviceInfoRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IMapper mapper, IDt_StationManagerRepository stationManagerRepository) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _mapper = mapper;
            _stationManagerRepository = stationManagerRepository;
        }
        public override WebResponseContent AddData(SaveModel saveModel)
@@ -57,5 +70,1195 @@
            List<Dt_DeviceInfo> deviceInfos = await Db.Queryable<Dt_DeviceInfo>().Where(x => x.DeviceStatus == ((int)DeviceStatusEnum.Enable).ToString()).Includes(x => x.ProtocolList).ToListAsync();
            return _mapper.Map<List<DeviceInfoDTO>>(deviceInfos);
        }
        #region  å †åž›æœºæ‰‹åЍ命令
        public WebResponseContent StackerHandTask(HandTask stackerhand)
        {
            WriteLog.GetLog("堆垛机手动任务").Write(JsonConvert.SerializeObject(stackerhand), "堆垛机手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
                if (stackerhand == null || stackerhand.TaskType == "")
                {
                    return content.Error("请输入任务命令");
                }
                stackerCraneTaskCommand.WorkType = (byte)Convert.ToSByte(stackerhand.TaskType);
                //入库
                if (stackerhand.TaskType == "17")
                {
                    string[] sourceCodes = stackerhand.SourceAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = (byte)Convert.ToSByte(GetRowCode(Convert.ToInt32(sourceCodes[0])));
                        stackerCraneTaskCommand.StartColumn = (byte)Convert.ToSByte(sourceCodes[2]);
                        stackerCraneTaskCommand.StartLayer = (byte)Convert.ToSByte(sourceCodes[1]);
                    }
                    else
                    {
                        return content.Error($"起点错误:【{stackerhand.SourceAddress}】");
                    }
                    string[] targetCodes = stackerhand.TargetAddress.Split("-");
                    if (targetCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.EndRow = (byte)Convert.ToSByte(GetRowCode(Convert.ToInt32(targetCodes[0])));
                        stackerCraneTaskCommand.EndColumn = (byte)Convert.ToSByte(targetCodes[2]);
                        stackerCraneTaskCommand.EndLayer = (byte)Convert.ToSByte(targetCodes[1]);
                    }
                    else
                    {
                        return content.Error($"终点错误:【{stackerhand.TargetAddress}】");
                    }
                }
                //出库
                else if (stackerhand.TaskType == "18")
                {
                    string[] sourceCodes = stackerhand.SourceAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = (byte)Convert.ToSByte(GetRowCode(Convert.ToInt32(sourceCodes[0])));
                        stackerCraneTaskCommand.StartColumn = (byte)Convert.ToSByte(sourceCodes[2]);
                        stackerCraneTaskCommand.StartLayer = (byte)Convert.ToSByte(sourceCodes[1]);
                    }
                    else
                    {
                        return content.Error($"起点错误:【{stackerhand.SourceAddress}】");
                    }
                    string[] targetCodes = stackerhand.TargetAddress.Split("-");
                    if (targetCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.EndRow = (byte)Convert.ToSByte(GetRowCode(Convert.ToInt32(targetCodes[0])));
                        stackerCraneTaskCommand.EndColumn = (byte)Convert.ToSByte(targetCodes[2]);
                        stackerCraneTaskCommand.EndLayer = (byte)Convert.ToSByte(targetCodes[1]);
                    }
                    else
                    {
                        return content.Error($"终点错误:【{stackerhand.TargetAddress}】");
                    }
                }
                //移库
                else if (stackerhand.TaskType == "20")
                {
                    string[] sourceCodes = stackerhand.SourceAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = (byte)Convert.ToSByte(GetRowCode(Convert.ToInt32(sourceCodes[0])));
                        stackerCraneTaskCommand.StartColumn = (byte)Convert.ToSByte(sourceCodes[2]);
                        stackerCraneTaskCommand.StartLayer = (byte)Convert.ToSByte(sourceCodes[1]);
                    }
                    else
                    {
                        return content.Error($"起点错误:【{stackerhand.SourceAddress}】");
                    }
                    string[] targetCodes = stackerhand.TargetAddress.Split("-");
                    if (targetCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.EndRow = (byte)Convert.ToSByte(GetRowCode(Convert.ToInt32(targetCodes[0])));
                        stackerCraneTaskCommand.EndColumn = (byte)Convert.ToSByte(targetCodes[2]);
                        stackerCraneTaskCommand.EndLayer = (byte)Convert.ToSByte(targetCodes[1]);
                    }
                    else
                    {
                        return content.Error($"终点错误:【{stackerhand.TargetAddress}】");
                    }
                }
                //取货
                else if (stackerhand.TaskType == "24")
                {
                    string[] sourceCodes = stackerhand.SourceAddress.Split("-");
                    if (sourceCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = (byte)Convert.ToSByte(GetRowCode(Convert.ToInt32(sourceCodes[0])));
                        stackerCraneTaskCommand.StartColumn = (byte)Convert.ToSByte(sourceCodes[2]);
                        stackerCraneTaskCommand.StartLayer = (byte)Convert.ToSByte(sourceCodes[1]);
                        stackerCraneTaskCommand.EndRow = (byte)Convert.ToSByte(0);
                        stackerCraneTaskCommand.EndColumn = (byte)Convert.ToSByte(0);
                        stackerCraneTaskCommand.EndLayer = (byte)Convert.ToSByte(0);
                    }
                    else
                    {
                        return content.Error($"起点错误:【{stackerhand.SourceAddress}】");
                    }
                }
                //放货
                else if (stackerhand.TaskType == "48")
                {
                    string[] targetCodes = stackerhand.TargetAddress.Split("-");
                    if (targetCodes.Length == 3)
                    {
                        stackerCraneTaskCommand.StartRow = (byte)Convert.ToSByte(0);
                        stackerCraneTaskCommand.StartColumn = (byte)Convert.ToSByte(0);
                        stackerCraneTaskCommand.StartLayer = (byte)Convert.ToSByte(0);
                        stackerCraneTaskCommand.EndRow = (byte)Convert.ToSByte(GetRowCode(Convert.ToInt32(targetCodes[0])));
                        stackerCraneTaskCommand.EndColumn = (byte)Convert.ToSByte(targetCodes[2]);
                        stackerCraneTaskCommand.EndLayer = (byte)Convert.ToSByte(targetCodes[1]);
                    }
                    else
                    {
                        return content.Error($"终点错误:【{stackerhand.SourceAddress}】");
                    }
                }
                else
                {
                    return content.Error("未找到任务命令");
                }
                StackerSendCommand(stackerCraneTaskCommand, stackerhand.DeviceCode);
                return content.OK("命令下发成功!");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent StackerReset(string DeviceCode)
        {
            WriteLog.GetLog("堆垛机手动任务").Write($"【复位命令】{JsonConvert.SerializeObject(DeviceCode)}", "堆垛机手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode);
                if (device != null)
                {
                    CommonStackerCrane commonStackerCrane = (CommonStackerCrane)device;
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, Convert.ToSByte(2));
                    return content.OK();
                }
                else
                {
                    return content.Error("未找到设备信息请联系IT");
                }
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent StackerEmergencyStop(string DeviceCode)
        {
            WriteLog.GetLog("堆垛机手动任务").Write($"【急停命令】{JsonConvert.SerializeObject(DeviceCode)}", "堆垛机手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode);
                if (device != null)
                {
                    CommonStackerCrane commonStackerCrane = (CommonStackerCrane)device;
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, Convert.ToSByte(2));
                    return content.OK();
                }
                else
                {
                    return content.Error("未找到设备信息请联系IT");
                }
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public void StackerSendCommand(StackerCraneTaskCommand? command,string DeviceCode)
        {
            IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode);
            if (device != null)
            {
                CommonStackerCrane commonStackerCrane = (CommonStackerCrane)device;
                if (commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Automatic) && !commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Fault) && !commonStackerCrane.GetValue<StackerCraneDBName, bool>(StackerCraneDBName.Running))
                {
                    commonStackerCrane.SetValue(StackerCraneDBName.TaskNum, (short)Convert.ToInt16(1));
                    Thread.Sleep(100);
                    commonStackerCrane.SetValue(StackerCraneDBName.StartRow, command.StartRow);
                    Thread.Sleep(100);
                    commonStackerCrane.SetValue(StackerCraneDBName.StartColumn, command.StartColumn);
                    Thread.Sleep(100);
                    commonStackerCrane.SetValue(StackerCraneDBName.StartLayer, command.StartLayer);
                    Thread.Sleep(100);
                    commonStackerCrane.SetValue(StackerCraneDBName.EndRow, command.EndRow);
                    Thread.Sleep(100);
                    commonStackerCrane.SetValue(StackerCraneDBName.EndColumn, command.EndColumn);
                    Thread.Sleep(100);
                    commonStackerCrane.SetValue(StackerCraneDBName.EndLayer, command.EndLayer);
                    Thread.Sleep(100);
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, Convert.ToSByte(command.WorkType));
                }
                else
                {
                    throw new Exception($"请在{commonStackerCrane.DeviceName}联机模式、待机状态、无故障的情况下下发任务");
                }
            }
            else
            {
                throw new Exception("未找到设备信息请联系IT");
            }
        }
        public int GetRowCode(int number)
        {
            if (number == 1 || number == 15 || number == 11 || number == 19)
            {
                return 1;
            }
            else if (number == 2 || number == 5 || number == 8 || number == 12 || number == 16 || number == 20)
            {
                return 2;
            }
            else if (number == 3 || number == 6 || number == 9 || number == 13 || number == 17 || number == 21)
            {
                return 3;
            }
            else if (number == 4 || number == 7 || number == 10 || number == 14 || number == 18 || number == 22)
            {
                return 4;
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(number), "输入的数字必须在1-16范围内");
            }
        }
        public class StackerCraneTaskCommand
        {
            #region <Public Menber>
            /// <summary>
            /// èµ·å§‹è¡Œ
            /// </summary>
            public byte StartRow { get; set; }
            /// <summary>
            /// èµ·å§‹åˆ—
            /// </summary>
            public byte StartColumn { get; set; }
            /// <summary>
            /// èµ·å§‹å±‚
            /// </summary>
            public byte StartLayer { get; set; }
            /// <summary>
            /// ç›®æ ‡è¡Œ
            /// </summary>
            public byte EndRow { get; set; }
            /// <summary>
            /// ç›®æ ‡åˆ—
            /// </summary>
            public byte EndColumn { get; set; }
            /// <summary>
            /// ç›®æ ‡å±‚
            /// </summary>
            public byte EndLayer { get; set; }
            /// <summary>
            /// ä½œä¸šç±»åž‹
            /// </summary>
            public byte WorkType { get; set; }
            /// <summary>
            /// é¢„留字节
            /// </summary>
            public byte Spare { get; set; }
            /// <summary>
            /// ä»»åŠ¡å·
            /// </summary>
            public short TaskNum { get; set; }
            #endregion <Public Menber>
        }
        public enum StackerCraneDBName
        {
            /// <summary>
            /// ä»»åŠ¡å·
            /// </summary>
            TaskNum,
            /// <summary>
            /// ä½œä¸šç±»åž‹
            /// </summary>
            WorkType,
            /// <summary>
            /// èµ·å§‹è¡Œ
            /// </summary>
            StartRow,
            /// <summary>
            /// èµ·å§‹åˆ—
            /// </summary>
            StartColumn,
            /// <summary>
            /// èµ·å§‹å±‚
            /// </summary>
            StartLayer,
            /// <summary>
            /// ç›®æ ‡è¡Œ
            /// </summary>
            EndRow,
            /// <summary>
            /// ç›®æ ‡åˆ—
            /// </summary>
            EndColumn,
            /// <summary>
            /// ç›®æ ‡å±‚
            /// </summary>
            EndLayer,
            /// <summary>
            /// æ‰˜ç›˜å·
            /// </summary>
            Barcode,
            /// <summary>
            /// èµ·ç‚¹
            /// </summary>
            StartAddress,
            /// <summary>
            /// ç»ˆç‚¹
            /// </summary>
            EndAddress,
            /// <summary>
            /// ä»»åŠ¡å®Œæˆ
            /// </summary>
            TaskCompleted,
            /// <summary>
            /// ä»»åŠ¡å‘é€
            /// </summary>
            SendTask,
            /// <summary>
            /// è‡ªåŠ¨çŠ¶æ€
            /// </summary>
            Automatic,
            /// <summary>
            /// ä½œä¸šå®Œæˆ
            /// </summary>
            WorkCompleted,
            /// <summary>
            /// è¿è¡Œä¸­
            /// </summary>
            Running,
            /// <summary>
            /// æ•…éšœ
            /// </summary>
            Fault,
            /// <summary>
            /// ä½œä¸šç±»åž‹
            /// </summary>
            TaskType,
            /// <summary>
            /// å½“前任务号
            /// </summary>
            CurrentTaskNum,
            /// <summary>
            /// æœ‰è´§
            /// </summary>
            InStock,
            PickupLocation,
            PutcargoLocation,
            /// <summary>
            /// å½“前列
            /// </summary>
            CurrentColumn,
            /// <summary>
            /// å½“前层
            /// </summary>
            CurrentLayer,
            /// <summary>
            /// æ°´å¹³ç‚¹ä½
            /// </summary>
            LevelPoint,
        }
        #endregion
        #region  è¾“送线手动命令
        public WebResponseContent GetConveyorLineInfo(string DeviceChildCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == DeviceChildCode);
                if (stationManager != null)
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC);
                    if (device != null)
                    {
                        CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                        short x = conveyorLine.GetValue<ConveyorLineDBName, short>(ConveyorLineDBName.InteractiveSignal, stationManager.stationChildCode);
                        var structs = BitConverter.GetBytes(x).ToArray().ToBoolArray();
                        short ConveyorLineTaskNum = conveyorLine.GetValue<ConveyorLineDBName, short>(ConveyorLineDBName.ConveyorLineTaskNum, stationManager.stationChildCode);
                        uint ConveyorLineAlarm = conveyorLine.GetValue<ConveyorLineDBName, uint>(ConveyorLineDBName.ConveyorLineAlarm, stationManager.stationChildCode);
                        ConveyorLineTaskCommand obj = new()
                        {
                            InStock = structs[2] == true ? "有货" : "空闲",
                            TaskNum = ConveyorLineTaskNum.ToString(),
                            Alarm = AlarmInfo(ConveyorLineAlarm)
                        };
                        return content.OK(data:obj);
                    }
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent ConveyorLineHandTask(HandTask ConveyorLinehand)
        {
            WriteLog.GetLog("输送线手动任务").Write(JsonConvert.SerializeObject(ConveyorLinehand), "输送线手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (ConveyorLinehand == null || ConveyorLinehand.TaskType == "")
                {
                    return content.Error("请输入任务命令");
                }
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == ConveyorLinehand.DeviceCode);
                if (stationManager != null)
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC);
                    if (device != null)
                    {
                        CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                        short x = conveyorLine.GetValue<ConveyorLineDBName, short>(ConveyorLineDBName.InteractiveSignal, stationManager.stationChildCode);
                        var structs = BitConverter.GetBytes(x).ToArray().ToBoolArray();
                        if (!structs[2])
                        {
                            return content.Error($"请在{stationManager.stationChildCode}输送线就绪情况下下发命令");
                        }
                        //入库
                        if (ConveyorLinehand.TaskType == "1")
                        {
                            conveyorLine.SetValue(ConveyorLineDBName.WriteConveyorLineTargetAddress, Convert.ToInt16(ConveyorLinehand.TargetAddress), stationManager.stationChildCode);
                            Thread.Sleep(100);
                            conveyorLine.SetValue(ConveyorLineDBName.WriteConveyorLineTaskNum, Convert.ToInt16(1), stationManager.stationChildCode);
                            Thread.Sleep(100);
                            conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(ConveyorLinehand.TaskType), stationManager.stationChildCode);
                            return content.OK("发送输送线入库任务成功!");
                        }
                        //出库
                        else if (ConveyorLinehand.TaskType == "2")
                        {
                            conveyorLine.SetValue(ConveyorLineDBName.WriteConveyorLineTargetAddress, Convert.ToInt16(ConveyorLinehand.TargetAddress), stationManager.stationChildCode);
                            Thread.Sleep(100);
                            conveyorLine.SetValue(ConveyorLineDBName.WriteConveyorLineTaskNum, Convert.ToInt16(1), stationManager.stationChildCode);
                            Thread.Sleep(100);
                            conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(ConveyorLinehand.TaskType), stationManager.stationChildCode);
                            return content.OK("发送输送线出库任务成功!");
                        }
                        else
                        {
                            return content.Error("未找到任务命令");
                        }
                    }
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent ConveyorLineReset(string DeviceChildCode)
        {
            WriteLog.GetLog("输送线手动任务").Write($"【复位命令】{JsonConvert.SerializeObject(DeviceChildCode)}", "输送线手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == DeviceChildCode);
                if (stationManager != null)
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC);
                    if (device != null)
                    {
                        CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                        conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(7), stationManager.stationChildCode);
                        return content.OK("复位成功");
                    }
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent ConveyorLineEmergencyStop(string DeviceChildCode)
        {
            WriteLog.GetLog("输送线手动任务").Write($"【急停命令】{JsonConvert.SerializeObject(DeviceChildCode)}", "输送线手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == DeviceChildCode);
                if (stationManager != null)
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC);
                    if (device != null)
                    {
                        CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                        conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(7), stationManager.stationChildCode);
                        return content.OK("停止成功");
                    }
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent ConveyorLineReturn(string DeviceChildCode)
        {
            WriteLog.GetLog("输送线手动任务").Write($"【退回命令】{JsonConvert.SerializeObject(DeviceChildCode)}", "输送线手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == DeviceChildCode);
                if (stationManager != null)
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC);
                    if (device != null)
                    {
                        CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                        conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(3), stationManager.stationChildCode);
                        return content.OK("退回成功");
                    }
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent ConveyorLineCancel(string DeviceChildCode)
        {
            WriteLog.GetLog("输送线手动任务").Write($"【取消命令】{JsonConvert.SerializeObject(DeviceChildCode)}", "输送线手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == DeviceChildCode);
                if (stationManager != null)
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC);
                    if (device != null)
                    {
                        CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                        conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(4), stationManager.stationChildCode);
                        return content.OK("取消成功");
                    }
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent ConveyorLineInitialize(string DeviceChildCode)
        {
            WriteLog.GetLog("输送线手动任务").Write($"【初始化命令】{JsonConvert.SerializeObject(DeviceChildCode)}", "输送线手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StationManager stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == DeviceChildCode);
                if (stationManager != null)
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == stationManager.stationPLC);
                    if (device != null)
                    {
                        CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                        conveyorLine.SetValue(ConveyorLineDBName.WriteInteractiveSignal, Convert.ToSByte(6), stationManager.stationChildCode);
                        return content.OK("初始化成功");
                    }
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        string AlarmInfo(uint Alarm) => Alarm switch
        {
            0 => "急停",
            1 => "变频器报警1",
            2 => "变频器报警2",
            3 => "左超宽",
            4 => "右超宽",
            5 => "超高",
            6 => "读码失败",
            7 => "货物超重",
            8 => "运行超时",
            9 => "工位传感器异常",
            10 => "预留1",
            11 => "预留2",
            12 => "预留3",
            13 => "预留4",
            14 => "预留5",
            15 => "预留6",
            _ => $"未知报警({Alarm})"
        };
        public enum ConveyorLineDBName
        {
            InteractiveSignal,
            ConveyorLineBarcode,
            ConveyorLineAlarm,
            ConveyorLineTargetAddress,
            ConveyorLineTaskNum,
            WriteInteractiveSignal,
            WriteConveyorLineTargetAddress,
            WriteConveyorLineTaskNum,
        }
        public class ConveyorLineTaskCommand
        {
            public string InStock { get; set; }
            public string TaskNum { get; set; }
            public string Alarm { get; set; }
        }
        #endregion
        #region  RGV手动命令
        public WebResponseContent RGVHandTask(HandTask RGVhand)
        {
            WriteLog.GetLog("RGV手动任务").Write(JsonConvert.SerializeObject(RGVhand), "RGV手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (RGVhand == null || RGVhand.TaskType == "")
                {
                    return content.Error("请输入任务命令");
                }
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == RGVhand.DeviceCode);
                if (device != null)
                {
                    if (RGVhand.DeviceCode == "RGV01" || RGVhand.DeviceCode == "RGV02")
                    {
                        CommonRGV_FirstFloor RGVcommand = (CommonRGV_FirstFloor)device;
                        if (RGVcommand.GetValue<RGVDBName, bool>(RGVDBName.Automatic) && !RGVcommand.GetValue<RGVDBName, bool>(RGVDBName.Fault) && !RGVcommand.GetValue<RGVDBName, bool>(RGVDBName.Running))
                        {
                            if (RGVhand.TaskType == "1")
                            {
                                if (RGVhand.SourceAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PickupLocation = (byte)Convert.ToSByte(RGVhand.SourceAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandFirstFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}取定位命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}取定位命令取货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "2")
                            {
                                if (RGVhand.TargetAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PutcargoLocation = (byte)Convert.ToSByte(RGVhand.TargetAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandFirstFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}放定位命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}放定位命令放货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "3")
                            {
                                if (RGVhand.SourceAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PickupLocation = (byte)Convert.ToSByte(RGVhand.SourceAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandFirstFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}取货命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}取货命令取货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "4")
                            {
                                if (RGVhand.TargetAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PutcargoLocation = (byte)Convert.ToSByte(RGVhand.TargetAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandFirstFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}放货命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}放货命令放货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "5")
                            {
                                if (RGVhand.TargetAddress != null && RGVhand.TargetAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PickupLocation = (byte)Convert.ToSByte(RGVhand.SourceAddress),
                                        PutcargoLocation = (byte)Convert.ToSByte(RGVhand.TargetAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandFirstFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}取放货命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}取放货命令放货位和取货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "6")
                            {
                                if (RGVhand.TargetAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PutcargoLocation = (byte)Convert.ToSByte(RGVhand.TargetAddress),
                                        TaskType = (byte)Convert.ToSByte(4),
                                    };
                                    SendCommandFirstFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}退命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}退回命令放货位不能为空");
                                }
                            }
                            else
                            {
                                return content.Error($"{device.DeviceName}未知任务命令");
                            }
                        }
                        else
                        {
                            return content.Error($"请在联机模式、无故障、待机状态时再下发{RGVcommand.DeviceName}命令");
                        }
                    }
                    else
                    {
                        CommonRGV RGVcommand = (CommonRGV)device;
                        if (RGVcommand.GetValue<RGVDBName, bool>(RGVDBName.Automatic) && !RGVcommand.GetValue<RGVDBName, bool>(RGVDBName.Fault) && !RGVcommand.GetValue<RGVDBName, bool>(RGVDBName.Running))
                        {
                            if (RGVhand.TaskType == "1")
                            {
                                if (RGVhand.SourceAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PickupLocation = (byte)Convert.ToSByte(RGVhand.SourceAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandSecondFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}取定位命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}取定位命令取货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "2")
                            {
                                if (RGVhand.TargetAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PutcargoLocation = (byte)Convert.ToSByte(RGVhand.TargetAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandSecondFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}放定位命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}放定位命令放货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "3")
                            {
                                if (RGVhand.SourceAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PickupLocation = (byte)Convert.ToSByte(RGVhand.SourceAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandSecondFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}取货命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}取货命令取货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "4")
                            {
                                if (RGVhand.TargetAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PutcargoLocation = (byte)Convert.ToSByte(RGVhand.TargetAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandSecondFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}放货命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}放货命令放货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "5")
                            {
                                if (RGVhand.TargetAddress != null && RGVhand.TargetAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PickupLocation = (byte)Convert.ToSByte(RGVhand.SourceAddress),
                                        PutcargoLocation = (byte)Convert.ToSByte(RGVhand.TargetAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandSecondFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}取放货命令下发成功");
                                }
                                else
                                {
                                    return content.Error("取放货命令放货位和取货位不能为空");
                                }
                            }
                            else if (RGVhand.TaskType == "6")
                            {
                                if (RGVhand.TargetAddress != null)
                                {
                                    RGVTaskCommand command = new RGVTaskCommand()
                                    {
                                        PutcargoLocation = (byte)Convert.ToSByte(RGVhand.TargetAddress),
                                        TaskType = (byte)Convert.ToSByte(RGVhand.TaskType),
                                    };
                                    SendCommandSecondFloor(RGVcommand, command);
                                    return content.OK($"{device.DeviceName}退回命令下发成功");
                                }
                                else
                                {
                                    return content.Error($"{device.DeviceName}退回命令放货位不能为空");
                                }
                            }
                            else
                            {
                                return content.Error($"{device.DeviceName}未知任务命令");
                            }
                        }
                        else
                        {
                            return content.Error($"请在联机模式、无故障、待机状态时再下发{RGVcommand.DeviceName}命令");
                        }
                    }
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent RGVReset(string DeviceCode)
        {
            WriteLog.GetLog("RGV手动任务").Write($"【复位命令】{JsonConvert.SerializeObject(DeviceCode)}", "RGV手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode);
                if (device != null)
                {
                    if (DeviceCode == "RGV01" || DeviceCode == "RGV02")
                    {
                        CommonRGV_FirstFloor commonRGV = (CommonRGV_FirstFloor)device;
                        commonRGV.SetValue(RGVDBName.TaskType, Convert.ToSByte(9));
                    }
                    else
                    {
                        CommonRGV commonRGV = (CommonRGV)device;
                        commonRGV.SetValue(RGVDBName.TaskType, Convert.ToSByte(9));
                    }
                    return content.OK($"{device.DeviceName}复位成功");
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent RGVEmergencyStop(string DeviceCode)
        {
            WriteLog.GetLog("RGV手动任务").Write($"【急停命令】{JsonConvert.SerializeObject(DeviceCode)}", "RGV手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode);
                if (device != null)
                {
                    if (DeviceCode == "RGV01" || DeviceCode == "RGV02")
                    {
                        CommonRGV_FirstFloor commonRGV = (CommonRGV_FirstFloor)device;
                        commonRGV.SetValue(StackerCraneDBName.TaskType, Convert.ToSByte(11));
                    }
                    else
                    {
                        CommonRGV commonRGV = (CommonRGV)device;
                        commonRGV.SetValue(StackerCraneDBName.TaskType, Convert.ToSByte(11));
                    }
                    return content.OK($"{device.DeviceName}停止成功");
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent RGVCancel(string DeviceCode)
        {
            WriteLog.GetLog("RGV手动任务").Write($"【取消任务命令】{JsonConvert.SerializeObject(DeviceCode)}", "RGV手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode);
                if (device != null)
                {
                    if (DeviceCode == "RGV01" || DeviceCode == "RGV02")
                    {
                        CommonRGV_FirstFloor commonRGV = (CommonRGV_FirstFloor)device;
                        commonRGV.SetValue(StackerCraneDBName.TaskType, Convert.ToSByte(7));
                    }
                    else
                    {
                        CommonRGV commonRGV = (CommonRGV)device;
                        commonRGV.SetValue(StackerCraneDBName.TaskType, Convert.ToSByte(7));
                    }
                    return content.OK($"{device.DeviceName}中断成功");
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent RGVInitialize(string DeviceCode)
        {
            WriteLog.GetLog("RGV手动任务").Write($"【初始化命令】{JsonConvert.SerializeObject(DeviceCode)}", "RGV手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode);
                if (device != null)
                {
                    if (DeviceCode == "RGV01" || DeviceCode == "RGV02")
                    {
                        CommonRGV_FirstFloor commonRGV = (CommonRGV_FirstFloor)device;
                        DeviceProDTO? devicePro = commonRGV.DeviceProDTOs.Where(x => x.DeviceChildCode == commonRGV.DeviceCode && x.DeviceProParamName == "TaskType").FirstOrDefault();
                        //byte[] byt = Encoding.UTF8.GetBytes("08");
                        byte valueToWrite = 8;
                        byte[] buffer = new byte[] { valueToWrite };
                        commonRGV.Communicator.Write(devicePro.DeviceProAddress, buffer);
                    }
                    else
                    {
                        CommonRGV commonRGV = (CommonRGV)device;
                        DeviceProDTO? devicePro = commonRGV.DeviceProDTOs.Where(x => x.DeviceChildCode == commonRGV.DeviceCode && x.DeviceProParamName == "TaskType").FirstOrDefault();
                        byte valueToWrite = 8;
                        byte[] buffer = new byte[] { valueToWrite };
                        commonRGV.Communicator.Write(devicePro.DeviceProAddress, buffer);
                    }
                    return content.OK($"{device.DeviceName}初始化成功");
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public WebResponseContent RGVPause(string DeviceCode)
        {
            WriteLog.GetLog("RGV手动任务").Write($"【停止命令】{JsonConvert.SerializeObject(DeviceCode)}", "RGV手动任务");
            WebResponseContent content = new WebResponseContent();
            try
            {
                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == DeviceCode);
                if (device != null)
                {
                    if (DeviceCode == "RGV01" || DeviceCode == "RGV02")
                    {
                        CommonRGV_FirstFloor commonRGV = (CommonRGV_FirstFloor)device;
                        commonRGV.SetValue(StackerCraneDBName.TaskType, Convert.ToSByte(10));
                    }
                    else
                    {
                        CommonRGV commonRGV = (CommonRGV)device;
                        commonRGV.SetValue(StackerCraneDBName.TaskType, Convert.ToSByte(10));
                    }
                    return content.OK($"{device.DeviceName}暂停成功");
                }
                return content.Error("未找到设备信息请联系IT");
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
        }
        public void SendCommandFirstFloor(CommonRGV_FirstFloor commonRGV, RGVTaskCommand? rgvTaskCommand)
        {
            commonRGV.SetValue(StackerCraneDBName.TaskNum, Convert.ToInt16(1));
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.PutcargoLocation, (byte)Convert.ToSByte(rgvTaskCommand.PutcargoLocation));
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.PickupLocation, (byte)Convert.ToSByte(rgvTaskCommand.PickupLocation));
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.TaskType, Convert.ToSByte(rgvTaskCommand.TaskType));
        }
        public void SendCommandSecondFloor(CommonRGV commonRGV, RGVTaskCommand? rgvTaskCommand)
        {
            commonRGV.SetValue(StackerCraneDBName.TaskNum, Convert.ToInt16(1));
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.PutcargoLocation, (byte)Convert.ToSByte(rgvTaskCommand.PutcargoLocation));
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.PickupLocation, (byte)Convert.ToSByte(rgvTaskCommand.PickupLocation));
            Thread.Sleep(100);
            commonRGV.SetValue(StackerCraneDBName.TaskType, Convert.ToSByte(rgvTaskCommand.TaskType));
        }
        public class RGVTaskCommand
        {
            #region <Public Menber>
            /// <summary>
            /// å–货位
            /// </summary>
            public byte PickupLocation { get; set; }
            /// <summary>
            /// æ”¾è´§ä½
            /// </summary>
            public byte PutcargoLocation { get; set; }
            /// <summary>
            /// ä»»åŠ¡ç±»åž‹
            /// </summary>
            public byte TaskType { get; set; }
            /// <summary>
            /// é¢„ç•™
            /// </summary>
            public byte Spare { get; set; }
            /// <summary>
            /// ä»»åŠ¡å·
            /// </summary>
            public short TaskNum { get; set; }
            #endregion <Public Menber>
        }
        public enum RGVDBName
        {
            SendTask,
            Automatic,
            WorkCompleted,
            Running,
            Fault,
            InStock,
            RGVTargetAddress,
            RGVTaskNum,
            PickupLocation,
            PutcargoLocation,
            TaskType,
            TaskNum,
            LevelPoint,
            CurrentTaskNum,
            RGVAlarm,
        }
        #endregion
    }
}