From 92a8e0d68f699225a3551aabd76d19c0c5fcd52a Mon Sep 17 00:00:00 2001 From: Riepma Date: Mon, 12 Oct 2020 16:59:23 +0200 Subject: [PATCH] Added RunSatelliteTask to GeneralService --- FarmmapsApi/Services/GeneralService.cs | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/FarmmapsApi/Services/GeneralService.cs b/FarmmapsApi/Services/GeneralService.cs index e553174..6244857 100644 --- a/FarmmapsApi/Services/GeneralService.cs +++ b/FarmmapsApi/Services/GeneralService.cs @@ -268,5 +268,49 @@ namespace FarmmapsApi.Services return shadowItem; } + + public async Task RunSatelliteTask(Item cropfieldItem) + { + var taskmapRequest = new TaskRequest { TaskType = SATELLITE_TASK }; + + string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskmapRequest); + + await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) => + { + var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, itemTaskCode); + _logger.LogInformation($"Waiting on collection satellite data; status: {itemTaskStatus.State}"); + if (itemTaskStatus.IsFinished) + 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; + } + + //the satellite item is a child of the cropfield + var itemName = "satellite"; + var satelliteItem = await FindChildItemAsync(cropfieldItem.Code, + TEMPORAL_ITEMTYPE, itemName); + if (satelliteItem == null) + { + _logger.LogError("Could not find the satellite data as a child item under the cropfield"); + return null; + } + + + // geotiffs can be found as a item of children under the satellite item + var allSatelliteItems = await _farmmapsApiService.GetItemChildrenAsync(satelliteItem.Code); + + var lastSatItem = allSatelliteItems[^1]; + + return lastSatItem; + } + + + + } } \ No newline at end of file