FarmMapsApiClient/FarmmapsZonering/Services/ZoneringService.cs

68 lines
2.9 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 itemName = $"VRAZonering";
2020-12-18 09:22:23 +00:00
var applianceMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
2020-11-12 14:49:17 +00:00
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
2021-01-28 14:08:32 +00:00
//i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
// i.Name.ToLower().Contains(itemName.ToLower()));
i =>
2020-11-12 14:49:17 +00:00
i.Name.ToLower().Contains(itemName.ToLower()));
if (applianceMapItem == null)
{
_logger.LogError("Could not find the VRAZonering geotiff child item under cropfield");
return null;
}
return applianceMapItem;
}
}
}