DigitalTwinController.cs 32.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
using HHECS.BllModel;
using HHECS.DAQServer.Dto.DigitalTwin;
using HHECS.DAQServer.Services;
using HHECS.DAQShared.Common.Enums;
using HHECS.DAQShared.Dto;
using HHECS.DAQShared.Models;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HHECS.DAQServer.Controllers
{
    /// <summary>
    /// 数字孪生接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DigitalTwinController : ControllerBase
    {
        private readonly IFreeSql _freeSql;
        private readonly CommonService _commonService;
        private readonly IDistributedCache _cache;

        public DigitalTwinController(IFreeSql freeSql, CommonService commonService, IDistributedCache cache)
        {
            _freeSql = freeSql;
            _commonService = commonService;
            _cache = cache;
        }

        /// <summary>
        /// 获取设备实时数据(Version 1)
        /// </summary>
        /// <param name="projectCode">项目编号</param>
        /// <param name="factoryCode">仓库编号</param>
        /// <param name="area">区域</param>
        /// <param name="displayAnnotation">显示注释</param>
        /// <remarks>通用设备数据查询</remarks>
        /// <returns></returns>
        [HttpGet("{projectCode}")]
        public async Task<BllResult<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>> GetEquipmentInfoV1(string projectCode, string factoryCode, string area = null, bool displayAnnotation = false)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(projectCode))
                {
                    return BllResultFactory.Error<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>($"项目编号[{nameof(projectCode)}]不能为空!");
                }

                if (string.IsNullOrWhiteSpace(factoryCode))
                {
                    return BllResultFactory.Error<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>($"仓库编号[{nameof(factoryCode)}]不能为空!");
                }

                //数据缓存1秒,避免前端重复请求
                var lastRequestCacheKey = $"{nameof(GetEquipmentInfoV1)}_{projectCode}_{factoryCode}_{area}_{displayAnnotation}";
                var lastRequestCacheOptions = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(1));
                var lastRequestCacheDataBytes = await _cache.GetAsync(lastRequestCacheKey);
                if (lastRequestCacheDataBytes != null)
                {
                    var lastRequestCacheData = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(Encoding.Default.GetString(lastRequestCacheDataBytes));
                    return BllResultFactory.Success(lastRequestCacheData);
                }

                var filter = PredicateBuilder.New<EquipmentExtend>(true);
                filter = filter.And(x => x.ProjectCode == projectCode);
                filter = filter.And(x => x.FactoryCode == factoryCode);
                if (!string.IsNullOrWhiteSpace(area))
                {
                    filter = filter.And(x => x.DestinationArea == area);
                }

                InitialEquipments(filter, lastRequestCacheKey, out var equipments, out var equipmentTypes);

                var data = equipmentTypes.ToDictionary(a => a.Code, b =>
                {
                    var equipmentTemps = equipments.Where(x => x.EquipmentTypeId == b.Id).ToList();
                    return equipmentTemps.ToDictionary(c => c.Code, d => d.EquipmentProps.ToDictionary(e => string.Concat(e.EquipmentTypePropTemplateCode[..1].ToLower(), e.EquipmentTypePropTemplateCode.AsSpan(1)), f => displayAnnotation ? f.EquipmentTypePropTemplate.Name : f.Value));
                });
                await _cache.SetAsync(lastRequestCacheKey, JsonSerializer.SerializeToUtf8Bytes(data), lastRequestCacheOptions);
                return BllResultFactory.Success(data);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(ex.Message);
            }
        }

        /// <summary>
        /// 获取设备实时数据(Version 2)
        /// </summary>
        /// <param name="projectCode">项目编号</param>
        /// <param name="factoryCode">仓库编号</param>
        /// <param name="area">区域</param>
        /// <remarks>定制化接口,新增设备类型需要单独开发</remarks>
        /// <returns></returns>
        [HttpGet("{projectCode}")]
        public async Task<BllResult<EquipmentInfoResponseV2Dto>> GetEquipmentInfoV2(string projectCode, string factoryCode, string area = null)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(projectCode))
                {
                    return BllResultFactory.Error<EquipmentInfoResponseV2Dto>($"项目编号[{nameof(projectCode)}]不能为空!");
                }

                if (string.IsNullOrWhiteSpace(factoryCode))
                {
                    return BllResultFactory.Error<EquipmentInfoResponseV2Dto>($"仓库编号[{nameof(factoryCode)}]不能为空!");
                }

                //数据缓存1秒,避免前端重复请求
                var lastRequestCacheKey = $"{nameof(GetEquipmentInfoV2)}_{projectCode}_{factoryCode}_{area}";
                var lastRequestCacheOptions = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(1));
                var lastRequestCacheDataBytes = await _cache.GetAsync(lastRequestCacheKey);
                if (lastRequestCacheDataBytes != null)
                {
                    var lastRequestCacheData = JsonSerializer.Deserialize<EquipmentInfoResponseV2Dto>(Encoding.Default.GetString(lastRequestCacheDataBytes));
                    return BllResultFactory.Success(lastRequestCacheData);
                }

                var filter = PredicateBuilder.New<EquipmentExtend>(true);
                filter = filter.And(x => x.ProjectCode == projectCode);
                filter = filter.And(x => x.FactoryCode == factoryCode);
                if (!string.IsNullOrWhiteSpace(area))
                {
                    filter = filter.And(x => x.DestinationArea == area);
                }

                InitialEquipments(filter, lastRequestCacheKey, out var equipments, out var equipmentTypes);

                if (equipments.Count == 0)
                {
                    return BllResultFactory.Error<EquipmentInfoResponseV2Dto>($"设备数据为空!");
                }

                var srmTypeCodes = new List<EquipmentTypeConst>
                {
                    EquipmentTypeConst.SingleForkSRM,
                    EquipmentTypeConst.SingleForkSRMV2,
                    EquipmentTypeConst.DoubleForkSRM,
                    EquipmentTypeConst.DoubleForkSRMV2,
                    EquipmentTypeConst.SingleForkSSRM,
                    EquipmentTypeConst.SingleForkSSRMV2,
                    EquipmentTypeConst.SingleForkSSRMV132,
                }.Select(x => x.ToString()).ToList();

                var data = new EquipmentInfoResponseV2Dto
                {
                    ProjectCode = projectCode,
                    FactoryCode = factoryCode,
                    Stacker = equipments.Where(x => srmTypeCodes.Contains(x.EquipmentType.Code)).ToDictionary(x => x.Code, srm =>
                    {
                        var (mainAlarm, detailAlarms) = _commonService.GetEquipmentAlarmInfo(srm);

                        _ = Enum.TryParse<SRMOperationModel>(srm[SRMProps.OperationModel.ToString()].Value, out var operationModel);

                        _ = int.TryParse(srm[SRMProps.HorizontalDistance.ToString()].Value, out var horizontalDistance);
                        _ = int.TryParse(srm[SRMProps.VerticalDistance.ToString()].Value, out var verticalDistance);
                        _ = int.TryParse(srm[SRMProps.ManageSmallColumn.ToString()].Value, out var manageSmallColumn);
                        _ = int.TryParse(srm[SRMProps.ManageMaxColumn.ToString()].Value, out var manageMaxColumn);
                        _ = int.TryParse(srm[SRMProps.CurrentColumn.ToString()].Value, out var currentColumn);

                        //货叉1
                        _ = int.TryParse(srm[SRMProps.Fork1Distance.ToString()].Value, out var fork1Distance);
                        _ = int.TryParse(srm[SRMProps.Fork1XPosition.ToString()].Value, out var fork1XPosition);
                        _ = int.TryParse(srm[SRMProps.Fork1YPosition.ToString()].Value, out var fork1YPosition);
                        _ = int.TryParse(srm[SRMProps.Fork1ZPosition.ToString()].Value, out var fork1ZPosition);

                        _ = bool.TryParse(srm[SRMProps.Fork1HasPallet.ToString()].Value, out var fork1HasPallet);
                        _ = bool.TryParse(srm[SRMProps.Fork1Center.ToString()].Value, out var fork1Center);
                        _ = bool.TryParse(srm[SRMProps.Fork1Block.ToString()].Value, out var fork1Block);

                        _ = int.TryParse(srm[SRMProps.Fork1TaskNo.ToString()].Value, out var fork1TaskNo);
                        _ = int.TryParse(srm[SRMProps.Fork1TaskExcuteStatus.ToString()].Value, out var fork1TaskExcuteStatus);
                        _ = int.TryParse(srm[SRMProps.Fork1TaskType.ToString()].Value, out var fork1TaskType);
                        _ = int.TryParse(srm[SRMProps.Fork1PickXPosition.ToString()].Value, out var fork1PickXPosition);
                        _ = int.TryParse(srm[SRMProps.Fork1PickYPosition.ToString()].Value, out var fork1PickYPosition);
                        _ = int.TryParse(srm[SRMProps.Fork1PickZPosition.ToString()].Value, out var fork1PickZPosition);
                        _ = int.TryParse(srm[SRMProps.Fork1PutXPosition.ToString()].Value, out var fork1PutXPosition);
                        _ = int.TryParse(srm[SRMProps.Fork1PutYPosition.ToString()].Value, out var fork1PutYPosition);
                        _ = int.TryParse(srm[SRMProps.Fork1PutZPosition.ToString()].Value, out var fork1PutZPosition);
                        var fork1GoodsBarcode = srm[SRMProps.Fork1GoodsBarcode.ToString()].Value;

                        var stackerDto = new StackerDto
                        {
                            EquipmentName = srm.Name,
                            EquipmentTypeCode = srm.EquipmentType.Code,

                            Number = srm[SRMProps.Number.ToString()].Value,
                            Status = _commonService.GetEquipmentStatus(srm).ToString(),
                            OperationModel = operationModel,
                            HorizontalDistance = horizontalDistance,
                            VerticalDistance = verticalDistance,
                            ManageSmallColumn = manageSmallColumn,
                            ManageMaxColumn = manageMaxColumn,
                            CurrentColumn = currentColumn,

                            Fork1Distance = fork1Distance,
                            Fork1XPosition = fork1XPosition,
                            Fork1YPosition = fork1YPosition,
                            Fork1ZPosition = fork1ZPosition,
                            Fork1HasPallet = fork1HasPallet,
                            Fork1Center = fork1Center,
                            Fork1Block = fork1Block,
                            Fork1TaskNo = fork1TaskNo,
                            Fork1TaskExcuteStatus = fork1TaskExcuteStatus,
                            Fork1TaskType = fork1TaskType,

                            Fork1PickXPosition = fork1PickXPosition,
                            Fork1PickYPosition = fork1PickYPosition,
                            Fork1PickZPosition = fork1PickZPosition,
                            Fork1PutXPosition = fork1PutXPosition,
                            Fork1PutYPosition = fork1PutYPosition,
                            Fork1PutZPosition = fork1PutZPosition,
                            Fork1GoodsBarcode = fork1GoodsBarcode,
                            MainAlarm = mainAlarm,
                            DetailAlarms = detailAlarms,
                        };

                        //堆垛机货叉1有货状态转换
                        var fork1SpareProp = srm.EquipmentProps.Where(x => x.EquipmentTypePropTemplateCode == "Fork1Spare").FirstOrDefault();
                        if (fork1SpareProp != null && !fork1Center)
                        {
                            //伸叉标志为1,缩叉标志为2,其他条件为0
                            _ = int.TryParse(fork1SpareProp.Value, out var fork1SpareValue);
                            //取货
                            if (fork1TaskType == 2 && fork1SpareValue == 2)
                            {
                                stackerDto.Fork1HasPallet = true;
                            }
                            //放货
                            else if (fork1TaskType == 3 && fork1SpareValue == 2)
                            {
                                stackerDto.Fork1HasPallet = false;
                            }
                        }

                        //堆垛机货叉1任务目标位置
                        if (fork1TaskType == (int)SRMForkAction.Walk || fork1TaskType == (int)SRMForkAction.Get)
                        {
                            stackerDto.Fork1NextXPosition = stackerDto.Fork1PickXPosition;
                            stackerDto.Fork1NextYPosition = stackerDto.Fork1PickYPosition;
                            stackerDto.Fork1NextZPosition = stackerDto.Fork1PickZPosition;
                        }
                        else if (fork1TaskType == (int)SRMForkAction.Put)
                        {
                            stackerDto.Fork1NextXPosition = stackerDto.Fork1PutXPosition;
                            stackerDto.Fork1NextYPosition = stackerDto.Fork1PutYPosition;
                            stackerDto.Fork1NextZPosition = stackerDto.Fork1PutZPosition;
                        }
                        else
                        {
                            stackerDto.Fork1NextXPosition = stackerDto.Fork1XPosition;
                            stackerDto.Fork1NextYPosition = stackerDto.Fork1YPosition;
                            stackerDto.Fork1NextZPosition = stackerDto.Fork1ZPosition;
                        }

                        var doubleForkSRMTypes = new List<EquipmentTypeConst>
                        {
                            EquipmentTypeConst.DoubleForkSRM,
                            EquipmentTypeConst.DoubleForkSRMV2,
                        }.Select(x => x.ToString()).ToList();

                        //双叉堆垛机
                        if (doubleForkSRMTypes.Contains(srm.EquipmentType.Code))
                        {
                            _ = int.TryParse(srm[SRMProps.Fork2Distance.ToString()].Value, out var fork2Distance);
                            _ = int.TryParse(srm[SRMProps.Fork2XPosition.ToString()].Value, out var fork2XPosition);
                            _ = int.TryParse(srm[SRMProps.Fork2YPosition.ToString()].Value, out var fork2YPosition);
                            _ = int.TryParse(srm[SRMProps.Fork2ZPosition.ToString()].Value, out var fork2ZPosition);

                            _ = bool.TryParse(srm[SRMProps.Fork2HasPallet.ToString()].Value, out var fork2HasPallet);
                            _ = bool.TryParse(srm[SRMProps.Fork2Center.ToString()].Value, out var fork2Center);
                            _ = bool.TryParse(srm[SRMProps.Fork2Block.ToString()].Value, out var fork2Block);

                            _ = int.TryParse(srm[SRMProps.Fork2TaskExcuteStatus.ToString()].Value, out var fork2TaskExcuteStatus);
                            _ = int.TryParse(srm[SRMProps.Fork2TaskType.ToString()].Value, out var fork2TaskType);
                            _ = int.TryParse(srm[SRMProps.Fork2PickXPosition.ToString()].Value, out var fork2PickXPosition);
                            _ = int.TryParse(srm[SRMProps.Fork2PickYPosition.ToString()].Value, out var fork2PickYPosition);
                            _ = int.TryParse(srm[SRMProps.Fork2PickZPosition.ToString()].Value, out var fork2PickZPosition);
                            _ = int.TryParse(srm[SRMProps.Fork2PutXPosition.ToString()].Value, out var fork2PutXPosition);
                            _ = int.TryParse(srm[SRMProps.Fork2PutYPosition.ToString()].Value, out var fork2PutYPosition);
                            _ = int.TryParse(srm[SRMProps.Fork2PutZPosition.ToString()].Value, out var fork2PutZPosition);
                            _ = int.TryParse(srm[SRMProps.Fork2TaskNo.ToString()].Value, out var fork2TaskNo);
                            var fork2GoodsBarcode = srm[SRMProps.Fork2GoodsBarcode.ToString()].Value;
                            stackerDto.ForkCount = 2;
                            stackerDto.Fork2Distance = fork2Distance;
                            stackerDto.Fork2XPosition = fork2XPosition;
                            stackerDto.Fork2YPosition = fork2YPosition;
                            stackerDto.Fork2ZPosition = fork2ZPosition;
                            stackerDto.Fork2HasPallet = fork2HasPallet;
                            stackerDto.Fork2Center = fork2Center;
                            stackerDto.Fork2Block = fork2Block;
                            stackerDto.Fork2TaskNo = fork2TaskNo;
                            stackerDto.Fork2TaskExcuteStatus = fork2TaskExcuteStatus;
                            stackerDto.Fork2TaskType = fork2TaskType;

                            stackerDto.Fork2PickXPosition = fork2PickXPosition;
                            stackerDto.Fork2PickYPosition = fork2PickYPosition;
                            stackerDto.Fork2PickZPosition = fork2PickZPosition;
                            stackerDto.Fork2PutXPosition = fork2PutXPosition;
                            stackerDto.Fork2PutYPosition = fork2PutYPosition;
                            stackerDto.Fork2PutZPosition = fork2PutZPosition;
                            stackerDto.Fork2GoodsBarcode = fork2GoodsBarcode;

                            //堆垛机货叉2任务目标位置
                            if (fork2TaskType == (int)SRMForkAction.Walk || fork2TaskType == (int)SRMForkAction.Get)
                            {
                                stackerDto.Fork2NextXPosition = stackerDto.Fork2PickXPosition;
                                stackerDto.Fork2NextYPosition = stackerDto.Fork2PickYPosition;
                                stackerDto.Fork2NextZPosition = stackerDto.Fork2PickZPosition;
                            }
                            else if (fork2TaskType == (int)SRMForkAction.Put)
                            {
                                stackerDto.Fork2NextXPosition = stackerDto.Fork2PutXPosition;
                                stackerDto.Fork2NextYPosition = stackerDto.Fork2PutYPosition;
                                stackerDto.Fork2NextZPosition = stackerDto.Fork2PutZPosition;
                            }
                            else
                            {
                                stackerDto.Fork2NextXPosition = stackerDto.Fork2XPosition;
                                stackerDto.Fork2NextYPosition = stackerDto.Fork2YPosition;
                                stackerDto.Fork2NextZPosition = stackerDto.Fork2ZPosition;
                            }

                            //堆垛机货2有货状态转换
                            var fork2SpareProp = srm.EquipmentProps.Where(x => x.EquipmentTypePropTemplateCode == "Fork2Spare").FirstOrDefault();
                            if (fork2SpareProp != null && !fork2Center)
                            {
                                //伸叉标志为1,缩叉标志为2,其他条件为0
                                _ = int.TryParse(fork2SpareProp.Value, out var fork2SpareValue);
                                //取货
                                if (fork2TaskType == 2 && fork2SpareValue == 2)
                                {
                                    stackerDto.Fork2HasPallet = true;
                                }
                                //放货
                                else if (fork2TaskType == 3 && fork2SpareValue == 2)
                                {
                                    stackerDto.Fork2HasPallet = false;
                                }
                            }
                        }

                        return stackerDto;
                    }),
                    RgvStations = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.RGVStation.ToString()).ToDictionary(x => x.Code, y =>
                    {
                        var (mainAlarm, detailAlarms) = _commonService.GetEquipmentAlarmInfo(y);
                        _ = int.TryParse(y[RGVStationProps.StationRGVPosition.ToString()].Value, out var stationRGVPosition);
                        _ = int.TryParse(y[RGVStationProps.StationRGVSmall.ToString()].Value, out var stationRGVSmall);
                        _ = int.TryParse(y[RGVStationProps.StationRGVMax.ToString()].Value, out var stationRGVMax);
                        _ = int.TryParse(y[RGVStationProps.StationRGVDestination.ToString()].Value, out var stationRGVDestination);
                        _ = int.TryParse(y[RGVStationProps.StationRGVActualPositionID.ToString()].Value, out var StationRGVActualPositionID);

                        var rgvStation = new RGVStationDto
                        {
                            Status = _commonService.GetEquipmentStatus(y).ToString(),
                            StationRGVPosition = stationRGVDestination,
                            StationRGVSmall = stationRGVSmall,
                            StationRGVMax = stationRGVMax,
                            StationRGVDestination = stationRGVDestination,
                            StationRGVActualPositionID = StationRGVActualPositionID,
                            MainAlarm = mainAlarm,
                            DetailAlarms = detailAlarms
                        };

                        return rgvStation;
                    }),
                    Agv = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.AGVForklift.ToString()).ToDictionary(x => x.Code, y => new AGVDto
                    {
                        EquipmentName = y.Name,
                        EquipmentTypeCode = y.EquipmentType.Code,
                        Status = _commonService.GetEquipmentStatus(y).ToString(),
                    }),
                    Stations = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.StationMonitor.ToString()).ToDictionary(x => x.Code, y =>
                    {
                        _ = int.TryParse(y[StationProps.StationDestination.ToString()].Value, out var stationDestination);
                        var stationMonitorOccupied = y[StationProps.StationMonitorOccupied.ToString()].Value == "1";

                        var stationCorotation = false;
                        var stationReverse = false;
                        if (y.EquipmentProps.Exists(x => x.EquipmentTypePropTemplateCode == StationProps.StationCorotation.ToString()))
                        {
                            _ = bool.TryParse(y[StationProps.StationCorotation.ToString()].Value, out stationCorotation);
                        }

                        if (y.EquipmentProps.Exists(x => x.EquipmentTypePropTemplateCode == StationProps.StationReverse.ToString()))
                        {
                            _ = bool.TryParse(y[StationProps.StationReverse.ToString()].Value, out stationReverse);
                        }

                        var station = new StationDto
                        {
                            EquipmentName = y.Name,
                            EquipmentTypeCode = y.EquipmentType.Code,
                            Status = _commonService.GetEquipmentStatus(y).ToString(),
                            StationMonitorBarcode = y[StationProps.StationMonitorBarcode.ToString()].Value,
                            StationMonitorOccupied = stationMonitorOccupied,
                            StationDestination = stationDestination,
                            StationCorotation = stationCorotation,
                            StationReverse = stationReverse
                        };
                        return station;
                    }),
                    WeldRobots = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.WeldRobot.ToString()).ToDictionary(x => x.Code, y =>
                    {
                        var weldRobotDto = new WeldRobotDto
                        {
                            EquipmentName = y.Name,
                            EquipmentTypeCode = y.EquipmentType.Code,
                            Status = _commonService.GetEquipmentStatus(y).ToString(),
                        };
                        return weldRobotDto;
                    }),
                    Hoists = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.Hoist.ToString()).ToDictionary(x => x.Code, y =>
                    {
                        _ = int.TryParse(y[HoistProps.HoistStatus.ToString()].Value, out var hoistStatus);
                        _ = int.TryParse(y[HoistProps.P1002ActualPositionID.ToString()].Value, out var actualPositionID);
                        var (mainAlarm, detailAlarms) = _commonService.GetEquipmentAlarmInfo(y);
                        var hoistDto = new HoistDto
                        {
                            EquipmentName = y.Name,
                            EquipmentTypeCode = y.EquipmentType.Code,
                            Status = _commonService.GetEquipmentStatus(y).ToString(),
                            HoistStatus = hoistStatus,
                            ActualPositionID = actualPositionID,
                            MainAlarm = mainAlarm,
                            DetailAlarms = detailAlarms
                        };
                        return hoistDto;
                    })
                };
                await _cache.SetAsync(lastRequestCacheKey, JsonSerializer.SerializeToUtf8Bytes(data), lastRequestCacheOptions);
                return BllResultFactory.Success(data);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<EquipmentInfoResponseV2Dto>(ex.Message);
            }
        }

        /// <summary>
        /// 初始化设备数据
        /// </summary>
        /// <param name="filter">数据过滤条件</param>
        /// <param name="cacheKey">数据过滤条件</param>
        /// <param name="equipments">设备数据</param>
        /// <param name="equipmentTypes">设备类型</param>
        /// <returns></returns>
        private void InitialEquipments(ExpressionStarter<EquipmentExtend> filter, string cacheKey, out List<EquipmentExtend> equipments, out List<EquipmentTypeExtend> equipmentTypes)
        {
            var jsonSerializerOptions = new JsonSerializerOptions
            {
                ReferenceHandler = ReferenceHandler.Preserve,
                WriteIndented = true
            };

            var equipmentCacheKey = $"Equipment_{cacheKey}";
            var equipmentTypeCacheKey = $"EquipmentType_{cacheKey}";

            //缓存10分钟,这里的缓存时间不能超过设备数据接收接口的缓存时间
            var cacheOptions = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(10));
            var equipmentCacheDataBytes = _cache.Get(equipmentCacheKey);
            var equipmentTypeCacheDataBytes = _cache.Get(equipmentTypeCacheKey);
            if (equipmentCacheDataBytes != null && equipmentTypeCacheDataBytes != null)
            {
                equipments = JsonSerializer.Deserialize<List<EquipmentExtend>>(Encoding.Default.GetString(equipmentCacheDataBytes), jsonSerializerOptions);
                equipmentTypes = JsonSerializer.Deserialize<List<EquipmentTypeExtend>>(Encoding.Default.GetString(equipmentTypeCacheDataBytes), jsonSerializerOptions);
                LoadCacheData(ref equipments);
                return;
            }

            using var equipmentTypeRepository = _freeSql.GetRepository<EquipmentTypeExtend>();
            using var equipmentRepository = _freeSql.GetRepository<EquipmentExtend>();
            using var equipmentPropRepository = _freeSql.GetRepository<EquipmentPropExtend>();
            using var equipmentTypePropTemplateRepository = _freeSql.GetRepository<EquipmentTypePropTemplateExtend>();

            equipments = equipmentRepository.Where(filter).WithLock(SqlServerLock.NoLock).ToList(x => new EquipmentExtend
            {
                Id = x.Id,
                Code = x.Code,
                Name = x.Name,
                EquipmentTypeId = x.EquipmentTypeId,
                ConnectName = x.ConnectName,
            });

            var equipmentIds = equipments.Select(x => x.Id).ToList();
            var equipmentProps = equipmentPropRepository.Where(x => equipmentIds.Contains(x.EquipmentId)).WithLock(SqlServerLock.NoLock).ToList(x => new EquipmentPropExtend
            {
                Id = x.Id,
                EquipmentTypePropTemplateCode = x.EquipmentTypePropTemplateCode,
                EquipmentTypePropTemplateId = x.EquipmentTypePropTemplateId,
                EquipmentId = x.EquipmentId,
                Address = x.Address,
                Value = x.Value,
                Updated = x.Updated,
            });

            var equipmentTypePropTemplateIds = equipmentProps.Select(x => x.EquipmentTypePropTemplateId).Distinct().ToList();
            var equipmentTypePropTemplates = equipmentTypePropTemplateRepository.Where(x => equipmentTypePropTemplateIds.Contains(x.Id))
                .WithLock(SqlServerLock.NoLock).ToList(x => new EquipmentTypePropTemplateExtend
                {
                    Id = x.Id,
                    Code = x.Code,
                    Name = x.Name,
                    PropType = x.PropType,
                    MonitorCompareValue = x.MonitorCompareValue,
                    MonitorFailure = x.MonitorFailure,
                });

            var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
            equipmentTypes = equipmentTypeRepository.Where(x => equipmentTypeIds.Contains(x.Id))
               .WithLock(SqlServerLock.NoLock).ToList(x => new EquipmentTypeExtend
               {
                   Id = x.Id,
                   Code = x.Code,
                   Name = x.Name,
               });

            foreach (var item in equipments)
            {
                item.EquipmentType = equipmentTypes.Find(x => x.Id == item.EquipmentTypeId);
                item.EquipmentProps = equipmentProps.Where(x => x.EquipmentId == item.Id).ToList();
                foreach (var propItem in item.EquipmentProps)
                {
                    propItem.Equipment = item;
                    propItem.EquipmentTypePropTemplate = equipmentTypePropTemplates.Find(x => x.Id == propItem.EquipmentTypePropTemplateId);
                }
            }
            LoadCacheData(ref equipments);
            _cache.Set(equipmentCacheKey, JsonSerializer.SerializeToUtf8Bytes(equipments, jsonSerializerOptions), cacheOptions);
            _cache.Set(equipmentTypeCacheKey, JsonSerializer.SerializeToUtf8Bytes(equipmentTypes, jsonSerializerOptions), cacheOptions);
        }

        /// <summary>
        /// 加载缓存数据并组装
        /// </summary>
        /// <param name="equipments">设备数据</param>
        private void LoadCacheData(ref List<EquipmentExtend> equipments)
        {
            //从缓存取值
            foreach (var item in equipments)
            {
                var cacheDataBytes = _cache.Get(item.Code);
                if (cacheDataBytes == null)
                {
                    continue;
                }
                var cacheData = JsonSerializer.Deserialize<EquipmentDataDto>(Encoding.Default.GetString(cacheDataBytes));
                var cacheDataSourceTime = _commonService.TimestampConvertToLocalDateTime(cacheData.Timestamp);
                foreach (var tagItem in cacheData.Reported)
                {
                    var propItem = item.EquipmentProps.Where(x => x.EquipmentTypePropTemplateCode == tagItem.Tag || x.Address == tagItem.Tag).FirstOrDefault();
                    if (propItem != null && cacheDataSourceTime > propItem.Updated)
                    {
                        propItem.Value = tagItem.Value;
                        propItem.Updated = cacheDataSourceTime;
                    }
                }
            }
        }
    }
}