FarmMapsApiClient_KB34_MAST/FarmmapsNbs/NitrogenService.cs

191 lines
9.6 KiB
C#

using System;
using System.Globalization;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using FarmmapsNbs.Models;
using Microsoft.Extensions.Logging;
using static FarmmapsApi.Extensions;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsNbs
{
public class NitrogenService
{
private readonly ILogger<NitrogenService> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly GeneralService _generalService;
public NitrogenService(ILogger<NitrogenService> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
}
public async Task<Item> CreateTargetNItem(Item cropfieldItem)
{
var itemRequest = new ItemRequest()
{
ParentCode = cropfieldItem.ParentCode,
ItemType = USERINPUT_ITEMTYPE,
Name = "TargetN"
};
return await _farmmapsApiService.CreateItemAsync(itemRequest);
}
/// <summary>
/// Calculates TargetN, makes the assumption the cropfield and user.input(targetn) item have the same parent
/// </summary>
/// <param name="cropfieldItem">The cropfield to base the calculations on</param>
/// <param name="targetNItem">The targetN item to save calculations in</param>
/// <param name="plantingDate">The date the crop is planted</param>
/// <param name="measurementDate">The date the measurements are taken</param>
/// <param name="purposeType">The crop purpose</param>
/// <param name="targetYield">The target yield input for the TargetN calculation</param>
/// <returns>The TargetN</returns>
public async Task<TargetNData> CalculateTargetN(Item cropfieldItem, Item targetNItem, DateTime plantingDate,
DateTime measurementDate, string purposeType, int targetYield)
{
var nbsTargetNRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsTargetNRequest.attributes["operation"] = "targetn";
nbsTargetNRequest.attributes["inputCode"] = targetNItem.Code;
nbsTargetNRequest.attributes["plantingDate"] = plantingDate.ToString("o");
nbsTargetNRequest.attributes["measurementDate"] = measurementDate.ToString("o");
nbsTargetNRequest.attributes["purposeType"] = purposeType.ToLower();
nbsTargetNRequest.attributes["targetYield"] = targetYield.ToString();
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsTargetNRequest);
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if(itemTask.State == ItemTaskState.Error)
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null;
}
var item = await _farmmapsApiService.GetItemAsync(targetNItem.Code);
return item.Data.ToObject<TargetNData>();
}
/// <summary>
/// Calculates the uptake map based on the given inputs
/// </summary>
/// <param name="cropfieldItem">The cropfield to base the calculations on</param>
/// <param name="inputItem"></param>
/// <param name="plantingDate">The date the crop is planted</param>
/// <param name="measurementDate">The date the measurements are taken</param>
/// <param name="inputType">Data type, could be yara, ci, irmi or wdvi</param>
/// <param name="inputLayerName">Column name in which the sensor value is stored</param>
/// <returns></returns>
public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
DateTime measurementDate, string inputType, string inputLayerName)
{
var nbsUptakeMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsUptakeMapRequest.attributes["operation"] = "uptake";
nbsUptakeMapRequest.attributes["inputCode"] = inputItem.Code;
nbsUptakeMapRequest.attributes["plantingDate"] = plantingDate.ToString("o");
nbsUptakeMapRequest.attributes["measurementDate"] = measurementDate.ToString("o");
nbsUptakeMapRequest.attributes["inputType"] = inputType.ToLower();
if (!(string.IsNullOrEmpty(inputLayerName))) nbsUptakeMapRequest.attributes["inputLayerName"] = inputLayerName;
//toevoeging FS. Kolom IRMI hernoemd als IMI. Deze wordt niet automatisch herkend. En moet dus gespecificeerd worden.
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsUptakeMapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
_logger.LogInformation($"Calculating uptake map; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTask.State == ItemTaskState.Error)
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null;
}
var itemName = "VRANbs uptake";
var uptakeMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
i.Name.ToLower().Contains(itemName.ToLower()));
if (uptakeMapItem == null)
{
_logger.LogError("Could not find the uptake geotiff child item under cropfield");
return null;
}
return uptakeMapItem;
}
/// <summary>
/// Creates the nitrogen application map based on given input data
/// </summary>
/// <param name="cropfieldItem">The cropfield to base the calculations on</param>
/// <param name="inputItem">The farmmaps item containing the geotiff data</param>
/// <param name="plantingDate">The date the crop is planted</param>
/// <param name="measurementDate">The date the measurements are taken</param>
/// <param name="inputType">The inputtype to use, could be yara, ci, irmi or wdvi</param>
/// <param name="targetN">The target nitrogen to use for the calculations</param>
/// <returns></returns>
public async Task<Item> CalculateApplicationMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
DateTime measurementDate, string inputType, double targetN, string inputLayerName)
{
var nbsApplicationMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsApplicationMapRequest.attributes["operation"] = "application";
nbsApplicationMapRequest.attributes["inputCode"] = inputItem.Code;
nbsApplicationMapRequest.attributes["plantingDate"] = plantingDate.ToString("o");
nbsApplicationMapRequest.attributes["measurementDate"] = measurementDate.ToString("o");
nbsApplicationMapRequest.attributes["inputCode"] = inputItem.Code;
nbsApplicationMapRequest.attributes["inputType"] = inputType.ToLower();
nbsApplicationMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
if (!(string.IsNullOrEmpty(inputLayerName))) nbsApplicationMapRequest.attributes["inputLayerName"] = inputLayerName;
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplicationMapRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
_logger.LogInformation($"Calculating application map; status: {itemTaskStatus.State}");
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if(itemTask.State == ItemTaskState.Error)
{
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
return null;
}
var itemName = $"VRANbs application";
var applicationMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
i.Name.ToLower().Contains(itemName.ToLower()));
if (applicationMapItem == null)
{
_logger.LogError("Could not find the application map geotiff child item under cropfield");
return null;
}
return applicationMapItem;
}
}
}