FarmMapsApiClient_WURtest/FarmmapsApiSamples/NbsApp.cs
2020-03-25 17:31:42 +01:00

210 lines
9.8 KiB
C#

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using Google.Apis.Upload;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using static FarmmapsApi.Extensions;
namespace FarmmapsApiSamples
{
public class NbsApp : IApp
{
private const string USERINPUT_ITEMTYPE = "vnd.farmmaps.itemtype.user.input";
private const string GEOTIFF_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff";
private const string CROPFIELD_ITEMTYPE = "vnd.farmmaps.itemtype.cropfield";
private const string VRANBS_TASK = "vnd.farmmaps.task.vranbs";
private readonly ILogger<NbsApp> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
public NbsApp(ILogger<NbsApp> logger, FarmmapsApiService farmmapsApiService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
farmmapsApiService.EventCallback += OnEvent;
}
private void OnEvent(EventMessage @event)
{
_logger.LogInformation(@event.EventType);
}
public async Task RunAsync()
{
try
{
_logger.LogInformation("NBS sample app started");
await _farmmapsApiService.AuthenticateAsync();
_logger.LogInformation("Authenticated client credentials");
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
// upload data to Uploaded
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot != null)
{
await _farmmapsApiService.UploadFile(Path.Combine("Data", "Scan_1_20190605.zip"), uploadedRoot.Code,
progress =>
{
_logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}");
if(progress.Status == UploadStatus.Failed)
_logger.LogError($"Uploading failed {progress.Exception.Message}");
});
// need to transform shape data to geotiff
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDriveRoot != null)
{
var cropfieldItem = await GetOrCreateCropfieldItem(myDriveRoot.Code);
_logger.LogInformation($"Calculating targetN with targetYield: {60}");
var targetN = await CalculateTargetN(cropfieldItem, 60);
_logger.LogInformation($"TargetN: {targetN}");
_logger.LogInformation("Calculating nitrogen map");
// var nitrogenMapItem = CalculateNitrogenMap(cropfieldItem,, targetN);
}
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}
private async Task<Item> GetOrCreateCropfieldItem(string parentItemCode)
{
var cropfieldItems = await
_farmmapsApiService.GetItemChildrenAsync(parentItemCode,CROPFIELD_ITEMTYPE);
if (cropfieldItems.Count > 0)
return cropfieldItems[0];
var currentYear = new DateTime(DateTime.UtcNow.Year, 1, 1);
var cropfieldItemRequest = new ItemRequest()
{
ParentCode = parentItemCode,
ItemType = "vnd.farmmaps.itemtype.cropfield",
Name = "Cropfield for VRA",
DataDate = currentYear,
DataEndDate = currentYear.AddYears(1),
Data = JObject.FromObject(new
{startDate = currentYear, endDate = currentYear.AddMonths(3)}),
Geometry = JObject.Parse(
@"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] }")
};
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
}
/// <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="targetYield">The target yield input for the TargetN calculation</param>
/// <returns>The TargetN</returns>
private async Task<double> CalculateTargetN(Item cropfieldItem, int targetYield)
{
var targetNItems = await
_farmmapsApiService.GetItemChildrenAsync(cropfieldItem.ParentCode, USERINPUT_ITEMTYPE);
Item targetNItem;
if (targetNItems.Count == 0)
{
_logger.LogInformation("Creating targetN item");
var itemRequest = CreateTargetNItemRequest(cropfieldItem.ParentCode);
targetNItem = await _farmmapsApiService.CreateItemAsync(itemRequest);
}
else
{
targetNItem = targetNItems[0];
}
var nbsTargetNRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsTargetNRequest.attributes["operation"] = "targetn";
nbsTargetNRequest.attributes["inputCode"] = targetNItem.Code;
nbsTargetNRequest.attributes["inputType"] = "irmi";
nbsTargetNRequest.attributes["purposeType"] = "consumption";
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.State != ItemTaskState.Processing && itemTaskStatus.State != ItemTaskState.Scheduled)
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 0;
}
var item = await _farmmapsApiService.GetItemAsync(targetNItem.Code);
if (item.Data.ContainsKey("TargetN"))
return item.Data["TargetN"].Value<double>();
return 0;
}
private async Task<Item> CalculateNitrogenMap(Item cropfieldItem, Item inputItem, double targetN)
{
var nbsNitrogenRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsNitrogenRequest.attributes["operation"] = "nitrogen";
nbsNitrogenRequest.attributes["inputCode"] = inputItem.Code;
nbsNitrogenRequest.attributes["inputType"] = "irmi";
nbsNitrogenRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsNitrogenRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode);
if (itemTaskStatus.State != ItemTaskState.Processing && itemTaskStatus.State != ItemTaskState.Scheduled)
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 geotiffItems = await
_farmmapsApiService.GetItemChildrenAsync(cropfieldItem.Code, GEOTIFF_ITEMTYPE);
// how to uniquely know which item is really created!?
var nitrogenItem = geotiffItems.SingleOrDefault(i => i.Name.Contains("nitrogen"));
if (nitrogenItem == null)
{
_logger.LogError("Could not find the nitrogen geotiff child item under cropfield");
return null;
}
return nitrogenItem;
}
private ItemRequest CreateTargetNItemRequest(string parentItemCode)
{
return new ItemRequest()
{
ParentCode = parentItemCode,
ItemType = USERINPUT_ITEMTYPE,
Name = "TargetN",
DataDate = DateTime.UtcNow
};
}
}
}