FarmMapsApiClient_WURtest/FarmmapsNbs/NitrogenService.cs

179 lines
8.8 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();
nbsTargetNRequest.attributes["measurementDate"] = measurementDate.ToString();
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-08 17:28:30 +00:00
DateTime measurementDate, string inputType = "irmi")
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();
nbsUptakeMapRequest.attributes["measurementDate"] = measurementDate.ToString();
2020-04-08 17:28:30 +00:00
nbsUptakeMapRequest.attributes["inputType"] = inputType.ToLower();
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 appliance 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-08 17:28:30 +00:00
DateTime measurementDate, string inputType = "irmi", double targetN = 60.0)
2020-03-25 16:49:00 +00:00
{
var nbsApplianceMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsApplianceMapRequest.attributes["operation"] = "application";
nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code;
nbsApplianceMapRequest.attributes["plantingDate"] = plantingDate.ToString();
nbsApplianceMapRequest.attributes["measurementDate"] = measurementDate.ToString();
nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code;
2020-04-08 17:28:30 +00:00
nbsApplianceMapRequest.attributes["inputType"] = inputType.ToLower();
nbsApplianceMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
2020-03-25 16:49:00 +00:00
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplianceMapRequest);
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();
});
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 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-04-06 12:45:28 +00:00
if (applianceMapItem == 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;
}
2020-04-06 12:45:28 +00:00
return applianceMapItem;
2020-03-25 16:49:00 +00:00
}
}
}