2020-05-05 14:08:32 +00:00
|
|
|
using System;
|
2020-04-22 11:41:49 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-04-22 10:51:53 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using FarmmapsApi;
|
2020-04-28 09:22:02 +00:00
|
|
|
using FarmmapsApi.Models;
|
2020-04-22 11:41:49 +00:00
|
|
|
using FarmmapsApi.Services;
|
2020-04-28 09:22:02 +00:00
|
|
|
using FarmmapsHaulmkilling.Models;
|
2020-04-22 11:41:49 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-28 09:22:02 +00:00
|
|
|
using Newtonsoft.Json;
|
2020-05-05 14:08:32 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
2020-04-28 10:37:56 +00:00
|
|
|
using static FarmmapsApiSamples.Constants;
|
|
|
|
|
2020-04-22 10:51:53 +00:00
|
|
|
namespace FarmmapsHaulmkilling
|
|
|
|
{
|
|
|
|
public class HaulmkillingApplication : IApplication
|
|
|
|
{
|
2020-04-22 11:41:49 +00:00
|
|
|
private const string DownloadFolder = "Downloads";
|
2020-04-28 09:22:02 +00:00
|
|
|
private const string SettingsFile = "settings.json";
|
2020-04-22 11:41:49 +00:00
|
|
|
|
|
|
|
private readonly ILogger<HaulmkillingApplication> _logger;
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
private readonly HaulmkillingService _haulmkillingService;
|
|
|
|
private readonly GeneralService _generalService;
|
2020-04-28 09:22:02 +00:00
|
|
|
|
|
|
|
private Settings _settings;
|
2020-04-22 11:41:49 +00:00
|
|
|
|
|
|
|
public HaulmkillingApplication(ILogger<HaulmkillingApplication> logger, FarmmapsApiService farmmapsApiService,
|
|
|
|
GeneralService generalService, HaulmkillingService haulmkillingService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_farmmapsApiService = farmmapsApiService;
|
|
|
|
_generalService = generalService;
|
|
|
|
_haulmkillingService = haulmkillingService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task RunAsync()
|
2020-04-22 10:51:53 +00:00
|
|
|
{
|
2020-04-22 11:41:49 +00:00
|
|
|
if (!Directory.Exists(DownloadFolder))
|
|
|
|
Directory.CreateDirectory(DownloadFolder);
|
2020-04-28 09:22:02 +00:00
|
|
|
|
|
|
|
LoadSettings();
|
2020-04-22 11:41:49 +00:00
|
|
|
|
|
|
|
// !! this call is needed the first time an api is called with a fresh clientid and secret !!
|
|
|
|
await _farmmapsApiService.GetCurrentUserCodeAsync();
|
|
|
|
var roots = await _farmmapsApiService.GetCurrentUserRootsAsync();
|
|
|
|
|
|
|
|
var myDrive = roots.SingleOrDefault(r => r.Name == "My drive");
|
|
|
|
if (myDrive == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var uploadedRoot = roots.SingleOrDefault(r => r.Name == "Uploaded");
|
|
|
|
if (uploadedRoot == null)
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not find a needed root item");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var agents = await _haulmkillingService.GetHaulmkillingAgents();
|
2020-05-05 14:08:32 +00:00
|
|
|
if (agents == null || agents.Count == 0)
|
2020-04-22 11:41:49 +00:00
|
|
|
{
|
|
|
|
_logger.LogError("No valid agents found for haulmkilling");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:22:02 +00:00
|
|
|
Item cropfieldItem;
|
|
|
|
if (string.IsNullOrEmpty(_settings.CropfieldItemCode))
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Creating cropfield");
|
|
|
|
cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield VRA Haulmkilling", 2020,
|
|
|
|
@"{""type"":""Polygon"",""coordinates"":[[[4.617786844284247,52.22533706956424],[4.618642601314543,52.225938364585989],[4.6192153806397,52.22563988897754],[4.619192414656403,52.2256242822442],[4.620306732153958,52.225031745661528],[4.620542019225217,52.22519855319158],[4.621157509147853,52.22487436515405],[4.623387917230182,52.22367660757213],[4.624563444939009,52.22304740241544],[4.624562779355982,52.223046635247019],[4.624534908813479,52.22302596787506],[4.627873021330343,52.221240670658399],[4.627504935938338,52.220104419135129],[4.627324878706837,52.22020569669098],[4.627320696113512,52.22020660117888],[4.626707169518044,52.22053923770041],[4.624700376420229,52.221619047547488],[4.623471571183885,52.22227447969577],[4.623471511010673,52.22227500174403],[4.623468838689317,52.22228052566992],[4.617786844284247,52.22533706956424]]]}");
|
|
|
|
_settings.CropfieldItemCode = cropfieldItem.Code;
|
|
|
|
SaveSettings();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Cropfield already exists trying to get");
|
|
|
|
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
|
|
|
|
}
|
2020-04-22 11:41:49 +00:00
|
|
|
|
2020-04-28 10:37:56 +00:00
|
|
|
ItemTaskStatus taskStatus;
|
2020-05-05 14:08:32 +00:00
|
|
|
if (string.IsNullOrEmpty(_settings.SatelliteTaskCode))
|
2020-04-28 09:22:02 +00:00
|
|
|
{
|
|
|
|
_logger.LogInformation("Gathering satellite information for cropfield, this might take a while!");
|
2020-04-28 10:37:56 +00:00
|
|
|
taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, SATELLITE_TASK, null, 20);
|
|
|
|
|
|
|
|
if (taskStatus.State == ItemTaskState.Error)
|
|
|
|
{
|
|
|
|
_logger.LogError($"Something went wrong when trying to process satellite data; {taskStatus.Message}");
|
|
|
|
return;
|
|
|
|
}
|
2020-04-28 09:22:02 +00:00
|
|
|
|
2020-04-28 10:37:56 +00:00
|
|
|
_settings.SatelliteTaskCode = taskStatus.Code;
|
2020-04-28 09:22:02 +00:00
|
|
|
SaveSettings();
|
|
|
|
|
|
|
|
}
|
2020-04-28 10:37:56 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
taskStatus = await _farmmapsApiService.GetTaskStatusAsync(_settings.CropfieldItemCode, _settings.SatelliteTaskCode);
|
|
|
|
}
|
2020-04-28 08:06:41 +00:00
|
|
|
|
2020-04-22 11:41:49 +00:00
|
|
|
// find ndvi or wdvi satellite data geotiffs
|
2020-05-05 14:08:32 +00:00
|
|
|
var temporalItem = await _generalService.FindChildItemAsync(cropfieldItem.Code, TEMPORAL_ITEMTYPE,
|
2020-04-28 10:37:56 +00:00
|
|
|
"Cropfield VRA Haulmkilling, Satellite", item => item.SourceTask == SATELLITE_TASK &&
|
|
|
|
taskStatus.Finished >= item.Created &&
|
|
|
|
taskStatus.Finished <= item.Created.Value.AddHours(1));
|
|
|
|
|
2020-05-05 14:08:32 +00:00
|
|
|
if (temporalItem == null)
|
2020-04-28 10:37:56 +00:00
|
|
|
{
|
2020-05-05 14:08:32 +00:00
|
|
|
_logger.LogError("Temporal item not found");
|
2020-04-28 10:37:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:08:32 +00:00
|
|
|
var satelliteTiffs = await _farmmapsApiService.GetItemChildrenAsync(temporalItem.Code);
|
2020-04-28 10:37:56 +00:00
|
|
|
var firstSatelliteItem = satelliteTiffs.FirstOrDefault();
|
|
|
|
|
|
|
|
if (firstSatelliteItem == null)
|
|
|
|
{
|
2020-05-05 14:08:32 +00:00
|
|
|
_logger.LogError("Satellite item not found");
|
2020-04-28 10:37:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-05 14:08:32 +00:00
|
|
|
|
|
|
|
// must be ndvi or wdvi
|
|
|
|
var inputType = (firstSatelliteItem.Data["layers"] as JArray)?[0]["name"].ToString();
|
|
|
|
if (string.IsNullOrEmpty(inputType))
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not get the input type name from the satellite item");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var selectedAgent = agents[0];
|
2021-04-12 15:01:17 +00:00
|
|
|
var selectedOption = selectedAgent.ValidOptions[0];
|
2020-05-05 14:08:32 +00:00
|
|
|
|
|
|
|
_logger.LogInformation("Calculating application map");
|
|
|
|
var applianceMapItem = await _haulmkillingService.CalculateApplicationMapAsync(cropfieldItem,
|
2021-04-12 15:01:17 +00:00
|
|
|
firstSatelliteItem, inputType, selectedOption, selectedAgent);
|
2020-05-05 14:08:32 +00:00
|
|
|
if (applianceMapItem == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Downloading application map");
|
|
|
|
await _farmmapsApiService.DownloadItemAsync(applianceMapItem.Code, Path.Combine(DownloadFolder,
|
|
|
|
$"{applianceMapItem.Name}_{Guid.NewGuid():N}.zip"));
|
2020-04-22 10:51:53 +00:00
|
|
|
}
|
2020-04-28 09:22:02 +00:00
|
|
|
|
|
|
|
private void LoadSettings()
|
|
|
|
{
|
|
|
|
if (File.Exists(SettingsFile))
|
|
|
|
{
|
|
|
|
var jsonText = File.ReadAllText(SettingsFile);
|
|
|
|
_settings = JsonConvert.DeserializeObject<Settings>(jsonText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_settings = new Settings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SaveSettings()
|
|
|
|
{
|
|
|
|
if (_settings == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var json = JsonConvert.SerializeObject(_settings);
|
|
|
|
File.WriteAllText(SettingsFile, json);
|
|
|
|
}
|
2020-04-22 10:51:53 +00:00
|
|
|
}
|
|
|
|
}
|