FarmMapsApiClient/FarmmapsApiSamples/Program.cs

78 lines
2.8 KiB
C#

using System;
using System.Threading.Tasks;
using FarmmapsApi;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace FarmmapsApiSamples
{
class Program
{
private readonly ILogger<Program> _logger;
private readonly FarmmapsApiService _farmmapsApiService;
private readonly FarmmapsEventHub _farmmapsEventHub;
private readonly NitrogenService _nitrogenService;
private readonly HerbicideService _herbicideService;
private static async Task Main(string[] args)
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.Build();
var configuration = config.Get<Configuration>();
var serviceProvider = new ServiceCollection()
.AddLogging(opts => opts
.AddConsole()
.AddFilter("System.Net.Http", LogLevel.Warning))
.AddFarmmapsServices(configuration)
.AddTransient<NitrogenService>()
.AddTransient<HerbicideService>()
.AddSingleton<Program>()
.BuildServiceProvider();
await serviceProvider.GetService<FarmmapsApiService>().AuthenticateAsync();
// await serviceProvider.GetService<FarmmapsEventHub>().StartEventHub();
await serviceProvider.GetService<Program>().RunAsync();
}
public Program(ILogger<Program> logger, FarmmapsApiService farmmapsApiService,
FarmmapsEventHub farmmapsEventHub, NitrogenService nitrogenService,
HerbicideService herbicideService)
{
_logger = logger;
_farmmapsApiService = farmmapsApiService;
_farmmapsEventHub = farmmapsEventHub;
_nitrogenService = nitrogenService;
_herbicideService = herbicideService;
_farmmapsEventHub.EventCallback += OnEvent;
}
private void OnEvent(EventMessage @event)
{
// _logger.LogInformation(@event.EventType);
}
public async Task RunAsync()
{
try
{
// !! 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();
await _nitrogenService.TestFlow(roots);
// await _herbicideService.TestFlow(roots);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}
}
}