public ISOConnection ExportConnection(int loggedDataOrWorkItemID, EquipmentConfiguration adaptConnection) { ISOConnection isoConnection = new ISOConnection(); //First Connector Connector connector1 = DataModel.Catalog.Connectors.FirstOrDefault(c => c.Id.ReferenceId == adaptConnection.Connector1Id); if (connector1 != null) { DeviceElementConfiguration config = DataModel.Catalog.DeviceElementConfigurations.FirstOrDefault(c => c.Id.ReferenceId == connector1.DeviceElementConfigurationId); if (config != null) { string isoDeviceElementID = TaskDataMapper.InstanceIDMap.GetISOID(config.DeviceElementId); DeviceElement deviceElement = DataModel.Catalog.DeviceElements.FirstOrDefault(d => d.Id.ReferenceId == config.DeviceElementId); if (deviceElement != null) { string isoDeviceID = TaskDataMapper.InstanceIDMap.GetISOID(deviceElement.DeviceModelId); if (!string.IsNullOrEmpty(isoDeviceElementID) && !string.IsNullOrEmpty(isoDeviceElementID)) { isoConnection.DeviceIdRef_0 = isoDeviceID; isoConnection.DeviceElementIdRef_0 = TaskDataMapper.InstanceIDMap.GetISOID(connector1.Id.ReferenceId); //We want to refer to the Connector DeviceElement, not its parent referred by the config element } } } } //Second Connector Connector connector2 = DataModel.Catalog.Connectors.FirstOrDefault(c => c.Id.ReferenceId == adaptConnection.Connector2Id); if (connector2 != null) { DeviceElementConfiguration config = DataModel.Catalog.DeviceElementConfigurations.FirstOrDefault(c => c.Id.ReferenceId == connector2.DeviceElementConfigurationId); if (config != null) { string isoDeviceElementID = TaskDataMapper.InstanceIDMap.GetISOID(config.DeviceElementId); DeviceElement deviceElement = DataModel.Catalog.DeviceElements.FirstOrDefault(d => d.Id.ReferenceId == config.DeviceElementId); if (deviceElement != null) { string isoDeviceID = TaskDataMapper.InstanceIDMap.GetISOID(deviceElement.DeviceModelId); if (!string.IsNullOrEmpty(isoDeviceElementID) && !string.IsNullOrEmpty(isoDeviceElementID)) { isoConnection.DeviceIdRef_1 = isoDeviceID; isoConnection.DeviceElementIdRef_1 = TaskDataMapper.InstanceIDMap.GetISOID(connector2.Id.ReferenceId); } } } } //DataLogTriggers if (adaptConnection.DataLogTriggers.Any()) { string taskID = TaskDataMapper.InstanceIDMap.GetISOID(loggedDataOrWorkItemID); ISOTask task = TaskDataMapper.ISOTaskData.ChildElements.OfType <ISOTask>().First(t => t.TaskID == taskID); DataLogTriggerMapper dltMapper = new DataLogTriggerMapper(TaskDataMapper); task.DataLogTriggers = dltMapper.ExportDataLogTriggers(adaptConnection.DataLogTriggers).ToList(); } return(isoConnection); }
public EquipmentConfiguration ImportConnection(ISOTask task, ISOConnection isoConnection) { EquipmentConfiguration equipConfig = new EquipmentConfiguration(); StringBuilder descriptionBuilder = new StringBuilder(); //First Device Element int?connectorID = TaskDataMapper.InstanceIDMap.GetADAPTID(isoConnection.DeviceElementIdRef_0); if (connectorID.HasValue) { Connector adaptConnector1 = DataModel.Catalog.Connectors.Single(d => d.Id.ReferenceId == connectorID.Value); equipConfig.Connector1Id = adaptConnector1.Id.ReferenceId; ISODeviceElement isoDeviceElement = TaskDataMapper.DeviceElementHierarchies.GetISODeviceElementFromID(isoConnection.DeviceElementIdRef_0); descriptionBuilder.Append(isoDeviceElement.Device.DeviceDesignator); descriptionBuilder.Append(":"); descriptionBuilder.Append(isoDeviceElement.DeviceElementDesignator); } else { descriptionBuilder.Append("Unknown"); } descriptionBuilder.Append("<->"); //Second Device Element connectorID = TaskDataMapper.InstanceIDMap.GetADAPTID(isoConnection.DeviceElementIdRef_1); if (connectorID.HasValue) { Connector adaptConnector2 = DataModel.Catalog.Connectors.Single(d => d.Id.ReferenceId == connectorID.Value); equipConfig.Connector2Id = adaptConnector2.Id.ReferenceId; ISODeviceElement isoDeviceElement = TaskDataMapper.DeviceElementHierarchies.GetISODeviceElementFromID(isoConnection.DeviceElementIdRef_1); descriptionBuilder.Append(isoDeviceElement.Device.DeviceDesignator); descriptionBuilder.Append(":"); descriptionBuilder.Append(isoDeviceElement.DeviceElementDesignator); } else { descriptionBuilder.Append("Unknown"); } equipConfig.Description = descriptionBuilder.ToString(); //DataLogTriggers if (task.DataLogTriggers.Any()) { DataLogTriggerMapper dltMapper = new DataLogTriggerMapper(TaskDataMapper); dltMapper.ImportDataLogTriggers(task.DataLogTriggers); } return(equipConfig); }
public ISOTask ExportPrescription(WorkItem workItem, int gridType, Prescription prescription) { ISOTask task = new ISOTask(TaskDataMapper.Version); //Task ID string taskID = workItem.Id.FindIsoId() ?? GenerateId(); task.TaskID = taskID; ExportIDs(workItem.Id, taskID); //Designator task.TaskDesignator = prescription.Description; //Customer Ref if (workItem.GrowerId.HasValue) { task.CustomerIdRef = TaskDataMapper.InstanceIDMap.GetISOID(workItem.GrowerId.Value); } //Farm Ref if (workItem.FarmId.HasValue) { task.FarmIdRef = TaskDataMapper.InstanceIDMap.GetISOID(workItem.FarmId.Value); } //Partfield Ref if (workItem.CropZoneId.HasValue) { task.PartFieldIdRef = TaskDataMapper.InstanceIDMap.GetISOID(workItem.CropZoneId.Value); } else if (workItem.FieldId.HasValue) { task.PartFieldIdRef = TaskDataMapper.InstanceIDMap.GetISOID(workItem.FieldId.Value); } //Comments if (workItem.Notes.Any()) { CommentAllocationMapper canMapper = new CommentAllocationMapper(TaskDataMapper); task.CommentAllocations = canMapper.ExportCommentAllocations(workItem.Notes).ToList(); } //Worker Allocations if (workItem.PeopleRoleIds.Any()) { WorkerAllocationMapper workerAllocationMapper = new WorkerAllocationMapper(TaskDataMapper); List <PersonRole> personRoles = new List <PersonRole>(); foreach (int id in workItem.PeopleRoleIds) { PersonRole personRole = DataModel.Catalog.PersonRoles.FirstOrDefault(p => p.Id.ReferenceId == id); if (personRole != null) { personRoles.Add(personRole); } } task.WorkerAllocations = workerAllocationMapper.ExportWorkerAllocations(personRoles).ToList(); } //Guidance Allocations if (workItem.GuidanceAllocationIds.Any()) { GuidanceAllocationMapper guidanceAllocationMapper = new GuidanceAllocationMapper(TaskDataMapper); List <GuidanceAllocation> allocations = new List <GuidanceAllocation>(); foreach (int id in workItem.GuidanceAllocationIds) { GuidanceAllocation allocation = DataModel.Documents.GuidanceAllocations.FirstOrDefault(p => p.Id.ReferenceId == id); if (allocation != null) { allocations.Add(allocation); } } task.GuidanceAllocations = guidanceAllocationMapper.ExportGuidanceAllocations(allocations).ToList(); } //Connections if (workItem.EquipmentConfigurationGroup != null) { task.Connections = _connectionMapper.ExportConnections(task, workItem.EquipmentConfigurationGroup.EquipmentConfigurations).ToList(); } //Status if (workItem.StatusUpdates == null || workItem.StatusUpdates.Count == 0) { task.TaskStatus = ISOEnumerations.ISOTaskStatus.Planned; } else if (workItem.StatusUpdates.Count == 1) { StatusUpdate lastStatus = workItem.StatusUpdates.OrderByDescending(su => su.TimeStamp).Last(); task.TaskStatus = ExportStatus(lastStatus.Status); } //Prescription if (prescription is RasterGridPrescription) { ExportRasterPrescription(task, prescription as RasterGridPrescription, gridType); } else if (prescription is VectorPrescription) { ExportVectorPrescription(task, prescription as VectorPrescription); } else if (prescription is ManualPrescription) { ExportManualPresciption(task, prescription as ManualPrescription); } //DataLogTriggers if (workItem.DataLogTriggers.Any()) { DataLogTriggerMapper dltMapper = new DataLogTriggerMapper(TaskDataMapper); task.DataLogTriggers = dltMapper.ExportDataLogTriggers(workItem.DataLogTriggers).ToList(); } return(task); }
private WorkItem ImportWorkItem(ISOTask isoPrescribedTask) { WorkItem workItem = new WorkItem(); //Task ID ImportIDs(workItem.Id, isoPrescribedTask.TaskID); //Grower ID workItem.GrowerId = TaskDataMapper.InstanceIDMap.GetADAPTID(isoPrescribedTask.CustomerIdRef); //Farm ID workItem.FarmId = TaskDataMapper.InstanceIDMap.GetADAPTID(isoPrescribedTask.FarmIdRef); //Field/CropZone int?pfdID = TaskDataMapper.InstanceIDMap.GetADAPTID(isoPrescribedTask.PartFieldIdRef); if (pfdID.HasValue) { if (DataModel.Catalog.CropZones.Any(c => c.Id.ReferenceId == pfdID.Value)) { workItem.CropZoneId = pfdID.Value; } else { workItem.FieldId = pfdID.Value; if (DataModel.Catalog.CropZones.Count(c => c.FieldId == pfdID) == 1) { //There is a single cropZone for the field. workItem.CropZoneId = DataModel.Catalog.CropZones.Single(c => c.FieldId == pfdID).Id.ReferenceId; } } } //Status workItem.StatusUpdates = new List <StatusUpdate>() { new StatusUpdate() { Status = ImportStatus(isoPrescribedTask.TaskStatus) } }; //Responsible Worker if (!string.IsNullOrEmpty(isoPrescribedTask.ResponsibleWorkerIdRef)) { ISOWorker worker = ISOTaskData.ChildElements.OfType <ISOWorker>().FirstOrDefault(w => w.WorkerId == isoPrescribedTask.ResponsibleWorkerIdRef); int? personID = TaskDataMapper.InstanceIDMap.GetADAPTID(isoPrescribedTask.ResponsibleWorkerIdRef); if (personID.HasValue) { //Create a Role PersonRole role = new PersonRole() { PersonId = personID.Value }; //Add to Catalog DataModel.Catalog.PersonRoles.Add(role); if (workItem.PeopleRoleIds == null) { workItem.PeopleRoleIds = new List <int>(); } //Add to Task workItem.PeopleRoleIds.Add(role.Id.ReferenceId); } } //Worker Allocation if (isoPrescribedTask.WorkerAllocations.Any()) { WorkerAllocationMapper wanMapper = new WorkerAllocationMapper(TaskDataMapper); List <PersonRole> personRoles = wanMapper.ImportWorkerAllocations(isoPrescribedTask.WorkerAllocations).ToList(); //Add to Catalog DataModel.Catalog.PersonRoles.AddRange(personRoles); //Add to Task if (workItem.PeopleRoleIds == null) { workItem.PeopleRoleIds = new List <int>(); } workItem.PeopleRoleIds.AddRange(personRoles.Select(p => p.Id.ReferenceId)); } if (isoPrescribedTask.GuidanceAllocations.Any()) { GuidanceAllocationMapper ganMapper = new GuidanceAllocationMapper(TaskDataMapper); List <GuidanceAllocation> allocations = ganMapper.ImportGuidanceAllocations(isoPrescribedTask.GuidanceAllocations).ToList(); //Add to Catalog List <GuidanceAllocation> guidanceAllocations = DataModel.Documents.GuidanceAllocations as List <GuidanceAllocation>; if (guidanceAllocations != null) { guidanceAllocations.AddRange(allocations); } //Add to Task if (workItem.GuidanceAllocationIds == null) { workItem.GuidanceAllocationIds = new List <int>(); } workItem.GuidanceAllocationIds.AddRange(allocations.Select(p => p.Id.ReferenceId)); } //Comments if (isoPrescribedTask.CommentAllocations.Any()) { CommentAllocationMapper canMapper = new CommentAllocationMapper(TaskDataMapper); workItem.Notes = canMapper.ImportCommentAllocations(isoPrescribedTask.CommentAllocations).ToList(); } //Prescription if (isoPrescribedTask.HasPrescription) { Prescription rx = PrescriptionMapper.ImportPrescription(isoPrescribedTask, workItem); if (rx == null) { return(workItem); } //Add to the Prescription the Catalog List <Prescription> prescriptions = DataModel.Catalog.Prescriptions as List <Prescription>; prescriptions?.Add(rx); //Add A WorkItemOperation WorkItemOperation operation = new WorkItemOperation(); operation.PrescriptionId = rx.Id.ReferenceId; //Add the operation to the Documents and reference on the WorkItem List <WorkItemOperation> operations = DataModel.Documents.WorkItemOperations as List <WorkItemOperation>; operations?.Add(operation); workItem.WorkItemOperationIds.Add(operation.Id.ReferenceId); //Track any prescription IDs to map to any completed TimeLog data _rxIDsByTask.Add(isoPrescribedTask.TaskID, rx.Id.ReferenceId); } //DataLogTriggers if (isoPrescribedTask.DataLogTriggers.Any()) { DataLogTriggerMapper dltMapper = new DataLogTriggerMapper(TaskDataMapper); workItem.DataLogTriggers = dltMapper.ImportDataLogTriggers(isoPrescribedTask.DataLogTriggers).ToList(); } return(workItem); }