From 2fe230ccbf17d774123ed835517ef673924d73ef Mon Sep 17 00:00:00 2001 From: Wilco Krikke Date: Thu, 10 Sep 2020 22:38:31 +0200 Subject: [PATCH] Added BlightApiClient --- FarmMapsBlight/BlightApplication.cs | 111 +++++++++++++++++++++++++++ FarmMapsBlight/BlightService.cs | 67 ++++++++++++++++ FarmMapsBlight/FarmMapsBlight.csproj | 18 +++++ FarmMapsBlight/Models/Settings.cs | 7 ++ FarmMapsBlight/Program.cs | 23 ++++++ FarmMapsBlight/appsettings.json | 12 +++ FarmmapsApi/Constants.cs | 4 +- FarmmapsApiSamples.sln | 8 +- 8 files changed, 247 insertions(+), 3 deletions(-) create mode 100644 FarmMapsBlight/BlightApplication.cs create mode 100644 FarmMapsBlight/BlightService.cs create mode 100644 FarmMapsBlight/FarmMapsBlight.csproj create mode 100644 FarmMapsBlight/Models/Settings.cs create mode 100644 FarmMapsBlight/Program.cs create mode 100644 FarmMapsBlight/appsettings.json diff --git a/FarmMapsBlight/BlightApplication.cs b/FarmMapsBlight/BlightApplication.cs new file mode 100644 index 0000000..f98f47f --- /dev/null +++ b/FarmMapsBlight/BlightApplication.cs @@ -0,0 +1,111 @@ +using FarmmapsApi; +using FarmmapsApi.Models; +using FarmmapsApi.Services; +using FarmMapsBlight.Models; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + + +namespace FarmMapsBlight +{ + public class BlightApplication : IApplication + { + private const string DownloadFolder = "Downloads"; + private const string SettingsFile = "settings.json"; + + private readonly ILogger _logger; + private readonly FarmmapsApiService _farmmapsApiService; + private readonly BlightService _blightService; + private readonly GeneralService _generalService; + + private Settings _settings; + + public BlightApplication(ILogger logger, FarmmapsApiService farmmapsApiService, + GeneralService generalService, BlightService haulmkillingService) + { + _logger = logger; + _farmmapsApiService = farmmapsApiService; + _generalService = generalService; + _blightService = haulmkillingService; + } + + public async Task RunAsync() + { + 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(); + 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; + } + + Item cropfieldItem; + if (string.IsNullOrEmpty(_settings.CropfieldItemCode)) + { + _logger.LogInformation("Creating cropfield"); + cropfieldItem = await _generalService.CreateCropfieldItemAsync(myDrive.Code, "Cropfield Blight", 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); + } + + DateTime plantingDate = new DateTime(2020, 3, 20); + DateTime emergeDate = new DateTime(2020, 5, 20); + + var blightItem = await _blightService.CreateAdvice(cropfieldItem, plantingDate, emergeDate); + if (blightItem == null) + { + return; + } + + // advice as json + var data = blightItem.Data; + } + + 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/FarmMapsBlight/BlightService.cs b/FarmMapsBlight/BlightService.cs new file mode 100644 index 0000000..7dc4c93 --- /dev/null +++ b/FarmMapsBlight/BlightService.cs @@ -0,0 +1,67 @@ +using FarmmapsApi.Models; +using FarmmapsApi.Services; +using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; +using static FarmmapsApi.Extensions; +using static FarmmapsApiSamples.Constants; + +namespace FarmMapsBlight +{ + public class BlightService + { + private readonly ILogger _logger; + private readonly FarmmapsApiService _farmmapsApiService; + private readonly GeneralService _generalService; + + public BlightService(ILogger logger, FarmmapsApiService farmmapsApiService, + GeneralService generalService) + { + _logger = logger; + _farmmapsApiService = farmmapsApiService; + _generalService = generalService; + } + + public async Task CreateAdvice(Item cropfieldItem, DateTime plantingDate, DateTime emergeDate) + { + var taskRequest = new TaskRequest() + { + TaskType = "vnd.farmmaps.task.blight" + }; + + taskRequest.attributes["plantingDate"] = plantingDate.ToUniversalTime().ToString("o"); + taskRequest.attributes["emergeDate"] = emergeDate.ToUniversalTime().ToString("o"); + + var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskRequest); + await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) => + { + _logger.LogInformation("Checking blight task status"); + var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode); + if (itemTaskStatus.IsFinished) + tokenSource.Cancel(); + }); + + var itemTask = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode); + if (itemTask.State == ItemTaskState.Error) + { + _logger.LogError($"Something went wrong with task execution: {itemTask.Message}"); + return null; + } + + var itemName = $"Blight"; + var blightAdviceItem = await _generalService.FindChildItemAsync(cropfieldItem.Code, + BLIGHT_ITEMTYPE, itemName); + + // incorrect filter task is finished after updating + + // i => i.Updated >= itemTask.Finished && i.Name.ToLower().Contains(itemName.ToLower()) + if (blightAdviceItem == null) + { + _logger.LogError("Could not find the blight item under cropfield"); + return null; + } + + return blightAdviceItem; + } + } +} \ No newline at end of file diff --git a/FarmMapsBlight/FarmMapsBlight.csproj b/FarmMapsBlight/FarmMapsBlight.csproj new file mode 100644 index 0000000..e39b1b4 --- /dev/null +++ b/FarmMapsBlight/FarmMapsBlight.csproj @@ -0,0 +1,18 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + Always + + + + diff --git a/FarmMapsBlight/Models/Settings.cs b/FarmMapsBlight/Models/Settings.cs new file mode 100644 index 0000000..524211e --- /dev/null +++ b/FarmMapsBlight/Models/Settings.cs @@ -0,0 +1,7 @@ +namespace FarmMapsBlight.Models +{ + public class Settings + { + public string CropfieldItemCode { get; set; } + } +} \ No newline at end of file diff --git a/FarmMapsBlight/Program.cs b/FarmMapsBlight/Program.cs new file mode 100644 index 0000000..a12ff49 --- /dev/null +++ b/FarmMapsBlight/Program.cs @@ -0,0 +1,23 @@ +using FarmmapsApi; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; + +namespace FarmMapsBlight +{ + public class Program : FarmmapsProgram + { + private static async Task Main(string[] args) + { + await new Program().Start(args); + } + + protected override void Configure(IServiceCollection serviceCollection) + { + serviceCollection.AddLogging(opts => opts + .AddConsole() + .AddFilter("System.Net.Http", LogLevel.Warning)) + .AddTransient(); + } + } +} \ No newline at end of file diff --git a/FarmMapsBlight/appsettings.json b/FarmMapsBlight/appsettings.json new file mode 100644 index 0000000..e5b5264 --- /dev/null +++ b/FarmMapsBlight/appsettings.json @@ -0,0 +1,12 @@ +{ + "Authority": "https://accounts.farmmaps.awtest.nl/", + //"Endpoint": "http://farmmaps.awtest.nl", + //"Endpoint": "http://localhost:8095", + "Endpoint": "http://localhost:8083", + "BasePath": "api/v1", + "DiscoveryEndpointUrl": "https://accounts.farmmaps.awtest.nl/.well-known/openid-configuration", + "RedirectUri": "http://example.nl/api", + "ClientId": "wurtest1", + "ClientSecret": "7