private List <ISO11783_TaskData> ReadDataCard(string dataPath) { var taskDataFiles = GetListOfTaskDataFiles(dataPath); if (!taskDataFiles.Any()) { return(null); } List <ISO11783_TaskData> taskDataObjects = new List <ISO11783_TaskData>(); foreach (var taskDataFile in taskDataFiles) { //Per ISO11783-10:2015(E) 8.5, all related files are in the same directory as the TASKDATA.xml file. //The TASKDATA directory is only required when exporting to removable media. //As such, the plugin will import data in any directory structure, and always export to a TASKDATA directory. string dataFolder = Path.GetDirectoryName(taskDataFile); //Deserialize the ISOXML into the ISO models XmlDocument document = new XmlDocument(); document.Load(taskDataFile); XmlNode rootNode = document.SelectSingleNode("ISO11783_TaskData"); ISO11783_TaskData taskData = ISO11783_TaskData.ReadXML(rootNode, dataFolder); taskData.DataFolder = dataFolder; taskData.FilePath = taskDataFile; taskDataObjects.Add(taskData); } return(taskDataObjects); }
public static void AreEqual(ApplicationDataModel applicationDataModel, ISO11783_TaskData isoTaskData, string cardPath) { var loggedData = applicationDataModel.Documents.LoggedData.ToList(); var tasks = isoTaskData.Items.Where(x => x.GetType() == typeof(TSK)).Cast <TSK>().ToList(); TskAssert.AreEqual(loggedData, tasks, applicationDataModel.Catalog, cardPath); }
public ApplicationDataModel.ADM.ApplicationDataModel Import(ISO11783_TaskData iso11783TaskData, string dataPath, ApplicationDataModel.ADM.ApplicationDataModel dataModel, Dictionary <string, List <UniqueId> > linkedIds) { if (dataModel.Catalog == null) { dataModel.Catalog = CreateCatalog(); } if (dataModel.Documents == null) { dataModel.Documents = CreateDocuments(); } if (dataModel.Documents.LoggedData == null) { dataModel.Documents.LoggedData = new List <LoggedData>(); } var isoObjects = iso11783TaskData.Items; if (isoObjects == null || isoObjects.Length == 0) { return(dataModel); } var tasks = isoObjects.GetItemsOfType <TSK>(); _documentMapper.Map(tasks, dataPath, dataModel, linkedIds); return(dataModel); }
public void GivenDataPathWhenImportThenApplicationDataModelsReturned() { var iso11783TaskData = new ISO11783_TaskData(); _xmlReaderMock.Setup(x => x.Read(Path.Combine(_dataPath, "taskdata.xml"))).Returns(iso11783TaskData); _plugin.Import(_dataPath); _importerMock.Verify(x => x.Import(iso11783TaskData, _dataPath, It.IsAny <ApplicationDataModel>(), It.IsAny <Dictionary <string, List <UniqueId> > >()), Times.Once); }
public void Setup() { _linkIds = new Dictionary <string, List <UniqueId> >(); _taskData = new ISO11783_TaskData(); _dataPath = Path.GetTempPath(); _applicationDataModel = new ApplicationDataModel(); _documentMapperMock = new Mock <IDocumentMapper>(); _importer = new Importer(_documentMapperMock.Object); }
void IPlugin.Export(ApplicationDataModel.ADM.ApplicationDataModel dataModel, string exportPath, Properties properties) { //Convert the ADAPT model into the ISO model string outputPath = exportPath.WithTaskDataPath(); TaskDataMapper taskDataMapper = new TaskDataMapper(outputPath, properties); ISO11783_TaskData taskData = taskDataMapper.Export(dataModel); //Serialize the ISO model to XML TaskDocumentWriter writer = new TaskDocumentWriter(); writer.WriteTaskData(exportPath, taskData); //Serialize the Link List writer.WriteLinkList(exportPath, taskData.LinkList); }
public XmlWriter WriteTaskData(string taskDataPath, ISO11783_TaskData taskData) { BaseFolder = taskDataPath; CreateFolderStructure(); XmlStream = new MemoryStream(); RootWriter = CreateWriter(XmlStream); RootWriter.WriteStartDocument(); taskData.WriteXML(RootWriter); RootWriter.WriteEndDocument(); RootWriter.Flush(); var xml = Encoding.UTF8.GetString(XmlStream.ToArray()); File.WriteAllText(Path.Combine(BaseFolder, "TASKDATA.XML"), xml); return(RootWriter); }
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); }
public ISO11783_TaskData Export(ApplicationDataModel.ADM.ApplicationDataModel adm) { AdaptDataModel = adm; //TaskData ISOTaskData = new ISO11783_TaskData(); ISOTaskData.VersionMajor = 4; ISOTaskData.VersionMinor = 0; ISOTaskData.ManagementSoftwareManufacturer = "AgGateway"; ISOTaskData.ManagementSoftwareVersion = "1.0"; ISOTaskData.DataTransferOrigin = ISOEnumerations.ISOTaskDataTransferOrigin.FMIS; ISOTaskData.TaskControllerManufacturer = ""; ISOTaskData.TaskControllerVersion = ""; //LinkList ISOTaskData.LinkList = new ISO11783_LinkList(); ISOTaskData.LinkList.VersionMajor = 4; ISOTaskData.LinkList.VersionMinor = 0; ISOTaskData.LinkList.ManagementSoftwareManufacturer = "AgGateway"; ISOTaskData.LinkList.ManagementSoftwareVersion = "1.0"; ISOTaskData.LinkList.DataTransferOrigin = ISOEnumerations.ISOTaskDataTransferOrigin.FMIS; ISOTaskData.LinkList.TaskControllerManufacturer = ""; ISOTaskData.LinkList.TaskControllerVersion = ""; ISOTaskData.LinkList.FileVersion = ""; UniqueIDMapper = new UniqueIdMapper(ISOTaskData.LinkList); //Crops if (adm.Catalog.Crops != null) { CropTypeMapper cropMapper = new CropTypeMapper(this, ProductGroupMapper); IEnumerable <ISOCropType> crops = cropMapper.ExportCropTypes(adm.Catalog.Crops); ISOTaskData.ChildElements.AddRange(crops); } //Products if (adm.Catalog.Products != null) { IEnumerable <Product> products = AdaptDataModel.Catalog.Products; if (products.Any()) { ProductMapper productMapper = new ProductMapper(this, ProductGroupMapper); IEnumerable <ISOProduct> isoProducts = productMapper.ExportProducts(products); ISOTaskData.ChildElements.AddRange(isoProducts); } } //Growers if (adm.Catalog.Growers != null) { CustomerMapper customerMapper = new CustomerMapper(this); IEnumerable <ISOCustomer> customers = customerMapper.Export(adm.Catalog.Growers); ISOTaskData.ChildElements.AddRange(customers); } //Farms if (adm.Catalog.Farms != null) { FarmMapper farmMapper = new FarmMapper(this); IEnumerable <ISOFarm> farms = farmMapper.Export(adm.Catalog.Farms); ISOTaskData.ChildElements.AddRange(farms); } //Fields & Cropzones if (adm.Catalog.Fields != null) { PartfieldMapper fieldMapper = new PartfieldMapper(this); foreach (Field field in adm.Catalog.Fields) { IEnumerable <CropZone> fieldCropZones = adm.Catalog.CropZones.Where(c => c.FieldId == field.Id.ReferenceId); if (fieldCropZones.Count() == 0) { //Export Field ISOPartfield isoField = fieldMapper.ExportField(field); ISOTaskData.ChildElements.Add(isoField); } else if (fieldCropZones.Count() == 1) { //Export Cropzone to retain the crop reference ISOPartfield isoField = fieldMapper.ExportCropZone(fieldCropZones.First()); ISOTaskData.ChildElements.Add(isoField); } else { //Export both ISOPartfield isoField = fieldMapper.ExportField(field); ISOTaskData.ChildElements.Add(isoField); foreach (CropZone cropZone in fieldCropZones) { ISOPartfield isoCropField = fieldMapper.ExportCropZone(cropZone); ISOTaskData.ChildElements.Add(isoCropField); } } } } //Workers if (adm.Catalog.Persons != null) { WorkerMapper workerMapper = new WorkerMapper(this); IEnumerable <ISOWorker> workers = workerMapper.Export(adm.Catalog.Persons); ISOTaskData.ChildElements.AddRange(workers); } //Devices if (adm.Catalog.DeviceModels.Any()) { DeviceMapper dvcMapper = new DeviceMapper(this); IEnumerable <ISODevice> devices = dvcMapper.ExportDevices(adm.Catalog.DeviceModels); ISOTaskData.ChildElements.AddRange(devices); } //Tasks if (AdaptDataModel.Documents.WorkItems.Any() || AdaptDataModel.Documents.LoggedData.Any()) { TaskMapper taskMapper = new TaskMapper(this); if (AdaptDataModel.Documents.WorkItems != null) { //Prescriptions int gridType = 1; if (Properties != null) { Int32.TryParse(Properties.GetProperty(ISOGrid.GridTypeProperty), out gridType); } if (gridType == 1 || gridType == 2) { IEnumerable <ISOTask> plannedTasks = taskMapper.Export(AdaptDataModel.Documents.WorkItems, gridType); ISOTaskData.ChildElements.AddRange(plannedTasks); } else { AddError($"Invalid Grid Type {gridType}. WorkItems will not be exported", null, "TaskDataMapper"); } } if (AdaptDataModel.Documents.LoggedData != null) { //LoggedData IEnumerable <ISOTask> loggedTasks = taskMapper.Export(AdaptDataModel.Documents.LoggedData); ISOTaskData.ChildElements.AddRange(loggedTasks.Where(t => !ISOTaskData.ChildElements.OfType <ISOTask>().Contains(t))); } } //Add Comments ISOTaskData.ChildElements.AddRange(CommentMapper.ExportedComments); //Add LinkList Attached File Reference if (ISOTaskData.LinkList.LinkGroups.Any()) { ISOAttachedFile afe = new ISOAttachedFile(); afe.FilenamewithExtension = "LINKLIST.XML"; afe.Preserve = ISOEnumerations.ISOAttachedFilePreserve.Preserve; afe.ManufacturerGLN = string.Empty; afe.FileType = 1; ISOTaskData.ChildElements.Add(afe); } return(ISOTaskData); }
public static UnitOfMeasure ToDisplayUnit(this ISOProcessDataVariable pdv, RepresentationMapper mapper, ISO11783_TaskData taskData) { ISOUnit userUnit = null; if (!string.IsNullOrEmpty(pdv.ValuePresentationIdRef)) { ISOValuePresentation vpn = taskData.ChildElements.OfType <ISOValuePresentation>().FirstOrDefault(v => v.ValuePresentationID == pdv.ValuePresentationIdRef); if (vpn != null) { userUnit = new ISOUnit(vpn); } } if (userUnit != null) { UnitOfMeasure adaptUnit = null; try { adaptUnit = userUnit.ToAdaptUnit(); } catch { //Suppressing this as a non-critical exception } return(adaptUnit); } return(null); }
public static NumericRepresentationValue AsNumericRepresentationValue(this ISOProcessDataVariable pdv, RepresentationMapper mapper, ISO11783_TaskData taskData) { return(pdv.ProcessDataValue.AsNumericRepresentationValue(pdv.ProcessDataDDI, mapper, pdv.ToDisplayUnit(mapper, taskData))); }
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); }