FarmMapsApiClient_KB34_MAST/FarmMapsBlight/BlightApplication.cs

113 lines
5.0 KiB
C#
Raw Normal View History

2020-09-10 20:38:31 +00:00
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");
string cropFieldData = "{\"area\":0,\"code\":\"0552ef068b0e4e6590c317a4c662faea\",\"name\":null,\"polygon\":null,\"soilCode\":\"60\",\"soilName\":null,\"fieldCode\":null,\"schemeUrl\":null,\"externalId\":null,\"schemeCode\":null,\"varietyCode\":\"11113\",\"varietyName\":\"Bintje\",\"cropTypeCode\":\"1010101\",\"cropTypeName\":\"Consumptieaardappelen\",\"productionTypeCode\":\"1\",\"productionPurposeCode\":null,\"productionPurposeName\":null,\"productionSequenceCode\":\"HFDTLT\"}";
2020-09-10 20:38:31 +00:00
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]]]}", cropFieldData);
2020-09-10 20:38:31 +00:00
_settings.CropfieldItemCode = cropfieldItem.Code;
SaveSettings();
}
else
{
_logger.LogInformation("Cropfield already exists trying to get");
cropfieldItem = await _farmmapsApiService.GetItemAsync(_settings.CropfieldItemCode);
}
2020-10-13 11:51:51 +00:00
DateTime adviceDate = DateTime.Now.Date;
2020-09-10 20:38:31 +00:00
DateTime plantingDate = new DateTime(2020, 3, 20);
DateTime emergeDate = new DateTime(2020, 5, 20);
2020-10-13 11:51:51 +00:00
var blightItem = await _blightService.CreateAdvice(cropfieldItem, adviceDate, plantingDate, emergeDate);
2020-09-10 20:38:31 +00:00
if (blightItem == null)
{
return;
}
// advice as json
Console.WriteLine(blightItem.Data);
2020-09-10 20:38:31 +00:00
}
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);
}
}
}