/// <summary>
        /// Looks up unit, converts and loads representation
        /// </summary>
        /// <param name="n"></param>
        /// <param name="ddi"></param>
        /// <param name="mapper"></param>
        /// <returns></returns>
        ///
        public static NumericRepresentationValue AsNumericRepresentationValue(this int n, int ddi, RepresentationMapper mapper, UnitOfMeasure userUnitOfMeasure = null)
        {
            //RepresentationValue
            NumericRepresentationValue returnValue = new ApplicationDataModel.Representations.NumericRepresentationValue();

            //Representation
            returnValue.Representation = mapper.Map(ddi) as NumericRepresentation;

            //Value
            double        convertedValue = (double)n;
            UnitOfMeasure uom            = null;
            ISOUnit       isoUnit        = UnitFactory.Instance.GetUnitByDDI(ddi);

            if (isoUnit != null)
            {
                convertedValue = isoUnit.ConvertFromIsoUnit(convertedValue);
                uom            = isoUnit.ToAdaptUnit();
            }
            returnValue.Value = new ApplicationDataModel.Representations.NumericValue(uom, convertedValue);

            //User UOM
            if (userUnitOfMeasure != null)
            {
                returnValue.UserProvidedUnitOfMeasure = userUnitOfMeasure;
            }

            return(returnValue);
        }
Exemple #2
0
        public ISODataLogTrigger ExportDataLogTrigger(DataLogTrigger adaptDataLogTrigger)
        {
            ISODataLogTrigger isoDataLogTrigger = new ISODataLogTrigger();

            int?ddi = RepresentationMapper.Map(adaptDataLogTrigger.Representation);

            if (ddi.HasValue) //Need DDI for a valid DLT
            {
                isoDataLogTrigger.DataLogDDI              = ddi.Value.AsHexDDI();
                isoDataLogTrigger.DataLogMethod           = ExportDataLogMethod(adaptDataLogTrigger.DataLogMethod);
                isoDataLogTrigger.DataLogDistanceInterval = adaptDataLogTrigger.DataLogDistanceInterval.AsIntViaMappedDDI(RepresentationMapper);
                isoDataLogTrigger.DataLogTimeInterval     = adaptDataLogTrigger.DataLogTimeInterval.AsIntViaMappedDDI(RepresentationMapper);
                isoDataLogTrigger.DataLogThresholdMinimum = adaptDataLogTrigger.DataLogThresholdMinimum.AsIntViaMappedDDI(RepresentationMapper);
                isoDataLogTrigger.DataLogThresholdMaximum = adaptDataLogTrigger.DataLogThresholdMaximum.AsIntViaMappedDDI(RepresentationMapper);
                isoDataLogTrigger.DataLogThresholdChange  = adaptDataLogTrigger.DataLogThresholdChange.AsIntViaMappedDDI(RepresentationMapper);
                if (adaptDataLogTrigger.DeviceElementId.HasValue)
                {
                    isoDataLogTrigger.DeviceElementIdRef = TaskDataMapper.InstanceIDMap.GetISOID(adaptDataLogTrigger.DeviceElementId.Value);
                }

                //Not yet implemented
                //isoDataLogTrigger.ValuePresentationIdRef = "";
                //isoDataLogTrigger.DataLogPGN = null;
                //isoDataLogTrigger.DataLogPGNStartBit = null;
                //isoDataLogTrigger.DataLogPGNStopBit = null;
            }

            return(isoDataLogTrigger);
        }
Exemple #3
0
        private static List <NumericRepresentationValue> LoadRateFromTreatmentZones(string zoneId, Dictionary <int, TreatmentZone> treatmentZones)
        {
            var rates = new List <NumericRepresentationValue>();
            int treatmentZoneId;

            if (!zoneId.ParseValue(out treatmentZoneId))
            {
                return(rates);
            }

            if (!treatmentZones.ContainsKey(treatmentZoneId))
            {
                return(rates);
            }

            var treatmentZone = treatmentZones[treatmentZoneId];

            if (treatmentZone.Variables == null || treatmentZone.Variables.Count == 0)
            {
                return(rates);
            }

            foreach (var dataVariable in treatmentZone.Variables)
            {
                rates.Add(new NumericRepresentationValue
                {
                    Representation            = _representationMapper.Map(dataVariable.Ddi) as NumericRepresentation,
                    Value                     = new NumericValue(dataVariable.IsoUnit.ToAdaptUnit(), dataVariable.Value),
                    UserProvidedUnitOfMeasure = dataVariable.UserUnit
                });
            }
            return(rates);
        }
