encapsulated nitrogen test flow to allow easier additions of more testflows.
This commit is contained in:
parent
7559b1ef78
commit
e01ac51095
48
FarmmapsApiSamples/Application.cs
Normal file
48
FarmmapsApiSamples/Application.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using FarmmapsApi.Models;
|
||||
using FarmmapsApi.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FarmmapsApiSamples
|
||||
{
|
||||
public class Application : IApp
|
||||
{
|
||||
private readonly ILogger<Application> _logger;
|
||||
private readonly FarmmapsApiService _farmmapsApiService;
|
||||
private readonly FarmmapsEventHub _farmmapsEventHub;
|
||||
private readonly NitrogenService _nitrogenService;
|
||||
|
||||
public Application(ILogger<Application> logger, FarmmapsApiService farmmapsApiService,
|
||||
FarmmapsEventHub farmmapsEventHub, NitrogenService nitrogenService)
|
||||
{
|
||||
_logger = logger;
|
||||
_farmmapsApiService = farmmapsApiService;
|
||||
_farmmapsEventHub = farmmapsEventHub;
|
||||
_nitrogenService = nitrogenService;
|
||||
|
||||
_farmmapsEventHub.EventCallback += OnEvent;
|
||||
}
|
||||
|
||||
private void OnEvent(EventMessage @event)
|
||||
{
|
||||
// _logger.LogInformation(@event.EventType);
|
||||
}
|
||||
|
||||
public async Task RunAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
// !! this call is needed the first time an api is called with a fresh clientid and secret !!
|
||||
await _farmmapsApiService.GetCurrentUserCodeAsync();
|
||||
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
|
||||
|
||||
await _nitrogenService.TestFlow(roots);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
using System;
|
||||
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 FarmmapsApiSamples.Constants;
|
||||
|
||||
namespace FarmmapsApiSamples
|
||||
{
|
||||
public class NbsApp : IApp
|
||||
{
|
||||
private readonly ILogger<NbsApp> _logger;
|
||||
private readonly FarmmapsApiService _farmmapsApiService;
|
||||
private readonly FarmmapsEventHub _farmmapsEventHub;
|
||||
private readonly NitrogenService _nitrogenService;
|
||||
|
||||
public NbsApp(ILogger<NbsApp> logger, FarmmapsApiService farmmapsApiService,
|
||||
FarmmapsEventHub farmmapsEventHub, NitrogenService nitrogenService)
|
||||
{
|
||||
_logger = logger;
|
||||
_farmmapsApiService = farmmapsApiService;
|
||||
_farmmapsEventHub = farmmapsEventHub;
|
||||
_nitrogenService = nitrogenService;
|
||||
|
||||
_farmmapsEventHub.EventCallback += OnEvent;
|
||||
}
|
||||
|
||||
private void OnEvent(EventMessage @event)
|
||||
{
|
||||
// _logger.LogInformation(@event.EventType);
|
||||
}
|
||||
|
||||
public async Task RunAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
// !! this call is needed the first time an api is called with a fresh clientid and secret !!
|
||||
await _farmmapsApiService.GetCurrentUserCodeAsync();
|
||||
|
||||
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
|
||||
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
|
||||
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
|
||||
if (uploadedRoot == null || myDriveRoot == null)
|
||||
{
|
||||
_logger.LogError("Could not find a needed root item");
|
||||
return;
|
||||
}
|
||||
|
||||
var dataPath = Path.Combine("Data", "Scan_1_20190605.zip");
|
||||
var result = await _farmmapsApiService.UploadFile(dataPath, uploadedRoot.Code,
|
||||
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
|
||||
|
||||
if (result.Progress.Status == UploadStatus.Failed)
|
||||
{
|
||||
_logger.LogError($"Uploading failed {result.Progress.Exception.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
var isariaGeotiffItem = await _nitrogenService.ProcessIsaria(uploadedRoot.Code,"Scan_1_20190605.zip");
|
||||
|
||||
// create and calculate target N
|
||||
var cropfieldItem = await GetOrCreateCropfieldItemAsync(myDriveRoot.Code);
|
||||
|
||||
_logger.LogInformation($"Calculating targetN with targetYield: {60}");
|
||||
var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, 60);
|
||||
_logger.LogInformation($"TargetN: {targetN}");
|
||||
|
||||
_logger.LogInformation("Calculating nitrogen map");
|
||||
var nitrogenMapItem =
|
||||
await _nitrogenService.CalculateNitrogenMap(cropfieldItem, isariaGeotiffItem, targetN);
|
||||
|
||||
_logger.LogInformation("Downloading nitrogen map");
|
||||
await _farmmapsApiService.DownloadItemAsync(nitrogenMapItem.Code, $"{nitrogenMapItem.Name}.zip");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<Item> GetOrCreateCropfieldItemAsync(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 = CROPFIELD_ITEMTYPE,
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,9 @@ 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;
|
||||
using static FarmmapsApiSamples.Constants;
|
||||
|
||||
@ -23,6 +25,73 @@ namespace FarmmapsApiSamples
|
||||
_farmmapsApiService = farmmapsApiService;
|
||||
}
|
||||
|
||||
public async Task TestFlow(List<UserRoot> roots)
|
||||
{
|
||||
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
|
||||
if (uploadedRoot == null)
|
||||
{
|
||||
_logger.LogError("Could not find a needed root item");
|
||||
return;
|
||||
}
|
||||
|
||||
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
|
||||
if (myDriveRoot == null)
|
||||
{
|
||||
_logger.LogError("Could not find a needed root item");
|
||||
return;
|
||||
}
|
||||
|
||||
var cropfieldItem = await GetOrCreateCropfieldItemAsync(myDriveRoot.Code);
|
||||
|
||||
var dataPath = Path.Combine("Data", "Scan_1_20190605.zip");
|
||||
var result = await _farmmapsApiService.UploadFile(dataPath, uploadedRoot.Code,
|
||||
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
|
||||
|
||||
if (result.Progress.Status == UploadStatus.Failed)
|
||||
{
|
||||
_logger.LogError($"Uploading failed {result.Progress.Exception.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
var isariaGeotiffItem = await ProcessIsaria(uploadedRoot.Code,"Scan_1_20190605.zip");
|
||||
|
||||
_logger.LogInformation($"Calculating targetN with targetYield: {60}");
|
||||
var targetN = await CalculateTargetN(cropfieldItem, 60);
|
||||
_logger.LogInformation($"TargetN: {targetN}");
|
||||
|
||||
_logger.LogInformation("Calculating nitrogen map");
|
||||
var nitrogenMapItem =
|
||||
await CalculateNitrogenMap(cropfieldItem, isariaGeotiffItem, targetN);
|
||||
|
||||
_logger.LogInformation("Downloading nitrogen map");
|
||||
await _farmmapsApiService.DownloadItemAsync(nitrogenMapItem.Code, $"{nitrogenMapItem.Name}.zip");
|
||||
}
|
||||
|
||||
private async Task<Item> GetOrCreateCropfieldItemAsync(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 = CROPFIELD_ITEMTYPE,
|
||||
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);
|
||||
}
|
||||
|
||||
public async Task<Item> ProcessIsaria(string parentCode, string zipName)
|
||||
{
|
||||
// get processed isaria data (this can end in an infinite loop...)
|
||||
|
@ -25,7 +25,7 @@ namespace FarmmapsApiSamples
|
||||
.AddFilter("System.Net.Http", LogLevel.Warning))
|
||||
.AddFarmmapsServices(configuration)
|
||||
.AddTransient<NitrogenService>()
|
||||
.AddSingleton<IApp, NbsApp>()
|
||||
.AddSingleton<IApp, Application>()
|
||||
.BuildServiceProvider();
|
||||
|
||||
await serviceProvider.GetService<FarmmapsApiService>().AuthenticateAsync();
|
||||
|
Loading…
Reference in New Issue
Block a user