public ApplicationDataModel.ADM.ApplicationDataModel Import(ISO11783_TaskData taskData) { ISOTaskData = taskData; UniqueIDMapper = new UniqueIdMapper(ISOTaskData.LinkList); AdaptDataModel = new ApplicationDataModel.ADM.ApplicationDataModel(); AdaptDataModel.Catalog = new Catalog() { Description = taskData.FilePath }; AdaptDataModel.Documents = new Documents(); //Comments CodedCommentListMapper commentListMapper = new CodedCommentListMapper(this); CodedCommentMapper commentMapper = new CodedCommentMapper(this, commentListMapper); //Crops - several dependencies require import prior to Products IEnumerable <ISOCropType> crops = taskData.ChildElements.OfType <ISOCropType>(); if (crops.Any()) { CropTypeMapper cropMapper = new CropTypeMapper(this, ProductGroupMapper); AdaptDataModel.Catalog.Crops.AddRange(cropMapper.ImportCropTypes(crops)); } //Products IEnumerable <ISOProduct> products = taskData.ChildElements.OfType <ISOProduct>(); if (products.Any()) { ProductMapper productMapper = new ProductMapper(this, ProductGroupMapper); IEnumerable <Product> adaptProducts = productMapper.ImportProducts(products); AdaptDataModel.Catalog.Products.AddRange(adaptProducts.Where(p => !AdaptDataModel.Catalog.Products.Contains(p))); } //Growers IEnumerable <ISOCustomer> customers = taskData.ChildElements.OfType <ISOCustomer>(); if (customers.Any()) { CustomerMapper customerMapper = new CustomerMapper(this); AdaptDataModel.Catalog.Growers.AddRange(customerMapper.Import(customers)); } //Farms IEnumerable <ISOFarm> farms = taskData.ChildElements.OfType <ISOFarm>(); if (farms.Any()) { FarmMapper farmMapper = new FarmMapper(this); AdaptDataModel.Catalog.Farms.AddRange(farmMapper.Import(farms)); } //Fields & Cropzones IEnumerable <ISOPartfield> partFields = taskData.ChildElements.OfType <ISOPartfield>(); if (partFields.Any()) { PartfieldMapper partFieldMapper = new PartfieldMapper(this); AdaptDataModel.Catalog.Fields.AddRange(partFieldMapper.ImportFields(partFields)); AdaptDataModel.Catalog.CropZones.AddRange(partFieldMapper.ImportCropZones(partFields)); } //Devices IEnumerable <ISODevice> devices = taskData.ChildElements.OfType <ISODevice>(); if (devices.Any()) { DeviceElementHierarchies = new DeviceElementHierarchies(devices, RepresentationMapper); DeviceMapper deviceMapper = new DeviceMapper(this); AdaptDataModel.Catalog.DeviceModels.AddRange(deviceMapper.ImportDevices(devices)); } //Workers IEnumerable <ISOWorker> workers = taskData.ChildElements.OfType <ISOWorker>(); if (workers.Any()) { WorkerMapper workerMapper = new WorkerMapper(this); AdaptDataModel.Catalog.Persons.AddRange(workerMapper.Import(workers)); } //Cultural Practices IEnumerable <ISOCulturalPractice> practices = taskData.ChildElements.OfType <ISOCulturalPractice>(); if (practices.Any()) { foreach (ISOCulturalPractice cpc in practices) { (AdaptDataModel.Documents.WorkOrders as List <WorkOrder>).Add(new WorkOrder() { Description = cpc.CulturalPracticeDesignator }); } } //OperationTechniques IEnumerable <ISOOperationTechnique> techniques = taskData.ChildElements.OfType <ISOOperationTechnique>(); if (techniques.Any()) { foreach (ISOOperationTechnique otq in techniques) { (AdaptDataModel.Documents.WorkOrders as List <WorkOrder>).Add(new WorkOrder() { Description = otq.OperationTechniqueDesignator }); } } IEnumerable <ISOTask> prescribedTasks = taskData.ChildElements.OfType <ISOTask>().Where(t => t.IsWorkItemTask); IEnumerable <ISOTask> loggedTasks = taskData.ChildElements.OfType <ISOTask>().Where(t => t.IsLoggedDataTask || t.TimeLogs.Any()); if (prescribedTasks.Any() || loggedTasks.Any()) { TaskMapper taskMapper = new TaskMapper(this); if (prescribedTasks.Any()) { //Prescribed Tasks IEnumerable <WorkItem> workItems = taskMapper.ImportWorkItems(prescribedTasks); AdaptDataModel.Documents.WorkItems = workItems; } if (loggedTasks.Any()) { //Logged Tasks IEnumerable <LoggedData> loggedDatas = taskMapper.ImportLoggedDatas(loggedTasks); AdaptDataModel.Documents.LoggedData = loggedDatas; //Create Work Records for Logged Tasks List <WorkRecord> workRecords = new List <WorkRecord>(); foreach (LoggedData data in loggedDatas) { WorkRecord record = new WorkRecord(); record.LoggedDataIds.Add(data.Id.ReferenceId); if (data.SummaryId.HasValue) { record.SummariesIds.Add(data.SummaryId.Value); Summary summary = AdaptDataModel.Documents.Summaries.FirstOrDefault(s => s.Id.ReferenceId == data.SummaryId); if (summary != null) { summary.WorkRecordId = record.Id.ReferenceId; } } workRecords.Add(record); } AdaptDataModel.Documents.WorkRecords = workRecords; } } return(AdaptDataModel); }
private static SpatialValue CreateSpatialValue(ISOTime templateTime, byte order, int value, DeviceElementHierarchies deviceHierarchies) { var dataLogValues = templateTime.DataLogValues; var matchingDlv = dataLogValues.ElementAtOrDefault(order); if (matchingDlv == null) { return(null); } ISODeviceElement det = deviceHierarchies.GetISODeviceElementFromID(matchingDlv.DeviceElementIdRef); ISODevice dvc = det?.Device; ISODeviceProcessData dpd = dvc?.DeviceProcessDatas?.FirstOrDefault(d => d.DDI == matchingDlv.ProcessDataDDI); var ddis = DdiLoader.Ddis; var resolution = 1d; if (matchingDlv.ProcessDataDDI != null && ddis.ContainsKey(matchingDlv.ProcessDataDDI.AsInt32DDI())) { resolution = ddis[matchingDlv.ProcessDataDDI.AsInt32DDI()].Resolution; } var spatialValue = new SpatialValue { Id = order, DataLogValue = matchingDlv, Value = value * resolution, DeviceProcessData = dpd }; return(spatialValue); }
public ApplicationDataModel.ADM.ApplicationDataModel Import(ISO11783_TaskData taskData) { if (Properties == null) { Properties = new Properties(); } ISOTaskData = taskData; UniqueIDMapper = new UniqueIdMapper(ISOTaskData.LinkList); AdaptDataModel = new ApplicationDataModel.ADM.ApplicationDataModel(); AdaptDataModel.Catalog = new Catalog() { Description = taskData.FilePath }; AdaptDataModel.Documents = new Documents(); //Comments CodedCommentListMapper commentListMapper = new CodedCommentListMapper(this); CodedCommentMapper commentMapper = new CodedCommentMapper(this, commentListMapper); //Crops - several dependencies require import prior to Products IEnumerable <ISOCropType> crops = taskData.ChildElements.OfType <ISOCropType>(); if (crops.Any()) { CropTypeMapper cropMapper = new CropTypeMapper(this, ProductGroupMapper); AdaptDataModel.Catalog.Crops.AddRange(cropMapper.ImportCropTypes(crops)); } //Products IEnumerable <ISOProduct> products = taskData.ChildElements.OfType <ISOProduct>(); if (products.Any()) { ProductMapper productMapper = new ProductMapper(this, ProductGroupMapper); IEnumerable <Product> adaptProducts = productMapper.ImportProducts(products); AdaptDataModel.Catalog.Products.AddRange(adaptProducts.Where(p => !AdaptDataModel.Catalog.Products.Contains(p))); } //Growers IEnumerable <ISOCustomer> customers = taskData.ChildElements.OfType <ISOCustomer>(); if (customers.Any()) { CustomerMapper customerMapper = new CustomerMapper(this); AdaptDataModel.Catalog.Growers.AddRange(customerMapper.Import(customers)); } //Farms IEnumerable <ISOFarm> farms = taskData.ChildElements.OfType <ISOFarm>(); if (farms.Any()) { FarmMapper farmMapper = new FarmMapper(this); AdaptDataModel.Catalog.Farms.AddRange(farmMapper.Import(farms)); } //Fields & Cropzones IEnumerable <ISOPartfield> partFields = taskData.ChildElements.OfType <ISOPartfield>(); if (partFields.Any()) { PartfieldMapper partFieldMapper = new PartfieldMapper(this); AdaptDataModel.Catalog.Fields.AddRange(partFieldMapper.ImportFields(partFields)); AdaptDataModel.Catalog.CropZones.AddRange(partFieldMapper.ImportCropZones(partFields, crops)); } //Devices IEnumerable <ISODevice> devices = taskData.ChildElements.OfType <ISODevice>(); if (devices.Any()) { //See explanation of MergeSingleBinsIntoBoom in DeviceElementHierarchy bool mergeBins; if (Properties == null || !bool.TryParse(Properties.GetProperty(MergeSingleBinsIntoBoom), out mergeBins)) { mergeBins = true; } //Load the internal objects modeling hierarchies of DETs per DVC DeviceElementHierarchies = new DeviceElementHierarchies(devices, RepresentationMapper, mergeBins, taskData.ChildElements.OfType <ISOTask>().SelectMany(t => t.TimeLogs), BaseFolder); //Import the ISO DVC & DET data into the actual ADAPT models. //During DET import, we use the DeviceElementHierarchies from above to map the actual hierarchies and fill in details. DeviceMapper deviceMapper = new DeviceMapper(this); AdaptDataModel.Catalog.DeviceModels.AddRange(deviceMapper.ImportDevices(devices)); } //Workers IEnumerable <ISOWorker> workers = taskData.ChildElements.OfType <ISOWorker>(); if (workers.Any()) { WorkerMapper workerMapper = new WorkerMapper(this); AdaptDataModel.Catalog.Persons.AddRange(workerMapper.Import(workers)); } //Cultural Practices IEnumerable <ISOCulturalPractice> practices = taskData.ChildElements.OfType <ISOCulturalPractice>(); if (practices.Any()) { foreach (ISOCulturalPractice cpc in practices) { (AdaptDataModel.Documents.WorkOrders as List <WorkOrder>).Add(new WorkOrder() { Description = cpc.CulturalPracticeDesignator }); } } //OperationTechniques IEnumerable <ISOOperationTechnique> techniques = taskData.ChildElements.OfType <ISOOperationTechnique>(); if (techniques.Any()) { foreach (ISOOperationTechnique otq in techniques) { (AdaptDataModel.Documents.WorkOrders as List <WorkOrder>).Add(new WorkOrder() { Description = otq.OperationTechniqueDesignator }); } } IEnumerable <ISOTask> prescribedTasks = taskData.ChildElements.OfType <ISOTask>().Where(t => t.IsWorkItemTask); IEnumerable <ISOTask> loggedTasks = taskData.ChildElements.OfType <ISOTask>().Where(t => t.IsLoggedDataTask || t.TimeLogs.Any()); if (prescribedTasks.Any() || loggedTasks.Any()) { TaskMapper taskMapper = new TaskMapper(this); if (prescribedTasks.Any()) { //Prescribed Tasks IEnumerable <WorkItem> workItems = taskMapper.ImportWorkItems(prescribedTasks); AdaptDataModel.Documents.WorkItems = workItems; } if (loggedTasks.Any()) { //Logged Tasks IEnumerable <LoggedData> loggedDatas = taskMapper.ImportLoggedDatas(loggedTasks); AdaptDataModel.Documents.LoggedData = loggedDatas; //Create Work Records for Logged Tasks List <WorkRecord> workRecords = new List <WorkRecord>(); foreach (LoggedData data in loggedDatas) { WorkRecord record = new WorkRecord(); record.LoggedDataIds.Add(data.Id.ReferenceId); if (data.SummaryId.HasValue) { record.SummariesIds.Add(data.SummaryId.Value); Summary summary = AdaptDataModel.Documents.Summaries.FirstOrDefault(s => s.Id.ReferenceId == data.SummaryId); if (summary != null) { summary.WorkRecordId = record.Id.ReferenceId; } } //Export Derived UTC Delta as a ContextItem //The value will be as accurate as the clock settings and thus terming "Delta" vs "Offset" to underscore this is not an accurate UTC offset for the local timezone //Not rounding value so that relatively accurate GPS UTC values can be reverse calculated from this value. string offset = GPSToLocalDelta.HasValue ? GPSToLocalDelta.Value.ToString(CultureInfo.InvariantCulture) : "no data"; ContextItem contextItem = new ContextItem() { Code = "GPSUTC_Local_Delta", Value = offset, ValueUOM = "hr" }; record.ContextItems.Add(contextItem); workRecords.Add(record); } AdaptDataModel.Documents.WorkRecords = workRecords; } } return(AdaptDataModel); }
public IEnumerable <ISOSpatialRow> Read(string fileName, ISOTime templateTime, DeviceElementHierarchies deviceHierarchies) { if (templateTime == null) { yield break; } if (!File.Exists(fileName)) { yield break; } using (var binaryReader = new System.IO.BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))) { while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { ISOPosition templatePosition = templateTime.Positions.FirstOrDefault(); var record = new ISOSpatialRow { TimeStart = GetStartTime(templateTime, binaryReader) }; if (templatePosition != null) { //North and East are required binary data record.NorthPosition = ReadInt32((double?)templatePosition.PositionNorth, templatePosition.HasPositionNorth, binaryReader).GetValueOrDefault(0); record.EastPosition = ReadInt32((double?)templatePosition.PositionEast, templatePosition.HasPositionEast, binaryReader).GetValueOrDefault(0); if (templatePosition.HasPositionUp) //Optional position attributes will be included in the binary only if a corresponding attribute is present in the PTN element { record.Elevation = ReadInt32(templatePosition.PositionUp, templatePosition.HasPositionUp, binaryReader); } //Position status is required record.PositionStatus = ReadByte((byte?)templatePosition.PositionStatus, templatePosition.HasPositionStatus, binaryReader); if (templatePosition.HasPDOP) { record.PDOP = ReadUShort((double?)templatePosition.PDOP, templatePosition.HasPDOP, binaryReader); } if (templatePosition.HasHDOP) { record.HDOP = ReadUShort((double?)templatePosition.HDOP, templatePosition.HasHDOP, binaryReader); } if (templatePosition.HasNumberOfSatellites) { record.NumberOfSatellites = ReadByte(templatePosition.NumberOfSatellites, templatePosition.HasNumberOfSatellites, binaryReader); } if (templatePosition.HasGpsUtcTime) { if (templatePosition.GpsUtcTime.HasValue) { record.GpsUtcTime = Convert.ToUInt32(templatePosition.GpsUtcTime.Value); } else { record.GpsUtcTime = binaryReader.ReadUInt32(); } } if (templatePosition.HasGpsUtcDate) { if (templatePosition.GpsUtcDate.HasValue) { record.GpsUtcDate = (ushort)templatePosition.GpsUtcDate.Value; } else { record.GpsUtcDate = binaryReader.ReadUInt16(); } } if (record.GpsUtcDate != null && record.GpsUtcTime != null) { record.GpsUtcDateTime = _firstDayOf1980.AddDays((double)record.GpsUtcDate).AddMilliseconds((double)record.GpsUtcTime); } } //Some datasets end here if (binaryReader.BaseStream.Position >= binaryReader.BaseStream.Length) { break; } var numberOfDLVs = binaryReader.ReadByte(); //Some datasets end here if (binaryReader.BaseStream.Position >= binaryReader.BaseStream.Length) { break; } //If the reported number of values does not fit into the stream, correct the numberOfDLVs if (numberOfDLVs > 0) { var endPosition = binaryReader.BaseStream.Position + 5 * numberOfDLVs; if (endPosition > binaryReader.BaseStream.Length) { numberOfDLVs = (byte)Math.Floor((endPosition - binaryReader.BaseStream.Length) / 5d); } } record.SpatialValues = new List <SpatialValue>(); //Read DLVs out of the TLG.bin for (int i = 0; i < numberOfDLVs; i++) { var order = binaryReader.ReadByte(); var value = binaryReader.ReadInt32(); SpatialValue spatialValue = CreateSpatialValue(templateTime, order, value, deviceHierarchies); if (spatialValue != null) { record.SpatialValues.Add(spatialValue); } } //Add any fixed values from the TLG.xml foreach (ISODataLogValue fixedValue in templateTime.DataLogValues.Where(dlv => dlv.ProcessDataValue.HasValue && !EnumeratedMeterFactory.IsCondensedMeter(dlv.ProcessDataDDI.AsInt32DDI()))) { byte order = (byte)templateTime.DataLogValues.IndexOf(fixedValue); if (record.SpatialValues.Any(s => s.Id == order)) //Check to ensure the binary data didn't already write this value { //Per the spec, any fixed value in the XML applies to all rows; as such, replace what was read from the binary SpatialValue matchingValue = record.SpatialValues.Single(s => s.Id == order); matchingValue.DataLogValue = fixedValue; } } yield return(record); } } }