EquipmentController.cs
3.63 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
using HHECS.BllModel;
using HHECS.DAQServer.DataAccess;
using HHECS.DAQServer.Dto.Equipment;
using HHECS.DAQServer.Models;
using HHECS.EquipmentModel;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using System.Text.Json;
namespace HHECS.DAQServer.Controllers
{
/// <summary>
/// 设备数据
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class EquipmentController : ControllerBase
{
private readonly DataContext _context;
public EquipmentController(DataContext dataContext)
{
_context = dataContext;
}
/// <summary>
/// 推送设备实时数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
[HttpPost]
public BllResult SendEquipmentData(IEnumerable<EquipmentDataDto> data)
{
try
{
var d = _context.EquipmentDataRecord.Where(x => true).Count();
if (!data.Any())
{
return BllResultFactory.Error($"数据不能为空!");
}
var records = data.Select(x => new EquipmentDataRecord
{
Plmeid = x.Plmeid,
EquipmentCode = x.EquipmentSN,
Tags = JsonSerializer.Serialize(x.Reported),
IsHandle = false,
Version = x.Version,
CreateTime = DateTime.Now,
Timestamp = x.Timestamp,
}).ToList();
_context.EquipmentDataRecord.AddRange(records);
_context.SaveChanges();
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
/// <summary>
/// 获取设备实时属性
/// </summary>
/// <param name="equipmentInfoQueryModel"></param>
/// <returns></returns>
[HttpPost]
public BllResult GetEquipmentProps(EquipmentInfoQueryModel equipmentInfoQueryModel)
{
try
{
Expression<Func<EquipmentProp, bool>> filter = x => true;
if (!string.IsNullOrWhiteSpace(equipmentInfoQueryModel.EquipmentCode))
{
filter = x => x.Equipment.Code == equipmentInfoQueryModel.EquipmentCode;
}
else if (!string.IsNullOrWhiteSpace(equipmentInfoQueryModel.Requestor))
{
var area = equipmentInfoQueryModel.Requestor;
if (!string.IsNullOrWhiteSpace(equipmentInfoQueryModel.InventoryCode))
{
area = equipmentInfoQueryModel.Requestor + "-" + equipmentInfoQueryModel.InventoryCode;
}
filter = x => x.Equipment.Code.StartsWith(equipmentInfoQueryModel.Requestor);
}
var res = _context.EquipmentProp.Where(filter).Include(x => x.Equipment).ToList();
var group = res.GroupBy(x => x.Equipment.Code);
var response = group.Select(x => new
{
EquipmentCode = x.Key,
Props = x.Select(y => new
{
Prop = y.EquipmentTypePropTemplateCode,
y.Value
})
});
return BllResultFactory.Success(response);
}
catch (Exception ex)
{
return BllResultFactory.Error($"{ex}");
}
}
}
}