Added BlightApiClient
This commit is contained in:
parent
54272e1339
commit
1691e09bf9
111
FarmMapsBlight/BlightApplication.cs
Normal file
111
FarmMapsBlight/BlightApplication.cs
Normal file
@ -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<BlightApplication> _logger;
|
||||||
|
private readonly FarmmapsApiService _farmmapsApiService;
|
||||||
|
private readonly BlightService _blightService;
|
||||||
|
private readonly GeneralService _generalService;
|
||||||
|
|
||||||
|
private Settings _settings;
|
||||||
|
|
||||||
|
public BlightApplication(ILogger<BlightApplication> 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<Settings>(jsonText);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_settings = new Settings();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SaveSettings()
|
||||||
|
{
|
||||||
|
if (_settings == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var json = JsonConvert.SerializeObject(_settings);
|
||||||
|
File.WriteAllText(SettingsFile, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
67
FarmMapsBlight/BlightService.cs
Normal file
67
FarmMapsBlight/BlightService.cs
Normal file
@ -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<BlightService> _logger;
|
||||||
|
private readonly FarmmapsApiService _farmmapsApiService;
|
||||||
|
private readonly GeneralService _generalService;
|
||||||
|
|
||||||
|
public BlightService(ILogger<BlightService> logger, FarmmapsApiService farmmapsApiService,
|
||||||
|
GeneralService generalService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_farmmapsApiService = farmmapsApiService;
|
||||||
|
_generalService = generalService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Item> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
FarmMapsBlight/FarmMapsBlight.csproj
Normal file
18
FarmMapsBlight/FarmMapsBlight.csproj
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FarmmapsApi\FarmmapsApi.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
7
FarmMapsBlight/Models/Settings.cs
Normal file
7
FarmMapsBlight/Models/Settings.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace FarmMapsBlight.Models
|
||||||
|
{
|
||||||
|
public class Settings
|
||||||
|
{
|
||||||
|
public string CropfieldItemCode { get; set; }
|
||||||
|
}
|
||||||
|
}
|
23
FarmMapsBlight/Program.cs
Normal file
23
FarmMapsBlight/Program.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using FarmmapsApi;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FarmMapsBlight
|
||||||
|
{
|
||||||
|
public class Program : FarmmapsProgram<BlightApplication>
|
||||||
|
{
|
||||||
|
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<BlightService>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
FarmMapsBlight/appsettings.json
Normal file
12
FarmMapsBlight/appsettings.json
Normal file
@ -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" ]
|
||||||
|
}
|
@ -10,6 +10,7 @@ namespace FarmmapsApiSamples
|
|||||||
public const string SHAPE_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.shape.processed";
|
public const string SHAPE_PROCESSED_ITEMTYPE = "vnd.farmmaps.itemtype.shape.processed";
|
||||||
public const string SHAPE_ITEMTYPE = "vnd.farmmaps.itemtype.shape";
|
public const string SHAPE_ITEMTYPE = "vnd.farmmaps.itemtype.shape";
|
||||||
public const string GEOJSON_ITEMTYPE = "vnd.farmmaps.itemtype.geojson";
|
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 VRANBS_TASK = "vnd.farmmaps.task.vranbs";
|
||||||
public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide";
|
public const string VRAHERBICIDE_TASK = "vnd.farmmaps.task.vraherbicide";
|
||||||
@ -21,6 +22,5 @@ namespace FarmmapsApiSamples
|
|||||||
public const string BOFEK_TASK = "vnd.farmmaps.task.bofek";
|
public const string BOFEK_TASK = "vnd.farmmaps.task.bofek";
|
||||||
public const string SHADOW_TASK = "vnd.farmmaps.task.shadow";
|
public const string SHADOW_TASK = "vnd.farmmaps.task.shadow";
|
||||||
public const string AHN_TASK = "vnd.farmmaps.task.ahn";
|
public const string AHN_TASK = "vnd.farmmaps.task.ahn";
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FarmmapsHerbicide", "Farmma
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FarmmapsHaulmkilling", "FarmmapsHaulmkilling\FarmmapsHaulmkilling.csproj", "{DFA89D0B-5400-4374-B824-8367B76B4B6E}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FarmmapsHaulmkilling", "FarmmapsHaulmkilling\FarmmapsHaulmkilling.csproj", "{DFA89D0B-5400-4374-B824-8367B76B4B6E}"
|
||||||
EndProject
|
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
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{AAFAB03A-6F5C-4D91-991F-867B7898F981}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
|
10
FarmmapsPoten/appsettings.json
Normal file
10
FarmmapsPoten/appsettings.json
Normal file
@ -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"]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user