2020-03-25 20:32:28 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
using FarmmapsApi;
|
|
|
|
|
using FarmmapsApi.Models;
|
2020-03-25 20:32:28 +00:00
|
|
|
|
using FarmmapsApi.Services;
|
2020-03-24 08:47:08 +00:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace FarmmapsApiSamples
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
2020-04-08 17:28:30 +00:00
|
|
|
|
private readonly ILogger<Program> _logger;
|
|
|
|
|
private readonly FarmmapsApiService _farmmapsApiService;
|
|
|
|
|
private readonly FarmmapsEventHub _farmmapsEventHub;
|
|
|
|
|
private readonly NitrogenService _nitrogenService;
|
|
|
|
|
private readonly HerbicideService _herbicideService;
|
|
|
|
|
|
2020-03-24 08:47:08 +00:00
|
|
|
|
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
|
2020-03-25 20:32:28 +00:00
|
|
|
|
.AddConsole()
|
2020-03-24 08:47:08 +00:00
|
|
|
|
.AddFilter("System.Net.Http", LogLevel.Warning))
|
|
|
|
|
.AddFarmmapsServices(configuration)
|
2020-03-25 16:49:00 +00:00
|
|
|
|
.AddTransient<NitrogenService>()
|
2020-04-06 11:37:11 +00:00
|
|
|
|
.AddTransient<HerbicideService>()
|
2020-04-08 17:28:30 +00:00
|
|
|
|
.AddSingleton<Program>()
|
2020-03-24 08:47:08 +00:00
|
|
|
|
.BuildServiceProvider();
|
2020-03-25 20:32:28 +00:00
|
|
|
|
|
|
|
|
|
await serviceProvider.GetService<FarmmapsApiService>().AuthenticateAsync();
|
2020-04-08 09:43:53 +00:00
|
|
|
|
// await serviceProvider.GetService<FarmmapsEventHub>().StartEventHub();
|
2020-04-08 17:28:30 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2020-03-24 08:47:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|