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;
|
|
|
|
|
|
|
|
private readonly string _downloadFolder = "Downloads";
|
|
|
|
|
|
|
|
public GeneralService(ILogger<GeneralService> logger, FarmmapsApiService farmmapsApiService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
}
|
|
|
|
|
2020-04-08 18:23:22 +00:00
|
|
|
public async Task<Item> CreateCropfieldItemAsync(string parentItemCode, string name, int year, string fieldGeomJson)
|
|
|
|
{
|
|
|
|
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),
|
|
|
|
Data = JObject.Parse("{}"),
|
|
|
|
Geometry = JObject.Parse(fieldGeomJson)
|
|
|
|
};
|
|
|
|
|
|
|
|
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-04-08 10:18:17 +00:00
|
|
|
|
2020-04-08 09:43:53 +00:00
|
|
|
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-04-08 10:18:17 +00:00
|
|
|
var zipName = Path.GetFileName(filePath);
|
2020-04-08 09:43:53 +00:00
|
|
|
Item shapeItem = null;
|
|
|
|
await PollTask(TimeSpan.FromSeconds(3), async source =>
|
|
|
|
{
|
2020-04-08 15:36:56 +00:00
|
|
|
_logger.LogInformation($"Searching for {itemName} item");
|
2020-04-08 09:43:53 +00:00
|
|
|
var uploadedFilesChildren = await _farmmapsApiService.GetItemChildrenAsync(root.Code);
|
|
|
|
var zipItems = uploadedFilesChildren.Where(i => i.Name.Contains(zipName));
|
|
|
|
|
2020-04-08 10:18:17 +00:00
|
|
|
foreach (var zipItem in zipItems)
|
2020-04-08 09:43:53 +00:00
|
|
|
{
|
2020-04-08 10:18:17 +00:00
|
|
|
List<Item> items = await _farmmapsApiService.GetItemChildrenAsync(zipItem.Code,
|
|
|
|
SHAPE_PROCESSED_ITEMTYPE);
|
2020-04-08 10:20:13 +00:00
|
|
|
|
|
|
|
items = items.Where(i => i.Created >= startUpload).OrderByDescending(i => i.Created).ToList();
|
2020-04-08 10:18:17 +00:00
|
|
|
|
2020-04-08 10:20:13 +00:00
|
|
|
if (items.Any())
|
2020-04-08 09:43:53 +00:00
|
|
|
{
|
2020-04-08 10:20:13 +00:00
|
|
|
shapeItem = items.First(i => i.Name.Contains(itemName));
|
2020-04-08 10:18:17 +00:00
|
|
|
if(shapeItem != null)
|
|
|
|
{
|
|
|
|
source.Cancel();
|
|
|
|
break;
|
|
|
|
}
|
2020-04-08 09:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return shapeItem;
|
|
|
|
}
|
|
|
|
|
2020-04-08 10:18:17 +00:00
|
|
|
public async Task<Item> ShapeToGeotiff(Item shapeItem)
|
2020-04-08 09:43:53 +00:00
|
|
|
{
|
|
|
|
var shapeToGeotiffRequest = new TaskRequest()
|
|
|
|
{
|
|
|
|
TaskType = "vnd.farmmaps.task.shapetogeotiff"
|
|
|
|
};
|
2020-04-08 10:18:17 +00:00
|
|
|
var taskCode = await _farmmapsApiService.QueueTaskAsync(shapeItem.Code, shapeToGeotiffRequest);
|
2020-04-08 09:43:53 +00:00
|
|
|
|
|
|
|
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Checking shapetogeotiff task status");
|
2020-04-08 10:18:17 +00:00
|
|
|
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(shapeItem.Code, taskCode);
|
2020-04-08 09:43:53 +00:00
|
|
|
if (itemTaskStatus.IsFinished)
|
|
|
|
tokenSource.Cancel();
|
|
|
|
});
|
|
|
|
|
|
|
|
_logger.LogInformation("Data shape converted to geotiff");
|
|
|
|
|
|
|
|
// the parent of the shape item is now the tiff item
|
2020-04-08 10:18:17 +00:00
|
|
|
shapeItem = await _farmmapsApiService.GetItemAsync(shapeItem.Code);
|
|
|
|
return await _farmmapsApiService.GetItemAsync(shapeItem.ParentCode);
|
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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|