UpdateManage.cs
48.7 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
using RCS.Model.ClientMoel;
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.WinClient.Common;
using RCS.WinClient.WebApi.Service;
using System.Configuration;
using System.Data;
namespace RCS.WinClient.Service
{
public class UpdateManage
{
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 UpdateClientMsg(ClientMsg clientMsg)
{
try
{
Base_Agv agv = clientMsg.ClientAgv;
agv.AgvState = (EnumMsg.State)clientMsg.NewState;
agv.HeightState = (EnumMsg.HeightState)clientMsg.NewHeightState;
agv.Voltage = clientMsg.NewVoltage;
agv.Heart = DateTime.Now;
agv.Error = ComnMethod.GetAgvError(clientMsg.ErrorList);
if (agv.HeightState == EnumMsg.HeightState.中位 && agv.AgvTask != null &&
agv.AgvTask.SubTaskList.Count > 0 && agv.AgvTask.SubTaskList[0].SubTaskType == EnumMsg.AgvTaskType.行走)
{
agv.Error += "agv处于中位异常";
}
if (agv.AgvTask != null && agv.StopReason.Contains("行走任务需载货"))
{
agv.Error += "agv行走任务需载货架,小车状态必须是高位;";
}
agv.AgvOri = clientMsg.NewAgvOri;
agv.DialOri = clientMsg.NewDialOri;
agv.RunDistance = clientMsg.RunDistance;
agv.XLength = clientMsg.NewX;
agv.YLength = clientMsg.NewY;
agv.InStock = (EnumMsg.InStock)ComnMethod.RollerStote(clientMsg.InStock)[0];
agv.RollerState = (EnumMsg.RollerState)ComnMethod.RollerStote(clientMsg.InStock)[1];
agv.Barcode = clientMsg.NewBarcode;
//如果是点位是空,就不往下走更新信息。
if (string.IsNullOrWhiteSpace(clientMsg.NewBarcode))
{
return BllResult.Sucess();
}
//获取当前点是否在地图中
Base_Point currentPoint = App.PointList.FirstOrDefault(a => a.Barcode == clientMsg.NewBarcode);
if (currentPoint == null)
{
clientMsg.ClientAgv.StopReason = ComnMethod.GetStopReason(clientMsg.ClientAgv.StopReason, clientMsg.NewErrMsg.ToString() + "新码值:" + clientMsg.NewBarcode + ":旧码值:" + clientMsg.ClientAgv.Barcode);
AcsPushBuild.PushAgv(agv);
return BllResult.Error();
}
agv.CurrMap = App.PointList.Find(a => a.Barcode == agv.Barcode).AreaType.ToString();
currentPoint.LockedAgv = clientMsg.ClientAgv;
if (clientMsg.ClientAgv.AgvState != EnumMsg.State.忙碌)
{
List<Base_Point> lockedPoint = new List<Base_Point>();
lockedPoint.Add(currentPoint);
RelieveLocked(lockedPoint, clientMsg.ClientAgv);
}
////赋值AGV顶升后的长度
//if (agv.HeightState != EnumMsg.HeightState.低位 && agv.AgvTask != null)
//{
// var station = App.StationList.Find(a => a.Barcode == agv.AgvTask.EndBarcode);
// if (station != null) agv.CurLength = station.Length;
//}
//else if(agv.HeightState == EnumMsg.HeightState.低位)
//{
// agv.CurLength = agv.Length;
//}
//20181030将有异常小车插入异常小车表中
if (agv.Error != "")
{
if (!App.errAgv.Contains(agv.AgvNo) && !agv.Error.Contains("agv"))
{
SqlErrAgv(agv, "insert");
App.errAgv.Add(agv.AgvNo);
}
}
else
{
//var queryresult = _agvDb.Query(it => it.ErrorTime != null && it.Error == "" && it.AgvNo == agv.AgvNo);
//if (queryresult.Data != null && queryresult.Data.Count > 0)
//{
// SqlErrAgv(agv, "update");
//}
if (App.errAgv.Contains(agv.AgvNo))
{
SqlErrAgv(agv, "update");
App.errAgv.Remove(agv.AgvNo);
}
}
AcsPushBuild.PushAgv(agv);
//_agvDb.Update(it => it.AgvNo == clientMsg.AgvNo, it => new Base_Agv()
//{
// Barcode = clientMsg.NewBarcode,
// AgvState = (EnumMsg.State)clientMsg.NewState,
// HeightState = (EnumMsg.HeightState)clientMsg.NewHeightState,
// Voltage = clientMsg.NewVoltage,
// CurLength = agv.CurLength,
// Heart = DateTime.Now,
// Error = ComnMethod.GetAgvError(clientMsg.ErrorList),
// CurrMap = agv.CurrMap,
//});
return BllResult.Sucess();
}
catch (Exception ex)
{
throw new Exception($"UpdateClientMsg:: {ex.Message},小车编码:{clientMsg.ClientAgv.AgvNo},\n 堆栈:{ex.StackTrace}");
}
}
/// <summary>
/// 更新点信息
/// </summary>
/// <param name="clientMsg">小车客户端信息</param>
/// <returns></returns>
public static BllResult SqlPointMsg(Base_Point point, string type)
{
try
{
if (type == "insert")
{
point.IsEnable = true;
point.IsOccupy = false;
point.OccupyAgvNo = "";
point.AgvOri = 0;
point.DialOri = 0;
var insertresult = _pointDb.Insert(point);
if (insertresult.Success)
{
return BllResult.Sucess();
}
}
else
{
var queryresult = _pointDb.Query(it => it.AreaType == point.AreaType);
if (!queryresult.Success)
{
if (queryresult.Msg == "未查询到数据")
{
return BllResult.Sucess();
}
else
{
return BllResult.Error();
}
}
else
{
var deleteresult = _pointDb.Delete(it => it.AreaType == point.AreaType);
if (deleteresult.Success)
{
return BllResult.Sucess();
}
}
//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 SqlDemoPointMsg(Base_Point point, string type)
{
try
{
if (type == "simInsert")
{
point.IsOccupy = false;
point.OccupyAgvNo = "";
point.AgvOri = 0;
point.DialOri = 0;
var insetresult = _demoPointDb.Insert(point);
if (insetresult.Success)
{
return BllResult.Sucess();
}
}
else
{
var queryresult = _demoPointDb.Query(it => it.AreaType == point.AreaType);
if (!queryresult.Success)
{
if (queryresult.Msg == "未查询到数据")
{
return BllResult.Sucess();
}
else
{
return BllResult.Error();
}
}
else
{
var deleteresult = _demoPointDb.Delete(it => it.AreaType == point.AreaType);
if (deleteresult.Success)
{
return BllResult.Sucess();
}
}
}
}
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 UpdateAgvMsg(Base_Agv agv)
{
try
{
var updateresult = _agvDb.Update(it => it.AgvNo == agv.AgvNo, it => new Base_Agv()
{
Barcode = agv.Barcode,
IsEnable = agv.IsEnable,
ChooseStation = agv.ChooseStation,
Milage = agv.Milage,
Group = agv.Group,
StartVoltage = agv.StartVoltage,
AgvState = agv.AgvState,
HeightState = agv.HeightState,
CurLength = agv.CurLength,
Heart = agv.Heart,
Error = agv.Error,
CurrMap = agv.CurrMap,
});
if (!updateresult.Success)
{
return BllResult.Error();
}
}
catch (Exception ex)
{
throw new Exception($"UpdateAgvMsg:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
#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.Success || !deleteresult2.Success)
{
return BllResult.Error();
}
}
else
{
var updateresult = _sonTaskDb.Update(sonTask, it => new { it.SubTaskNo, it.TaskNo });
if (!updateresult.Success)
{
return BllResult.Error();
}
}
}
catch (Exception ex)
{
throw new Exception($"UpdateSonTask:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
/// <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.Success)
{
return BllResult.Error();
}
}
catch (Exception ex)
{
throw new Exception($"UpdateStation:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
/// <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,
Length = station.Length,
});
if (!updateresult.Success)
{
return BllResult.Error();
}
}
catch (Exception ex)
{
throw new Exception($"UpdateStationBarcode:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
/// <summary>
/// 解除小车不应该lock的点
/// </summary>
/// <param name="lockedPointList">需要继续锁定的点</param>
/// <param name="agv"></param>
public static BllResult RelieveLocked(List<Base_Point> lockedPointList, Base_Agv agv)
{
try
{
List<Base_Point> lockedList = App.PointList.Where(a => a.LockedAgv == agv).ToList();
foreach (Base_Point point in lockedList)
{
if (lockedPointList.Contains(point)) continue;
point.LockedAgv = null;
}
}
catch (Exception ex)
{
throw new Exception($"relieveLocked:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
/// <summary>
/// 重建路径
/// 删除路径,更新子任务状态
/// </summary>
/// <param name="sonTask"></param>
public static BllResult ResetPath(Base_SubTask sonTask)
{
try
{
if (sonTask.SubTaskType != EnumMsg.AgvTaskType.行走) return BllResult.Sucess();
var deleteresult = _pathPointDb.Delete(it => it.SubTaskNo == sonTask.SubTaskNo);
var updateresult = _sonTaskDb.Update(it => it.SubTaskNo == sonTask.SubTaskNo,
it => new Base_SubTask() { SubTaskState = EnumMsg.SubTaskState.重建路径中 });
if (deleteresult.Success && updateresult.Success)
{
sonTask.PathPointList.Clear();
sonTask.SubTaskState = EnumMsg.SubTaskState.重建路径中;
}
}
catch (Exception ex)
{
throw new Exception($"ResetPath:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
/// <summary>
/// 将异常小车插入异常信息表和任务表中
/// </summary>
/// <param name="agv"></param>
/// <returns></returns>
public static BllResult SqlErrAgv(Base_Agv agv, string type)
{
#region SQL语句
//try
//{
// string sql = "";
// if (type == "insert")
// {
// string taskNo = "";
// if (agv.AgvTask != null) taskNo = agv.AgvTask.TaskNo;
// sql = string.Format(@"INSERT INTO t_log_agverrmsg (strAgvNo , strBarcode , agvErrMessage , startErrTime , strTaskNo)
// VALUES ('{0}','{1}','{2}','{3}','{4}');"
// , agv.AgvNo, agv.Barcode, agv.AgvError, agv.AgvHeart.ToString(), taskNo);
// sql += string.Format(@"UPDATE T_Base_Agv SET ErrorTime='{1}' WHERE strAgvNo='{0}';", agv.AgvNo, agv.AgvHeart.ToString());
// if (agv.AgvTask != null)
// {
// sql += string.Format(@"UPDATE T_Task SET taskErrMsg = '{1}' WHERE strTaskNo = '{0}';", agv.AgvTask.TaskNo, agv.AgvError);
// }
// }
// else
// {
// sql = string.Format(@"UPDATE t_log_agverrmsg SET endErrTime='{1}' WHERE strAgvNo='{0}' AND endErrTime IS NULL;",
// agv.AgvNo, agv.AgvHeart.ToString());
// sql += string.Format(@"UPDATE T_Base_Agv SET ErrorTime = null WHERE strAgvNo='{0}';", agv.AgvNo);
// if (agv.AgvTask != null)
// {
// sql += string.Format(@"UPDATE T_Task SET agvErrorCode = 0,taskErrMsg = null WHERE strTaskNo = '{0}';", agv.AgvTask.TaskNo);
// }
// }
// bool isSqlResult = DbHelperSQL.SqlTransOperator(sql);
// return isSqlResult;
//}
//catch (Exception ex)
//{
// throw new Exception("SqlErrAgv:{ex.Message},\n 堆栈:{ex.StackTrace}");
//}
#endregion SQL语句
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);
//更新当前AGV异常时间
//_agvDb.Update(it => it.AgvNo == agv.AgvNo, it => new Base_Agv() { ErrorTime = agv.Heart });
//如果当前AGV存在任务,更新任务异常信息
//if (!string.IsNullOrEmpty(taskNo))
//{
// _taskDb.Update(it => it.TaskNo == taskNo, it => new Base_Task() { TaskErrMsg = agv.Error });
//}
}
else
{
_agvErrMsgLogDb.Update(it => it.AgvNo == agv.AgvNo && it.EndErrTime == null,
it => new Log_AgvErrMsg() { EndErrTime = agv.Heart });
//_agvDb.Update(it => it.AgvNo == agv.AgvNo, it => new Base_Agv() { ErrorTime = null });
//if (!string.IsNullOrEmpty(taskNo))
//{
// _taskDb.Update(it => it.TaskNo == taskNo, it => new Base_Task() { TaskErrMsg = "" });
//}
}
}
catch (Exception ex)
{
throw new Exception($"SqlErrAgv:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
/// <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
{
bool isInsert = false;
//查询AGV插入数据库之前的状态
var queryresult = _agvStateLogDb.Query(it => it.AgvNo == agv.AgvNo && it.EndTime == null, it => it.StartTime);
if (queryresult.Success)
{
//判断插入的数据和待插入的是否一样
if (queryresult.Data.AgvState != state)
{
//更新之前的结束时间
_agvStateLogDb.Update(it => it.AgvNo == agv.AgvNo && it.EndTime == null,
it => new Log_AgvState() { EndTime = DateTime.Now });
//插入新数据
isInsert = true;
}
}
else if (queryresult.Msg == "未查询到数据")
{
//插入新数据
isInsert = true;
}
if (isInsert)
{
var addAgv = new Log_AgvState()
{
AgvNo = agv.AgvNo,
AgvState = state,
StartTime = DateTime.Now,
EndTime = null,
};
var insertresult = _agvStateLogDb.Insert(addAgv);
if (!insertresult.Success)
{
return BllResult.Error();
}
}
}
catch (Exception ex)
{
throw new Exception($"InsertAgvState:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
/// <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.TaskID, 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.Success && result1.Success)
{
return BllResult.Sucess();
}
}
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.TaskID == "充电")
{
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.Sucess();
}
/// <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.Success)
{
return BllResult.Sucess();
}
}
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,
StationOri = station.StationOri,
Length = station.Length,
Tim = station.Tim,
});
}
else
{
bllResult = _stationDb.Delete(station);
}
if (bllResult.Success)
{
return BllResult.Sucess();
}
}
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.TaskGroup == taskAgvGroup.TaskGroup
&& it.AgvGroup == taskAgvGroup.AgvGroup);
}
if (bllResult.Success)
{
return BllResult.Sucess();
}
}
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 agvQuery = _agvDb.Query(a => a.AgvNo == clientControl.AgvNo);
if (agvQuery.Success)
{
var findAgv = agvQuery.Data[0];
if (findAgv.IP == "" || findAgv.IP != clientControl.ClientIp)
{
Dictionary<string, object> lstColumnValues = new Dictionary<string, object>();
lstColumnValues.Add("Ip", clientControl.ClientIp);
_agvDb.Update(lstColumnValues, a => a.AgvNo == findAgv.AgvNo);
}
}
return BllResult.Sucess();
}
/// <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.Sucess();
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.Success)
{
return BllResult.Sucess();
}
}
catch (Exception ex)
{
throw new Exception($"SqlConfigPointMsg:: {ex.Message},\n 堆栈:{ex.StackTrace}");
}
return BllResult.Sucess();
}
#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.Success)
{
_sonTaskLogDb.Delete(t => selectResult.Data.Contains(t.TaskNo));
_taskLogDb.Delete(t => selectResult.Data.Contains(t.TaskNo));
}
}
}
}