2020-05-05 14:08:32 +00:00
|
|
|
using System;
|
2020-04-22 11:41:49 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-05-05 14:08:32 +00:00
|
|
|
using FarmmapsApi.Models;
|
2020-04-22 11:41:49 +00:00
|
|
|
using FarmmapsApi.Services;
|
|
|
|
using FarmmapsHaulmkilling.Models;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-05-05 14:08:32 +00:00
|
|
|
using static FarmmapsApi.Extensions;
|
|
|
|
using static FarmmapsApiSamples.Constants;
|
2020-04-22 11:41:49 +00:00
|
|
|
|
2020-04-22 10:51:53 +00:00
|
|
|
namespace FarmmapsHaulmkilling
|
|
|
|
{
|
|
|
|
public class HaulmkillingService
|
|
|
|
{
|
2020-04-22 11:41:49 +00:00
|
|
|
private readonly ILogger<HaulmkillingService> _logger;
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
private readonly GeneralService _generalService;
|
|
|
|
|
|
|
|
public HaulmkillingService(ILogger<HaulmkillingService> logger, FarmmapsApiService farmmapsApiService,
|
|
|
|
GeneralService generalService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
_generalService = generalService;
|
|
|
|
}
|
2020-04-22 10:51:53 +00:00
|
|
|
|
2020-05-05 14:08:32 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the list of available haulmkilling agents
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>List of haulmkilling agents</returns>
|
2020-04-22 11:41:49 +00:00
|
|
|
public async Task<List<HaulmkillingAgent>> GetHaulmkillingAgents()
|
|
|
|
{
|
|
|
|
var itemType = "vnd.farmmaps.package.vra.haulmkilling";
|
|
|
|
var vraHerbicideDataItems = await _farmmapsApiService.GetItemsAsync(string.Empty, itemType);
|
|
|
|
|
|
|
|
var item = vraHerbicideDataItems.FirstOrDefault();
|
|
|
|
if (item == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return item.Data.ContainsKey("agents") ? item.Data["agents"].ToObject<List<HaulmkillingAgent>>() : null;
|
|
|
|
}
|
2020-05-05 14:08:32 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an haulmkilling application map
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="cropfieldItem">The context cropfield item to use</param>
|
|
|
|
/// <param name="inputItem">The geotiff item to use</param>
|
|
|
|
/// <param name="inputType">WDVI or NDVI</param>
|
|
|
|
/// <param name="agentName">One of the available agents</param>
|
|
|
|
/// <param name="selectedOption">One of the available options</param>
|
|
|
|
/// <returns>Haulmkilling application map item</returns>
|
|
|
|
public async Task<Item> CalculateApplicationMapAsync(Item cropfieldItem, Item inputItem, string inputType,
|
|
|
|
string agentName, string selectedOption)
|
|
|
|
{
|
|
|
|
var taskRequest = new TaskRequest()
|
|
|
|
{
|
|
|
|
TaskType = "vnd.farmmaps.task.vrahaulmkilling"
|
|
|
|
};
|
|
|
|
taskRequest.attributes["inputCode"] = inputItem.Code;
|
|
|
|
taskRequest.attributes["inputType"] = inputType;
|
|
|
|
taskRequest.attributes["agentName"] = agentName;
|
|
|
|
taskRequest.attributes["selectedOption"] = selectedOption;
|
2020-05-10 14:04:10 +00:00
|
|
|
taskRequest.attributes["minPercentile"] = "0.0";
|
2020-05-05 14:08:32 +00:00
|
|
|
|
|
|
|
var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskRequest);
|
|
|
|
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Checking vrahaulmkilling task status");
|
|
|
|
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
|
|
|
|
if (itemTaskStatus.IsFinished)
|
|
|
|
tokenSource.Cancel();
|
|
|
|
});
|
|
|
|
|
|
|
|
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
|
|
|
|
if (itemTask.State == ItemTaskState.Error)
|
|
|
|
{
|
|
|
|
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var itemName = $"VRAHaulmkilling {agentName}";
|
|
|
|
var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
|
|
|
|
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
|
2020-05-06 12:28:35 +00:00
|
|
|
i => i.Updated >= itemTask.Finished &&
|
2020-05-05 14:08:32 +00:00
|
|
|
i.Name.ToLower().Contains(itemName.ToLower()));
|
|
|
|
|
|
|
|
if (applianceMapItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find the VRAHaulmkilling geotiff child item under cropfield");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return applianceMapItem;
|
|
|
|
}
|
2020-04-22 10:51:53 +00:00
|
|
|
}
|
|
|
|
}
|