DigitalTwinController.cs
4.39 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
using HHECS.BllModel;
using HHECS.DAQServer.DataAccess;
using HHECS.DAQServer.Dto.DigitalTwin;
using HHECS.DAQServer.Dto.Equipment;
using HHECS.EquipmentModel;
using LinqKit;
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 DigitalTwinController : ControllerBase
{
private readonly DataContext _context;
private readonly IDistributedCache _cache;
public DigitalTwinController(DataContext dataContext, IDistributedCache cache)
{
_context = dataContext;
_cache = cache;
}
/// <summary>
/// 获取设备实时数据
/// </summary>
/// <param name="projectCode">项目编号</param>
/// <param name="area">区域</param>
/// <param name="equipmentTypeCode">设备类型</param>
/// <returns></returns>
[HttpGet("{projectCode}")]
public async Task<BllResult<List<EquipmentInfoResponseDto>>> GetEquipmentInfo(string projectCode, string area = null, string equipmentTypeCode = null)
{
try
{
if (string.IsNullOrWhiteSpace(projectCode))
{
return BllResultFactory.Error<List<EquipmentInfoResponseDto>>($"{nameof(projectCode)}不能为空!");
}
var filter = PredicateBuilder.New<Equipment>(true);
if (!string.IsNullOrWhiteSpace(projectCode))
{
if (string.IsNullOrWhiteSpace(area))
{
filter = filter.And(x => x.Code.StartsWith($"{projectCode}-"));
}
else
{
filter = filter.And(x => x.Code.StartsWith($"{projectCode}-{area}-"));
}
}
if (!string.IsNullOrWhiteSpace(equipmentTypeCode))
{
filter = filter.And(x => x.EquipmentType.Code == equipmentTypeCode);
}
var equipmentData = _context.Equipment.Where(filter).WithLock(SqlServerLock.NoLock).Include(x => x.EquipmentType).ToList(x => new EquipmentInfoResponseDto
{
EquipmentCode = x.Code,
EquipmentName = x.Name,
Remark = x.ConnectName,
EquipmentTypeCode = x.EquipmentType.Code,
EquipmentTypeName = x.EquipmentType.Name,
EquipmentProps = _context.EquipmentProp.Where(p => p.EquipmentId == x.Id).ToList(p => new EquipmentPropDto
{
EquipmentPropCode = p.EquipmentTypePropTemplateCode,
EquipmentPropName = p.Remark,
DataAddress = p.Address,
Value = p.Value,
DataSourceTime = p.Updated,
})
});
//从缓存取值
foreach (var item in equipmentData)
{
var cacheDataBytes = await _cache.GetAsync(item.EquipmentCode);
if (cacheDataBytes == null)
{
continue;
}
var cacheData = JsonSerializer.Deserialize<EquipmentDataDto>(Encoding.Default.GetString(cacheDataBytes));
var cacheDataSourceTime = DateTimeOffset.FromUnixTimeSeconds(cacheData.Timestamp).LocalDateTime;
foreach (var tagItem in cacheData.Reported)
{
var propItem = item.EquipmentProps.Where(x => x.DataAddress == tagItem.Tag).FirstOrDefault();
if (propItem != null && cacheDataSourceTime >= propItem.DataSourceTime)
{
propItem.Value = tagItem.Value;
propItem.DataSourceTime = cacheDataSourceTime;
}
}
}
return BllResultFactory.Success(equipmentData);
}
catch (Exception ex)
{
return BllResultFactory.Error<List<EquipmentInfoResponseDto>>(ex.Message);
}
}
}
}