Skip to content

Commit d89121d

Browse files
ericpohlEric Pohl
andauthored
Catch OverflowException in BinaryReader.ReadImplementGeometryValues (#245)
and add the error to plugin errors collection Handle null TaskMapper.DeviceElementHierarchies in TimeLogMapper.ImportTimeLog() Remove duplicate using statement in GuidancePatternMapper Co-authored-by: Eric Pohl <eric.pohl@corteva.com>
1 parent 45296e8 commit d89121d

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

ISOv4Plugin/Mappers/GuidancePatternMapper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using AgGateway.ADAPT.ISOv4Plugin.ISOEnumerations;
1414
using AgGateway.ADAPT.ISOv4Plugin.ISOModels;
1515
using AgGateway.ADAPT.Representation.UnitSystem;
16-
using AgGateway.ADAPT.ISOv4Plugin.ExtensionMethods;
1716

1817
namespace AgGateway.ADAPT.ISOv4Plugin.Mappers
1918
{

ISOv4Plugin/Mappers/TimeLogMapper.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.IO;
88
using System.Linq;
9+
using AgGateway.ADAPT.ApplicationDataModel.ADM;
910
using AgGateway.ADAPT.ApplicationDataModel.Common;
1011
using AgGateway.ADAPT.ApplicationDataModel.Equipment;
1112
using AgGateway.ADAPT.ApplicationDataModel.LoggedData;
@@ -347,7 +348,7 @@ protected IEnumerable<OperationData> ImportTimeLog(ISOTask loggedTask, ISOTimeLo
347348
Dictionary<ISODevice, HashSet<string>> loggedDeviceElementsByDevice = new Dictionary<ISODevice, HashSet<string>>();
348349
foreach (string deviceElementID in deviceElementIDs)
349350
{
350-
ISODeviceElement isoDeviceElement = TaskDataMapper.DeviceElementHierarchies.GetISODeviceElementFromID(deviceElementID);
351+
ISODeviceElement isoDeviceElement = TaskDataMapper.DeviceElementHierarchies?.GetISODeviceElementFromID(deviceElementID);
351352
if (isoDeviceElement != null)
352353
{
353354
ISODevice device = isoDeviceElement.Device;
@@ -724,16 +725,16 @@ protected virtual IEnumerable<ISOSpatialRow> ReadTimeLog(ISOTimeLog timeLog, str
724725
return null;
725726
}
726727

727-
internal static Dictionary<byte, int> ReadImplementGeometryValues(IEnumerable<byte> dlvsToRead, ISOTime templateTime, string filePath, int version)
728+
internal static Dictionary<byte, int> ReadImplementGeometryValues(IEnumerable<byte> dlvsToRead, ISOTime templateTime, string filePath, int version, IList<IError> errors)
728729
{
729-
return BinaryReader.ReadImplementGeometryValues(filePath, templateTime, dlvsToRead, version);
730+
return BinaryReader.ReadImplementGeometryValues(filePath, templateTime, dlvsToRead, version, errors);
730731
}
731732

732733
protected class BinaryReader
733734
{
734735
private static readonly DateTime _firstDayOf1980 = new DateTime(1980, 01, 01);
735736

736-
public static Dictionary<byte, int> ReadImplementGeometryValues(string filePath, ISOTime templateTime, IEnumerable<byte> desiredDLVIndices, int version)
737+
public static Dictionary<byte, int> ReadImplementGeometryValues(string filePath, ISOTime templateTime, IEnumerable<byte> desiredDLVIndices, int version, IList<IError> errors)
737738
{
738739
Dictionary<byte, int> output = new Dictionary<byte, int>();
739740
List<byte> desiredIndexes = desiredDLVIndices.ToList();
@@ -775,14 +776,22 @@ public static Dictionary<byte, int> ReadImplementGeometryValues(string filePath,
775776
{
776777
//A desired DLV is reported here
777778
int value = ReadInt32(null, true, false, binaryReader).GetValueOrDefault();
778-
if (!output.ContainsKey(dlvIndex))
779+
try
779780
{
780-
output.Add(dlvIndex, value);
781+
if (!output.ContainsKey(dlvIndex))
782+
{
783+
output.Add(dlvIndex, value);
784+
}
785+
else if (Math.Abs(value) > Math.Abs(output[dlvIndex]))
786+
{
787+
//Values should be all the same, but prefer the furthest from 0
788+
output[dlvIndex] = value;
789+
}
781790
}
782-
else if (Math.Abs(value) > Math.Abs(output[dlvIndex]))
791+
catch (OverflowException ex)
783792
{
784-
//Values should be all the same, but prefer the furthest from 0
785-
output[dlvIndex] = value;
793+
// If value == int.MinValue, Math.Abs(value) will throw System.OverflowException: Negating the minimum value of a twos complement number is invalid.
794+
errors.Add(new Error() { Description = ex.Message, Id = ex.GetType().ToString(), Source = ex.Source, StackTrace = ex.StackTrace });
786795
}
787796
}
788797
else
@@ -1047,7 +1056,7 @@ private static SpatialValue CreateSpatialValue(ISOTime templateTime, byte order,
10471056
if (matchingDlv == null)
10481057
return null;
10491058

1050-
ISODeviceElement det = deviceHierarchies.GetISODeviceElementFromID(matchingDlv.DeviceElementIdRef);
1059+
ISODeviceElement det = deviceHierarchies?.GetISODeviceElementFromID(matchingDlv.DeviceElementIdRef);
10511060
ISODevice dvc = det?.Device;
10521061
ISODeviceProcessData dpd = dvc?.FirstOrDefaultDeviceProcessData(matchingDlv.ProcessDataIntDDI);
10531062

ISOv4Plugin/ObjectModel/DeviceElementHierarchy.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
using System.Collections.Generic;
1212
using System.Linq;
1313
using System.IO;
14-
using System.Runtime.InteropServices;
15-
using System.Security.Cryptography.X509Certificates;
1614
using AgGateway.ADAPT.ApplicationDataModel.Equipment;
1715
using AgGateway.ADAPT.ISOv4Plugin.Mappers.Manufacturers;
1816
using AgGateway.ADAPT.ISOv4Plugin.Mappers;
17+
using AgGateway.ADAPT.ApplicationDataModel.ADM;
1918

2019
namespace AgGateway.ADAPT.ISOv4Plugin.ObjectModel
2120
{
2221
public class DeviceElementHierarchies
2322
{
23+
private readonly List<IError> _errors;
24+
2425
public DeviceElementHierarchies(IEnumerable<ISODevice> devices,
2526
RepresentationMapper representationMapper,
2627
bool mergeBins,
@@ -29,6 +30,7 @@ public DeviceElementHierarchies(IEnumerable<ISODevice> devices,
2930
TaskDataMapper taskDataMapper)
3031
{
3132
Items = new Dictionary<string, DeviceHierarchyElement>();
33+
_errors = taskDataMapper.Errors;
3234

3335
//Track any device element geometries not logged as a DPT
3436
Dictionary<string, List<string>> missingGeometryDefinitions = new Dictionary<string, List<string>>();
@@ -156,7 +158,7 @@ public void FillDPDGeometryDefinitions(Dictionary<string, List<string>> missingD
156158
string binaryPath = taskDataPath.GetDirectoryFiles(binaryName, SearchOption.TopDirectoryOnly).FirstOrDefault();
157159
if (binaryPath != null)
158160
{
159-
Dictionary<byte, int> timelogValues = Mappers.TimeLogMapper.ReadImplementGeometryValues(dlvsToRead.Select(d => d.Index), time, binaryPath, version);
161+
Dictionary<byte, int> timelogValues = Mappers.TimeLogMapper.ReadImplementGeometryValues(dlvsToRead.Select(d => d.Index), time, binaryPath, version, _errors);
160162

161163
foreach (byte reportedDLVIndex in timelogValues.Keys)
162164
{

0 commit comments

Comments
 (0)