Platfrom_CylinderHandler.cs 3 KB
using HaHRCS.Rcs.Executor.PLC;
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands.PlatformInteraction;
using Rcs.Domain.Repositories;
using Rcs.Executor.PLC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rcs.Infrastructure.MessageBus.Handlers.Commands.PLCInteraction
{
    public class Platfrom_CylinderHandler : IConsumer<GetCylinderStatu>
    {
        private readonly EquipmentExecutor _equipmentExecutor;
        private readonly ILogger<Platfrom_CylinderHandler> _logger;
        private readonly PlatformPickRepository _platformRepository;
        public Platfrom_CylinderHandler(
            ILogger<Platfrom_CylinderHandler> logger,
            PlatformPickRepository platformRepository,
            EquipmentExecutor equipmentExecutor)
        {
            _logger = logger;
            _platformRepository = platformRepository;
            _equipmentExecutor = equipmentExecutor;
        }
        public async Task Consume(ConsumeContext<GetCylinderStatu> context)
        {
            var command = context.Message;
            try
            {
                ValidateCommand(command, context);

                //Thread.Sleep(5000);
                //await context.RespondAsync(ApiResponse.Successful($"开门成功"));
                var equipment = _equipmentExecutor.equipment.First(t => t.Code == command.LocationCode);
                if (equipment == null || string.IsNullOrEmpty(equipment.Code))
                  await  context.RespondAsync(ApiResponse.ErrorToPlc("无效站台设备,请检查设备名称"));
                var isExtended1 = equipment[$"{CylinderProps.Retracted}"].Value;
                var istrue = equipment[$"{CylinderProps.Extended}"].Value;
                if (isExtended1 == "" && istrue == "")
                {
                    await context.RespondAsync(ApiResponse.ErrorToPlc("请检查设备是否离线,设备不在线"));
                }
                if (isExtended1 == "True")
                {
                    await context.RespondAsync(ApiResponse.SuccessfulToPlc("设备伸出状态"));
                }
                if (istrue == "True")
                {
                    await context.RespondAsync(ApiResponse.SuccessfulToPlc("设备缩回状态"));
                }
                //await HandlePickupCommand(command, equipment, context);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"处理查询指令异常,设备编码:{command?.LocationCode}");
                throw new InvalidOperationException(ex.Message);
            }
        }
        private void ValidateCommand(GetCylinderStatu command, ConsumeContext<GetCylinderStatu> context)
        {
            if (string.IsNullOrEmpty(command.LocationCode))
            {
                context.RespondAsync(ApiResponse.ErrorToPlc("请确认设备"));
                return;
            }
        }
    }
}