2020-04-08 09:43:53 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FarmmapsApi.Models;
|
|
|
|
using Google.Apis.Upload;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-08 18:23:22 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
2020-04-08 09:43:53 +00:00
|
|
|
using static FarmmapsApi.Extensions;
|
|
|
|
using static FarmmapsApiSamples.Constants;
|
|
|
|
|
|
|
|
namespace FarmmapsApi.Services
|
|
|
|
{
|
|
|
|
public class GeneralService
|
|
|
|
{
|
|
|
|
private readonly ILogger<GeneralService> _logger;
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
|
|
|
|
public GeneralService(ILogger<GeneralService> logger, FarmmapsApiService farmmapsApiService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
}
|
2020-08-19 11:40:42 +00:00
|
|
|
|
|
|
|
public async Task<Item> CreateCropfieldItemAsync(string parentItemCode, string name, int year,
|
2020-10-03 10:04:15 +00:00
|
|
|
string fieldGeomJson, string data = "{}")
|
2020-04-08 18:23:22 +00:00
|
|
|
{
|
|
|
|
var currentYear = new DateTime(year, 1, 1);
|
|
|
|
var cropfieldItemRequest = new ItemRequest()
|
|
|
|
{
|
|
|
|
ParentCode = parentItemCode,
|
|
|
|
ItemType = CROPFIELD_ITEMTYPE,
|
|
|
|
Name = name,
|
|
|
|
DataDate = currentYear,
|
|
|
|
DataEndDate = currentYear.AddYears(1).AddDays(-1),
|
2020-10-03 10:04:15 +00:00
|
|
|
Data = JObject.Parse(data),
|
2020-04-08 18:23:22 +00:00
|
|
|
Geometry = JObject.Parse(fieldGeomJson)
|
|
|
|
};
|
|
|
|
|
|
|
|
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
|
|
|
|
}
|
2020-08-19 11:40:42 +00:00
|
|
|
|
2020-04-08 09:43:53 +00:00
|
|
|
public async Task<Item> UploadDataAsync(UserRoot root, string itemType, string filePath, string itemName)
|
|
|
|
{
|
|
|
|
var startUpload = DateTime.UtcNow;
|
|
|
|
var result = await _farmmapsApiService.UploadFile(filePath, root.Code,
|
|
|
|
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
|
|
|
|
|
|
|
|
if (result.Progress.Status == UploadStatus.Failed)
|
|
|
|
return null;
|
|
|
|
|
2020-08-19 11:40:42 +00:00
|
|
|
return await FindChildItemAsync(root.Code, itemType, itemName,
|
|
|
|
i => i.Created >= startUpload &&
|
2020-04-08 09:43:53 +00:00
|
|
|
i.Name.ToLower().Contains(itemName.ToLower()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Item> UploadZipWithShapeAsync(UserRoot root, string filePath, string itemName)
|
|
|
|
{
|
|
|
|
var startUpload = DateTime.UtcNow;
|
|
|
|
var result = await _farmmapsApiService.UploadFile(filePath, root.Code,
|
|
|
|
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
|
|
|
|
|
|
|
|
if (result.Progress.Status == UploadStatus.Failed)
|
|
|
|
return null;
|
|
|
|
|
2020-10-06 13:50:05 +00:00
|
|
|
return await FindChildItemAsync(root.Code, SHAPE_PROCESSED_ITEMTYPE, itemName,
|
|
|
|
i => i.Created >= startUpload &&
|
|
|
|
i.Name.ToLower().Contains(itemName.ToLower()));;
|
2020-04-08 09:43:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 10:18:17 +00:00
|
|
|
public async Task<Item> ShapeToGeotiff(Item shapeItem)
|
2020-04-08 09:43:53 +00:00
|
|
|
{
|
2020-04-28 08:06:41 +00:00
|
|
|
await RunAndWaitForTask(shapeItem, "vnd.farmmaps.task.shapetogeotiff");
|
|
|
|
|
|
|
|
// the parent of the shape item is now the tiff item
|
|
|
|
shapeItem = await _farmmapsApiService.GetItemAsync(shapeItem.Code);
|
|
|
|
return await _farmmapsApiService.GetItemAsync(shapeItem.ParentCode);
|
|
|
|
}
|
|
|
|
|
2020-08-19 11:40:42 +00:00
|
|
|
|
|
|
|
public async Task<Item> GeotiffToShape(Item tiffItem)
|
|
|
|
{
|
|
|
|
var taskmapRequest = new TaskRequest {TaskType = TASKMAP_TASK};
|
|
|
|
|
|
|
|
string itemTaskCode = await _farmmapsApiService.QueueTaskAsync(tiffItem.Code, taskmapRequest);
|
|
|
|
|
|
|
|
await PollTask(TimeSpan.FromSeconds(5), async (tokenSource) =>
|
|
|
|
{
|
|
|
|
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(tiffItem.Code, itemTaskCode);
|
|
|
|
_logger.LogInformation($"Waiting on converting geotiff to shape; status: {itemTaskStatus.State}");
|
|
|
|
if (itemTaskStatus.IsFinished)
|
|
|
|
tokenSource.Cancel();
|
|
|
|
});
|
|
|
|
|
|
|
|
var itemTask = await _farmmapsApiService.GetTaskStatusAsync(tiffItem.Code, itemTaskCode);
|
|
|
|
if (itemTask.State == ItemTaskState.Error)
|
|
|
|
{
|
|
|
|
_logger.LogError($"Something went wrong with task execution: {itemTask.Message}");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//the taskmap is a child of the input tiff
|
|
|
|
var itemName = "Taskmap";
|
|
|
|
var taskMapItem = await FindChildItemAsync(tiffItem.Code,
|
|
|
|
SHAPE_PROCESSED_ITEMTYPE, itemName);
|
|
|
|
if (taskMapItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find the shape taskmap as a child item under the input");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return taskMapItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<ItemTaskStatus> RunAndWaitForTask(Item subjectItem, string taskIdentifier,
|
2020-04-28 08:06:41 +00:00
|
|
|
Action<TaskRequest> configureCallback = null, int retrySeconds = 3)
|
|
|
|
{
|
|
|
|
var taskRequest = new TaskRequest()
|
2020-04-08 09:43:53 +00:00
|
|
|
{
|
2020-04-28 08:06:41 +00:00
|
|
|
TaskType = taskIdentifier
|
2020-04-08 09:43:53 +00:00
|
|
|
};
|
2020-04-28 08:06:41 +00:00
|
|
|
configureCallback?.Invoke(taskRequest);
|
2020-08-19 11:40:42 +00:00
|
|
|
|
2020-04-28 08:06:41 +00:00
|
|
|
var taskCode = await _farmmapsApiService.QueueTaskAsync(subjectItem.Code, taskRequest);
|
2020-04-08 09:43:53 +00:00
|
|
|
|
2020-04-28 08:06:41 +00:00
|
|
|
await PollTask(TimeSpan.FromSeconds(retrySeconds), async (tokenSource) =>
|
2020-04-08 09:43:53 +00:00
|
|
|
{
|
2020-04-28 08:06:41 +00:00
|
|
|
_logger.LogInformation($"Checking {taskIdentifier} task status");
|
|
|
|
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(subjectItem.Code, taskCode);
|
2020-04-08 09:43:53 +00:00
|
|
|
if (itemTaskStatus.IsFinished)
|
|
|
|
tokenSource.Cancel();
|
|
|
|
});
|
2020-08-19 11:40:42 +00:00
|
|
|
|
2020-04-28 08:06:41 +00:00
|
|
|
_logger.LogInformation($"{taskIdentifier} finished");
|
2020-08-19 11:40:42 +00:00
|
|
|
|
2020-04-28 08:06:41 +00:00
|
|
|
return await _farmmapsApiService.GetTaskStatusAsync(subjectItem.Code, taskCode);
|
2020-04-08 09:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Item> FindChildItemAsync(string parentCode, string itemType, string containsName,
|
|
|
|
Func<Item, bool> filter = null, int maxTries = 10)
|
|
|
|
{
|
|
|
|
Item dataItem = null;
|
|
|
|
int tries = 0;
|
|
|
|
await PollTask(TimeSpan.FromSeconds(3), async source =>
|
|
|
|
{
|
|
|
|
_logger.LogInformation($"Trying to get {containsName} data");
|
|
|
|
var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(parentCode, itemType);
|
2020-10-06 13:50:05 +00:00
|
|
|
Func<Item, bool> func = filter ?? (i => i.Name.ToLower().Contains(containsName.ToLower()));
|
|
|
|
dataItem = uploadedFilesChildren.FirstOrDefault(func);
|
|
|
|
if (dataItem != null || tries == maxTries)
|
2020-04-08 09:43:53 +00:00
|
|
|
{
|
|
|
|
source.Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
tries++;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (dataItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("dataItem not found");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation($"Found {containsName} item");
|
|
|
|
return dataItem;
|
|
|
|
}
|
2020-08-19 11:40:42 +00:00
|
|
|
|
|
|
|
public async Task<Item> RunBofekTask(Item cropfieldItem)
|
|
|
|
{
|
|
|
|
var taskmapRequest = new TaskRequest {TaskType = BOFEK_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 retreiving BOFEK 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 BOFEK data is a child of the cropfield
|
|
|
|
var itemName = "bofek";
|
|
|
|
var bofekItem = await FindChildItemAsync(cropfieldItem.Code,
|
|
|
|
SHAPE_PROCESSED_ITEMTYPE, itemName);
|
|
|
|
if (bofekItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find the BOFEK data as a child item under the cropfield");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bofekItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Item> RunAhnTask(Item cropfieldItem)
|
|
|
|
{
|
|
|
|
var taskmapRequest = new TaskRequest {TaskType = AHN_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 retreiving AHN 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 AHN data is a child of the cropfield
|
|
|
|
var itemName = "ahn";
|
|
|
|
var ahnItem = await FindChildItemAsync(cropfieldItem.Code,
|
|
|
|
GEOTIFF_PROCESSED_ITEMTYPE, itemName);
|
|
|
|
if (ahnItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find the AHN data as a child item under the cropfield");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ahnItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Item> RunShadowTask(Item cropfieldItem)
|
|
|
|
{
|
|
|
|
var taskmapRequest = new TaskRequest {TaskType = SHADOW_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 calculation shadow 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 shadow data is a child of the cropfield
|
|
|
|
var itemName = "shadow";
|
|
|
|
var shadowItem = await FindChildItemAsync(cropfieldItem.Code,
|
|
|
|
GEOTIFF_PROCESSED_ITEMTYPE, itemName);
|
|
|
|
if (shadowItem == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find the shadow data as a child item under the cropfield");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shadowItem;
|
|
|
|
}
|
2020-04-08 09:43:53 +00:00
|
|
|
}
|
|
|
|
}
|