2020-11-12 14:49:17 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FarmmapsApi.Models;
|
2020-10-15 13:14:29 +00:00
|
|
|
using FarmmapsApi.Services;
|
2020-11-12 14:49:17 +00:00
|
|
|
using FarmmapsHaulmkilling.Models;
|
|
|
|
using FarmmapsZonering.Models;
|
2020-10-15 13:14:29 +00:00
|
|
|
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;
|
2020-10-15 13:14:29 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-03-01 13:04:18 +00:00
|
|
|
|
|
|
|
var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code);
|
|
|
|
//int last = uploadedFilesChildren.Count - 1;
|
|
|
|
//var applianceMapItem = uploadedFilesChildren[last];
|
|
|
|
|
|
|
|
|
|
|
|
var itemName = output.Name;
|
|
|
|
//var itemName = $"VRAZonering";
|
|
|
|
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()));
|
|
|
|
|
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;
|
|
|
|
}
|
2020-10-15 13:14:29 +00:00
|
|
|
}
|
|
|
|
}
|