EquipmentController.cs
5.81 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
using HHECS.DAQShared.DataAccess;
using HHECS.DAQShared.Dto;
using HHECS.DAQShared.Dto.Board;
using HHECS.DAQShared.Dto.Equipment;
using HHECS.DAQShared.Models;
using Microsoft.AspNetCore.Mvc;
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 APIResult SendEqipmentData(IEnumerable<EquipmentDataDto> data)
{
try
{
if (data.Count() == 0)
{
return new APIResult($"数据不能为空!");
}
var tempSNs = data.Select(x => x.EquipmentSN).Distinct().ToList();
if (tempSNs.Any(string.IsNullOrWhiteSpace))
{
return new APIResult($"{nameof(EquipmentDataDto.EquipmentSN)}不能为空!");
}
var dbSNs = _context.Equipment.Where(x => tempSNs.Contains(x.Code)).ToList(x => x.Code).Distinct().ToList();
if (dbSNs.Count != tempSNs.Distinct().Count())
{
return new APIResult($"设备SN:[{string.Join(',', tempSNs.Except(dbSNs))}]未配置!");
}
var records = data.Select(x => new EquipmentDataRecord
{
EquipmentCode = x.EquipmentSN,
Tags = JsonSerializer.Serialize(x.Reported),
IsHandle = false,
Version = x.Version,
CreateTime = DateTime.Now,
Timestamp = x.Timestamp,
});
_context.EquipmentDataRecord.AddRange(records);
_context.SaveChanges();
return new APIResult("成功", 200);
}
catch (Exception ex)
{
return new APIResult(ex.Message);
}
}
/// <summary>
/// 同步设备数据
/// </summary>
/// <param name="equipments"></param>
/// <returns></returns>
[HttpPost]
public APIResult SyncEquipment(IEnumerable<EquipmentDto> equipments)
{
try
{
//if (!equipments.Any())
//{
// return new APIResult($"数据不能为空");
//}
//else if (equipments.Where(x => string.IsNullOrWhiteSpace(x.EquipmentSN)).Any())
//{
// return new APIResult($"{nameof(EquipmentDto.EquipmentSN)}不能为空!");
//}
//var equipmentSNs = equipments.Select(x => x.EquipmentSN).ToList();
//var equipmentTemps = _context.Equipment.Where(x => equipmentSNs.Contains(x.EquipmentSN)).IncludeMany(x => x.EquipmentProperties).ToList();
//foreach (var item in equipments)
//{
// var temp = equipmentTemps.Where(x => x.EquipmentSN.Equals(item.EquipmentSN)).FirstOrDefault();
// //存在,则更新
// if (temp != null)
// {
// temp.Code = item.Code;
// temp.Name = item.Name;
// temp.Type = item.Type;
// temp.WarehouseCode = item.WarehouseCode;
// temp.Remark = item.Remark;
// temp.UpdateTime = DateTime.Now;
// _context.EquipmentProperties.RemoveRange(temp.EquipmentProperties);
// temp.EquipmentProperties = item.EquipmentProperties.Select(x => new EquipmentProperty
// {
// Code = x.Code,
// Name = x.Name,
// Tag = x.Tag,
// Value = string.Empty,
// Remark = x.Remark,
// CreateTime = DateTime.Now,
// UpdateTime = DateTime.Now,
// }).ToList();
// _context.Equipment.Update(temp);
// continue;
// }
// //不存在,则新增
// temp = new Equipment
// {
// Code = item.Code,
// Name = item.Name,
// Type = item.Type,
// EquipmentSN = item.EquipmentSN,
// EquipmentProperties = item.EquipmentProperties.Select(x => new EquipmentProperty
// {
// Code = item.Code,
// Name = x.Name,
// Tag = x.Tag,
// Value = string.Empty,
// Remark = x.Remark,
// CreateTime = DateTime.Now,
// UpdateTime = DateTime.Now,
// }).ToList(),
// WarehouseCode = item.WarehouseCode,
// Remark = item.Remark,
// CreateTime = DateTime.Now,
// UpdateTime = DateTime.Now,
// };
// _context.Equipment.Add(temp);
//}
//_context.SaveChanges();
return new APIResult("成功", 200);
}
catch (Exception ex)
{
return new APIResult($"操作失败:{ex.Message}");
}
}
}
}