Added create uptake method sample.

This commit is contained in:
Mark van der Wal 2020-04-06 14:45:28 +02:00
parent 92a8d9573c
commit 5f854ec820
2 changed files with 65 additions and 18 deletions

View File

@ -40,8 +40,8 @@ namespace FarmmapsApiSamples
await _farmmapsApiService.GetCurrentUserCodeAsync(); await _farmmapsApiService.GetCurrentUserCodeAsync();
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync(); var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
// await _nitrogenService.TestFlow(roots); await _nitrogenService.TestFlow(roots);
await _herbicideService.TestFlow(roots); // await _herbicideService.TestFlow(roots);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -58,13 +58,20 @@ namespace FarmmapsApiSamples
_logger.LogInformation($"Calculating targetN with targetYield: {60}"); _logger.LogInformation($"Calculating targetN with targetYield: {60}");
var targetN = await CalculateTargetN(cropfieldItem, 60); var targetN = await CalculateTargetN(cropfieldItem, 60);
_logger.LogInformation($"TargetN: {targetN}"); _logger.LogInformation($"TargetN: {targetN}");
_logger.LogInformation("Calculating uptake map");
var uptakeMapItem =
await CalculateUptakeMap(cropfieldItem, isariaGeotiffItem);
_logger.LogInformation("Downloading uptake map");
await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code, $"{uptakeMapItem.Name}.zip");
_logger.LogInformation("Calculating nitrogen map"); _logger.LogInformation("Calculating appliance map");
var nitrogenMapItem = var applianceMapItem =
await CalculateNitrogenMap(cropfieldItem, isariaGeotiffItem, targetN); await CalculateApplianceMap(cropfieldItem, isariaGeotiffItem, targetN);
_logger.LogInformation("Downloading nitrogen map"); _logger.LogInformation("Downloading appliance map");
await _farmmapsApiService.DownloadItemAsync(nitrogenMapItem.Code, $"{nitrogenMapItem.Name}.zip"); await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, $"{applianceMapItem.Name}.zip");
} }
private async Task<Item> GetOrCreateCropfieldItemAsync(string parentItemCode) private async Task<Item> GetOrCreateCropfieldItemAsync(string parentItemCode)
@ -200,16 +207,15 @@ namespace FarmmapsApiSamples
return 0; return 0;
} }
public async Task<Item> CalculateNitrogenMap(Item cropfieldItem, Item inputItem, double targetN) public async Task<Item> CalculateUptakeMap(Item cropfieldItem, Item inputItem)
{ {
var nbsNitrogenRequest = new TaskRequest {TaskType = VRANBS_TASK}; var nbsApplianceMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsNitrogenRequest.attributes["operation"] = "nitrogen"; nbsApplianceMapRequest.attributes["operation"] = "uptake";
nbsNitrogenRequest.attributes["inputCode"] = inputItem.Code; nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code;
nbsNitrogenRequest.attributes["inputType"] = "irmi"; nbsApplianceMapRequest.attributes["inputType"] = "irmi";
nbsNitrogenRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplianceMapRequest);
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsNitrogenRequest);
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
{ {
@ -231,14 +237,55 @@ namespace FarmmapsApiSamples
// maybe poll here to, to wait for geotiff task process to be finished? // maybe poll here to, to wait for geotiff task process to be finished?
// how to uniquely know which item is really created!? // how to uniquely know which item is really created!?
var nitrogenItem = geotiffItems.SingleOrDefault(i => i.Name.Contains("nitrogen") && i.Updated >= itemTask.Finished); var nitrogenItem = geotiffItems.SingleOrDefault(i => i.Name.Contains("uptake") && i.Updated >= itemTask.Finished);
if (nitrogenItem == null) if (nitrogenItem == null)
{
_logger.LogError("Could not find the uptake geotiff child item under cropfield");
return null;
}
return nitrogenItem;
}
public async Task<Item> CalculateApplianceMap(Item cropfieldItem, Item inputItem, double targetN)
{
var applianceMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
applianceMapRequest.attributes["operation"] = "nitrogen";
applianceMapRequest.attributes["inputCode"] = inputItem.Code;
applianceMapRequest.attributes["inputType"] = "irmi";
applianceMapRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, applianceMapRequest);
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_PROCESSED_ITEMTYPE);
// maybe poll here to, to wait for geotiff task process to be finished?
// how to uniquely know which item is really created!?
var applianceMapItem = geotiffItems.SingleOrDefault(i => i.Name.Contains("nitrogen") && i.Updated >= itemTask.Finished);
if (applianceMapItem == null)
{ {
_logger.LogError("Could not find the nitrogen geotiff child item under cropfield"); _logger.LogError("Could not find the nitrogen geotiff child item under cropfield");
return null; return null;
} }
return nitrogenItem; return applianceMapItem;
} }
private ItemRequest CreateTargetNItemRequest(string parentItemCode) private ItemRequest CreateTargetNItemRequest(string parentItemCode)