Created a generalservice for helper methods.

Moved constants to Farmmaps API project.
This commit is contained in:
2020-04-08 11:43:53 +02:00
parent 04235a4fd2
commit 418446e9d2
7 changed files with 150 additions and 117 deletions

13
FarmmapsApi/Constants.cs Normal file
View File

@@ -0,0 +1,13 @@
namespace FarmmapsApiSamples
{
public static class Constants
{
public const string USERINPUT_ITEMTYPE = "vnd.farmmaps.itemtype.user.input";
public const string GEOTIFF_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff";
public const string GEOTIFF_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.geotiff.processed";
public const string CROPFIELD_ITEMTYPE = "vnd.farmmaps.itemtype.cropfield";
public const string SHAPE_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.shape.processed";
public const string SHAPE_ITEMTYPE = "vnd.farmmaps.itemtype.shape";
public const string VRANBS_TASK = "vnd.farmmaps.task.vranbs";
}
}

View File

@@ -27,6 +27,7 @@ namespace FarmmapsApi
.AddSingleton<FarmmapsEventHub>()
.AddTransient<OpenIdConnectService>()
.AddTransient<FarmmapsAuthenticationHandler>()
.AddTransient<GeneralService>()
.AddHttpClient<FarmmapsApiService>()
.AddHttpMessageHandler<FarmmapsAuthenticationHandler>()
.Services;

View File

@@ -0,0 +1,135 @@
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;
using static FarmmapsApi.Extensions;
using static FarmmapsApiSamples.Constants;
namespace FarmmapsApi.Services
{
public class GeneralService
{
private readonly ILogger<GeneralService> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly string _downloadFolder = "Downloads";
public GeneralService(ILogger<GeneralService> logger, FarmmapsApiService farmmapsApiService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
}
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;
return await FindChildItemAsync(root.Code, itemType, itemName,
i => i.Created >= startUpload &&
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;
var zipName = Path.GetFileNameWithoutExtension(filePath);
Item shapeItem = null;
await PollTask(TimeSpan.FromSeconds(3), async source =>
{
var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(root.Code);
var zipItems = uploadedFilesChildren.Where(i => i.Name.Contains(zipName));
var childrenTasks = new List<Task<List<Item>>>();
foreach (var zipItem in zipItems)
{
childrenTasks.Add(_farmmapsApiService.GetItemChildrenAsync(zipItem.Code,
SHAPE_PROCESSED_ITEMTYPE));
}
List<Item>[] items = await Task.WhenAll(childrenTasks);
if (items.Length > 0)
{
var flatItems = items.SelectMany(i => i).Where(i => i.Name.Contains(itemName)).ToList();
if (flatItems.Count > 0)
{
shapeItem = flatItems.Where(i => i.Created >= startUpload).OrderByDescending(i => i.Created)
.First();
source.Cancel();
}
}
});
return shapeItem;
}
public async Task<Item> ShapeToGeotiff(Item shapeItemCode)
{
var shapeToGeotiffRequest = new TaskRequest()
{
TaskType = "vnd.farmmaps.task.shapetogeotiff"
};
var taskCode = await _farmmapsApiService.QueueTaskAsync(shapeItemCode.Code, shapeToGeotiffRequest);
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
{
_logger.LogInformation("Checking shapetogeotiff task status");
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(shapeItemCode.Code, taskCode);
if (itemTaskStatus.IsFinished)
tokenSource.Cancel();
});
_logger.LogInformation("Data shape converted to geotiff");
// the parent of the shape item is now the tiff item
shapeItemCode = await _farmmapsApiService.GetItemAsync(shapeItemCode.Code);
return await _farmmapsApiService.GetItemAsync(shapeItemCode.ParentCode);
}
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);
if (uploadedFilesChildren.Count > 0)
{
Func<Item, bool> func = filter ?? (i => i.Name.ToLower().Contains(containsName.ToLower()));
dataItem = uploadedFilesChildren.FirstOrDefault(func);
source.Cancel();
}
else if (tries == maxTries)
{
source.Cancel();
}
tries++;
});
if (dataItem == null)
{
_logger.LogError("dataItem not found");
return null;
}
_logger.LogInformation($"Found {containsName} item");
return dataItem;
}
}
}