FarmMapsApiClient/FarmmapsApiSamples/NbsApp.cs

117 lines
5.6 KiB
C#
Raw Normal View History

2020-03-24 22:26:02 +00:00
using System;
using System.IO;
2020-03-24 22:26:02 +00:00
using System.Linq;
using System.Threading.Tasks;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using Google.Apis.Upload;
2020-03-24 22:26:02 +00:00
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
2020-03-25 16:49:00 +00:00
using static FarmmapsApiSamples.Constants;
2020-03-24 22:26:02 +00:00
namespace FarmmapsApiSamples
{
public class NbsApp : IApp
{
private readonly ILogger<NbsApp> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly FarmmapsEventHub _farmmapsEventHub;
2020-03-25 16:49:00 +00:00
private readonly NitrogenService _nitrogenService;
2020-03-24 22:26:02 +00:00
2020-03-25 16:49:00 +00:00
public NbsApp(ILogger<NbsApp> logger, FarmmapsApiService farmmapsApiService,
FarmmapsEventHub farmmapsEventHub, NitrogenService nitrogenService)
2020-03-24 22:26:02 +00:00
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_farmmapsEventHub = farmmapsEventHub;
2020-03-25 16:49:00 +00:00
_nitrogenService = nitrogenService;
2020-03-24 22:26:02 +00:00
_farmmapsEventHub.EventCallback += OnEvent;
2020-03-24 22:26:02 +00:00
}
private void OnEvent(EventMessage @event)
{
// _logger.LogInformation(@event.EventType);
2020-03-24 22:26:02 +00:00
}
public async Task RunAsync()
{
try
{
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
2020-03-25 16:49:00 +00:00
// upload data to Uploaded
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
if (uploadedRoot != null)
2020-03-24 22:26:02 +00:00
{
var dataPath = Path.Combine("Data", "Scan_1_20190605.zip");
var result = await _farmmapsApiService.UploadFile(dataPath, uploadedRoot.Code,
progress => _logger.LogInformation($"Status: {progress.Status} - BytesSent: {progress.BytesSent}"));
if (result.Progress.Status == UploadStatus.Failed)
{
_logger.LogError($"Uploading failed {result.Progress.Exception.Message}");
return;
}
// find file we need to use, we use delay as hack until events work correctly
// await Task.Delay(10000);
// var isariaDataFilter = JObject.Parse(@"");
// var uploadedFilesChildren = await
// _farmmapsApiService.GetItemChildrenAsync(uploadedRoot.Code, SHAPE_PROCESSED_ITEMTYPE);
2020-03-25 16:49:00 +00:00
// need to transform shape data to geotiff
// var shapeToGeotiffRequest = new TaskRequest()
// {
// TaskType = "vnd.farmmaps.task.shapetogeotiff"
// };
// var taskCode = await _farmmapsApiService.QueueTaskAsync(, shapeToGeotiffRequest);
var myDriveRoot = roots.SingleOrDefault(r => r.Name == "My drive");
if (myDriveRoot != null)
{
var cropfieldItem = await GetOrCreateCropfieldItem(myDriveRoot.Code);
2020-03-25 16:49:00 +00:00
_logger.LogInformation($"Calculating targetN with targetYield: {60}");
2020-03-25 16:49:00 +00:00
var targetN = await _nitrogenService.CalculateTargetN(cropfieldItem, 60);
_logger.LogInformation($"TargetN: {targetN}");
2020-03-25 16:49:00 +00:00
_logger.LogInformation("Calculating nitrogen map");
// var nitrogenMapItem = CalculateNitrogenMap(cropfieldItem,, targetN);
}
2020-03-24 22:26:02 +00:00
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}
2020-03-25 10:39:26 +00:00
private async Task<Item> GetOrCreateCropfieldItem(string parentItemCode)
2020-03-24 22:26:02 +00:00
{
var cropfieldItems = await
2020-03-25 16:49:00 +00:00
_farmmapsApiService.GetItemChildrenAsync(parentItemCode, CROPFIELD_ITEMTYPE);
2020-03-24 22:26:02 +00:00
2020-03-25 16:49:00 +00:00
if (cropfieldItems.Count > 0)
2020-03-24 22:26:02 +00:00
return cropfieldItems[0];
var currentYear = new DateTime(DateTime.UtcNow.Year, 1, 1);
var cropfieldItemRequest = new ItemRequest()
{
ParentCode = parentItemCode,
ItemType = "vnd.farmmaps.itemtype.cropfield",
Name = "Cropfield for VRA",
2020-03-24 22:26:02 +00:00
DataDate = currentYear,
DataEndDate = currentYear.AddYears(1),
Data = JObject.FromObject(new
{startDate = currentYear, endDate = currentYear.AddMonths(3)}),
2020-03-24 22:26:02 +00:00
Geometry = JObject.Parse(
@"{ ""type"": ""Polygon"", ""coordinates"": [ [ [ 3.40843828875524, 50.638966444680605 ], [ 3.408953272886064, 50.639197789621612 ], [ 3.409242951459603, 50.639469958681836 ], [ 3.409328782148028, 50.639612846807708 ], [ 3.409457528180712, 50.639789755314411 ], [ 3.409639918393741, 50.640014292074966 ], [ 3.409833037442765, 50.640211611372706 ], [ 3.410069071836049, 50.640395321698435 ], [ 3.410380208081761, 50.640572227259661 ], [ 3.410605513638958, 50.640715112034222 ], [ 3.411925160474145, 50.641177783561204 ], [ 3.411935889310142, 50.640728720085136 ], [ 3.412590348309737, 50.63948356709389 ], [ 3.413244807309242, 50.638224772339846 ], [ 3.413400375432099, 50.637901562841307 ], [ 3.413539850300779, 50.637449065809889 ], [ 3.413475477284437, 50.637418445552932 ], [ 3.40999396998362, 50.637449065810451 ], [ 3.409940325803365, 50.638102293212661 ], [ 3.409575545377398, 50.638483338338325 ], [ 3.409060561246574, 50.638707881340494 ], [ 3.40843828875524, 50.638966444680605 ] ] ] }")
2020-03-24 22:26:02 +00:00
};
return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
}
}
}