FarmMapsApiClient_KB34_MAST/FarmmapsZonering/Services/ZoneringService.cs

66 lines
2.7 KiB
C#
Raw Normal View History

2020-11-12 14:49:17 +00:00
using System;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
2020-11-12 14:49:17 +00:00
using FarmmapsHaulmkilling.Models;
using FarmmapsZonering.Models;
using Microsoft.Extensions.Logging;
2020-11-12 21:12:31 +00:00
using Newtonsoft.Json;
2020-11-12 14:49:17 +00:00
using static FarmmapsApi.Extensions;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsZonering.Services
{
public class ZoneringService
{
private readonly ILogger<ZoneringService> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly GeneralService _generalService;
public ZoneringService(ILogger<ZoneringService> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
}
2020-11-12 14:49:17 +00:00
public async Task<Item> CreateApplicationMapAsync(Item cropfieldItem, string formula, Output output, params InputParameter[] inputItemCodes)
{
var zoneringTaskRequest = new TaskRequest() {TaskType = VRAZONERING_TASK};
2020-11-12 21:12:31 +00:00
zoneringTaskRequest.attributes["formula"] = formula;
zoneringTaskRequest.attributes["output"] = JsonConvert.SerializeObject(output);
zoneringTaskRequest.attributes["inputs"] = JsonConvert.SerializeObject(inputItemCodes);
2020-11-12 14:49:17 +00:00
var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, zoneringTaskRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
_logger.LogInformation($"Waiting on calculation of application map; Status: {itemTaskStatus.State}");
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 applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
2021-02-15 09:17:04 +00:00
GEOTIFF_PROCESSED_ITEMTYPE, output.Name,
2020-11-12 14:49:17 +00:00
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
2021-02-15 09:17:04 +00:00
i.Name.ToLower().Contains(output.Name.ToLower()));
2020-11-12 14:49:17 +00:00
if (applianceMapItem == null)
{
_logger.LogError("Could not find the VRAZonering geotiff child item under cropfield");
return null;
}
return applianceMapItem;
}
}
}