forked from FarmMaps/FarmMapsApiClient
84 lines
5.4 KiB
C#
84 lines
5.4 KiB
C#
using FarmmapsApi.Models;
|
|
using FarmmapsApi.Services;
|
|
using FarmMapsBlight.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
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 adviceDate, DateTime plantingDate, DateTime emergeDate)
|
|
{
|
|
var taskRequest = new TaskRequest()
|
|
{
|
|
TaskType = "vnd.farmmaps.task.blight"
|
|
};
|
|
|
|
taskRequest.attributes["d"] = adviceDate.ToUniversalTime().ToString("o");
|
|
taskRequest.attributes["plantingDate"] = plantingDate.ToUniversalTime().ToString("o");
|
|
taskRequest.attributes["emergeDate"] = emergeDate.ToUniversalTime().ToString("o");
|
|
|
|
var fungicide1 = "{\"ai1\": \"propamocarb\", \"ai2\": \"fluopicolide\", \"ai3\": \"cymoxanil\", \"code\": \"infinito12curz\", \"name\": \"infinito 1,2 l + curzate partner 0,2 kg\", \"maxdose\": \"1\", \"mindose\": \"1\", \"safedays\": \"14\", \"emergence\": true, \"newgrowth\": \"1\", \"contentai1\": \"525.2\", \"contentai2\": \"62.5\", \"contentai3\": \"600\", \"fastgrowth\": true, \"contentunit\": \"l/ha + kg/ha\", \"rainfastness\": \"2.5\", \"tuberfilling\": true, \"aidescription\": \"(propamocarb + fluopicolide) 1.2 l/ha + cymoxanil 0.2 kg/ha\", \"curativescore\": \"2\", \"dryingtimemax\": \"2\", \"dryingtimemin\": \"2\", \"vracompatible\": false, \"maxapplications\": \"4\", \"preventivescore\": \"3\", \"recommendeddose\": \"1\", \"tuberprotection\": \"3.3\", \"eradicativescore\": \"2\", \"earlytubersetting\": true, \"protectioncategory\": \"2\", \"applicationrateunit\": null, \"ctgbregistrationnumber\": \"12927 n + 12755 n\"}";
|
|
var fungicide2 = "{\"ai1\": \"fluazinam\", \"ai2\": \"cymoxanil\", \"ai3\": null, \"code\": \"kunshi\", \"name\": \"kunshi\", \"maxdose\": \"0.5\", \"mindose\": \"0.4\", \"safedays\": \"1\", \"emergence\": false, \"newgrowth\": \"1\", \"contentai1\": \"375\", \"contentai2\": \"250\", \"contentai3\": \"0\", \"fastgrowth\": true, \"contentunit\": \"g/kg\", \"rainfastness\": \"2.5\", \"tuberfilling\": false, \"aidescription\": \"(fluazinam + cymoxanil) 0.5 kg/ha\", \"curativescore\": \"2\", \"dryingtimemax\": \"2\", \"dryingtimemin\": \"1\", \"vracompatible\": true, \"maxapplications\": \"5\", \"preventivescore\": \"2.9\", \"recommendeddose\": \"0.5\", \"tuberprotection\": \"3.3\", \"eradicativescore\": \"1\", \"earlytubersetting\": false, \"protectioncategory\": \"1\", \"applicationrateunit\": \"kg/ha\", \"ctgbregistrationnumber\": \"14371 n\"}";
|
|
|
|
List<Spray> sprays = new List<Spray>();
|
|
sprays.Add(new Spray() { fungicide = JsonConvert.DeserializeObject<Fungicide>(fungicide1), sprayTime = new DateTime(2020, 6, 24), dose = 0.6, isVRA = false });
|
|
sprays.Add(new Spray() { fungicide = JsonConvert.DeserializeObject<Fungicide>(fungicide2), sprayTime = new DateTime(2020, 7, 2), dose = 0.6, isVRA = false });
|
|
//sprays.Add(new Spray() { fungicideCode = "FLEX", SprayTime = new DateTime(2020, 7, 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);
|
|
|
|
var taskCode = await _farmmapsApiService.QueueTaskAsync(cropfieldItem.Code, taskRequest);
|
|
await PollTask(TimeSpan.FromSeconds(3), async (tokenSource) =>
|
|
{
|
|
var itemTaskStatus = await _farmmapsApiService.GetTaskStatusAsync(cropfieldItem.Code, taskCode);
|
|
_logger.LogInformation($"Checking blight task status: { itemTaskStatus.State}");
|
|
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;
|
|
}
|
|
}
|
|
} |