Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,6 @@ FakesAssemblies/

# Jetbrains Rider
.idea/

# VSCode
.vscode/
14 changes: 0 additions & 14 deletions .vscode/launch.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public IEnumerable<SpatialRecord> Map(IEnumerable<ISOSpatialRow> isoSpatialRows,
pan.AllocationStamp.Start.Value.Minute == firstSpatialRow.TimeStart.Minute &&
pan.AllocationStamp.Start.Value.Second == firstSpatialRow.TimeStart.Second)
{
_effectiveTimeZoneOffset = firstSpatialRow.TimeStart - pan.AllocationStamp.Start.Value;
_effectiveTimeZoneOffset = TaskDataMapper.ValidateTimezoneOffset(firstSpatialRow.TimeStart, pan.AllocationStamp.Start.Value);
if (!_effectiveTimeZoneOffset.HasValue)
{
_taskDataMapper.AddError($"Unable to determine effective timezone offset from comparison of spatial record and product allocation timestamps. Monitor date/time setting may be invalid.");
}
}
}
}
Expand Down Expand Up @@ -202,22 +206,24 @@ private bool GovernsTimestamp(ISOProductAllocation p, SpatialRecord spatialRecor

// Comparing DateTime values with different Kind values leads to inaccurate results.
// Convert DateTimes to UTC if possible before comparing them
private DateTime? ToUtc(DateTime? nullableDateTime, TimeSpan? timezoneOffset)
private static DateTime? ToUtc(DateTime? nullableDateTime, TimeSpan? timezoneOffset)
{
return nullableDateTime.HasValue ? ToUtc(nullableDateTime.Value, timezoneOffset) : nullableDateTime;
}

private DateTime ToUtc(DateTime dateTime, TimeSpan? timezoneOffset)
private static DateTime ToUtc(DateTime dateTime, TimeSpan? timezoneOffset)
{
if (dateTime.Kind == DateTimeKind.Utc)
return dateTime;

if (_taskDataMapper.TimezoneOffset.HasValue)
if (timezoneOffset.HasValue)
{
// Convert from local time to UTC using the timezone offset.
// We're relying on the upstream guard ensuring the timezone offset is
// within 14 hours
var localTime = new DateTimeOffset(dateTime.Year, dateTime.Month, dateTime.Day,
dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond,
_taskDataMapper.TimezoneOffset.Value);
timezoneOffset.Value);
DateTime utc = localTime.UtcDateTime;
return utc;
}
Expand Down
21 changes: 21 additions & 0 deletions ISOv4Plugin/Mappers/TaskDataMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,27 @@ public void AddError(string error, string id = null, string source = null, strin
Errors.Add(new Error() { Description = error, Id = id, Source = source, StackTrace = stackTrace });
}

/// <summary>
/// Validates and processes a timezone offset calculated from local and UTC times.
/// Calculates offset = localTime - utcTime, rounds to nearest minute, and validates it's within ±14 hours.
/// </summary>
/// <returns>The validated TimeSpan offset, or null if the offset is outside the acceptable ±14 hour range.</returns>
public static TimeSpan? ValidateTimezoneOffset(DateTime localTime, DateTime utcTime)
{
TimeSpan offset = localTime - utcTime;
// Round offset to nearest minute for use in timezone offset
offset = TimeSpan.FromMinutes(Math.Round(offset.TotalMinutes));
// DateTimeOffset requires the offset to be within ±14 hours
if (Math.Abs(offset.TotalHours) <= 14)
{
return offset;
}
else
{
return null;
}
}

public ISO11783_TaskData Export(ApplicationDataModel.ADM.ApplicationDataModel adm)
{
AdaptDataModel = adm;
Expand Down
25 changes: 16 additions & 9 deletions ISOv4Plugin/Mappers/TimeLogMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,17 @@ protected IEnumerable<OperationData> ImportTimeLog(ISOTask loggedTask, ISOTimeLo
var firstRecord = isoRecords.FirstOrDefault(r => r.GpsUtcDateTime.HasValue && r.GpsUtcDate != ushort.MaxValue && r.GpsUtcDate != 0);
if (firstRecord != null)
{
//Local - UTC = Delta. This value will be rough based on the accuracy of the clock settings
// Local - UTC = Delta. This value will be rough based on the accuracy of the clock settings
// but will expose the ability to derive the UTC times from the exported local times.
TimeSpan offset = firstRecord.TimeStart - firstRecord.GpsUtcDateTime.Value;
// Round offset to nearest minute for use in timezone offset
TaskDataMapper.TimezoneOffset = TimeSpan.FromMinutes(Math.Round(offset.TotalMinutes));
TimeSpan? offset = TaskDataMapper.ValidateTimezoneOffset(firstRecord.TimeStart, firstRecord.GpsUtcDateTime.Value);
if (offset.HasValue)
{
TaskDataMapper.TimezoneOffset = offset.Value;
}
else
{
TaskDataMapper.AddError($"GPS time offset of {firstRecord.TimeStart - firstRecord.GpsUtcDateTime.Value} is outside the acceptable range. Monitor date/time setting is probably invalid. Product allocation logic may be impacted.");
}
}
}
}
Expand Down Expand Up @@ -412,7 +418,8 @@ protected IEnumerable<OperationData> ImportTimeLog(ISOTask loggedTask, ISOTimeLo
operationData.DeviceElementUses = sectionMapper.ConvertToBaseTypes(sections.ToList());
operationData.GetDeviceElementUses = x => operationData.DeviceElementUses.Where(s => s.Depth == x).ToList();
operationData.PrescriptionId = prescriptionID;
operationData.OperationType = GetOperationType(productIDs, time, workingDatas);
var adaptDeviceModelId = TaskDataMapper.InstanceIDMap.GetADAPTID(dvc.DeviceId);
operationData.OperationType = GetOperationType(productIDs, time, workingDatas, adaptDeviceModelId);
operationData.ProductIds = productIDs;
if (!useDeferredExecution)
{
Expand Down Expand Up @@ -656,15 +663,15 @@ private void AddProductAllocationsForDeviceElement(Dictionary<string, Dictionary
}
}

private OperationTypeEnum GetOperationType(List<int> productIds, ISOTime time, List<WorkingData> workingDatas)
private OperationTypeEnum GetOperationType(List<int> productIds, ISOTime time, List<WorkingData> workingDatas, int? adaptDeviceModelId)
{
var productCategories = productIds
.Select(x => TaskDataMapper.AdaptDataModel.Catalog.Products.FirstOrDefault(y => y.Id.ReferenceId == x))
.Where(x => x != null && x.Category != CategoryEnum.Unknown)
.Select(x => x.Category)
.ToList();

var deviceOperationType = GetOperationTypeFromLoggingDevices(time);
var deviceOperationType = GetOperationTypeFromLoggingDevices(time, adaptDeviceModelId);

// Prefer product category to determine operation type where possible
switch (productCategories.FirstOrDefault())
Expand Down Expand Up @@ -712,7 +719,7 @@ private OperationTypeEnum GetOperationType(List<int> productIds, ISOTime time, L
}
}

private OperationTypeEnum GetOperationTypeFromLoggingDevices(ISOTime time)
private OperationTypeEnum GetOperationTypeFromLoggingDevices(ISOTime time, int? adaptDeviceModelId)
{
HashSet<DeviceOperationType> representedTypes = new HashSet<DeviceOperationType>();
IEnumerable<string> distinctDeviceElementIDs = time.DataLogValues.Select(d => d.DeviceElementIdRef).Distinct();
Expand All @@ -722,7 +729,7 @@ private OperationTypeEnum GetOperationTypeFromLoggingDevices(ISOTime time)
if (deviceElementID.HasValue)
{
DeviceElement deviceElement = DataModel.Catalog.DeviceElements.FirstOrDefault(d => d.Id.ReferenceId == deviceElementID.Value);
if (deviceElement != null && deviceElement.DeviceClassification != null)
if (deviceElement != null && deviceElement.DeviceClassification != null && deviceElement.DeviceModelId == adaptDeviceModelId)
{
DeviceOperationType deviceOperationType = DeviceOperationTypes.FirstOrDefault(d => d.MachineEnumerationMember.ToModelEnumMember().Value == deviceElement.DeviceClassification.Value.Value);
if (deviceOperationType != null)
Expand Down
Loading