Beispiel #1
0
        public static void CheckWeeds(Field field, IDataProvider dataProvider, IRabbitMqBus bus)
        {
            var result = WeedCheckDataGenerator.Generate(dataProvider.GetWeedsAsync().Result);

            if (result != null)
            {
                var severity = DangerLevel.Low;
                if (result.Percentage >= 10 && result.Percentage <= 60)
                {
                    severity = DangerLevel.High;
                }
                else if (result.Percentage > 60)
                {
                    severity = DangerLevel.Critical;
                }

                var message = new WeedDetectedMessage()
                {
                    FieldId        = field.Id,
                    OrganizationId = field.OrganizationId,
                    WeedId         = result.WeedId,
                    Percentage     = result.Percentage,
                    Severity       = severity
                };

                bus.Publish(message);
            }
        }
        public async Task <WeedDetectedRecommendation> GetWeedDetectedRecommendation(WeedDetectedMessage message)
        {
            var field = await _fieldRepository.GetAsync(message.FieldId);

            if (field == null)
            {
                Console.WriteLine($"Field {message.FieldId} was not found.");
                return(null);
            }

            var pesticides = (await _pesticideRepository.BrowseAsync(x => true));

            if (pesticides == null)
            {
                Console.WriteLine($"Error while getting pesticides.");
                return(null);
            }

            var weed = await _weedRepository.GetAsync(message.WeedId);

            if (weed == null)
            {
                Console.WriteLine($"Weed {message.FieldId} was not found.");
                return(null);
            }

            if (field.CurrentCrop == null)
            {
                Console.WriteLine("Field is empty. No need to recommend.");
                return(null);
            }

            var currentCulture = await _cultureRepository.GetAsync(field.CurrentCrop.CultureId);

            pesticides = pesticides.Where(x => weed.Pesticides.Any(p => p.PesticideId == x.Id) &&
                                          !x.ComponentsIds.Intersect(currentCulture.ForbiddenComponents).Any());

            var pesticidesWithDose = weed.Pesticides.Where(x => pesticides.Any(p => p.Id == x.PesticideId)).ToList();

            if (pesticidesWithDose.Count == 0)
            {
                Console.WriteLine("Unable to find recommendation");
                return(null);
            }

            return(new WeedDetectedRecommendation()
            {
                Pesticides = pesticidesWithDose
            });
        }