FarmMapsApiClient/FarmmapsNbs/NitrogenService.cs

187 lines
9.2 KiB
C#
Raw Normal View History

2020-03-25 16:49:00 +00:00
using System;
using System.Globalization;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using FarmmapsNbs.Models;
2020-03-25 16:49:00 +00:00
using Microsoft.Extensions.Logging;
using static FarmmapsApi.Extensions;
using static FarmmapsApiSamples.Constants;
2020-04-08 18:39:38 +00:00
namespace FarmmapsNbs
2020-03-25 16:49:00 +00:00
{
2020-04-08 18:23:22 +00:00
public class NitrogenService
2020-03-25 16:49:00 +00:00
{
private readonly ILogger<NitrogenService> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly GeneralService _generalService;
2020-03-25 16:49:00 +00:00
public NitrogenService(ILogger<NitrogenService> logger, FarmmapsApiService farmmapsApiService,
GeneralService generalService)
2020-03-25 16:49:00 +00:00
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
2020-03-25 16:49:00 +00:00
}
2020-04-08 17:28:30 +00:00
public async Task<Item> CreateTargetNItem(Item cropfieldItem)
{
var itemRequest = new ItemRequest()
{
ParentCode = cropfieldItem.ParentCode,
ItemType = USERINPUT_ITEMTYPE,
Name = "TargetN"
};
return await _farmmapsApiService.CreateItemAsync(itemRequest);
}
2020-04-10 11:12:15 +00:00
2020-03-25 16:49:00 +00:00
/// <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>
2020-04-10 11:12:15 +00:00
/// <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>
2020-04-10 11:12:15 +00:00
/// <param name="purposeType">The crop purpose</param>
/// <param name="targetYield">The target yield input for the TargetN calculation</param>
2020-03-25 16:49:00 +00:00
/// <returns>The TargetN</returns>
public async Task<TargetNData> CalculateTargetN(Item cropfieldItem, Item targetNItem, DateTime plantingDate,
2020-04-10 11:12:15 +00:00
DateTime measurementDate, string purposeType, int targetYield)
2020-03-25 16:49:00 +00:00
{
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");
2020-04-08 17:28:30 +00:00
nbsTargetNRequest.attributes["purposeType"] = purposeType.ToLower();
2020-03-25 16:49:00 +00:00
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)
2020-03-25 16:49:00 +00:00
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;
2020-03-25 16:49:00 +00:00
}
var item = await _farmmapsApiService.GetItemAsync(targetNItem.Code);
return item.Data.ToObject<TargetNData>();
2020-03-25 16:49:00 +00:00
}
2020-04-06 12:45:28 +00:00
/// <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>
/// <returns></returns>
public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem, DateTime plantingDate,
2020-04-28 14:35:14 +00:00
DateTime measurementDate, string inputType)
2020-04-06 12:45:28 +00:00
{
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");
2020-04-08 17:28:30 +00:00
nbsUptakeMapRequest.attributes["inputType"] = inputType.ToLower();
nbsUptakeMapRequest.attributes["inputLayerName"] = "IRMI"; //toevoeging FS. Kolom IRMI hernoemd als IMI. Deze wordt niet automatisch herkend. En moet dus gespecificeerd worden.
//var layers = inputItem.Data["layers"]; //toevoeging FS, check welke data lagen worden omgezet
//_logger.LogInformation($"DataLayers: {layers}"); //toevoeging FS check welke data lagen worden omgezet
2020-04-06 12:45:28 +00:00
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsUptakeMapRequest);
2020-04-06 12:45:28 +00:00
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTaskStatus.IsFinished)
2020-04-06 12:45:28 +00:00
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;
}
2020-04-08 17:28:30 +00:00
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)
2020-04-06 12:45:28 +00:00
{
_logger.LogError("Could not find the uptake geotiff child item under cropfield");
return null;
}
2020-04-08 17:28:30 +00:00
return uptakeMapItem;
2020-04-06 12:45:28 +00:00
}
2020-03-25 16:49:00 +00:00
/// <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>
2020-04-08 17:28:30 +00:00
/// <param name="inputType">The inputtype to use</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,
2020-04-28 14:35:14 +00:00
DateTime measurementDate, string inputType, double targetN)
2020-03-25 16:49:00 +00:00
{
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);
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplicationMapRequest);
2020-03-25 16:49:00 +00:00
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTaskStatus.IsFinished)
2020-03-25 16:49:00 +00:00
tokenSource.Cancel();
});
2020-03-25 16:49:00 +00:00
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;
}
2020-04-08 17:28:30 +00:00
var itemName = $"VRANbs application";
var applicationMapItem = await _generalService.FindChildItemAsync(cropfieldItem.Code,
2020-04-08 17:28:30 +00:00
GEOTIFF_PROCESSED_ITEMTYPE, itemName,
i => i.Updated >= itemTask.Finished.GetValueOrDefault(DateTime.UtcNow) &&
i.Name.ToLower().Contains(itemName.ToLower()));
if (applicationMapItem == null)
2020-03-25 16:49:00 +00:00
{
_logger.LogError("Could not find the application map geotiff child item under cropfield");
2020-03-25 16:49:00 +00:00
return null;
}
return applicationMapItem;
2020-03-25 16:49:00 +00:00
}
}
}