EquipmentController.cs 19 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
using HHECS.BllModel;
using HHECS.DAQServer.Dto.Equipment;
using HHECS.DAQServer.Services;
using HHECS.DAQShared.Common.Enums;
using HHECS.DAQShared.Dto;
using HHECS.DAQShared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System.Text;
using System.Text.Json;

namespace HHECS.DAQServer.Controllers
{
    /// <summary>
    /// 设备数据
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EquipmentController : ControllerBase
    {
        private readonly IFreeSql _freeSql;
        private readonly DataCacheService _dataCacheService;
        private readonly CommonService _commonService;
        private readonly IDistributedCache _cache;

        /// <summary>
        ///小于此时间,认为是无效数据,ORM框架有限制,应与实体类配置保持一致
        /// </summary>
        private readonly DateTime minStartTime;

        public EquipmentController(IFreeSql freeSql, DataCacheService dataCacheService, CommonService commonService, IDistributedCache cache)
        {
            _freeSql = freeSql;
            _dataCacheService = dataCacheService;
            _commonService = commonService;
            _cache = cache;
            minStartTime = DateTime.Parse("2024-6-11");
        }

        /// <summary>
        /// 推送设备实时数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BllResult> SendEquipmentData(IEnumerable<EquipmentDataDto> data)
        {
            try
            {
                if (!data.Any())
                {
                    return BllResultFactory.Error($"数据不能为空!");
                }

                if (data.Where(x => x.Version == 0).Any())
                {
                    return BllResultFactory.Error($"Vesion不能为“0”");
                }

                if (data.Where(x => _commonService.TimestampConvertToLocalDateTime(x.Timestamp) == default).Any())
                {
                    return BllResultFactory.Error($"时间戳“Timestamp”字段数据不正确,请参考 https://tool.lu/timestamp");
                }

                //缓存配置
                var options = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10));

                //缓存设备实时数据
                foreach (var record in data.GroupBy(x => x.EquipmentSN))
                {
                    //获取最新时间戳的数据
                    var lastItem = record.OrderByDescending(x => x.Timestamp).First();

                    //获取当前缓存数据
                    var cacheDataBytes = await _cache.GetAsync(record.Key);
                    if (cacheDataBytes != null)
                    {
                        //设备状态数据队列
                        _dataCacheService.EquipmentStatusDictionary.AddOrUpdate(record.Key, lastItem, (key, oldValue) =>
                        {
                            if (lastItem.Timestamp > oldValue.Timestamp)
                            {
                                return lastItem;
                            }
                            return oldValue;
                        });

                        //数据缓存
                        var cacheData = JsonSerializer.Deserialize<EquipmentDataDto>(Encoding.Default.GetString(cacheDataBytes));
                        if (cacheData.Timestamp >= lastItem.Timestamp)
                        {
                            continue;
                        }
                    }

                    var encodedCurrentData = JsonSerializer.SerializeToUtf8Bytes(lastItem);
                    await _cache.SetAsync(record.Key, encodedCurrentData, options);
                }
                var maxCacheCount = _commonService.GetMaxCacheCount();

                //缓存队列超过设定值,暂时不记录到队列,降低数据丢失的风险
                var currentQueueCount = _dataCacheService.EquipmentDataRecordQueue.Count;
                if (currentQueueCount >= maxCacheCount)
                {
                    return BllResultFactory.Error($"数据队列缓存超过设定值({maxCacheCount}),待当前队列数据({currentQueueCount})处理完成后才能继续");
                }

                var records = data.Where(x =>
                {
                    var time = _commonService.TimestampConvertToLocalDateTime(x.Timestamp);
                    if (time == default)
                    {
                        return false;
                    }

                    if (time < minStartTime)
                    {
                        return false;
                    }
                    return true;
                }).OrderBy(x => x.Timestamp).ToList();

                //加入缓存队列,存入数据库
                foreach (var item in records)
                {
                    _dataCacheService.EquipmentDataRecordQueue.Enqueue(item);
                }

                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        /// <summary>
        /// 推送某段时间的设备数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BllResult> SendEquipmentDataV2(IEnumerable<EquipmentDataV2Dto> data)
        {
            try
            {
                var maxCacheCount = _commonService.GetMaxCacheCount();
                var currentQueueCount = _dataCacheService.EquipmentDataRecordQueue.Count;
                if (currentQueueCount >= maxCacheCount)
                {
                    return BllResultFactory.Error($"数据队列缓存超过设定值[{maxCacheCount}],待当前队列数据[{currentQueueCount}]处理完成后才能继续");
                }

                foreach (var item in data.OrderBy(x => x.EquipmentSN).ThenBy(x => x.TimestampStart))
                {
                    var startTime = _commonService.TimestampConvertToLocalDateTime(item.TimestampStart);
                    var endTime = _commonService.TimestampConvertToLocalDateTime(item.TimestampEnd);
                    //无效时间
                    if (startTime <= minStartTime || startTime == default || endTime == default)
                    {
                        continue;
                    }

                    var nextStartTime = startTime;
                    //3秒间隔,分割数据
                    const int seccond = 3;
                    EquipmentDataDto lastRecord;
                    do
                    {
                        var currentTime = nextStartTime < endTime ? nextStartTime : endTime;
                        DateTimeOffset localTime = DateTime.SpecifyKind(currentTime, DateTimeKind.Local);
                        var timestamp = localTime.ToUnixTimeMilliseconds();
                        var record = new EquipmentDataDto
                        {
                            Plmeid = item.Plmeid,
                            EquipmentSN = item.EquipmentSN,
                            Reported = item.Reported,
                            Version = item.Version,
                            Timestamp = timestamp,
                        };
                        _dataCacheService.EquipmentDataRecordQueue.Enqueue(record);
                        nextStartTime = nextStartTime.AddSeconds(seccond);
                        lastRecord = new EquipmentDataDto
                        {
                            Plmeid = item.Plmeid,
                            EquipmentSN = item.EquipmentSN,
                            Reported = item.Reported,
                            Timestamp = timestamp,
                            Version = item.Version,
                        };
                    } while (nextStartTime <= endTime.AddSeconds(seccond));

                    if (lastRecord != null)
                    {
                        _dataCacheService.EquipmentStatusDictionary.AddOrUpdate(item.EquipmentSN, lastRecord, (key, oldValue) =>
                        {
                            if (lastRecord.Timestamp > oldValue.Timestamp)
                            {
                                return lastRecord;
                            }
                            return oldValue;
                        });
                    }
                }
                await Task.Delay(1);
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        /// <summary>
        /// 推送设备任务完成数
        /// </summary>
        /// <param name="taskInfos"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BllResult> EquipmentTask(List<EquipmentTaskDto> taskInfos)
        {
            try
            {
                if (taskInfos.Count == 0)
                {
                    return BllResultFactory.Error($"数据不能为空!");
                }

                var equipmentCodes = taskInfos.Select(x => x.EquipmentCode).Distinct().ToList();
                var equipments = await _freeSql.Queryable<Equipment>().Where(x => equipmentCodes.Contains(x.Code)).ToListAsync(x => new Equipment
                {
                    Code = x.Code,
                    Name = x.Name,
                    EquipmentTypeId = x.EquipmentTypeId,
                    FactoryCode = x.FactoryCode,
                    ProjectCode = x.ProjectCode,
                });

                if (equipmentCodes.Count != equipments.Count)
                {
                    var temps = equipmentCodes.Except(equipments.Select(x => x.Code));
                    return BllResultFactory.Error($"设备编码[{string.Join(',', temps)}]不存在,请检查数据并重试!");
                }

                var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
                var equipmentTypes = _freeSql.Queryable<EquipmentType>().Where(x => equipmentTypeIds.Contains(x.Id)).ToList(x => new EquipmentType
                {
                    Id = x.Id,
                    Code = x.Code,
                });

                using var equipmentTaskRepository = _freeSql.GetRepository<EquipmentTaskInfo>();
                var addTemps = new List<EquipmentTaskInfo>();
                var updateTemps = new List<EquipmentTaskInfo>();
                foreach (var item in taskInfos)
                {
                    var record = equipmentTaskRepository.Where(x => x.EquipmentCode == item.EquipmentCode && x.Created.Value.Date == item.DateTime.Date).First();
                    //更新
                    if (record != null)
                    {
                        equipmentTaskRepository.Attach(record);
                        record.TotalTask = item.TotalTask;
                        record.Updated = item.DateTime;
                        updateTemps.Add(record);
                        continue;
                    }
                    //新增
                    var currentEquipment = equipments.Where(x => x.Code == item.EquipmentCode).First();
                    var currentEquipmentTypeCode = equipmentTypes.Where(x => x.Id == currentEquipment.EquipmentTypeId).Select(x => x.Code).FirstOrDefault();
                    addTemps.Add(new EquipmentTaskInfo
                    {
                        EquipmentCode = currentEquipment.Code,
                        EquipmentName = currentEquipment.Name,
                        EquipmentTypeCode = currentEquipmentTypeCode,
                        ProjectCode = currentEquipment.ProjectCode,
                        FactoryCode = currentEquipment.FactoryCode,
                        TotalTask = item.TotalTask,
                        Created = item.DateTime,
                    });
                }

                var updateTotal = equipmentTaskRepository.Update(updateTemps);
                var addTotal = equipmentTaskRepository.Insert(addTemps);
                return BllResultFactory.Success($"操作成功,新增{addTotal.Count}条记录,更新{updateTotal}条记录");
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"程序异常:{ex.Message}");
            }
        }

        /// <summary>
        /// 更新客户端在线状态
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BllResult> UpdateClientStatus(ClientStatusDto data)
        {
            try
            {
                var newValueToAdd = DateTime.Now;
                var clientStatusRepository = _freeSql.GetRepository<ClientStatus>();
                if (!await clientStatusRepository.Where(x => x.ClientKeys == data.ClientId).AnyAsync())
                {
                    return BllResultFactory.Error($"客户端标识:{data.ClientId}不存在,请查验后再试!");
                }
                //添加到队列,统一更新
                _dataCacheService.ClientStatusDictionary.AddOrUpdate(data.ClientId, newValueToAdd, (key, oldValue) => newValueToAdd);
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        /// <summary>
        /// 更新AGV点位信息
        /// </summary>
        /// <param name="agvPoints"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BllResult> UpdateAGVPoint(List<AGVPointDto> agvPoints)
        {
            try
            {
                if (agvPoints.Count == 0)
                {
                    return BllResultFactory.Error($"数据不能为空!");
                }

                if (agvPoints.Where(x => string.IsNullOrWhiteSpace(x.EquipmentSN)).Any())
                {
                    return BllResultFactory.Error($"EquipmentSN数据不能为空");
                }

                var equipmentSNs = agvPoints.Select(x => x.EquipmentSN).ToList();
                var equipments = await _freeSql.Queryable<Equipment>().Where(x => equipmentSNs.Contains(x.Code)).ToListAsync(x => new Equipment
                {
                    Id = x.Id,
                    Code = x.Code,
                    ProjectCode = x.ProjectCode,
                    FactoryCode = x.FactoryCode,
                });

                var temp = equipmentSNs.Except(equipments.Select(x => x.Code)).Distinct();
                if (temp.Any())
                {
                    var str = string.Join(',', temp);
                    return BllResultFactory.Error($"设备SN[{str}]在系统中不存在,请检测数据是否正确,或联系管理员获取!");
                }

                var allPoints = _freeSql.Queryable<AGVPoint>().Where(x => equipmentSNs.Contains(x.EquipmentCode)).ToList();

                foreach (var item in agvPoints)
                {
                    //数据库现有数据
                    var points = allPoints.Where(x => x.EquipmentCode == item.EquipmentSN).ToList();
                    var oldBarCodes = points.Select(x => x.BarCode).ToList();
                    var newBarCodes = item.BarCodes;

                    var currentEquipment = equipments.Where(x => x.Code == item.EquipmentSN).FirstOrDefault();
                    if (currentEquipment == null)
                    {
                        return BllResultFactory.Error($"设备SN“{item.EquipmentSN}”不存在,请检查数据是否正确,或联系管理员处理!");
                    }

                    if (string.IsNullOrWhiteSpace(currentEquipment.ProjectCode))
                    {
                        return BllResultFactory.Error($"{nameof(item.EquipmentSN)}的“{nameof(currentEquipment.ProjectCode)}”字段数据未配置,请联系管理员处理!");
                    }

                    if (string.IsNullOrWhiteSpace(currentEquipment.FactoryCode))
                    {
                        return BllResultFactory.Error($"{nameof(item.EquipmentSN)}为的“{nameof(currentEquipment.FactoryCode)}”字段数据未配置,请联系管理员处理!");
                    }

                    var addTemps = newBarCodes.Except(oldBarCodes);
                    var removeTemps = oldBarCodes.Except(newBarCodes);

                    _freeSql.Delete<AGVPoint>().Where(x => x.EquipmentCode == item.EquipmentSN && removeTemps.Contains(x.BarCode)).ExecuteAffrows();
                    var temps = addTemps.Select(x => new AGVPoint
                    {
                        EquipmentCode = currentEquipment.Code,
                        BarCode = x,
                        ProjectCode = currentEquipment.ProjectCode,
                        FactoryCode = currentEquipment.FactoryCode,
                        Created = DateTime.Now
                    }).ToList();
                    _freeSql.Insert(temps).ExecuteAffrows();
                }

                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        /// <summary>
        /// 获取客户端设备数据
        /// </summary>
        /// <param name="clientId"></param>
        /// <remarks>用于客户端主动同步数据使用</remarks>
        /// <returns></returns>
        [HttpGet]
        public BllResult<List<EquipmentType>> GetClientEquipmentData(Guid clientId)
        {
            try
            {
                var client = _freeSql.Queryable<ClientStatus>().Where(x => x.ClientKeys == clientId).First();
                if (client == null)
                {
                    return BllResultFactory.Error<List<EquipmentType>>($"未找到ClientId“{clientId}”的客户端信息!");
                }

                if (string.IsNullOrWhiteSpace(client.ProjectCode))
                {
                    return BllResultFactory.Error<List<EquipmentType>>($"未配置客户端“{clientId}”的项目编号!");
                }

                //需过滤的设备类型
                var filterEquipmentTypeCodes = new List<EquipmentTypeConst>
                {
                    EquipmentTypeConst.WeldRobot,
                    EquipmentTypeConst.WeldRobotV2,
                    EquipmentTypeConst.AGVForklift,
                }.Select(x => x.ToString()).ToList();

                var filterEquipmentTypeIds = _freeSql.Queryable<EquipmentType>().Where(x => filterEquipmentTypeCodes.Contains(x.Code)).ToList(x => x.Id);
                var equipments = _freeSql.Queryable<Equipment>().Where(x => x.ProjectCode == client.ProjectCode && !filterEquipmentTypeIds.Contains(x.EquipmentTypeId)).IncludeMany(x => x.EquipmentProps).ToList();
                var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
                var equipmentTypes = _freeSql.Queryable<EquipmentType>().Where(x => equipmentTypeIds.Contains(x.Id)).IncludeMany(x => x.EquipmentTypePropTemplates).ToList();
                foreach (var equipmentType in equipmentTypes)
                {
                    equipmentType.Equipments = equipments.Where(x => x.EquipmentTypeId == equipmentType.Id).ToList();
                }
                return BllResultFactory.Success(equipmentTypes);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<EquipmentType>>(ex.Message);
            }
        }
    }
}