FarmMapsApiClient_WURtest/FarmmapsHerbicide/HerbicideService.cs

93 lines
3.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
2020-04-08 18:53:08 +00:00
using FarmmapsHerbicide.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using static FarmmapsApi.Extensions;
using static FarmmapsApiSamples.Constants;
2020-04-08 18:53:08 +00:00
namespace FarmmapsHerbicide
{
2020-04-08 18:23:22 +00:00
public class HerbicideService
{
private readonly ILogger<HerbicideService> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly GeneralService _generalService;
public HerbicideService(ILogger<HerbicideService> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
}
public async Task<List<HerbicideAgent>> GetHerbicideAgents()
{
var itemType = "vnd.farmmaps.package.vra.herbicide";
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<HerbicideAgent>>() : null;
}
2020-04-08 18:53:08 +00:00
public async Task<Item> CalculateApplicationMapAsync(Item cropfieldItem, HerbicideAgent agent, params Item[] inputItem)
{
if (inputItem.Length == 0)
return null;
var taskRequest = new TaskRequest()
{
TaskType = "vnd.farmmaps.task.vraherbicide"
};
taskRequest.attributes["inputCode"] = inputItem[0].Code;
taskRequest.attributes["agent"] = JsonConvert.SerializeObject(agent);
2020-06-10 13:24:08 +00:00
// check for the layer name
taskRequest.attributes["inputLayerName"] = inputItem[0].Data["layers"][0]["name"].ToString();
if (inputItem.Length > 1)
2020-06-10 13:24:08 +00:00
{
taskRequest.attributes["extraInputCode"] = inputItem[1].Code;
2020-06-10 13:24:08 +00:00
taskRequest.attributes["extraInputLayerName"] = inputItem[1].Data["layers"][0]["name"].ToString();
}
var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskRequest);
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
{
_logger.LogInformation("Checking vraherbicide 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;
}
2020-04-14 07:49:43 +00:00
var itemName = $"VRAHerbicide {agent.Name}";
var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
i.Name.ToLower().Contains(itemName.ToLower()));
if (applianceMapItem == null)
{
_logger.LogError("Could not find the VRAHerbicide geotiff child item under cropfield");
return null;
}
return applianceMapItem;
}
}
}