From 1691e09bf944838dc16adfd9fe99421b428d1a3a 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 +- FarmmapsPoten/FarmmapsPoten.csproj | 2 +- FarmmapsPoten/appsettings.json | 10 +++ 10 files changed, 258 insertions(+), 4 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 create mode 100644 FarmmapsPoten/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..c81c149 --- /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": "", + "ClientSecret": "", + "Scopes": [ "api" ] +} \ No newline at end of file diff --git a/FarmmapsApi/Constants.cs b/FarmmapsApi/Constants.cs index d8d1bdc..ddb8e9c 100644 --- a/FarmmapsApi/Constants.cs +++ b/FarmmapsApi/Constants.cs @@ -10,7 +10,8 @@ namespace FarmmapsApiSamples public const string SHAPE_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.shape.processed"; public const string SHAPE_ITEMTYPE = "vnd.farmmaps.itemtype.shape"; public const string GEOJSON_ITEMTYPE = "vnd.farmmaps.itemtype.geojson"; - + public const string BLIGHT_ITEMTYPE = "vnd.farmmaps.itemtype.blight"; + public const string VRANBS_TASK = "vnd.farmmaps.task.vranbs"; public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide"; public const string VRAHAULMKILLING_TASK = "vnd.farmmaps.task.vrahaulmkilling"; @@ -21,6 +22,5 @@ namespace FarmmapsApiSamples public const string BOFEK_TASK = "vnd.farmmaps.task.bofek"; public const string SHADOW_TASK = "vnd.farmmaps.task.shadow"; public const string AHN_TASK = "vnd.farmmaps.task.ahn"; - } } diff --git a/FarmmapsApiSamples.sln b/FarmmapsApiSamples.sln index 3d96af1..f4c5fdb 100644 --- a/FarmmapsApiSamples.sln +++ b/FarmmapsApiSamples.sln @@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FarmmapsHerbicide", "Farmma EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FarmmapsHaulmkilling", "FarmmapsHaulmkilling\FarmmapsHaulmkilling.csproj", "{DFA89D0B-5400-4374-B824-8367B76B4B6E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FarmmapsPoten", "FarmmapsPoten\FarmmapsPoten.csproj", "{AAFAB03A-6F5C-4D91-991F-867B7898F981}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FarmmapsPoten", "FarmmapsPoten\FarmmapsPoten.csproj", "{AAFAB03A-6F5C-4D91-991F-867B7898F981}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FarmMapsBlight", "FarmMapsBlight\FarmMapsBlight.csproj", "{892E0932-5D11-4A37-979E-CEDB39C2E181}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {AAFAB03A-6F5C-4D91-991F-867B7898F981}.Debug|Any CPU.Build.0 = Debug|Any CPU {AAFAB03A-6F5C-4D91-991F-867B7898F981}.Release|Any CPU.ActiveCfg = Release|Any CPU {AAFAB03A-6F5C-4D91-991F-867B7898F981}.Release|Any CPU.Build.0 = Release|Any CPU + {892E0932-5D11-4A37-979E-CEDB39C2E181}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {892E0932-5D11-4A37-979E-CEDB39C2E181}.Debug|Any CPU.Build.0 = Debug|Any CPU + {892E0932-5D11-4A37-979E-CEDB39C2E181}.Release|Any CPU.ActiveCfg = Release|Any CPU + {892E0932-5D11-4A37-979E-CEDB39C2E181}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FarmmapsPoten/FarmmapsPoten.csproj b/FarmmapsPoten/FarmmapsPoten.csproj index 01fd4da..f76d9da 100644 --- a/FarmmapsPoten/FarmmapsPoten.csproj +++ b/FarmmapsPoten/FarmmapsPoten.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/FarmmapsPoten/appsettings.json b/FarmmapsPoten/appsettings.json new file mode 100644 index 0000000..6d97d71 --- /dev/null +++ b/FarmmapsPoten/appsettings.json @@ -0,0 +1,10 @@ +{ + "Authority": "https://accounts.farmmaps.awtest.nl/", + "Endpoint": "http://localhost:8095/", + "BasePath": "api/v1", + "DiscoveryEndpointUrl": "https://accounts.farmmaps.awtest.nl/.well-known/openid-configuration", + "RedirectUri": "http://example.nl/api", + "ClientId": "", + "ClientSecret": "", + "Scopes": ["api"] +} \ No newline at end of file