FarmMapsApiClient_WURtest/FarmMapsBlight/BlightService.cs

79 lines
3.3 KiB
C#
Raw Normal View History

2020-09-10 20:38:31 +00:00
using FarmmapsApi.Models;
using FarmmapsApi.Services;
2020-09-26 18:11:22 +00:00
using FarmMapsBlight.Models;
2020-09-10 20:38:31 +00:00
using Microsoft.Extensions.Logging;
2020-09-26 18:11:22 +00:00
using Newtonsoft.Json;
2020-09-10 20:38:31 +00:00
using System;
2020-09-26 18:11:22 +00:00
using System.Collections.Generic;
2020-09-10 20:38:31 +00:00
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;
}
2020-10-13 11:51:51 +00:00
public async Task<Item> CreateAdvice(Item cropfieldItem, DateTime adviceDate, DateTime plantingDate, DateTime emergeDate)
2020-09-10 20:38:31 +00:00
{
var taskRequest = new TaskRequest()
{
TaskType = "vnd.farmmaps.task.blight"
};
2020-10-13 11:51:51 +00:00
taskRequest.attributes["d"] = DateTime.Now.Date.ToString("o");
2020-09-10 20:38:31 +00:00
taskRequest.attributes["plantingDate"] = plantingDate.ToUniversalTime().ToString("o");
taskRequest.attributes["emergeDate"] = emergeDate.ToUniversalTime().ToString("o");
2020-09-26 18:11:22 +00:00
List<Spray> sprays = new List<Spray>();
sprays.Add(new Spray() { fungicideCode = "FLEX", SprayTime = new DateTime(2020, 9, 1), dose = 0.6, isVRA = false });
taskRequest.attributes["sprays"] = JsonConvert.SerializeObject(sprays);
List<Irrigation> irrigations = new List<Irrigation>();
irrigations.Add(new Irrigation() { startTime = new DateTime(2020, 7, 1, 2, 0, 0), endTime = new DateTime(2020, 7, 1, 14, 0, 0) });
taskRequest.attributes["irrigations"] = JsonConvert.SerializeObject(irrigations);
2020-09-10 20:38:31 +00:00
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;
}
}
}