From 276cce1808ae3a94644698c962a4c3ba422edf21 Mon Sep 17 00:00:00 2001 From: Mark van der Wal Date: Tue, 28 Apr 2020 11:22:02 +0200 Subject: [PATCH] now saves cropfield item code and if satellite data is processed --- .../HaulmkillingApplication.cs | 60 ++++++++++++++++--- FarmmapsHaulmkilling/Models/Settings.cs | 8 +++ 2 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 FarmmapsHaulmkilling/Models/Settings.cs diff --git a/FarmmapsHaulmkilling/HaulmkillingApplication.cs b/FarmmapsHaulmkilling/HaulmkillingApplication.cs index 44af239..720f457 100644 --- a/FarmmapsHaulmkilling/HaulmkillingApplication.cs +++ b/FarmmapsHaulmkilling/HaulmkillingApplication.cs @@ -2,19 +2,25 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using FarmmapsApi; +using FarmmapsApi.Models; using FarmmapsApi.Services; +using FarmmapsHaulmkilling.Models; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; namespace FarmmapsHaulmkilling { public class HaulmkillingApplication : IApplication { private const string DownloadFolder = "Downloads"; + private const string SettingsFile = "settings.json"; private readonly ILogger _logger; private readonly FarmmapsApiService _farmmapsApiService; private readonly HaulmkillingService _haulmkillingService; private readonly GeneralService _generalService; + + private Settings _settings; public HaulmkillingApplication(ILogger logger, FarmmapsApiService farmmapsApiService, GeneralService generalService, HaulmkillingService haulmkillingService) @@ -29,6 +35,8 @@ namespace FarmmapsHaulmkilling { if (!Directory.Exists(DownloadFolder)) Directory.CreateDirectory(DownloadFolder); + + LoadSettings(); // !! this call is needed the first time an api is called with a fresh clientid and secret !! await _farmmapsApiService.GetCurrentUserCodeAsync(); @@ -55,14 +63,30 @@ namespace FarmmapsHaulmkilling return; } - // save cropfield item and check if already exists and created (this to not have to run satellite everytime) - _logger.LogInformation("Creating cropfield"); - var 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]]]}"); + 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); + } - // run satellite task on just added cropfield - _logger.LogInformation("Gathering satellite information for cropfield, this might take a while!"); - await _generalService.RunAndWaitForTask(cropfieldItem, "vnd.farmmaps.task.satellite"); + if (!_settings.ProcessedSatellite) + { + _logger.LogInformation("Gathering satellite information for cropfield, this might take a while!"); + var taskStatus = await _generalService.RunAndWaitForTask(cropfieldItem, "vnd.farmmaps.task.satellite", null, 20); + + _settings.ProcessedSatellite = taskStatus.State == ItemTaskState.Ok; + SaveSettings(); + + } // find ndvi or wdvi satellite data geotiffs @@ -70,5 +94,27 @@ namespace FarmmapsHaulmkilling // download application map } + + private void LoadSettings() + { + if (File.Exists(SettingsFile)) + { + var jsonText = File.ReadAllText(SettingsFile); + _settings = JsonConvert.DeserializeObject(jsonText); + } + else + { + _settings = new Settings(); + } + } + + private void SaveSettings() + { + if (_settings == null) + return; + + var json = JsonConvert.SerializeObject(_settings); + File.WriteAllText(SettingsFile, json); + } } } \ No newline at end of file diff --git a/FarmmapsHaulmkilling/Models/Settings.cs b/FarmmapsHaulmkilling/Models/Settings.cs new file mode 100644 index 0000000..6093495 --- /dev/null +++ b/FarmmapsHaulmkilling/Models/Settings.cs @@ -0,0 +1,8 @@ +namespace FarmmapsHaulmkilling.Models +{ + public class Settings + { + public string CropfieldItemCode { get; set; } + public bool ProcessedSatellite { get; set; } + } +} \ No newline at end of file