private ISOTimeLog ExportTimeLog(OperationData operation, IEnumerable <SpatialRecord> spatialRecords, string dataPath) { ISOTimeLog isoTimeLog = new ISOTimeLog(); //ID string id = operation.Id.FindIsoId() ?? GenerateId(5); isoTimeLog.Filename = id; ExportIDs(operation.Id, id); List <DeviceElementUse> deviceElementUses = operation.GetAllSections(); List <WorkingData> workingDatas = deviceElementUses.SelectMany(x => x.GetWorkingDatas()).ToList(); ISOTime isoTime = new ISOTime(); isoTime.HasStart = true; isoTime.Type = ISOTimeType.Effective; isoTime.DataLogValues = ExportDataLogValues(workingDatas, deviceElementUses).ToList(); //Set the timelog data definition for PTN ISOPosition position = new ISOPosition(); position.HasPositionNorth = true; position.HasPositionEast = true; position.HasPositionUp = true; position.HasPositionStatus = true; position.HasPDOP = false; position.HasHDOP = false; position.HasNumberOfSatellites = false; position.HasGpsUtcTime = false; position.HasGpsUtcTime = false; isoTime.Positions.Add(position); //Write XML TaskDocumentWriter xmlWriter = new TaskDocumentWriter(); xmlWriter.WriteTimeLog(dataPath, isoTimeLog, isoTime); //Write BIN var binFilePath = Path.Combine(dataPath, isoTimeLog.Filename + ".bin"); BinaryWriter writer = new BinaryWriter(_dataLogValueOrdersByWorkingDataID); writer.Write(binFilePath, workingDatas.ToList(), spatialRecords); return(isoTimeLog); }
public IEnumerable <ISOTimeLog> ExportTimeLogs(IEnumerable <OperationData> operationDatas, string dataPath) { _dataLogValueOrdersByWorkingDataID = new Dictionary <int, int>(); List <ISOTimeLog> timeLogs = new List <ISOTimeLog>(); foreach (OperationData operation in operationDatas) { IEnumerable <SpatialRecord> spatialRecords = operation.GetSpatialRecords != null?operation.GetSpatialRecords() : null; if (spatialRecords != null && spatialRecords.Any()) //No need to export a timelog if no data { ISOTimeLog timeLog = ExportTimeLog(operation, spatialRecords, dataPath); timeLogs.Add(timeLog); } } return(timeLogs); }
public XmlWriter WriteTimeLog(string exportPath, ISOTimeLog timeLog, ISOTime time) { BaseFolder = exportPath; CreateFolderStructure(); XmlStream = new MemoryStream(); RootWriter = CreateWriter(XmlStream); RootWriter.WriteStartDocument(); time.WriteXML(RootWriter); RootWriter.WriteEndDocument(); RootWriter.Flush(); var xml = Encoding.UTF8.GetString(XmlStream.ToArray()); File.WriteAllText(Path.Combine(BaseFolder, timeLog.Filename + ".xml"), xml); return(RootWriter); }
protected override IEnumerable <ISOSpatialRow> ReadTimeLog(ISOTimeLog _timeLog, string _dataPath) { List <BinaryReaderHelper> readers = new List <BinaryReaderHelper>(); try { readers = CreateBinaryReaders(); // Below alogrithm is using queues for each binary file and matching records on TimeStart/Position. // At start of each iteration a single record is read from binary file into queue. // Records with earliest TimeStart are merged together and removed from each file queue. while (true) { // Read next record from each time log var readersWithData = ReadNextRecords(readers); if (readersWithData.Count == 0) { // No more records in each file. Stop processing. break; } // Group records by TimeStart and East/North position, and then grab ones with earliest TimeStart. // This leads to processing earliest records from any file first and keeping other records untouched. // They will be processed in the next loop iteration along with any records read from already processed files. var candidates = readersWithData.GroupBy(x => new { x.CurrentRecord.TimeStart, x.CurrentRecord.EastPosition, x.CurrentRecord.NorthPosition }) .OrderBy(x => x.Key.TimeStart) .First().ToList(); // Merge data from all candidates ISOSpatialRow result = MergeRecords(candidates); yield return(result); } } finally { // Clean up readers DisposeBinaryReaders(readers); } }
private IEnumerable <OperationData> ImportTimeLog(ISOTask loggedTask, ISOTimeLog isoTimeLog, int?prescriptionID) { WorkingDataMapper workingDataMapper = new WorkingDataMapper(new EnumeratedMeterFactory(), TaskDataMapper); SectionMapper sectionMapper = new SectionMapper(workingDataMapper, TaskDataMapper); SpatialRecordMapper spatialMapper = new SpatialRecordMapper(new RepresentationValueInterpolator(), sectionMapper, workingDataMapper, TaskDataMapper); IEnumerable <ISOSpatialRow> isoRecords = ReadTimeLog(isoTimeLog, this.TaskDataPath); if (isoRecords != null) { isoRecords = isoRecords.ToList(); //Avoids multiple reads ISOTime time = isoTimeLog.GetTimeElement(this.TaskDataPath); //Identify unique devices represented in this TimeLog data IEnumerable <string> deviceElementIDs = time.DataLogValues.Where(d => d.ProcessDataDDI != "DFFF" && d.ProcessDataDDI != "DFFE").Select(d => d.DeviceElementIdRef); Dictionary <ISODevice, HashSet <string> > loggedDeviceElementsByDevice = new Dictionary <ISODevice, HashSet <string> >(); foreach (string deviceElementID in deviceElementIDs) { ISODeviceElement isoDeviceElement = TaskDataMapper.DeviceElementHierarchies.GetISODeviceElementFromID(deviceElementID); if (isoDeviceElement != null) { ISODevice device = isoDeviceElement.Device; if (!loggedDeviceElementsByDevice.ContainsKey(device)) { loggedDeviceElementsByDevice.Add(device, new HashSet <string>()); } loggedDeviceElementsByDevice[device].Add(deviceElementID); } } //Split all devices in the same TimeLog into separate OperationData objects to handle multi-implement scenarios //This will ensure implement geometries/DeviceElementUse Depths & Orders do not get confused between implements List <OperationData> operationDatas = new List <OperationData>(); foreach (ISODevice dvc in loggedDeviceElementsByDevice.Keys) { OperationData operationData = new OperationData(); //Determine products Dictionary <string, List <ISOProductAllocation> > productAllocations = GetProductAllocationsByDeviceElement(loggedTask, dvc); List <int> productIDs = GetDistinctProductIDs(TaskDataMapper, productAllocations); //This line will necessarily invoke a spatial read in order to find //1)The correct number of CondensedWorkState working datas to create //2)Any Widths and Offsets stored in the spatial data IEnumerable <DeviceElementUse> sections = sectionMapper.Map(time, isoRecords, operationData.Id.ReferenceId, loggedDeviceElementsByDevice[dvc], productAllocations); var workingDatas = sections != null?sections.SelectMany(x => x.GetWorkingDatas()).ToList() : new List <WorkingData>(); operationData.GetSpatialRecords = () => spatialMapper.Map(isoRecords, workingDatas, productAllocations); operationData.MaxDepth = sections.Count() > 0 ? sections.Select(s => s.Depth).Max() : 0; operationData.GetDeviceElementUses = x => sectionMapper.ConvertToBaseTypes(sections.Where(s => s.Depth == x).ToList()); operationData.PrescriptionId = prescriptionID; operationData.OperationType = GetOperationTypeFromLoggingDevices(time); operationData.ProductIds = productIDs; operationData.SpatialRecordCount = isoRecords.Count(); operationDatas.Add(operationData); } //Set the CoincidentOperationDataIds property identifying Operation Datas from the same TimeLog. operationDatas.ForEach(o => o.CoincidentOperationDataIds = operationDatas.Where(o2 => o2.Id.ReferenceId != o.Id.ReferenceId).Select(o3 => o3.Id.ReferenceId).ToList()); return(operationDatas); } return(null); }
protected override ISOTime GetTimeElementFromTimeLog(ISOTimeLog isoTimeLog) { // Always return a combined ISOTime record. return(_combinedTime); }
private IEnumerable <OperationData> ImportTimeLog(ISOTask loggedTask, ISOTimeLog isoTimeLog, int?prescriptionID) { WorkingDataMapper workingDataMapper = new WorkingDataMapper(new EnumeratedMeterFactory(), TaskDataMapper); SectionMapper sectionMapper = new SectionMapper(workingDataMapper, TaskDataMapper); SpatialRecordMapper spatialMapper = new SpatialRecordMapper(new RepresentationValueInterpolator(), sectionMapper, workingDataMapper, TaskDataMapper); IEnumerable <ISOSpatialRow> isoRecords = ReadTimeLog(isoTimeLog, this.TaskDataPath); bool useDeferredExecution = false; if (isoRecords != null) { try { if (TaskDataMapper.Properties != null) { //Set this property to override the default behavior of pre-iterating the data bool.TryParse(TaskDataMapper.Properties.GetProperty("SpatialRecordDeferredExecution"), out useDeferredExecution); } if (!useDeferredExecution) { isoRecords = isoRecords.ToList(); //Avoids multiple reads } //Set a UTC "delta" from the first record where possible. We set only one per data import. if (!TaskDataMapper.GPSToLocalDelta.HasValue) { var firstRecord = isoRecords.FirstOrDefault(); if (firstRecord != null && firstRecord.GpsUtcDateTime.HasValue) { //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. TaskDataMapper.GPSToLocalDelta = (firstRecord.TimeStart - firstRecord.GpsUtcDateTime.Value).TotalHours; } } } catch (Exception ex) { TaskDataMapper.AddError($"Timelog file {isoTimeLog.Filename} is invalid. Skipping.", ex.Message, null, ex.StackTrace); return(null); } ISOTime time = isoTimeLog.GetTimeElement(this.TaskDataPath); //Identify unique devices represented in this TimeLog data IEnumerable <string> deviceElementIDs = time.DataLogValues.Where(d => d.ProcessDataDDI != "DFFF" && d.ProcessDataDDI != "DFFE").Select(d => d.DeviceElementIdRef); Dictionary <ISODevice, HashSet <string> > loggedDeviceElementsByDevice = new Dictionary <ISODevice, HashSet <string> >(); foreach (string deviceElementID in deviceElementIDs) { ISODeviceElement isoDeviceElement = TaskDataMapper.DeviceElementHierarchies.GetISODeviceElementFromID(deviceElementID); if (isoDeviceElement != null) { ISODevice device = isoDeviceElement.Device; if (!loggedDeviceElementsByDevice.ContainsKey(device)) { loggedDeviceElementsByDevice.Add(device, new HashSet <string>()); } loggedDeviceElementsByDevice[device].Add(deviceElementID); } } //Split all devices in the same TimeLog into separate OperationData objects to handle multi-implement scenarios //This will ensure implement geometries/DeviceElementUse Depths & Orders do not get confused between implements List <OperationData> operationDatas = new List <OperationData>(); foreach (ISODevice dvc in loggedDeviceElementsByDevice.Keys) { OperationData operationData = new OperationData(); //Determine products Dictionary <string, List <ISOProductAllocation> > productAllocations = GetProductAllocationsByDeviceElement(loggedTask, dvc); List <int> productIDs = GetDistinctProductIDs(TaskDataMapper, productAllocations); //This line will necessarily invoke a spatial read in order to find //1)The correct number of CondensedWorkState working datas to create //2)Any Widths and Offsets stored in the spatial data IEnumerable <DeviceElementUse> sections = sectionMapper.Map(time, isoRecords, operationData.Id.ReferenceId, loggedDeviceElementsByDevice[dvc], productAllocations); var workingDatas = sections != null?sections.SelectMany(x => x.GetWorkingDatas()).ToList() : new List <WorkingData>(); operationData.GetSpatialRecords = () => spatialMapper.Map(isoRecords, workingDatas, productAllocations); operationData.MaxDepth = sections.Count() > 0 ? sections.Select(s => s.Depth).Max() : 0; operationData.GetDeviceElementUses = x => sectionMapper.ConvertToBaseTypes(sections.Where(s => s.Depth == x).ToList()); operationData.PrescriptionId = prescriptionID; operationData.OperationType = GetOperationTypeFromLoggingDevices(time); operationData.ProductIds = productIDs; operationData.SpatialRecordCount = isoRecords.Count(); operationDatas.Add(operationData); } //Set the CoincidentOperationDataIds property identifying Operation Datas from the same TimeLog. operationDatas.ForEach(o => o.CoincidentOperationDataIds = operationDatas.Where(o2 => o2.Id.ReferenceId != o.Id.ReferenceId).Select(o3 => o3.Id.ReferenceId).ToList()); return(operationDatas); } return(null); }
protected virtual ISOTime GetTimeElementFromTimeLog(ISOTimeLog isoTimeLog) { return(isoTimeLog.GetTimeElement(this.TaskDataPath)); }