UpdateManage.cs
51.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
using Newtonsoft.Json;
using RCS.Dal;
using RCS.Model.ClientMoel;
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.WinClient.Common;
using System.Configuration;
using System.Data;
namespace RCS.WinClient.Service
{
public class UpdateManage
{
public static object _lockError = new object();
private static BaseDal<Base_Point> _pointDb = new BaseDal<Base_Point>();
private static BaseDal<Base_Point> _demoPointDb = new BaseDal<Base_Point>();
private static BaseDal<Base_Task> _taskDb = new BaseDal<Base_Task>();
private static BaseDal<Log_Task> _taskLogDb = new BaseDal<Log_Task>();
private static BaseDal<Base_SubTask> _sonTaskDb = new BaseDal<Base_SubTask>();
private static BaseDal<Log_SubTask> _sonTaskLogDb = new BaseDal<Log_SubTask>();
private static BaseDal<Base_PathPoint> _pathPointDb = new BaseDal<Base_PathPoint>();
private static BaseDal<Base_Agv> _agvDb = new BaseDal<Base_Agv>();
private static BaseDal<Base_Station> _stationDb = new BaseDal<Base_Station>();
private static BaseDal<Config_TaskSplit> _taskSplitDb = new BaseDal<Config_TaskSplit>();
private static BaseDal<Config_ErrMsg> _errMsgDb = new BaseDal<Config_ErrMsg>();
private static BaseDal<Config_KeyValue> _keyValueDb = new BaseDal<Config_KeyValue>();
private static BaseDal<Config_TaskAgvGroup> _taskAgvGroupDb = new BaseDal<Config_TaskAgvGroup>();
private static BaseDal<Config_Point> _cgPointDb = new BaseDal<Config_Point>();
private static BaseDal<Log_AgvCharge> _agvChargeLogDb = new BaseDal<Log_AgvCharge>();
private static BaseDal<Log_AgvMilage> _agvMilageLogDb = new BaseDal<Log_AgvMilage>();
private static BaseDal<Log_AgvState> _agvStateLogDb = new BaseDal<Log_AgvState>();
private static BaseDal<Log_AgvErrMsg> _agvErrMsgLogDb = new BaseDal<Log_AgvErrMsg>();
/// <summary>
/// 更新AGV锁定地标、报警信息
/// </summary>
/// <param name="clientMsg">小车客户端信息</param>
/// <returns></returns>
public static BllResult UpdateAgvMsg(Base_Agv agv)
{
try
{
//获取当前点是否在地图中
Base_Point currentPoint = App.PointList.FirstOrDefault(a => a.Barcode == agv.Barcode);
if (currentPoint == null)
{
var msg = $"地标错误,地图中不存在地标【{agv.Barcode}】";
agv.StopReason = ComnMethod.GetStopReason(agv.StopReason, msg);
return BllResult.Error(msg);
}
//区域
agv.CurrMap = currentPoint.AreaType.ToString();
//报警信息
agv.Error = ComnMethod.GetAgvError(agv.ErrorList);
//小车所有的地标,设置由当前小车锁定
currentPoint.LockedAgv = agv;
//如果不是忙碌,就将小车不在的地标都解锁
if (agv.AgvState != EnumMsg.AGVState.自动忙碌)
{
var lockedPoint = new List<Base_Point>() { currentPoint };
ClearPointLocked(lockedPoint, agv);
}
//处理异常信息
SqlErrAgv(agv);
//保存报警信息到数据库
//if (agv.Error != "")
//{
// if (!App.errAgv.Contains(agv.AgvNo) && !agv.Error.Contains("agv"))
// {
// SqlErrAgv(agv, "insert");
// App.errAgv.Add(agv.AgvNo);
// }
//}
//else
//{
// if (App.errAgv.Contains(agv.AgvNo))
// {
// SqlErrAgv(agv, "update");
// App.errAgv.Remove(agv.AgvNo);
// }
//}
return BllResult.Success();
}
catch (Exception ex)
{
throw new Exception($"UpdateClientMsg:: {ex.Message},小车编码:{agv.AgvNo},\n 堆栈:{ex.StackTrace}");
}
}
/// <summary>
/// 更新点信息
/// </summary>
/// <param name="clientMsg">小车客户端信息</param>
/// <returns></returns>
public static BllResult SqlPoint(Base_Point point, string type)
{
try
{
if (type == "insert")
{
point.IsEnable = true;
point.IsOccupy = false;
point.OccupyAgvNo = "";
point.AgvAngle = 0;
var insertresult = _pointDb.Insert(point);
if (insertresult.IsSuccess)
{
return BllResult.Success();
}
}
else
{
var queryresult = _pointDb.Query(it => it.AreaType == point.AreaType);
if (!queryresult.IsSuccess)
{
if (queryresult.Message == "未查询到数据")
{
return BllResult.Success();
}
else
{
return BllResult.Error();
}
}
else
{
var deleteresult = _pointDb.Delete(it => it.AreaType == point.AreaType);
if (deleteresult.IsSuccess)
{
return BllResult.Success();
}
}
//sql += string.Format(@"DELETE FROM T_Base_Station WHERE stationArea = '{0}';", point.AreaType);
//sql += string.Format(@"DELETE FROM T_Config_Point WHERE AreaType = '{0}';", point.AreaType);
}
}
catch (Exception ex)
{
throw new Exception($"SqlPointMsg:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Error();
}
/// <summary>
/// 更新Demo点信息
/// </summary>
/// <param name="point"></param>
/// <param name="type"></param>
/// <returns></returns>
public static BllResult SqlDemoPoint(Base_Point point, string type)
{
try
{
if (type == "simInsert")
{
point.IsOccupy = false;
point.OccupyAgvNo = "";
var insetresult = _demoPointDb.Insert(point);
if (insetresult.IsSuccess)
{
return BllResult.Success();
}
}
else
{
var queryresult = _demoPointDb.Query(it => it.AreaType == point.AreaType);
if (!queryresult.IsSuccess)
{
if (queryresult.Message == "未查询到数据")
{
return BllResult.Success();
}
else
{
return BllResult.Error();
}
}
else
{
var deleteresult = _demoPointDb.Delete(it => it.AreaType == point.AreaType);
if (deleteresult.IsSuccess)
{
return BllResult.Success();
}
}
}
}
catch (Exception ex)
{
throw new Exception($"SqlDemoPointMsg:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Error();
}
/// <summary>
/// 更新小车信息
/// </summary>
/// <param name="clientMsg">小车客户端信息</param>
/// <returns></returns>
public static BllResult UpdateAgv(Base_Agv agv)
{
try
{
var updateresult = _agvDb.Update(it => it.AgvNo == agv.AgvNo, it => new Base_Agv()
{
Barcode = agv.Barcode,
IsEnable = agv.IsEnable,
//LockStation = agv.LockStation,
Milage = agv.Milage,
Group = agv.Group,
StartVoltage = agv.StartVoltage,
Voltage = agv.Voltage,
AgvState = agv.AgvState,
HeightState = agv.HeightState,
IP = agv.IP,
//TailLength = agv.TailLength,
Heart = agv.Heart,
Error = agv.Error,
CurrMap = agv.CurrMap,
});
if (!updateresult.IsSuccess)
{
return BllResult.Error();
}
}
catch (Exception ex)
{
throw new Exception($"UpdateAgvMsg:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
#region 更新完成
/// <summary>
/// 更新子任务信息
/// </summary>
/// <param name="clientMsg">小车客户端信息</param>
/// <returns></returns>
public static BllResult UpdateSonTask(Base_SubTask sonTask)
{
#region SQL语句
//try
//{
// string sqlUpdate = "";
// int flag = 0;
// if (sonTask.SubTaskState == EnumMsg.SubTaskState.已完成)
// {
// sqlUpdate += string.Format("DELETE FROM T_Base_PathPoint WHERE intSonTaskNo = '{0}';", sonTask.SubTaskNo);
// sqlUpdate += string.Format("DELETE FROM T_SonTask WHERE intSonTaskNo = '{0}'AND strTaskNo='{1}';", sonTask.SubTaskNo, sonTask.TaskNo);
// flag = 99;
// }
// else
// {
// sqlUpdate += string.Format(@"UPDATE T_SonTask SET intSonTaskNo = '{1}' ,strTaskNo = '{2}' ,sonTaskType = '{3}' ,startPoint = '{4}' ,endPoint = '{5}'
// ,isCarry = '{6}' ,taskRequestType = '{7}' ,dialDirection = '{8}' ,agvDirection = '{9}' ,sonTaskState = '{10}'
// ,strBeginTime = '{11}' ,strEndTime = '{12}' WHERE intSonTaskNo = '{0}';", sonTask.SubTaskNo
// , sonTask.SubTaskNo, sonTask.TaskNo, (int)sonTask.SubTaskType, sonTask.StartPoint.Barcode, sonTask.EndPoint.Barcode
// , sonTask.IsCarry == false ? "0" : "1", (int)sonTask.TaskRequestType, (int)sonTask.DialOri, (int)sonTask.AgvOri, (int)sonTask.SubTaskState
// , sonTask.BeginTime.GetDateString(), sonTask.EndTime.GetDateString());
// }
// if (flag == 99)
// {
// bool isSqleResult = false;
// while (true)
// {
// isSqleResult = DbHelperSQL.SqlTransOperator(sqlUpdate);
// if (isSqleResult) break;
// }
// }
// else
// {
// DbHelperSQL.ExecuteSql(sqlUpdate);
// }
// return true;
//}
//catch (Exception ex)
//{
// throw new Exception("UpdateSonTask:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
#endregion SQL语句
try
{
//如果当前子任务完成
if (sonTask.SubTaskState >= EnumMsg.SubTaskState.自动完成)
{
var deleteresult1 = _pathPointDb.Delete(it => it.SubTaskNo == sonTask.SubTaskNo);
var deleteresult2 = _sonTaskDb.Delete(it => it.TaskNo == sonTask.TaskNo && it.SubTaskNo == sonTask.SubTaskNo);
if (!deleteresult1.IsSuccess || !deleteresult2.IsSuccess)
{
return BllResult.Error();
}
}
else
{
var updateresult = _sonTaskDb.Update(sonTask, it => new { it.SubTaskNo, it.TaskNo });
if (!updateresult.IsSuccess)
{
return BllResult.Error();
}
}
}
catch (Exception ex)
{
throw new Exception($"UpdateSonTask:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
/// <summary>
/// 更新站台信息
/// </summary>
/// <param name="station"></param>
/// <returns></returns>
public static BllResult UpdateStation(Base_Station station)
{
#region SQL语句
//try
//{
// string sqlUpdate = string.Format(
// @"UPDATE T_Base_Station SET Enable = '{1}', IsLocked = '{2}',StationState = '{3}' WHERE Name = '{0}';",
// station.Name, station.IsEnable == false ? "0" : "1", station.IsLocked == false ? "0" : "1",
// (int)station.StationState);
// int intSqlResult = DbHelperSQL.ExecuteSql(sqlUpdate);
// if (intSqlResult < 1) return false;
// return true;
//}
//catch (Exception ex)
//{
// throw new Exception("UpdateStation:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
#endregion SQL语句
try
{
var updateresult = _stationDb.Update(it => it.Name == station.Name, it => new Base_Station()
{
IsEnable = station.IsEnable,
IsLocked = station.IsLocked,
StationState = station.StationState,
});
if (!updateresult.IsSuccess)
{
return BllResult.Error();
}
}
catch (Exception ex)
{
throw new Exception($"UpdateStation:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
/// <summary>
/// 更新站台的点位和防撞属性
/// </summary>
/// <param name="station"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static BllResult UpdateStationBarcode(string[] stationNames, Base_Station station)
{
try
{
var updateresult = _stationDb.Update(it => stationNames.Contains(it.Name), it => new Base_Station()
{
Barcode = station.Barcode,
PreBarcode = station.PreBarcode,
Tim = station.Tim,
LowHeight = station.LowHeight,
});
if (!updateresult.IsSuccess)
{
return BllResult.Error();
}
}
catch (Exception ex)
{
throw new Exception($"UpdateStationBarcode:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
/// <summary>
/// 解除AGV锁定点位
/// </summary>
/// <param name="lockedPointList">需要继续锁定的点</param>
/// <param name="agv"></param>
public static BllResult ClearPointLocked(List<Base_Point> lockedPointList, Base_Agv agv)
{
try
{
//找出所有锁定的地标
var lockedList = App.PointList.Where(a => a.LockedAgv == agv).ToList();
foreach (var point in lockedList)
{
//将不需要锁定的地标解锁
if (!lockedPointList.Contains(point))
{
point.LockedAgv = null;
}
}
}
catch (Exception ex)
{
throw new Exception($"ClearPointLocked:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
/// <summary>
/// 重建路径
/// 删除路径,更新子任务状态
/// </summary>
/// <param name="sonTask"></param>
public static BllResult ResetPath(Base_Agv agv, Base_SubTask sonTask)
{
try
{
if (sonTask.ActionType != EnumMsg.ActionType.普通行走)
{
return BllResult.Success();
}
var task = App.TaskList.FirstOrDefault(x => x.TaskNo == sonTask.TaskNo)
?? throw new Exception($"agv{agv.AgvNo}在执行任务{sonTask.TaskNo}的子任务{sonTask.SubTaskNo}重新规划路径时查找任务失败");
Base_Point? point = null;
if (agv.AgvType == EnumMsg.AgvType.叉车)
{
var config = sonTask.ConfigTaskSplit
?? throw new Exception($"agv{agv.AgvNo}在执行任务{sonTask.TaskNo}的子任务{sonTask.SubTaskNo}重新规划路径时查找任务分解数据{task.TaskType}-{sonTask.SerialNo}失败"); ;
point = ComnMethod.GetTaskPoint(task, config.ToPoint)
?? throw new Exception($"agv{agv.AgvNo}在执行任务{sonTask.TaskNo}的子任务{sonTask.SubTaskNo}重新规划路径时查找目标点失败");
}
var endBarcode = point?.Barcode ?? sonTask.EndBarcode;
var deleteResult = _pathPointDb.Delete(it => it.SubTaskNo == sonTask.SubTaskNo);
var updateResult = _sonTaskDb.Update(it => it.SubTaskNo == sonTask.SubTaskNo,
it => new Base_SubTask()
{
EndBarcode = endBarcode,
SubTaskState = EnumMsg.SubTaskState.重建路径中
}, true);
if (deleteResult.IsSuccess && updateResult.IsSuccess)
{
agv.PathPointList.Clear();
sonTask.PathPointList.Clear();
sonTask.SubTaskState = EnumMsg.SubTaskState.重建路径中;
if (agv.AgvType == EnumMsg.AgvType.叉车 && point != null)
{
sonTask.EndPoint = point;
}
}
}
catch (Exception ex)
{
throw new Exception($"ResetPath:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
/// <summary>
/// 将异常小车插入异常信息表和任务表中
/// </summary>
/// <param name="agv"></param>
/// <returns></returns>
public static BllResult SqlErrAgv(Base_Agv agv, string type)
{
try
{
string taskNo = "";
if (agv.AgvTask != null) taskNo = agv.AgvTask.TaskNo;
if (type == "insert")
{
//插入异常报表
var addAgvErrMsg = new Log_AgvErrMsg()
{
AgvNo = agv.AgvNo,
Barcode = agv.Barcode,
ErrorMsg = agv.Error,
StartErrTime = agv.Heart,
EndErrTime = null,
TaskNo = taskNo,
};
_agvErrMsgLogDb.Insert(addAgvErrMsg);
}
else
{
_agvErrMsgLogDb.Update(it => it.AgvNo == agv.AgvNo && it.EndErrTime == null,
it => new Log_AgvErrMsg() { EndErrTime = agv.Heart });
}
}
catch (Exception ex)
{
throw new Exception($"SqlErrAgv:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
/// <summary>
/// 将异常小车插入异常信息表和任务表中
/// </summary>
/// <param name="agvData"></param>
/// <returns></returns>
public static BllResult SqlErrAgv(Base_Agv agv)
{
try
{
//获取所有未结束的异常信息
lock (_lockError)
{
var heartTime = agv.Heart;
IEnumerable<Log_AgvErrMsg> agvErrors = App.AgvErrMsgs.Where(x => x.AgvNo == agv.AgvNo
&& x.EndErrTime == null
&& x.UpdateErrTime != null
&& (heartTime - (DateTime)x.UpdateErrTime).TotalSeconds <= App.AppConfig.ErrorTimeOut);
//无异常返回
if (!agvErrors.Any() && agv.Error.IsNullOrEmpty())
{
return BllResult.Success();
}
//后续按需改成可配置,排除特定状态的异常
if (!agv.Error.IsNullOrEmpty() && agv.AgvState < EnumMsg.AGVState.离线)
{
var errors = agv.Error.Split(';');
foreach (var error in errors)
{
if (error.IsNullOrEmpty()) continue;
var agvError = agvErrors.FirstOrDefault(x => x.ErrorMsg == error);
if (agvError == null)
{
agvError = new Log_AgvErrMsg()
{
StartErrTime = heartTime,
UpdateErrTime = heartTime,
ErrorMsg = error,
AgvState = agv.AgvState,
AgvNo = agv.AgvNo,
TaskNo = agv.AgvTask?.TaskNo,
Barcode = agv.Barcode,
IsInsert = false,
EndErrTime = null,
};
App.AgvErrMsgs.Add(agvError);
App.ExFile.MessageError("SqlErrAgv", $"AGV{agv.AgvNo}在执行任务{agv.AgvTask?.TaskNo}时发生异常{error},接收时间{heartTime},详细信息{JsonConvert.SerializeObject(agvError)}");
}
else
{
if (heartTime < agvError.UpdateErrTime)
{
App.ExFile.MessageError("SqlErrAgv", $"AGV{agv.AgvNo}在执行任务{agv.AgvTask?.TaskNo}更新异常信息时发生异常,接收时间{heartTime}小于最后更新时间{agvError.UpdateErrTime}");
continue;
}
agvError.UpdateErrTime = heartTime;
}
}
}
}
return BllResult.Success();
}
catch (Exception Ex)
{
App.ExFile.MessageError("SqlErrAgv", $"{Ex}");
return BllResult.Error(Ex.Message);
}
}
/// <summary>
/// agv异常信息处理
/// </summary>
public static void UpdateAgvErrorToDB()
{
Task.Run(async () =>
{
while (true)
{
try
{
lock (_lockError)
{
//获取所有未被更新的异常
var agvErrors = App.AgvErrMsgs.Where(x => x.UpdateErrTime != null && (DateTime.Now - (DateTime)x.UpdateErrTime).TotalSeconds > App.AppConfig.ErrorTimeOut);
foreach (var item in agvErrors)
{
item.EndErrTime = item.UpdateErrTime ?? ((DateTime)item.StartErrTime).AddSeconds(App.AppConfig.ErrorMinDuration);
App.ExFile.MessageError("UpdateAgvErrorToDB", $"AGV{item.AgvNo}在执行任务{item.TaskNo}时发生异常{item.ErrorMsg},结束时间{item.EndErrTime},详细信息{JsonConvert.SerializeObject(item)}");
}
//插入数据
var insertData = App.AgvErrMsgs.Where(x => !x.IsInsert);
foreach (var item in insertData)
{
var insertRes = _agvErrMsgLogDb.Insert(item);
if (!insertRes.IsSuccess && insertRes.Message != "无插入数据")
{
App.ExFile.MessageError("UpdateAgvErrorToDB", $"插入AGV报警信息失败:{insertRes.Message}");
}
if (insertRes.IsSuccess)
{
item.IsInsert = true;
}
}
if (App.AgvErrMsgs.Find(x => x.StartErrTime > x.UpdateErrTime) is Log_AgvErrMsg errorInfo && errorInfo != null)
{
App.ExFile.MessageError("UpdateAgvErrorToDB", $"插入AGV报警信息出现异常数据:开始时间{errorInfo.StartErrTime}大于更新时间{errorInfo.UpdateErrTime}");
}
//更新数据
var updateData = App.AgvErrMsgs.Where(x => x.StartErrTime != x.UpdateErrTime || x.EndErrTime != null);
var updateRes = _agvErrMsgLogDb.Update(updateData.ToArray());
if (!updateRes.IsSuccess && updateRes.Message != "无更新数据")
{
App.ExFile.MessageError("UpdateAgvErrorToDB", $"插入AGV报警信息失败:{updateRes.Message}");
}
if (updateRes.IsSuccess)
{
//移除数据
var removeErrors = App.AgvErrMsgs.FindAll(x => x.EndErrTime != null);
App.AgvErrMsgs.RemoveAll(x => x.EndErrTime != null);
if (removeErrors.Count > 0)
{
var msg = string.Join(",", removeErrors.Select(x => $"{x.AgvNo}_{x.ErrorMsg}_{x.EndErrTime};").ToArray());
App.ExFile.MessageError("UpdateAgvErrorToDB", $"异常{msg}结束,数量{removeErrors.Count}");
}
}
}
}
catch (Exception ex)
{
App.ExFile.MessageError("UpdateAgvErrorToDB", $"异常数据处理失败:{ex}");
}
await Task.Delay(App.AppConfig.ErrorUpdateGap * 1000);
}
});
}
/// <summary>
/// 插入AGV状态到AGV状态日志表
/// </summary>
/// <param name="agv">AGV信息</param>
/// <param name="state">当前AGV状态</param>
/// <returns></returns>
public static BllResult InsertAgvState(Base_Agv agv, int state)
{
#region SQL语句
//try
//{
// string sqlData = string.Empty;
// string sqlInsert = string.Format(
// $"INSERT INTO t_log_agvstate (agvNo,agvState,startTime)VALUES('{agv.AgvNo}','{state}','{DateTime.Now}')");
// DataSet ds =
// DbHelperSQL.Query(
// $"SELECT * FROM t_log_agvstate WHERE agvNo = '{agv.AgvNo}' AND endTime IS NULL ORDER BY startTime DESC LIMIT 1");
// if (ds.Tables[0].Rows.Count == 0)
// {
// sqlData += sqlInsert;
// }
// else
// {
// var ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
// var agvState = int.Parse(ds.Tables[0].Rows[0]["agvState"].ToString());
// if (agvState != state)
// {
// //更新当前状态的结束时间
// var updateSql = $"UPDATE t_log_agvstate SET endTime= '{DateTime.Now}' WHERE ID='{ID}';";
// //插入新状态数据
// sqlData += updateSql + sqlInsert;
// }
// }
// var intSqlResult = DbHelperSQL.SqlTransOperator(sqlData);
// if (!intSqlResult) return false;
//}
//catch (Exception ex)
//{
// throw new Exception("InsertAgvState:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
//return true;
#endregion SQL语句
try
{
//查询AGV插入数据库之前的状态
var queryresult = _agvStateLogDb.QueryEntity(it => it.AgvNo == agv.AgvNo && it.EndTime == null, it => it.StartTime);
if (queryresult.IsSuccess && queryresult.Data != null)
{
//判断插入的数据和待插入的是否一样
if (queryresult.Data.AgvState != state)
{
//更新之前的结束时间
_agvStateLogDb.Update(it => it.AgvNo == agv.AgvNo && it.EndTime == null,
it => new Log_AgvState() { EndTime = DateTime.Now });
}
}
else if (queryresult.Message == "未查询到数据")
{
//插入新数据
var addAgv = new Log_AgvState()
{
AgvNo = agv.AgvNo,
AgvState = state,
StartTime = DateTime.Now,
EndTime = null,
};
var insertresult = _agvStateLogDb.Insert(addAgv);
if (!insertresult.IsSuccess)
{
return BllResult.Error();
}
}
}
catch (Exception ex)
{
throw new Exception($"InsertAgvState:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
/// <summary>
/// 插入AGV里程
/// </summary>
/// <param name="task"></param>
/// <returns></returns>
public static BllResult InsertAgvMilage(Base_Task task)
{
#region SQL语句
//try
//{
// Base_Agv agv = App.AgvList.FirstOrDefault(a => a.AgvNo == task.TaskAgvNo);
// if (agv == null) return true;
// string sqlInsert = string.Format(
// @"INSERT INTO T_Log_agvMilage (strAgvNo, startpoint, endpoint, mileage, starttime, endtime, taskno, groupname)
// VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');"
// , agv.AgvNo, task.StartPoint.Barcode, task.EndPoint.Barcode, agv.AgvMilage,
// task.BeginTime.GetDateString(), task.EndTime.GetDateString(), agv.AgvTask.TaskType, agv.AgvTask.TaskGroup);
// string sqlUpdate = string.Format(@"UPDATE T_Base_Agv SET agvMilage='0'WHERE AgvNo = '{0}';", agv.AgvNo);
// bool intSqlResult = DbHelperSQL.SqlTransOperator(sqlInsert + sqlUpdate);
// if (!intSqlResult) return false;
//}
//catch (Exception ex)
//{
// throw new Exception("InsertAgvMilage:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
//return true;
#endregion SQL语句
try
{
Base_Agv agv = App.AgvList.FirstOrDefault(a => a.AgvNo == task.TaskAgvNo);
if (agv == null) return BllResult.Error();
var agvMilage = new Log_AgvMilage()
{
AgvNo = agv.AgvNo,
StartBarcode = task.StartBarcode,
EndBarcode = task.EndBarcode,
Mileage = agv.Milage,
StartTime = task.BeginTime,
EndTime = task.EndTime,
TaskNo = task.TaskNo,
TaskGroup = task.TaskGroup,
};
var result0 = _agvMilageLogDb.Insert(agvMilage);
var result1 = _agvDb.Update(it => it.AgvNo == agv.AgvNo, it => new Base_Agv { Milage = 0 });
if (result0.IsSuccess && result1.IsSuccess)
{
return BllResult.Success();
}
}
catch (Exception ex)
{
throw new Exception($"InsertAgvMilage:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Error();
}
/// <summary>
/// 插入充电记录
/// </summary>
/// <param name="task"></param>
public static BllResult InsertAgvCharge(Base_Task task)
{
#region SQL语句
//try
//{
// string sqlInsert = "";
// Base_Agv agv = App.AgvList.First(a => a.AgvNo == task.TaskAgvNo);
// if (task.TaskID == "充电")
// {
// sqlInsert = string.Format(@"INSERT INTO T_Log_AgvCharge(AgvNo, startCharge, startTime, endCharge, endTime)
// VALUES('{0}','{1}','{2}','0','null');"
// , agv.AgvNo, (int)agv.AgvVoltage, task.EndTime.GetDateString());
// }
// else
// {
// sqlInsert = string.Format(@"UPDATE T_Log_AgvCharge SET endCharge = '{1}' ,endTime = '{2}'
// WHERE AgvNo='{0}' AND endCharge=0;"
// , agv.AgvNo, (int)agv.AgvVoltage, task.BeginTime.GetDateString());
// }
// int intSqlResult = DbHelperSQL.ExecuteSql(sqlInsert);
//}
//catch (Exception ex)
//{
// throw new Exception("InsertAgvCharge:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
#endregion SQL语句
try
{
var agv = App.AgvList.First(a => a.AgvNo == task.TaskAgvNo);
var chargeLog = new Log_AgvCharge();
if (task.TaskType == "充电")
{
chargeLog.AgvNo = agv.AgvNo;
chargeLog.StartVoltage = (int)agv.Voltage;
chargeLog.StartTime = task.EndTime;
chargeLog.EndVoltage = 0;
_agvChargeLogDb.Insert(chargeLog, it => new { it.EndTime });
}
else
{
_agvChargeLogDb.Update(it => it.AgvNo == agv.AgvNo && it.EndVoltage == 0,
it => new Log_AgvCharge()
{
EndVoltage = (int)agv.Voltage,
EndTime = task.BeginTime,
});
agv.StartVoltage = 0;
}
}
catch (Exception ex)
{
throw new Exception($"InsertAgvCharge:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
/// <summary>
/// 插入删除agv信息
/// </summary>
/// <param name="agv"></param>
/// <param name="type"></param>
/// <returns></returns>
public static BllResult SqlAgvMsg(Base_Agv agv, string type)
{
#region SQL语句
//try
//{
// string sql = "";
// if (type == "insert")
// {
// sql = string.Format(@"INSERT INTO T_Base_Agv(AgvNo,AgvGroup,strBarcode,agvState,heightState,currentCharge,strChooseStation,isEnable,agvHeart,agvMilage,strAgvError,agvmap,startCharges)VALUES
// ('{0}','{1}','0','0','0',0,'{2}','{3}','{4}',0,'','{5}',0);"
// , agv.AgvNo, agv.AgvGroup, agv.ChooseStation, agv.IsEnable == false ? "0" : "1", DateTime.Now, agv.AgvMap);
// }
// else
// {
// sql = string.Format(@"DELETE FROM T_Base_Agv WHERE strAgvNo = '{0}';", agv.AgvNo);
// }
// int intSqlResult = DbHelperSQL.ExecuteSql(sql);
// if (intSqlResult < 1) return false;
//}
//catch (Exception ex)
//{
// throw new Exception("InsertAgvMsg:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
//return true;
#endregion SQL语句
try
{
BllResult result = null;
if (type == "insert")
{
result = _agvDb.Insert(agv);
}
else
{
result = _agvDb.Delete(it => it.AgvNo == agv.AgvNo);
}
if (result.IsSuccess)
{
return BllResult.Success();
}
}
catch (Exception ex)
{
throw new Exception($"InsertAgvMsg:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Error();
}
/// <summary>
/// 插入删除站点信息
/// </summary>
/// <param name="station">站点信息</param>
/// <param name="type">增删</param>
/// <returns></returns>
public static BllResult SqlStationMsg(Base_Station station, string type)
{
#region SQL语句
//try
//{
// string sql = "";
// if (type == "insert")
// {
// sql = string.Format(@"INSERT INTO T_Base_Station(id,StationType,StationArea,IsEnable,IsLocked,Barcode,PreBarcode,StationNo,StationOri,StationLength,StationTim,StationState)VALUES
// ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}');"
// , station.ID, (int)station.StationType, station.StationArea, station.IsEnable == false ? "0" : "1", station.IsLocked == false ? "0" : "1", station.Barcode, station.PreBarcode, station.Name, (int)station.StationOri, station.StationLength, station.StationTim, station.StationState);
// }
// else if (type == "webpointupdate")
// {
// var isEnable = station.IsEnable == false ? "0" : "1";
// var isLocked = station.IsLocked == false ? "0" : "1";
// sql = string.Format(
// $"UPDATE T_Base_Station SET StationType='{(int)station.StationType}',IsEnable='{isEnable}',Locked='{isLocked}',Barcode='{station.Barcode}',PreBarcode='{station.PreBarcode}',StationNo='{station.Name}',StationOri='{(int)station.StationOri}',StationLength='{station.StationLength}',StationTim='{station.StationTim}' WHERE ID='{station.ID}';");
// }
// else
// {
// sql = string.Format(@"DELETE FROM T_Base_Station WHERE id = '{0}';", station.ID);
// }
// int intSqlResult = DbHelperSQL.ExecuteSql(sql);
// if (intSqlResult < 1) return false;
//}
//catch (Exception ex)
//{
// throw new Exception("SqlStationMsg:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
//return true;
#endregion SQL语句
try
{
BllResult bllResult = null;
if (type == "insert")
{
bllResult = _stationDb.Insert(station);
//return BllResult.Sucess();
}
else if (type == "webpointupdate")
{
bllResult = _stationDb.Update(a => a.ID == station.ID, a => new Base_Station()
{
StationType = station.StationType,
IsEnable = station.IsEnable,
IsLocked = station.IsLocked,
Barcode = station.Barcode,
PreBarcode = station.PreBarcode,
Name = station.Name,
StationDirection = station.StationDirection,
LowHeight = station.LowHeight,
Tim = station.Tim,
});
}
else
{
bllResult = _stationDb.Delete(station);
}
if (bllResult.IsSuccess)
{
return BllResult.Success();
}
}
catch (Exception ex)
{
throw new Exception($"SqlStationMsg:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Error();
}
/// <summary>
/// AGV-任务群组绑定
/// </summary>
/// <param name="taskAgvGroup"></param>
/// <param name="type"></param>
/// <returns></returns>
public static BllResult SqlTaskAgvGroup(Config_TaskAgvGroup taskAgvGroup, string type)
{
#region SQL语句
//try
//{
// string sql = "";
// BllResult bllResult = null;
// if (type == "insert")
// {
// sql = string.Format(@"INSERT INTO T_Config_TaskAgvGroup (taskGroup ,agvGroup) VALUES ('{0}','{1}');", taskAgvGroup.TaskGroup, taskAgvGroup.AgvGroup);
// bllResult = _taskAgvGroupDb.Insert(taskAgvGroup);
// }
// else
// {
// sql = string.Format(@"DELETE FROM T_Config_TaskAgvGroup WHERE taskGroup = '{0}' AND agvGroup = '{1}';", taskAgvGroup.TaskGroup, taskAgvGroup.AgvGroup);
// bllResult = _taskAgvGroupDb.Delete(taskAgvGroup);
// }
// int intSqlResult = DbHelperSQL.ExecuteSql(sql);
// if (intSqlResult < 1) return false;
//}
//catch (Exception ex)
//{
// throw new Exception("SqlTaskAgvGroup:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
//return true;
#endregion SQL语句
try
{
BllResult bllResult = null;
if (type == "insert")
{
bllResult = _taskAgvGroupDb.Insert(taskAgvGroup);
}
else
{
bllResult = _taskAgvGroupDb.Delete(it => it.TaskType == taskAgvGroup.TaskType
&& it.AgvGroup == taskAgvGroup.AgvGroup);
}
if (bllResult.IsSuccess)
{
return BllResult.Success();
}
}
catch (Exception ex)
{
throw new Exception($"SqlTaskAgvGroup:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Error();
}
/// <summary>
/// 更新AGV的IP地址到数据库
/// </summary>
/// <returns></returns>
public static BllResult UpdateAgvIP(ClientControl clientControl)
{
#region SQL语句
//DataSet ds = DbHelperSQL.Query("SELECT Ip FROM T_Base_Agv WHERE strAgvNo = '" + clientControl.AgvNo + "';");
//string agvIP = ds.Tables[0].Rows[0].ItemArray[0].ToString();
//if (agvIP == "" || agvIP != clientControl.ClientIp)
//{
// string sqlUpdate = string.Format(@"UPDATE T_Base_Agv SET Ip = '{1}' WHERE strAgvNo = '{0}';", clientControl.AgvNo, clientControl.ClientIp);
// int intSqlResult = DbHelperSQL.ExecuteSql(sqlUpdate);
//}
#endregion SQL语句
var agv = App.AgvList.FirstOrDefault(x => x.AgvNo == clientControl.AgvNo);
if (agv != null)
{
if (agv.IP == "" || agv.IP != clientControl.ClientIp)
{
Dictionary<string, object> lstColumnValues = new Dictionary<string, object>
{
{ "Ip", clientControl.ClientIp }
};
_agvDb.Update(lstColumnValues, a => a.AgvNo == agv.AgvNo);
}
}
return BllResult.Success();
}
/// <summary>
/// 更新旋转点及其周围点
/// </summary>
/// <param name="point"></param>
/// <param name="type"></param>
/// <returns></returns>
public static BllResult SqlConfigPointMsg(List<Base_Point> point)
{
try
{
List<Base_Point> points = new List<Base_Point>();
List<Config_Point> cpoints = new List<Config_Point>();
Dictionary<string, List<Base_Point>> keyValues = new Dictionary<string, List<Base_Point>>();
foreach (Base_Point turnPoint in point.FindAll(a => a.PointType == EnumMsg.PointType.整体旋转点 || a.PointType == EnumMsg.PointType.托盘旋转点))
{
List<Base_Point> aroundPoints = new List<Base_Point>();
if (turnPoint.XPosPoint != null) { aroundPoints.Add(turnPoint.XPosPoint); }
if (turnPoint.XNegPoint != null) { aroundPoints.Add(turnPoint.XNegPoint); }
if (turnPoint.YPosPoint != null) { aroundPoints.Add(turnPoint.YPosPoint); }
if (turnPoint.YNegPoint != null) { aroundPoints.Add(turnPoint.YNegPoint); }
Base_Point rightUpPoint = App.PointList.Find(a => a.IntX == turnPoint.IntX + 3 && a.IntY == turnPoint.IntY + 3);
if (rightUpPoint != null && rightUpPoint.PointType == EnumMsg.PointType.整体旋转点)
{
if (keyValues.ContainsKey(rightUpPoint.Barcode)) continue;
aroundPoints.Add(rightUpPoint);
}
Base_Point rightDownPoint = App.PointList.Find(a => a.IntX == turnPoint.IntX + 3 && a.IntY == turnPoint.IntY - 3);
if (rightDownPoint != null && rightDownPoint.PointType == EnumMsg.PointType.整体旋转点)
{
if (keyValues.ContainsKey(rightDownPoint.Barcode)) continue;
aroundPoints.Add(rightDownPoint);
}
Base_Point leftUpPoint = App.PointList.Find(a => a.IntX == turnPoint.IntX - 3 && a.IntY == turnPoint.IntY + 3);
if (leftUpPoint != null && leftUpPoint.PointType == EnumMsg.PointType.整体旋转点)
{
if (keyValues.ContainsKey(leftUpPoint.Barcode)) continue;
aroundPoints.Add(leftUpPoint);
}
Base_Point leftDownPoint = App.PointList.Find(a => a.IntX == turnPoint.IntX - 3 && a.IntY == turnPoint.IntY - 3);
if (leftDownPoint != null && (leftDownPoint.PointType == EnumMsg.PointType.整体旋转点 || leftDownPoint.XLength < 1000))
{
if (keyValues.ContainsKey(leftDownPoint.Barcode)) continue;
aroundPoints.Add(leftDownPoint);
}
if (aroundPoints.Count < 4 && turnPoint.PointType == EnumMsg.PointType.整体旋转点) continue;
keyValues.Add(turnPoint.Barcode, aroundPoints);
}
if (keyValues.Keys.Count == 0) return BllResult.Success();
for (int i = 0; i < keyValues.Keys.Count; i++)
{
int no = i + 1;
string turnBarcode = keyValues.ElementAt(i).Key;
Base_Point turnPoint = App.PointList.First(a => a.Barcode == turnBarcode);
//获取旋转点
var newCPoint = new Config_Point()
{
No = no,
IntX = turnPoint.IntX,
IntY = turnPoint.IntY,
Barcode = turnBarcode,
AreaType = turnPoint.AreaType,
RegionName = ""
};
cpoints.Add(newCPoint);
for (int j = 0; j < keyValues.ElementAt(i).Value.Count; j++)
{
int num = keyValues.ElementAt(i).Value.Count;
//获取旋转周围附属点
Base_Point aroundPoint = keyValues.ElementAt(i).Value.ElementAt(j);
var newCPoint1 = new Config_Point()
{
No = no,
IntX = aroundPoint.IntX,
IntY = aroundPoint.IntY,
Barcode = aroundPoint.Barcode,
AreaType = aroundPoint.AreaType,
RegionName = ""
};
cpoints.Add(newCPoint1);
}
}
var inCpoint = _cgPointDb.Insert(cpoints);
if (inCpoint.IsSuccess)
{
return BllResult.Success();
}
}
catch (Exception ex)
{
throw new Exception($"SqlConfigPointMsg:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Success();
}
#endregion 更新完成
/// <summary>
/// 删除过期日志
/// </summary>
/// <returns></returns>
public static void DataMaintain()
{
if (!int.TryParse(ConfigurationManager.AppSettings["DataPeriod"], out int dataPeriod))
{
var msg = "配置文件没有设置【日志过期时间】,无法进行数据维护,请设置!";
App.ExFile.MessageError("DataMaintain", msg);
return;
};
var acceptTime = DateTime.Now.AddDays(dataPeriod);
var selectResult = _taskLogDb.Query<string>(t => t.AcceptTime < acceptTime,
t => t.TaskNo,
t => t.AcceptTime,
500);
if (selectResult.IsSuccess)
{
_sonTaskLogDb.Delete(t => selectResult.Data.Contains(t.TaskNo));
_taskLogDb.Delete(t => selectResult.Data.Contains(t.TaskNo));
}
}
}
}