Repository navigation
Expand file tree
/
Copy pathDetectVehicleBmw.cs
More file actions
1243 lines (1091 loc) · 53.3 KB
/
Copy pathDetectVehicleBmw.cs
File metadata and controls
1243 lines (1091 loc) · 53.3 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using BmwDeepObd;
using EdiabasLib;
namespace BmwFileReader
{
public class DetectVehicleBmw : DetectVehicleBmwBase
{
[XmlInclude(typeof(EcuInfo)), XmlInclude(typeof(VehicleStructsBmw.VehicleSeriesInfo))]
[XmlType("VehicleDataBmw")]
public class VehicleDataBmw
{
public const string DataVersion = "9";
public string GetVersionString(DetectVehicleBmw detectVehicleBmw)
{
StringBuilder sb = new StringBuilder();
sb.Append(DataVersion);
if (!string.IsNullOrEmpty(detectVehicleBmw._fileTimeStamp))
{
sb.Append("/");
sb.Append(detectVehicleBmw._fileTimeStamp);
}
string seriesTimeStamp = VehicleInfoBmw.GetVehicleSeriesInfoTimeStamp();
if (!string.IsNullOrEmpty(seriesTimeStamp))
{
sb.Append("/");
sb.Append(seriesTimeStamp);
}
sb.Append(DataVersion);
return sb.ToString();
}
public VehicleDataBmw()
{
Salapa = new List<string>();
HoWords = new List<string>();
EWords = new List<string>();
ZbWords = new List<string>();
}
public VehicleDataBmw(DetectVehicleBmw detectVehicleBmw)
{
Version = GetVersionString(detectVehicleBmw);
Vin = detectVehicleBmw.Vin;
TypeKey = detectVehicleBmw.TypeKey;
GroupSgdb = detectVehicleBmw.GroupSgbd;
SgdbAdd = detectVehicleBmw.SgbdAdd;
BrName = detectVehicleBmw.BrName;
Series = detectVehicleBmw.Series;
ProductType = detectVehicleBmw.ProductType;
BnType = detectVehicleBmw.BnType;
Brand = detectVehicleBmw.Brand;
TransmissionType = detectVehicleBmw.TransmissionType;
Motor = detectVehicleBmw.Motor;
VehicleSeriesInfo = detectVehicleBmw.VehicleSeriesInfo?.Clone();
EcuList = detectVehicleBmw.EcuList?.ConvertAll(x => x.Clone()).ToList();
// ConstructDate is not stored
ConstructYear = detectVehicleBmw.ConstructYear;
ConstructMonth = detectVehicleBmw.ConstructMonth;
Paint = detectVehicleBmw.Paint;
Upholstery = detectVehicleBmw.Upholstery;
StandardFa = detectVehicleBmw.StandardFa;
Salapa = detectVehicleBmw.Salapa;
HoWords = detectVehicleBmw.HoWords;
EWords = detectVehicleBmw.EWords;
ZbWords = detectVehicleBmw.ZbWords;
ILevelShip = detectVehicleBmw.ILevelShip;
ILevelCurrent = detectVehicleBmw.ILevelCurrent;
ILevelBackup = detectVehicleBmw.ILevelBackup;
LifeStartDate = detectVehicleBmw.LifeStartDate;
EcuNameIdentDict = detectVehicleBmw.EcuNameIdentDict != null ? new SerializableDictionary<string, string>(detectVehicleBmw.EcuNameIdentDict).Clone() : null;
Ds2Vehicle = detectVehicleBmw.Ds2Vehicle;
Ds2GroupFiles = detectVehicleBmw.Ds2GroupFiles;
Pin78ConnectRequire = detectVehicleBmw.Pin78ConnectRequire;
TypeKeyProperties = detectVehicleBmw.TypeKeyProperties != null ? new SerializableDictionary<string, string>(detectVehicleBmw.TypeKeyProperties).Clone() : null;
}
public bool Restore(DetectVehicleBmw detectVehicleBmw)
{
string versionString = GetVersionString(detectVehicleBmw);
if (!string.IsNullOrEmpty(Version) && string.Compare(Version, versionString, StringComparison.InvariantCulture) != 0)
{
return false;
}
detectVehicleBmw.Vin = Vin;
detectVehicleBmw.TypeKey = TypeKey;
detectVehicleBmw.GroupSgbd = GroupSgdb;
detectVehicleBmw.SgbdAdd = SgdbAdd;
detectVehicleBmw.BrName = BrName;
detectVehicleBmw.Series = Series;
detectVehicleBmw.ProductType = ProductType;
detectVehicleBmw.BnType = BnType;
detectVehicleBmw.Brand = Brand;
detectVehicleBmw.TransmissionType = TransmissionType;
detectVehicleBmw.Motor = Motor;
detectVehicleBmw.VehicleSeriesInfo = VehicleSeriesInfo?.Clone();
detectVehicleBmw.EcuList = EcuList?.ConvertAll(x => x.Clone()).ToList();
detectVehicleBmw.ConstructYear = ConstructYear;
detectVehicleBmw.ConstructMonth = ConstructMonth;
detectVehicleBmw.Paint = Paint;
detectVehicleBmw.Upholstery = Upholstery;
detectVehicleBmw.StandardFa = StandardFa;
detectVehicleBmw.Salapa = Salapa ?? new List<string>();
detectVehicleBmw.HoWords = HoWords ?? new List<string>();
detectVehicleBmw.EWords = EWords ?? new List<string>();
detectVehicleBmw.ZbWords = ZbWords ?? new List<string>();
detectVehicleBmw.ILevelShip = ILevelShip;
detectVehicleBmw.ILevelCurrent = ILevelCurrent;
detectVehicleBmw.ILevelBackup = ILevelBackup;
detectVehicleBmw.LifeStartDate = LifeStartDate;
detectVehicleBmw.EcuNameIdentDict = EcuNameIdentDict?.Clone();
detectVehicleBmw.Ds2Vehicle = Ds2Vehicle;
detectVehicleBmw.Ds2GroupFiles = Ds2GroupFiles;
detectVehicleBmw.Pin78ConnectRequire = Pin78ConnectRequire;
detectVehicleBmw.TypeKeyProperties = TypeKeyProperties?.Clone();
return true;
}
[XmlElement("Version")] public string Version { get; set; }
[XmlElement("Vin"), DefaultValue(null)] public string Vin { get; set; }
[XmlElement("TypeKey"), DefaultValue(null)] public string TypeKey { get; set; }
[XmlElement("GroupSgdb"), DefaultValue(null)] public string GroupSgdb { get; set; }
[XmlElement("SgdbAdd"), DefaultValue(null)] string SgdbAdd { get; set; }
[XmlElement("BrName"), DefaultValue(null)] public string BrName { get; set; }
[XmlElement("Series"), DefaultValue(null)] public string Series { get; set; }
[XmlElement("ProductType"), DefaultValue(null)] public string ProductType { get; set; }
[XmlElement("BnType"), DefaultValue(null)] public string BnType { get; set; }
[XmlElement("Brand"), DefaultValue(null)] public string Brand { get; set; }
[XmlElement("TransmissionType"), DefaultValue(null)] public string TransmissionType { get; set; }
[XmlElement("Motor"), DefaultValue(null)] public string Motor { get; set; }
[XmlElement("VehicleSeriesInfo"), DefaultValue(null)] public VehicleStructsBmw.VehicleSeriesInfo VehicleSeriesInfo { get; set; }
[XmlElement("EcuList"), DefaultValue(null)] public List<EcuInfo> EcuList { get; set; }
[XmlElement("ConstructYear"), DefaultValue(null)] public string ConstructYear { get; set; }
[XmlElement("ConstructMonth"), DefaultValue(null)] public string ConstructMonth { get; set; }
[XmlElement("Paint"), DefaultValue(null)] public string Paint { get; set; }
[XmlElement("Upholstery"), DefaultValue(null)] public string Upholstery { get; set; }
[XmlElement("StandardFa"), DefaultValue(null)] public string StandardFa { get; set; }
[XmlElement("Salapa"), DefaultValue(null)] public List<string> Salapa { get; set; }
[XmlElement("HoWords"), DefaultValue(null)] public List<string> HoWords { get; set; }
[XmlElement("EWords"), DefaultValue(null)] public List<string> EWords { get; set; }
[XmlElement("ZbWords"), DefaultValue(null)] public List<string> ZbWords { get; set; }
[XmlElement("ILevelShip"), DefaultValue(null)] public string ILevelShip { get; set; }
[XmlElement("ILevelCurrent"), DefaultValue(null)] public string ILevelCurrent { get; set; }
[XmlElement("ILevelBackup"), DefaultValue(null)] public string ILevelBackup { get; set; }
[XmlElement("LifeStartDate"), DefaultValue(null)] public DateTime? LifeStartDate { get; set; }
[XmlElement("EcuNameIdentDict")] public SerializableDictionary<string, string> EcuNameIdentDict { get; set; }
[XmlElement("Ds2Vehicle"), DefaultValue(false)] public bool Ds2Vehicle { get; set; }
[XmlElement("Ds2GroupFiles"), DefaultValue(null)] public string Ds2GroupFiles { get; set; }
[XmlElement("Pin78ConnectRequire")] public bool Pin78ConnectRequire { get; set; }
[XmlElement("TypeKeyProperties")] public SerializableDictionary<string, string> TypeKeyProperties { get; set; }
}
public delegate bool AbortDelegate();
public delegate void ProgressDelegate(int percent);
public AbortDelegate AbortFunc { get; set; }
public ProgressDelegate ProgressFunc { get; set; }
public bool Valid { get; private set; }
public bool Ds2Vehicle { get; private set; }
public string Ds2GroupFiles { get; private set; }
public bool Pin78ConnectRequire { get; private set; }
public Dictionary<string, string> TypeKeyProperties { get; private set; }
private string _bmwDir;
private string _fileTimeStamp;
public const string DataFileExtension = "_VehicleDataBmw.xml";
public DetectVehicleBmw(EdiabasNet ediabas, string bmwDir) : base(ediabas)
{
_bmwDir = bmwDir;
}
public bool DetectVehicleBmwFast(bool detectMotorbikes = false)
{
LogInfoFormat("Try to detect vehicle BMW fast, Motorbikes: {0}", detectMotorbikes);
if (Ediabas == null)
{
LogErrorFormat("DetectVehicleBmwFast: Ediabas not initialized");
return false;
}
ResetValues();
HashSet<string> invalidSgbdSet = new HashSet<string>();
try
{
List<JobInfo> readVinJobsBmwFast = new List<JobInfo>(ReadVinJobsBmwFast);
List<JobInfo> readFaJobsBmwFast = new List<JobInfo>(ReadFaJobsBmwFast);
List<JobInfo> readILevelJobsBmwFast = new List<JobInfo>(ReadILevelJobsBmwFast);
if (!detectMotorbikes)
{
readVinJobsBmwFast.RemoveAll(x => x.Motorbike);
readFaJobsBmwFast.RemoveAll(x => x.Motorbike);
readILevelJobsBmwFast.RemoveAll(x => x.Motorbike);
}
List<Dictionary<string, EdiabasNet.ResultData>> resultSets;
ProgressFunc?.Invoke(0);
JobInfo jobInfoVin = null;
JobInfo jobInfoEcuList = null;
int jobCount = readVinJobsBmwFast.Count + readFaJobsBmwFast.Count + readILevelJobsBmwFast.Count;
int indexOffset = 0;
int index = 0;
foreach (JobInfo jobInfo in readVinJobsBmwFast)
{
LogInfoFormat("Read VIN job: {0} {1}", jobInfo.SgdbName, jobInfo.JobName);
try
{
if (AbortFunc != null && AbortFunc())
{
return false;
}
ProgressFunc?.Invoke(100 * index / jobCount);
ActivityCommon.ResolveSgbdFile(_ediabas, jobInfo.SgdbName);
_ediabas.ArgString = string.Empty;
if (!string.IsNullOrEmpty(jobInfo.JobArgs))
{
_ediabas.ArgString = jobInfo.JobArgs;
}
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob(jobInfo.JobName);
invalidSgbdSet.Remove(jobInfo.SgdbName.ToUpperInvariant());
if (!string.IsNullOrEmpty(jobInfo.EcuListJob))
{
jobInfoEcuList = jobInfo;
}
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (resultDict.TryGetValue(jobInfo.JobResult, out EdiabasNet.ResultData resultData))
{
string vin = resultData.OpData as string;
// ReSharper disable once AssignNullToNotNullAttribute
if (!string.IsNullOrEmpty(vin) && VinRegex.IsMatch(vin))
{
jobInfoVin = jobInfo;
Vin = vin;
BnType = jobInfo.BnType;
LogInfoFormat("Detected VIN: {0}, BnType={1}", Vin, BnType);
break;
}
}
}
}
catch (Exception)
{
invalidSgbdSet.Add(jobInfo.SgdbName.ToUpperInvariant());
LogErrorFormat("No VIN response");
// ignored
}
index++;
}
indexOffset += readVinJobsBmwFast.Count;
index = indexOffset;
if (string.IsNullOrEmpty(Vin))
{
LogErrorFormat("VIN detection failed");
return false;
}
TypeKeyProperties = VehicleInfoBmw.GetVehiclePropertiesFromVin(Vin, _ediabas, _bmwDir, out VehicleInfoBmw.VinRangeInfo vinRangeInfo);
if (vinRangeInfo != null)
{
TypeKey = vinRangeInfo.TypeKey;
ConstructYear = vinRangeInfo.ProdYear;
ConstructMonth = vinRangeInfo.ProdMonth;
}
foreach (JobInfo jobInfo in readFaJobsBmwFast)
{
if (AbortFunc != null && AbortFunc())
{
return false;
}
LogInfoFormat("Read FA job: {0} {1} {2}", jobInfo.SgdbName, jobInfo.JobName, jobInfo.JobArgs ?? string.Empty);
try
{
ProgressFunc?.Invoke(100 * index / jobCount);
if (string.Compare(BnType, jobInfo.BnType, StringComparison.OrdinalIgnoreCase) != 0)
{
LogInfoFormat("Invalid BnType job ignored: {0}, BnType={1}", jobInfo.SgdbName, jobInfo.BnType);
index++;
continue;
}
if (invalidSgbdSet.Contains(jobInfo.SgdbName.ToUpperInvariant()))
{
LogInfoFormat("Invalid SGBD job ignored: {0}, BnType={1}", jobInfo.SgdbName, jobInfo.BnType);
index++;
continue;
}
bool statVcm = string.Compare(jobInfo.JobName, "STATUS_VCM_GET_FA", StringComparison.OrdinalIgnoreCase) == 0;
ActivityCommon.ResolveSgbdFile(_ediabas, jobInfo.SgdbName);
_ediabas.ArgString = string.Empty;
if (!string.IsNullOrEmpty(jobInfo.JobArgs))
{
_ediabas.ArgString = jobInfo.JobArgs;
}
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob(jobInfo.JobName);
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (resultDict.TryGetValue(jobInfo.JobResult, out EdiabasNet.ResultData resultData))
{
if (!statVcm)
{
string fa = resultData.OpData as string;
if (!string.IsNullOrEmpty(fa))
{
ActivityCommon.ResolveSgbdFile(_ediabas, "FA");
_ediabas.ArgString = "1;" + fa;
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob("FA_STREAM2STRUCT");
List<Dictionary<string, EdiabasNet.ResultData>> resultSetsFa = _ediabas.ResultSets;
if (resultSetsFa != null && resultSetsFa.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDictFa = resultSetsFa[1];
if (SetStreamToStructInfo(resultDictFa))
{
break;
}
}
}
}
else
{
string br = resultData.OpData as string;
if (!string.IsNullOrEmpty(br))
{
SetBrInfo(br);
SetStatVcmInfo(resultDict);
SetStatVcmSalpaInfo(resultSets);
break;
}
}
}
}
}
catch (Exception)
{
LogErrorFormat("No BR response");
// ignored
}
index++;
}
indexOffset += readFaJobsBmwFast.Count;
index = indexOffset;
UpdateTypeKeyProperties();
if (!string.IsNullOrEmpty(ConstructYear) && !string.IsNullOrEmpty(ConstructMonth))
{
LogInfoFormat("Construct date: {0}.{1}", ConstructYear, ConstructMonth);
}
bool sp2021Gateway = false;
foreach (JobInfo jobInfo in readILevelJobsBmwFast)
{
if (AbortFunc != null && AbortFunc())
{
return false;
}
ProgressFunc?.Invoke(100 * index / jobCount);
LogInfoFormat("Read ILevel job: {0},{1}", jobInfo.SgdbName, jobInfo.JobName);
if (invalidSgbdSet.Contains(jobInfo.SgdbName.ToUpperInvariant()))
{
LogInfoFormat("Job ignored: {0}", jobInfo.SgdbName);
index++;
continue;
}
if (IsSp2021Gateway(jobInfo.EcuName) && !sp2021Gateway)
{
LogInfoFormat("Job ignored: {0}, EcuName: {1}", jobInfo.SgdbName, jobInfo.EcuName);
index++;
continue;
}
try
{
_ediabas.ResolveSgbdFile(jobInfo.SgdbName);
_ediabas.ArgString = string.Empty;
if (!string.IsNullOrEmpty(jobInfo.JobArgs))
{
_ediabas.ArgString = jobInfo.JobArgs;
}
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob(jobInfo.JobName);
string ecuName = Path.GetFileNameWithoutExtension(_ediabas.SgbdFileName) ?? string.Empty;
if (IsSp2021Gateway(ecuName))
{
sp2021Gateway = true;
}
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
if (SetILevel(resultSets[1], ecuName))
{
break;
}
}
}
catch (Exception)
{
LogErrorFormat("No ILevel response");
// ignored
}
index++;
}
indexOffset += readILevelJobsBmwFast.Count;
index = indexOffset;
if (string.IsNullOrEmpty(ILevelShip))
{
LogErrorFormat("ILevel not found");
}
VehicleStructsBmw.VehicleSeriesInfo vehicleSeriesInfo = VehicleInfoBmw.GetVehicleSeriesInfo(this);
if (vehicleSeriesInfo == null)
{
if (!jobInfoVin.Motorbike)
{
LogInfoFormat("Vehicle series info not found, aborting");
return false;
}
GroupSgbd = jobInfoVin.SgdbName;
LogInfoFormat("Vehicle series info not found, using motorbike group SGBD: {0}", GroupSgbd);
}
else
{
VehicleSeriesInfo = vehicleSeriesInfo;
GroupSgbd = vehicleSeriesInfo.BrSgbd;
SgbdAdd = vehicleSeriesInfo.SgbdAdd;
if (!string.IsNullOrEmpty(vehicleSeriesInfo.BnType))
{
BnType = vehicleSeriesInfo.BnType;
}
Brand = vehicleSeriesInfo.Brand;
}
LogInfoFormat("Group SGBD: {0}, BnType: {1}", GroupSgbd ?? string.Empty, BnType ?? string.Empty);
EcuList.Clear();
if (jobInfoEcuList != null)
{
if (AbortFunc != null && AbortFunc())
{
return false;
}
try
{
ProgressFunc?.Invoke(100 * index / jobCount);
_ediabas.ResolveSgbdFile(jobInfoEcuList.SgdbName);
_ediabas.ArgString = string.Empty;
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob(jobInfoEcuList.EcuListJob);
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
int dictIndex = 0;
foreach (Dictionary<string, EdiabasNet.ResultData> resultDict in resultSets)
{
if (dictIndex == 0)
{
dictIndex++;
continue;
}
string ecuName = string.Empty;
Int64 ecuAdr = -1;
// ReSharper disable once InlineOutVariableDeclaration
EdiabasNet.ResultData resultData;
if (resultDict.TryGetValue("STAT_SG_NAME_TEXT", out resultData))
{
if (resultData.OpData is string)
{
ecuName = (string)resultData.OpData;
}
}
if (resultDict.TryGetValue("STAT_SG_DIAG_ADRESSE", out resultData))
{
if (resultData.OpData is string)
{
string ecuAdrStr = (string)resultData.OpData;
if (!string.IsNullOrEmpty(ecuAdrStr) && ecuAdrStr.Length > 1)
{
string hexString = ecuAdrStr.Trim().Substring(2);
if (Int32.TryParse(hexString, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out Int32 addrValue))
{
ecuAdr = addrValue;
}
}
}
}
if (!string.IsNullOrEmpty(ecuName) && ecuAdr >= 0 && ecuAdr <= VehicleStructsBmw.MaxEcuAddr)
{
if (EcuList.All(ecuInfo => ecuInfo.Address != ecuAdr))
{
string groupSgbd = null;
string sgbd = null;
if (vehicleSeriesInfo != null && vehicleSeriesInfo.EcuList != null)
{
foreach (VehicleStructsBmw.VehicleEcuInfo vehicleEcuInfo in vehicleSeriesInfo.EcuList)
{
if (vehicleEcuInfo.DiagAddr == ecuAdr)
{
groupSgbd = vehicleEcuInfo.GroupSgbd;
sgbd = vehicleEcuInfo.Sgbd;
break;
}
}
}
if (!string.IsNullOrEmpty(groupSgbd))
{
EcuInfo ecuInfo = new EcuInfo(ecuName, ecuAdr, groupSgbd, sgbd);
EcuList.Add(ecuInfo);
}
}
}
dictIndex++;
}
}
}
catch (Exception)
{
LogErrorFormat("No ecu list response");
// ignored
}
indexOffset++;
jobCount++;
index++;
}
ProgressFunc?.Invoke(100 * index / jobCount);
HandleSpecialEcus();
SetVehicleLifeStartDate();
Ds2Vehicle = false;
Valid = true;
return true;
}
catch (Exception)
{
return false;
}
}
public bool DetectVehicleDs2()
{
LogInfoFormat("Try to detect DS2 vehicle");
if (Ediabas == null)
{
LogErrorFormat("DetectVehicleDs2: Ediabas not initialized");
return false;
}
ResetValues();
int jobCount = 1 + ReadVinJobsDs2.Count + ReadIdentJobsDs2.Count + ReadFaJobsDs2.Count;
int indexOffset = 0;
int index = 0;
try
{
List<Dictionary<string, EdiabasNet.ResultData>> resultSets;
ProgressFunc?.Invoke(0);
string groupFiles = null;
try
{
ProgressFunc?.Invoke(100 * index / jobCount);
ActivityCommon.ResolveSgbdFile(_ediabas, "d_0044");
_ediabas.ArgString = "6";
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob("KD_DATEN_LESEN");
string kdData1 = null;
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (resultDict.TryGetValue("KD_DATEN_TEXT", out EdiabasNet.ResultData resultData))
{
if (resultData.OpData is string)
{
kdData1 = (string)resultData.OpData;
}
}
}
_ediabas.ArgString = "7";
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob("KD_DATEN_LESEN");
string kdData2 = null;
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (resultDict.TryGetValue("KD_DATEN_TEXT", out EdiabasNet.ResultData resultData))
{
if (resultData.OpData is string)
{
kdData2 = (string)resultData.OpData;
}
}
}
if (AbortFunc != null && AbortFunc())
{
return false;
}
if (!string.IsNullOrEmpty(kdData1) && !string.IsNullOrEmpty(kdData2))
{
ActivityCommon.ResolveSgbdFile(_ediabas, "grpliste");
_ediabas.ArgString = kdData1 + kdData2 + ";ja";
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob("GRUPPENDATEI_ERZEUGE_LISTE_AUS_DATEN");
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (resultDict.TryGetValue("GRUPPENDATEI", out EdiabasNet.ResultData resultData))
{
if (resultData.OpData is string)
{
groupFiles = (string)resultData.OpData;
LogInfoFormat("KD group files: {0}", groupFiles);
}
}
}
}
if (string.IsNullOrEmpty(groupFiles))
{
LogErrorFormat("KD data empty, using fallback");
groupFiles = AllDs2GroupFiles;
}
}
catch (Exception)
{
LogErrorFormat("Read KD data failed");
// ignored
}
indexOffset += 1;
index = indexOffset;
string detectedVin = null;
if (!string.IsNullOrEmpty(groupFiles))
{
foreach (JobInfo jobInfo in ReadVinJobsDs2)
{
LogInfoFormat("Read VIN job: {0} {1}", jobInfo.SgdbName, jobInfo.JobName);
try
{
ProgressFunc?.Invoke(100 * index / jobCount);
ActivityCommon.ResolveSgbdFile(_ediabas, jobInfo.SgdbName);
_ediabas.ArgString = string.Empty;
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob(jobInfo.JobName);
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (resultDict.TryGetValue(jobInfo.JobResult, out EdiabasNet.ResultData resultData))
{
string vin = resultData.OpData as string;
// ReSharper disable once AssignNullToNotNullAttribute
if (!string.IsNullOrEmpty(vin) && VinRegex.IsMatch(vin))
{
detectedVin = vin;
LogInfoFormat("Detected VIN: {0}", detectedVin);
break;
}
}
}
}
catch (Exception)
{
LogErrorFormat("No VIN response");
// ignored
}
index++;
}
}
else
{
foreach (string fileName in ReadMotorJobsDs2)
{
try
{
LogInfoFormat("Read motor job: {0}", fileName);
ProgressFunc?.Invoke(100 * index / jobCount);
ActivityCommon.ResolveSgbdFile(_ediabas, fileName);
_ediabas.ArgString = string.Empty;
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob("IDENT");
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (EdiabasThread.IsJobStatusOk(resultDict))
{
groupFiles = fileName;
Pin78ConnectRequire = true;
LogInfoFormat("Motor ECUs detected: {0}", groupFiles);
break;
}
}
}
catch (Exception)
{
// ignored
}
index++;
}
}
indexOffset += ReadVinJobsDs2.Count;
index = indexOffset;
Vin = detectedVin;
TypeKeyProperties = VehicleInfoBmw.GetVehiclePropertiesFromVin(detectedVin, _ediabas, _bmwDir, out VehicleInfoBmw.VinRangeInfo vinRangeInfo);
if (vinRangeInfo != null)
{
TypeKey = vinRangeInfo.TypeKey;
ConstructYear = vinRangeInfo.ProdYear;
ConstructMonth = vinRangeInfo.ProdMonth;
}
if (string.IsNullOrEmpty(ConstructYear) || string.IsNullOrEmpty(ConstructMonth))
{
int modelYear = VehicleInfoBmw.GetModelYearFromVin(detectedVin);
if (modelYear >= 0)
{
LogInfoFormat("Model year: {0}", modelYear);
ConstructYear = string.Format(CultureInfo.InvariantCulture, "{0:0000}", modelYear);
ConstructMonth = "01";
}
}
if (!string.IsNullOrEmpty(ConstructYear) && !string.IsNullOrEmpty(ConstructMonth))
{
LogInfoFormat("Construct date: {0}.{1}", ConstructYear, ConstructMonth);
}
if (!string.IsNullOrEmpty(detectedVin) && detectedVin.Length == 17)
{
string typeSnr = detectedVin.Substring(3, 4);
LogInfoFormat("Type SNR: {0}", typeSnr);
foreach (JobInfo jobInfo in ReadIdentJobsDs2)
{
LogInfoFormat("Read vehicle type job: {0} {1}", jobInfo.SgdbName, jobInfo.JobName);
try
{
ProgressFunc?.Invoke(100 * index / jobCount);
ActivityCommon.ResolveSgbdFile(_ediabas, jobInfo.SgdbName);
_ediabas.ArgString = typeSnr;
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob(jobInfo.JobName);
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (resultDict.TryGetValue(jobInfo.JobResult, out EdiabasNet.ResultData resultData))
{
string detectedSeries = resultData.OpData as string;
if (!string.IsNullOrEmpty(detectedSeries) &&
string.Compare(detectedSeries, VehicleInfoBmw.ResultUnknown, StringComparison.OrdinalIgnoreCase) != 0)
{
Series = detectedSeries;
LogInfoFormat("Detected Vehicle series: {0}", Series);
break;
}
}
}
}
catch (Exception)
{
LogErrorFormat("No vehicle type response");
// ignored
}
index++;
}
}
indexOffset += ReadIdentJobsDs2.Count;
index = indexOffset;
if (!string.IsNullOrEmpty(groupFiles))
{
string[] groupArray = groupFiles.Split(',');
foreach (JobInfo jobInfo in ReadFaJobsDs2)
{
LogInfoFormat("Read vehicle FA job: {0} {1}", jobInfo.SgdbName, jobInfo.JobName);
if (groupArray.All(x => string.Compare(x, jobInfo.SgdbName, StringComparison.OrdinalIgnoreCase) != 0))
{
LogInfoFormat("Missing FA SGDB ignored: {0}", jobInfo.SgdbName);
index++;
continue;
}
try
{
ProgressFunc?.Invoke(100 * index / jobCount);
ActivityCommon.ResolveSgbdFile(_ediabas, jobInfo.SgdbName);
_ediabas.ArgString = string.Empty;
if (!string.IsNullOrEmpty(jobInfo.JobArgs))
{
_ediabas.ArgString = jobInfo.JobArgs;
}
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob(jobInfo.JobName);
resultSets = _ediabas.ResultSets;
if (resultSets != null && resultSets.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDict = resultSets[1];
if (resultDict.TryGetValue(jobInfo.JobResult, out EdiabasNet.ResultData resultData))
{
string fa = resultData.OpData as string;
if (!string.IsNullOrEmpty(fa))
{
ActivityCommon.ResolveSgbdFile(_ediabas, "FA");
_ediabas.ArgString = "1;" + fa;
_ediabas.ArgBinaryStd = null;
_ediabas.ResultsRequests = string.Empty;
_ediabas.ExecuteJob("FA_STREAM2STRUCT");
List<Dictionary<string, EdiabasNet.ResultData>> resultSetsFa = _ediabas.ResultSets;
if (resultSetsFa != null && resultSetsFa.Count >= 2)
{
Dictionary<string, EdiabasNet.ResultData> resultDictFa = resultSetsFa[1];
if (SetStreamToStructInfo(resultDictFa))
{
break;
}
}
}
}
}
}
catch (Exception)
{
LogErrorFormat("No read FA response");
// ignored
}
index++;
}
}
indexOffset += ReadFaJobsDs2.Count;
index = indexOffset;
UpdateTypeKeyProperties();
VehicleStructsBmw.VehicleSeriesInfo vehicleSeriesInfo = VehicleInfoBmw.GetVehicleSeriesInfo(this);
if (vehicleSeriesInfo != null)
{
VehicleSeriesInfo = vehicleSeriesInfo;
SgbdAdd = vehicleSeriesInfo.SgbdAdd;
if (!string.IsNullOrEmpty(vehicleSeriesInfo.BnType))
{
BnType = vehicleSeriesInfo.BnType;
}
Brand = vehicleSeriesInfo.Brand;
}
ProgressFunc?.Invoke(100 * index / jobCount);
LogInfoFormat("BnType: {0}", BnType ?? string.Empty);
Ds2GroupFiles = groupFiles;
if (string.IsNullOrEmpty(groupFiles))
{
return false;
}
Ds2Vehicle = true;
Valid = true;
return true;
}
catch (Exception)
{
return false;
}
}
public bool SaveDataToFile(string fileName, string fileTimeStamp = null)
{
try
{
if (!Valid)
{
return false;
}
_fileTimeStamp = fileTimeStamp;
VehicleDataBmw vehicleDataBmw = new VehicleDataBmw(this);
XmlSerializer serializer = new XmlSerializer(typeof(VehicleDataBmw));
using (FileStream fileStream = File.Create(fileName))
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t"
};
using (XmlWriter writer = XmlWriter.Create(fileStream, settings))
{
serializer.Serialize(writer, vehicleDataBmw);
}
}
}
catch (Exception ex)
{