ConfigOperation.cs
9.9 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
using Common;
using DeviceLib.Model.AGV;
using DeviceLib.Model.AGV.MiR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace DeviceLib.DB.Config
{
public class ConfigOperation
{
#region 机器人
public static List<Robot> GetRobots()
{
List<Robot> list = new List<Robot>();
using (ConfigContext context = new ConfigContext())
{
list = context.Robots.ToList();
foreach (var agv in list)
{
agv.io_modules = new List<IOModule>();
string[] ios = agv.io_module_ids?.Split('#');
if (ios != null)
foreach (string ios2 in ios)
{
int.TryParse(ios2, out int id);
IOModule iOModule = context.IOModules.Where(s => s.id == id).FirstOrDefault();
if (iOModule != null)
agv.io_modules.Add(iOModule);
}
agv.fleet = context.Fleets.Where(s => s.id == agv.fleet_id).FirstOrDefault();
agv.workshop = context.Workshops.Where(s => s.id == agv.workshop_id).FirstOrDefault();
agv.mission_group = context.MissionGroups.Where(x => x.id.Equals(agv.group_id)).FirstOrDefault();
agv.load_infos = context.LoadInfos.Where(s => s.robot_id == agv.id).ToList();
int robotType = agv.type;
RobotType robotType1 = context.RobotTypes.Where(s => s.id == robotType).FirstOrDefault();
if (robotType1 != null)
{
switch (robotType1.name.ToLower())
{
case "mir":
agv.operation = new MiROperation(agv);
break;
}
}
else
{
agv.operation = new MiROperation(agv);
}
}
}
return list;
}
public static bool SetRobot(Robot robot)
{
if (robot != null)
{
if (Monitor.TryEnter(robot, 3000))
{
try
{
using (ConfigContext context = new ConfigContext())
{
var find = context.Robots.FirstOrDefault(x => x.id == robot.id);
if (find != null)
{
find.auto = robot.auto;
find.use_fleet = robot.use_fleet;
find.is_debug = robot.is_debug;
}
foreach (var item in robot.load_infos)
{
var loadInfo = context.LoadInfos.FirstOrDefault(s => s.id == item.id);
if (loadInfo != null)
{
loadInfo.shelf_type = item.shelf_type;
loadInfo.shelf_id = item.shelf_id;
}
}
context.SaveChangesAsync();
return true;
}
}
catch (Exception ex)
{
LogUtil.Error($"SetRobot {robot?.name}",ex);
}
finally { Monitor.Exit(robot); }
}
}
return false;
}
#endregion
#region 节点
public static List<ClientNode> GetNodes()
{
List<ClientNode> list;
using (ConfigContext context = new ConfigContext())
{
list = context.Nodes.ToList();
foreach (var item in list)
{
if (string.IsNullOrEmpty(item.ip)) item.online = true;
item.workshop = context.Workshops.Where(s => s.id.Equals(item.workshop_id)).FirstOrDefault();
}
}
return list;
}
public static bool SetNode(ClientNode clientNode)
{
using (ConfigContext context = new ConfigContext())
{
var find = context.Nodes.Where(s => s.id == clientNode.id).FirstOrDefault();
if (find != null)
{
find.opened = clientNode.opened;
find.online = clientNode.online;
find.status = clientNode.status;
find.description = clientNode.description;
find.level = clientNode.level;
find.shelf_id = clientNode.shelf_id;
find.shielded = clientNode.shielded;
context.SaveChangesAsync();
}
}
return false;
}
public static bool AddNode(ClientNode clientNode)
{
using (ConfigContext context = new ConfigContext())
{
var find = context.Nodes.Where(s => s.id == clientNode.id).FirstOrDefault();
if (find == null)
{
context.Nodes.Add(clientNode);
context.SaveChangesAsync();
return true;
}
}
return false;
}
#endregion
#region 任务
public static List<Mission> Get_Missions()
{
List<Mission> list;
using (ConfigContext context = new ConfigContext())
{
list = context.Missions.ToList();
foreach (var item in list)
{
item.type = context.MissionTypes.Where(s => s.id.Equals(item.type_id)).FirstOrDefault();
item.group = context.MissionGroups.Where(s => s.id.Equals(item.group_id)).FirstOrDefault();
}
}
return list;
}
#endregion
#region 订单
public static List<Order> Get_Orders()
{
List<Order> list;
using (ConfigContext context = new ConfigContext())
{
list = context.Orders.ToList();
}
return list;
}
public static bool Add_Order(Order order)
{
using (ConfigContext context = new ConfigContext())
{
var find = context.Orders.Where(s => s.id.Equals(order.id)).FirstOrDefault();
if (find == null)
{
context.Orders.Add(order);
context.SaveChangesAsync();
return true;
}
}
return false;
}
public static bool Del_Order(Order order)
{
using (ConfigContext context = new ConfigContext())
{
var find = context.Orders.Where(s => s.id.Equals(order.id)).FirstOrDefault();
if (find != null)
{
context.Orders.Remove(find);
context.SaveChangesAsync();
return true;
}
}
return false;
}
/// <summary>
/// 删除任务编号
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool Del_Order(string id)
{
using (ConfigContext context = new ConfigContext())
{
var find = context.Orders.Where(s => s.id.Equals(id)).FirstOrDefault();
if (find != null)
{
context.Orders.Remove(find);
context.SaveChangesAsync();
return true;
}
}
return false;
}
/// <summary>
/// 删除任务类型
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool Del_Order(int typeId)
{
using (ConfigContext context = new ConfigContext())
{
var find = context.Orders.Where(s => s.type.Equals(typeId)).FirstOrDefault();
if (find != null)
{
context.Orders.Remove(find);
context.SaveChangesAsync();
return true;
}
}
return false;
}
public static bool SetOrder(Order order)
{
using (ConfigContext context = new ConfigContext())
{
var find = context.Orders.Where(s => s.id.Equals(order.id)).FirstOrDefault();
if (find != null)
{
find.state = order.state;
find.robot_id = order.robot_id;
context.SaveChangesAsync();
return true;
}
}
return false;
}
public static OrderState GetOrderState(int id)
{
using (ConfigContext context = new ConfigContext())
{
return context.OrderStates.Where(s => s.id.Equals(id)).FirstOrDefault();
}
}
public static OrderType GetOrderType(int id)
{
using (ConfigContext context = new ConfigContext())
{
return context.OrderTypes.Where(s => s.id.Equals(id)).FirstOrDefault();
}
}
public static List<OrderType> GetAllOrderTypes()
{
using (ConfigContext context = new ConfigContext())
{
return context.OrderTypes.ToList();
}
}
#endregion
}
}