2020-03-25 16:49:00 +00:00
using System ;
using System.Globalization ;
using System.Threading.Tasks ;
using FarmmapsApi.Models ;
using FarmmapsApi.Services ;
2020-04-08 20:17:39 +00:00
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 ;
2020-04-08 10:18:17 +00:00
private readonly GeneralService _generalService ;
2020-03-25 16:49:00 +00:00
2020-04-08 10:18:17 +00:00
public NitrogenService ( ILogger < NitrogenService > logger , FarmmapsApiService farmmapsApiService ,
GeneralService generalService )
2020-03-25 16:49:00 +00:00
{
_logger = logger ;
_farmmapsApiService = farmmapsApiService ;
2020-04-08 10:18:17 +00:00
_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>
2020-04-06 14:44:17 +00:00
/// <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>
2020-04-08 20:17:39 +00:00
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 ;
2020-08-19 11:40:42 +00:00
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 ) ;
2020-04-08 10:18:17 +00:00
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}" ) ;
2020-04-08 20:17:39 +00:00
return null ;
2020-03-25 16:49:00 +00:00
}
var item = await _farmmapsApiService . GetItemAsync ( targetNItem . Code ) ;
2020-04-08 20:17:39 +00:00
return item . Data . ToObject < TargetNData > ( ) ;
2020-03-25 16:49:00 +00:00
}
2020-04-06 12:45:28 +00:00
2020-04-06 14:44:17 +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
{
2020-04-06 14:44:17 +00:00
var nbsUptakeMapRequest = new TaskRequest { TaskType = VRANBS_TASK } ;
nbsUptakeMapRequest . attributes [ "operation" ] = "uptake" ;
nbsUptakeMapRequest . attributes [ "inputCode" ] = inputItem . Code ;
2020-08-19 11:40:42 +00:00
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 ( ) ;
2020-08-19 11:40:42 +00:00
nbsUptakeMapRequest . attributes [ "inputLayerName" ] = "IRMI" ; //toevoeging FS. Kolom IRMI hernoemd als IMI. Deze wordt niet automatisch herkend. En moet dus gespecificeerd worden.
2020-10-07 20:06:56 +00:00
//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-08-19 11:40:42 +00:00
2020-04-06 12:45:28 +00:00
2020-04-06 14:44:17 +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 ) ;
2020-04-08 10:18:17 +00:00
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
2020-04-06 14:44:17 +00:00
/// <summary>
2020-04-29 12:58:30 +00:00
/// Creates the nitrogen application map based on given input data
2020-04-06 14:44:17 +00:00
/// </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>
2020-04-06 14:44:17 +00:00
/// <param name="targetN">The target nitrogen to use for the calculations</param>
/// <returns></returns>
2020-04-08 15:36:56 +00:00
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
{
2020-04-29 12:58:30 +00:00
var nbsApplicationMapRequest = new TaskRequest { TaskType = VRANBS_TASK } ;
nbsApplicationMapRequest . attributes [ "operation" ] = "application" ;
nbsApplicationMapRequest . attributes [ "inputCode" ] = inputItem . Code ;
2020-08-19 11:40:42 +00:00
nbsApplicationMapRequest . attributes [ "plantingDate" ] = plantingDate . ToString ( "o" ) ;
nbsApplicationMapRequest . attributes [ "measurementDate" ] = measurementDate . ToString ( "o" ) ;
2020-04-29 12:58:30 +00:00
nbsApplicationMapRequest . attributes [ "inputCode" ] = inputItem . Code ;
nbsApplicationMapRequest . attributes [ "inputType" ] = inputType . ToLower ( ) ;
nbsApplicationMapRequest . attributes [ "targetN" ] = targetN . ToString ( CultureInfo . InvariantCulture ) ;
2020-08-19 11:40:42 +00:00
2020-04-29 12:58:30 +00:00
string itemTaskCode = await _farmmapsApiService . QueueTaskAsync ( cropfieldItem . Code , nbsApplicationMapRequest ) ;
2020-08-19 11:40:42 +00:00
2020-03-25 16:49:00 +00:00
await PollTask ( TimeSpan . FromSeconds ( 5 ) , async ( tokenSource ) = >
{
var itemTaskStatus = await _farmmapsApiService . GetTaskStatusAsync ( cropfieldItem . Code , itemTaskCode ) ;
2020-08-19 11:40:42 +00:00
2020-04-08 10:18:17 +00:00
if ( itemTaskStatus . IsFinished )
2020-03-25 16:49:00 +00:00
tokenSource . Cancel ( ) ;
} ) ;
2020-08-19 11:40:42 +00:00
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" ;
2020-04-29 12:58:30 +00:00
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 ( ) ) ) ;
2020-04-29 12:58:30 +00:00
if ( applicationMapItem = = null )
2020-03-25 16:49:00 +00:00
{
2020-04-08 15:36:56 +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 ;
}
2020-04-29 12:58:30 +00:00
return applicationMapItem ;
2020-03-25 16:49:00 +00:00
}
}
}