Exemple #4
0
        private MeteredValue GetSummaryMeteredValue(ISODataLogValue dlv)
        {
            if (dlv.ProcessDataDDI == null)
            {
                return(null);
            }
            int ddi = dlv.ProcessDataDDI.AsInt32DDI();

            int dataValue = 0;

            if (dlv.ProcessDataValue.HasValue)
            {
                dataValue = dlv.ProcessDataValue.Value;
            }

            var unitOfMeasure = RepresentationMapper.GetUnitForDdi(ddi);

            if (!DDIs.ContainsKey(ddi) || unitOfMeasure == null)
            {
                return(null);
            }

            DdiDefinition ddiDefintion = DDIs[ddi];

            return(new MeteredValue
            {
                Value = new NumericRepresentationValue(RepresentationMapper.Map(ddi) as NumericRepresentation,
                                                       unitOfMeasure, new NumericValue(unitOfMeasure, dataValue * ddiDefintion.Resolution))
            });
        }
        public DataLogTrigger ImportDataLogTrigger(ISODataLogTrigger isoDataLogTrigger)
        {
            int ddi = isoDataLogTrigger.DataLogDDI.AsInt32DDI();

            ApplicationDataModel.Representations.Representation representation = null;
            if (ddi != DefaultSet)
            {
                representation = RepresentationMapper.Map(ddi);
            }
            if (ddi == DefaultSet || representation != null) //Check that we can map to something meaningful before creating the DLT
            {
                DataLogTrigger adaptTrigger = new DataLogTrigger();

                if (ddi == DefaultSet)
                {
                    adaptTrigger.RequestDefaultProcessData = true;
                }
                else
                {
                    adaptTrigger.Representation = representation;
                }
                adaptTrigger.DataLogMethods = ImportDataLogMethods(isoDataLogTrigger.DataLogMethod);

                //Obsolete behavior
                adaptTrigger.DataLogMethod = adaptTrigger.DataLogMethods.Any() ? adaptTrigger.DataLogMethods.First() : LoggingMethodEnum.Total;

                if (isoDataLogTrigger.DataLogDistanceInterval.HasValue)
                {
                    adaptTrigger.DataLogDistanceInterval = isoDataLogTrigger.DataLogDistanceInterval.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                if (isoDataLogTrigger.DataLogTimeInterval.HasValue)
                {
                    adaptTrigger.DataLogTimeInterval = isoDataLogTrigger.DataLogTimeInterval.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                if (isoDataLogTrigger.DataLogThresholdMinimum.HasValue)
                {
                    adaptTrigger.DataLogThresholdMinimum = isoDataLogTrigger.DataLogThresholdMinimum.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                if (isoDataLogTrigger.DataLogThresholdMaximum.HasValue)
                {
                    adaptTrigger.DataLogThresholdMaximum = isoDataLogTrigger.DataLogThresholdMaximum.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                if (isoDataLogTrigger.DataLogThresholdChange.HasValue)
                {
                    adaptTrigger.DataLogThresholdChange = isoDataLogTrigger.DataLogThresholdChange.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                adaptTrigger.DeviceElementId = TaskDataMapper.InstanceIDMap.GetADAPTID(isoDataLogTrigger.DeviceElementIdRef);

                //Not yet implemented
                //adaptTrigger.LoggingLevel = null;

                return(adaptTrigger);
            }
            return(null);
        }
        private NumericWorkingData MapNumericMeter(ISODataLogValue dlv, int deviceElementUseId)
        {
            var meter = new NumericWorkingData
            {
                UnitOfMeasure      = RepresentationMapper.GetUnitForDdi(dlv.ProcessDataDDI.AsInt32DDI()),
                DeviceElementUseId = deviceElementUseId,
                Representation     = RepresentationMapper.Map(dlv.ProcessDataDDI.AsInt32DDI())
            };

            return(meter);
        }
Exemple #7
0
        public IEnumerable <ISOProduct> ExportProducts(IEnumerable <Product> adaptProducts)
        {
            List <ISOProduct> isoProducts = new List <ISOProduct>();

            //Add all the products
            foreach (Product adaptProduct in adaptProducts)
            {
                ISOProduct product = ExportProduct(adaptProduct);
                isoProducts.Add(product);
            }

            //Fill in detail on the product mixes
            if (adaptProducts.OfType <MixProduct>().Any())
            {
                foreach (MixProduct adaptMixProduct in adaptProducts.OfType <MixProduct>())
                {
                    //Find the ISO Product
                    ISOProduct isoMixProduct = isoProducts.Single(p => p.ProductId == TaskDataMapper.InstanceIDMap.GetISOID(adaptMixProduct.Id.ReferenceId));

                    foreach (ProductComponent component in adaptMixProduct.ProductComponents)
                    {
                        //Components may map to either an ingredient or a product
                        //See comments at ProductComponent in ADAPT repo.
                        Product adaptProduct = DataModel.Catalog.Products.FirstOrDefault(i => i.Id.ReferenceId == component.IngredientId);
                        if (adaptProduct != null)
                        {
                            ExportProductRelation(isoProducts, adaptProduct.Description, component.Quantity, isoMixProduct);
                        }
                        else
                        {
                            Ingredient adaptIngredient = DataModel.Catalog.Ingredients.FirstOrDefault(i => i.Id.ReferenceId == component.IngredientId);
                            if (adaptIngredient != null)
                            {
                                ExportProductRelation(isoProducts, adaptIngredient.Description, component.Quantity, isoMixProduct);
                            }
                        }
                    }

                    //Total Quantity
                    isoMixProduct.MixtureRecipeQuantity = adaptMixProduct.TotalQuantity.AsIntViaMappedDDI(RepresentationMapper);

                    //Quantity DDI
                    int?ddi = RepresentationMapper.Map(adaptMixProduct.TotalQuantity.Representation);
                    if (ddi.HasValue)
                    {
                        isoMixProduct.QuantityDDI = ddi.Value.AsHexDDI();
                    }
                }
            }
            return(isoProducts);
        }
Exemple #8
0
        private List <ISOTime> ExportSummary(Summary summary)
        {
            List <ISOTime> times = new List <ISOTime>();

            foreach (StampedMeteredValues values in summary.SummaryData)
            {
                ISOTime time = new ISOTime();
                time.Start = values.Stamp.TimeStamp1;
                time.Stop  = values.Stamp.TimeStamp2;
                if (values.Stamp.Duration.HasValue)
                {
                    time.Duration = (uint)values.Stamp.Duration.Value.TotalSeconds;
                }
                time.Type = values.Stamp.DateContext == DateContextEnum.ProposedStart ? ISOEnumerations.ISOTimeType.Planned : ISOEnumerations.ISOTimeType.Effective;

                foreach (MeteredValue value in values.Values)
                {
                    if (value.Value != null && value.Value is NumericRepresentationValue)
                    {
                        NumericRepresentationValue numericValue = value.Value as NumericRepresentationValue;
                        ISODataLogValue            dlv          = new ISODataLogValue();
                        int?ddi = RepresentationMapper.Map(numericValue.Representation);
                        if (ddi.HasValue)
                        {
                            dlv.ProcessDataDDI = ddi.Value.AsHexDDI();
                            DdiDefinition DdiDefinition = DDIs[ddi.Value];
                            dlv.ProcessDataValue = (int)(numericValue.Value.Value / (DdiDefinition.Resolution != 0 ? DdiDefinition.Resolution : 1d));
                        }
                        else
                        {
                            if (numericValue.Representation.CodeSource == RepresentationCodeSourceEnum.ISO11783_DDI)
                            {
                                dlv.ProcessDataDDI   = numericValue.Representation.Code;
                                dlv.ProcessDataValue = (int)numericValue.Value.Value;
                            }
                        }
                        if (value.DeviceConfigurationId.HasValue)
                        {
                            DeviceElementConfiguration config = DataModel.Catalog.DeviceElementConfigurations.FirstOrDefault(c => c.Id.ReferenceId == value.DeviceConfigurationId.Value);
                            if (config != null)
                            {
                                dlv.DeviceElementIdRef = TaskDataMapper.InstanceIDMap.GetISOID(config.DeviceElementId);
                            }
                        }
                        time.DataLogValues.Add(dlv);
                    }
                }
                times.Add(time);
            }
            return(times);
        }
Exemple #9
0
        public IEnumerable <ISODataLogValue> ExportDataLogValues(List <WorkingData> workingDatas, List <DeviceElementUse> deviceElementUses)
        {
            if (workingDatas == null)
            {
                return(null);
            }

            List <ISODataLogValue> dlvs = new List <ISODataLogValue>();
            int i = 0;

            foreach (WorkingData workingData in workingDatas)
            {
                //DDI
                int?mappedDDI = RepresentationMapper.Map(workingData.Representation);
                var dlv       = new ISODataLogValue();
                if (mappedDDI != null)
                {
                    if (workingData.Representation != null && workingData.Representation.Code == "dtRecordingStatus" && workingData.DeviceElementUseId != 0)
                    {
                        dlv.ProcessDataDDI = 141.AsHexDDI(); //No support for exporting CondensedWorkState at this time
                    }
                    else
                    {
                        dlv.ProcessDataDDI = mappedDDI.Value.AsHexDDI();
                    }
                }
                else if (workingData.Representation.CodeSource == ApplicationDataModel.Representations.RepresentationCodeSourceEnum.ISO11783_DDI)
                {
                    dlv.ProcessDataDDI = workingData.Representation.Code;
                }

                //DeviceElementIdRef
                DeviceElementUse use = deviceElementUses.FirstOrDefault(d => d.Id.ReferenceId == workingData.DeviceElementUseId);
                if (use != null)
                {
                    DeviceElementConfiguration deviceElementConfiguration = DataModel.Catalog.DeviceElementConfigurations.FirstOrDefault(d => d.Id.ReferenceId == use.DeviceConfigurationId);
                    if (deviceElementConfiguration != null)
                    {
                        //This requires the Devices will have been mapped prior to the LoggedData
                        dlv.DeviceElementIdRef = TaskDataMapper.InstanceIDMap.GetISOID(deviceElementConfiguration.DeviceElementId);
                    }
                }

                if (dlv.ProcessDataDDI != null && dlv.DeviceElementIdRef != null)
                {
                    dlvs.Add(dlv);
                    _dataLogValueOrdersByWorkingDataID.Add(workingData.Id.ReferenceId, i++);
                }
            }
            return(dlvs);
        }
Exemple #10
0
        private MeteredValue GetSummaryMeteredValue(ISODataLogValue dlv)
        {
            if (dlv.ProcessDataDDI == null)
            {
                return(null);
            }
            int ddi = dlv.ProcessDataDDI.AsInt32DDI();

            int dataValue = 0;

            if (dlv.ProcessDataValue.HasValue)
            {
                dataValue = dlv.ProcessDataValue.Value;
            }

            var unitOfMeasure = RepresentationMapper.GetUnitForDdi(ddi);

            if (!DDIs.ContainsKey(ddi) || unitOfMeasure == null)
            {
                return(null);
            }

            DdiDefinition ddiDefintion = DDIs[ddi];

            int?deviceConfigurationID = null;
            int?deviceElementID       = TaskDataMapper.InstanceIDMap.GetADAPTID(dlv.DeviceElementIdRef);

            if (deviceElementID.HasValue)
            {
                //Since Device creation is on-demand, we need to call GetDeviceElementConfiguration here to ensure the relevant device is created if it hasn't been yet.
                var hierarchy          = TaskDataMapper?.DeviceElementHierarchies?.GetRelevantHierarchy(dlv.DeviceElementIdRef);
                var adaptDeviceElement = DataModel?.Catalog?.DeviceElements?.FirstOrDefault(d => d?.Id?.ReferenceId == deviceElementID.Value);
                if (hierarchy != null && adaptDeviceElement != null)
                {
                    DeviceElementConfiguration config = DeviceElementMapper.GetDeviceElementConfiguration(adaptDeviceElement, hierarchy, DataModel.Catalog);
                    if (config != null)
                    {
                        deviceConfigurationID = config.Id.ReferenceId;
                    }
                }
            }

            return(new MeteredValue
            {
                Value = new NumericRepresentationValue(RepresentationMapper.Map(ddi) as NumericRepresentation,
                                                       unitOfMeasure,
                                                       new NumericValue(unitOfMeasure, dataValue * ddiDefintion.Resolution)),
                DeviceConfigurationId = deviceConfigurationID
            });
        }
        public IEnumerable <ISOProduct> ExportProducts(IEnumerable <Product> adaptProducts)
        {
            List <ISOProduct> isoProducts = new List <ISOProduct>();

            //Add all the products
            foreach (Product adaptProduct in adaptProducts)
            {
                ISOProduct product = ExportProduct(adaptProduct);
                isoProducts.Add(product);
            }

            //Fill in detail on the product mixes
            if (adaptProducts.OfType <MixProduct>().Any())
            {
                foreach (MixProduct adaptMixProduct in adaptProducts.OfType <MixProduct>())
                {
                    //Find the ISO Product
                    ISOProduct isoMixProduct = isoProducts.Single(p => p.ProductId == TaskDataMapper.InstanceIDMap.GetISOID(adaptMixProduct.Id.ReferenceId));

                    foreach (ProductComponent component in adaptMixProduct.ProductComponents)
                    {
                        Ingredient ingredient = DataModel.Catalog.Ingredients.FirstOrDefault(i => i.Id.ReferenceId == component.IngredientId);
                        if (ingredient != null)
                        {
                            ISOProduct componentProduct = isoProducts.FirstOrDefault(p => p.ProductDesignator == ingredient.Description); //Matches on name; assumes all ingredients are also products
                            if (componentProduct != null)
                            {
                                //Create PRNs if we can match to pre-existing products
                                ISOProductRelation relation = new ISOProductRelation();
                                relation.ProductIdRef  = componentProduct.ProductId;
                                relation.QuantityValue = component.Quantity.AsIntViaMappedDDI(RepresentationMapper);
                                isoMixProduct.ProductRelations.Add(relation);
                            }
                        }
                    }

                    //Total Quantity
                    isoMixProduct.MixtureRecipeQuantity = adaptMixProduct.TotalQuantity.AsIntViaMappedDDI(RepresentationMapper);

                    //Quantity DDI
                    int?ddi = RepresentationMapper.Map(adaptMixProduct.TotalQuantity.Representation);
                    if (ddi.HasValue)
                    {
                        isoMixProduct.QuantityDDI = ddi.Value.AsHexDDI();
                    }
                }
            }
            return(isoProducts);
        }
        /// <summary>
        /// Converts an ADAPT NumericRepresentation value with an included Representation mapped to a DDI to the appropriate int value for ISO
        /// </summary>
        /// <param name="value"></param>
        /// <param name="mapper"></param>
        /// <returns></returns>
        public static int AsIntViaMappedDDI(this NumericRepresentationValue value, RepresentationMapper mapper)
        {
            int?ddi = mapper.Map(value.Representation);

            if (ddi.HasValue)
            {
                ISOUnit unit = UnitFactory.Instance.GetUnitByDDI(ddi.Value);
                return((int)unit.ConvertToIsoUnit(value.Value.Value));
            }
            else if (value.Representation != null && value.Representation.CodeSource == RepresentationCodeSourceEnum.ISO11783_DDI)
            {
                //No need to convert if the value is natively a DDI
                return((int)value.Value.Value);
            }
            return(0);
        }
Exemple #13
0
        private MeteredValue GetSummaryMeteredValue(ISODataLogValue dlv)
        {
            if (dlv.ProcessDataDDI == null)
            {
                return(null);
            }
            int ddi = dlv.ProcessDataDDI.AsInt32DDI();

            int dataValue = 0;

            if (dlv.ProcessDataValue.HasValue)
            {
                dataValue = dlv.ProcessDataValue.Value;
            }

            var unitOfMeasure = RepresentationMapper.GetUnitForDdi(ddi);

            if (!DDIs.ContainsKey(ddi) || unitOfMeasure == null)
            {
                return(null);
            }

            DdiDefinition ddiDefintion = DDIs[ddi];

            int?deviceConfigurationID = null;
            int?deviceElementID       = TaskDataMapper.InstanceIDMap.GetADAPTID(dlv.DeviceElementIdRef);

            if (deviceElementID.HasValue)
            {
                DeviceElementConfiguration config = DataModel.Catalog.DeviceElementConfigurations.FirstOrDefault(c => c.DeviceElementId == deviceElementID.Value);
                if (config != null)
                {
                    deviceConfigurationID = config.Id.ReferenceId;
                }
            }

            return(new MeteredValue
            {
                Value = new NumericRepresentationValue(RepresentationMapper.Map(ddi) as NumericRepresentation,
                                                       unitOfMeasure,
                                                       new NumericValue(unitOfMeasure, dataValue * ddiDefintion.Resolution)),
                DeviceConfigurationId = deviceConfigurationID
            });
        }
        public ISODataLogTrigger ExportDataLogTrigger(DataLogTrigger adaptDataLogTrigger)
        {
            ISODataLogTrigger isoDataLogTrigger = new ISODataLogTrigger();

            int?ddi = null;

            if (adaptDataLogTrigger.RequestDefaultProcessData)
            {
                ddi = DefaultSet; //DFFF
            }
            else if (adaptDataLogTrigger.Representation != null)
            {
                ddi = RepresentationMapper.Map(adaptDataLogTrigger.Representation);
            }

            if (ddi.HasValue) //Need DDI for a valid DLT
            {
                isoDataLogTrigger.DataLogDDI = ddi.Value.AsHexDDI();
                var loggingMethods = adaptDataLogTrigger.DataLogMethods.Any() ? adaptDataLogTrigger.DataLogMethods : new List <LoggingMethodEnum> {
                    adaptDataLogTrigger.DataLogMethod
                };
                isoDataLogTrigger.DataLogMethod           = ExportDataLogMethods(loggingMethods);
                isoDataLogTrigger.DataLogDistanceInterval = adaptDataLogTrigger.DataLogDistanceInterval?.AsIntViaMappedDDI(RepresentationMapper);
                isoDataLogTrigger.DataLogTimeInterval     = adaptDataLogTrigger.DataLogTimeInterval?.AsIntViaMappedDDI(RepresentationMapper);
                isoDataLogTrigger.DataLogThresholdMinimum = adaptDataLogTrigger.DataLogThresholdMinimum?.AsIntViaMappedDDI(RepresentationMapper);
                isoDataLogTrigger.DataLogThresholdMaximum = adaptDataLogTrigger.DataLogThresholdMaximum?.AsIntViaMappedDDI(RepresentationMapper);
                isoDataLogTrigger.DataLogThresholdChange  = adaptDataLogTrigger.DataLogThresholdChange?.AsIntViaMappedDDI(RepresentationMapper);
                if (adaptDataLogTrigger.DeviceElementId.HasValue)
                {
                    isoDataLogTrigger.DeviceElementIdRef = TaskDataMapper.InstanceIDMap.GetISOID(adaptDataLogTrigger.DeviceElementId.Value);
                }

                //Not yet implemented
                //isoDataLogTrigger.ValuePresentationIdRef = "";
                //isoDataLogTrigger.DataLogPGN = null;
                //isoDataLogTrigger.DataLogPGNStartBit = null;
                //isoDataLogTrigger.DataLogPGNStopBit = null;
            }

            return(isoDataLogTrigger);
        }
Exemple #15
0
        public DataLogTrigger ImportDataLogTrigger(ISODataLogTrigger isoDataLogTrigger)
        {
            int ddi = isoDataLogTrigger.DataLogDDI.AsInt32DDI();

            ApplicationDataModel.Representations.Representation representation = RepresentationMapper.Map(ddi);
            if (representation != null)
            {
                DataLogTrigger adaptTrigger = new DataLogTrigger();
                adaptTrigger.Representation = representation;
                adaptTrigger.DataLogMethod  = ImportDataLogMethod(isoDataLogTrigger.DataLogMethod);
                if (isoDataLogTrigger.DataLogDistanceInterval.HasValue)
                {
                    adaptTrigger.DataLogDistanceInterval = isoDataLogTrigger.DataLogDistanceInterval.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                if (isoDataLogTrigger.DataLogTimeInterval.HasValue)
                {
                    adaptTrigger.DataLogTimeInterval = isoDataLogTrigger.DataLogTimeInterval.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                if (isoDataLogTrigger.DataLogThresholdMinimum.HasValue)
                {
                    adaptTrigger.DataLogThresholdMinimum = isoDataLogTrigger.DataLogThresholdMinimum.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                if (isoDataLogTrigger.DataLogThresholdMaximum.HasValue)
                {
                    adaptTrigger.DataLogThresholdMaximum = isoDataLogTrigger.DataLogThresholdMaximum.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                if (isoDataLogTrigger.DataLogThresholdChange.HasValue)
                {
                    adaptTrigger.DataLogThresholdChange = isoDataLogTrigger.DataLogThresholdChange.Value.AsNumericRepresentationValue(ddi, RepresentationMapper);
                }
                adaptTrigger.DeviceElementId = TaskDataMapper.InstanceIDMap.GetADAPTID(isoDataLogTrigger.DeviceElementIdRef);

                //Not yet implemented
                //adaptTrigger.LoggingLevel = null;

                return(adaptTrigger);
            }
            return(null);
        }
        /// <summary>
        /// If an implementer wants to export to a custom DDI or otherwise one that doesn't map,
        /// the appropriate DDI may be set in the Prescription prior to exporting.
        /// A ISO11783_DDI representation is as such the first mapping attempted.
        /// </summary>
        /// <param name="representation"></param>
        /// <param name="adaptUnit"></param>
        /// <returns></returns>
        private int DetermineVariableDDI(NumericRepresentation representation, UnitOfMeasure adaptUnit)
        {
            if (representation != null)
            {
                if (representation.CodeSource == RepresentationCodeSourceEnum.ISO11783_DDI)
                {
                    return(Int32.Parse(representation.Code));
                }

                int?mappedDDI = RepresentationMapper.Map(representation);
                if (mappedDDI.HasValue)
                {
                    return(mappedDDI.Value);
                }
            }

            if (adaptUnit != null && UnitFactory.DimensionToDdi.ContainsKey(adaptUnit.Dimension))
            {
                return(UnitFactory.DimensionToDdi[adaptUnit.Dimension]);
            }

            TaskDataMapper.AddError($"Unable to determine DDI for Prescription export {representation.Code}.", $"Representation ID : {representation.Id.ReferenceId}", "PrescriptionMapper.DetermineVariableDDI()");
            return(0); //Return an invalid DDI
        }
Exemple #17
0
        public void GivenDdiWhenMapThenNumericRepresentationIsReturned()
        {
            var result = _representationMapper.Map(183);

            Assert.AreEqual(RepresentationInstanceList.vrYieldMass.DomainId, result.Code);
        }
        internal void ImportSharedPrescriptionProperties(ISOTask task, WorkItem workItem, Prescription prescription)
        {
            //Description
            prescription.Description = task.TaskDesignator;

            //CropZone/Field
            if (workItem.CropZoneId.HasValue)
            {
                prescription.CropZoneId = workItem.CropZoneId.Value;
            }
            else if (workItem.FieldId.HasValue)
            {
                prescription.FieldId = workItem.FieldId.Value;
            }

            //Products
            prescription.ProductIds = new List <int>();
            foreach (ISOTreatmentZone treatmentZone in task.TreatmentZones)
            {
                foreach (var dataVariable in treatmentZone.ProcessDataVariables)
                {
                    if (!string.IsNullOrEmpty(dataVariable.ProductIdRef))
                    {
                        int?productID = TaskDataMapper.InstanceIDMap.GetADAPTID(dataVariable.ProductIdRef);
                        if (productID.HasValue)
                        {
                            //ProductIDs
                            if (!prescription.ProductIds.Contains(productID.Value))
                            {
                                prescription.ProductIds.Add(productID.Value);
                            }

                            //Product Lookups
                            int ddi             = dataVariable.ProcessDataDDI.AsInt32DDI();
                            var rxProductLookup = new RxProductLookup
                            {
                                ProductId      = productID,
                                UnitOfMeasure  = UnitFactory.Instance.GetUnitByDDI(ddi).ToAdaptUnit(),
                                Representation = (NumericRepresentation)RepresentationMapper.Map(ddi)
                            };

                            if (!prescription.RxProductLookups.Any(r => r.ProductId == rxProductLookup.ProductId))
                            {
                                prescription.RxProductLookups.Add(rxProductLookup);
                            }
                        }
                    }
                }
            }

            //Connections
            if (task.Connections.Any())
            {
                IEnumerable <EquipmentConfiguration> equipConfigs = _connectionMapper.ImportConnections(task);

                workItem.EquipmentConfigurationGroup = new EquipmentConfigurationGroup();
                workItem.EquipmentConfigurationGroup.EquipmentConfigurations = equipConfigs.ToList();

                DataModel.Catalog.EquipmentConfigurations.AddRange(equipConfigs);
            }
        }
        private ISOTreatmentZone ExportTreatmentZonesForType2(ISOTask task, RasterGridPrescription prescription)
        {
            if (prescription.ProductIds == null)
            {
                TaskDataMapper.AddError($"No Products are present for Grid Type 2 Prescription export: {prescription.Description}", prescription.Id.ReferenceId.ToString());
                return(null);
            }

            var lossOfSignalTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Loss of GPS", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };
            var outOfFieldTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Out of Field", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };
            var defaultTreatmentZone = new ISOTreatmentZone {
                TreatmentZoneDesignator = "Default", ProcessDataVariables = new List <ISOProcessDataVariable>()
            };

            foreach (var productId in prescription.ProductIds)
            {
                var isoUnit = DetermineIsoUnit(prescription.RxProductLookups.First(p => p.ProductId == productId).UnitOfMeasure);

                string          isoProductId  = TaskDataMapper.InstanceIDMap.GetISOID(productId) ?? string.Empty;
                RxProductLookup productLookup = prescription.RxProductLookups.FirstOrDefault(p => p.ProductId == productId);

                ISOProcessDataVariable lossPDV = ExportProcessDataVariable(productLookup?.LossOfGpsRate ?? prescription.LossOfGpsRate, isoProductId, isoUnit);
                if (lossPDV != null)
                {
                    lossOfSignalTreatmentZone.ProcessDataVariables.Add(lossPDV);
                }

                ISOProcessDataVariable oofPDV = ExportProcessDataVariable(productLookup?.OutOfFieldRate ?? prescription.OutOfFieldRate, isoProductId, isoUnit);
                if (oofPDV != null)
                {
                    outOfFieldTreatmentZone.ProcessDataVariables.Add(oofPDV);
                }

                NumericRepresentation defaultRepresentation = productLookup?.LossOfGpsRate.Representation; //We can reuse the loss of gps representation here if it exists
                if (defaultRepresentation == null)
                {
                    //Determine the representation based on the unit of the product to be applied
                    var unitDimension = isoUnit.ToAdaptUnit().Dimension;
                    if (UnitFactory.DimensionToDdi.ContainsKey(unitDimension))
                    {
                        int ddi = UnitFactory.DimensionToDdi[unitDimension];
                        RepresentationMapper representationMapper = new RepresentationMapper();
                        var representation = representationMapper.Map(ddi) as NumericRepresentation;
                        if (representation == null)
                        {
                            representation = new NumericRepresentation
                            {
                                Code       = ddi.ToString(),
                                CodeSource = RepresentationCodeSourceEnum.ISO11783_DDI
                            };
                        }
                    }
                    else
                    {
                        TaskDataMapper.AddError($"Unable to identify a default representation: {prescription.Description}", prescription.Id.ReferenceId.ToString());
                        return(null);
                    }
                }
                //Add 0 as the default rate in the PDV; actual values are in the binary
                var defaultRate = new NumericRepresentationValue(defaultRepresentation, new NumericValue(prescription.RxProductLookups.First(p => p.ProductId == productId).UnitOfMeasure, 0d));
                ISOProcessDataVariable defaultPDV = ExportProcessDataVariable(defaultRate, isoProductId, isoUnit);
                defaultTreatmentZone.ProcessDataVariables.Add(defaultPDV);
            }

            if (lossOfSignalTreatmentZone.ProcessDataVariables.Count > 0)
            {
                lossOfSignalTreatmentZone.TreatmentZoneCode = 253;
                task.TreatmentZones.Add(lossOfSignalTreatmentZone);
                task.PositionLostTreatmentZoneCode = lossOfSignalTreatmentZone.TreatmentZoneCode;
            }

            if (outOfFieldTreatmentZone.ProcessDataVariables.Count > 0)
            {
                outOfFieldTreatmentZone.TreatmentZoneCode = 254;
                task.TreatmentZones.Add(outOfFieldTreatmentZone);
                task.OutOfFieldTreatmentZoneCode = outOfFieldTreatmentZone.TreatmentZoneCode;
            }

            defaultTreatmentZone.TreatmentZoneCode = 1;
            task.TreatmentZones.Add(defaultTreatmentZone);
            task.DefaultTreatmentZoneCode = defaultTreatmentZone.TreatmentZoneCode;

            return(defaultTreatmentZone);
        }