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();
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
// await _nitrogenService.TestFlow(roots);
await _herbicideService.TestFlow(roots);
await _nitrogenService.TestFlow(roots);
// await _herbicideService.TestFlow(roots);
}
catch (Exception ex)
{

View File

@ -59,12 +59,19 @@ namespace FarmmapsApiSamples
var targetN = await CalculateTargetN(cropfieldItem, 60);
_logger.LogInformation($"TargetN: {targetN}");
_logger.LogInformation("Calculating nitrogen map");
var nitrogenMapItem =
await CalculateNitrogenMap(cropfieldItem, isariaGeotiffItem, targetN);
_logger.LogInformation("Calculating uptake map");
var uptakeMapItem =
await CalculateUptakeMap(cropfieldItem, isariaGeotiffItem);
_logger.LogInformation("Downloading nitrogen map");
await _farmmapsApiService.DownloadItemAsync(nitrogenMapItem.Code, $"{nitrogenMapItem.Name}.zip");
_logger.LogInformation("Downloading uptake map");
await _farmmapsApiService.DownloadItemAsync(uptakeMapItem.Code, $"{uptakeMapItem.Name}.zip");
_logger.LogInformation("Calculating appliance map");
var applianceMapItem =
await CalculateApplianceMap(cropfieldItem, isariaGeotiffItem, targetN);
_logger.LogInformation("Downloading appliance map");
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, $"{applianceMapItem.Name}.zip");
}
private async Task<Item> GetOrCreateCropfieldItemAsync(string parentItemCode)
@ -201,15 +208,14 @@ namespace FarmmapsApiSamples
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};
nbsNitrogenRequest.attributes["operation"] = "nitrogen";
nbsNitrogenRequest.attributes["inputCode"] = inputItem.Code;
nbsNitrogenRequest.attributes["inputType"] = "irmi";
nbsNitrogenRequest.attributes["targetN"] = targetN.ToString(CultureInfo.InvariantCulture);
var nbsApplianceMapRequest = new TaskRequest {TaskType = VRANBS_TASK};
nbsApplianceMapRequest.attributes["operation"] = "uptake";
nbsApplianceMapRequest.attributes["inputCode"] = inputItem.Code;
nbsApplianceMapRequest.attributes["inputType"] = "irmi";
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsNitrogenRequest);
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, nbsApplianceMapRequest);
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?
// 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)
{
_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");
return null;
}
return nitrogenItem;
return applianceMapItem;
}
private ItemRequest CreateTargetNItemRequest(string parentItemCode)