error catching and console input to ask which KPIinput file to use

This commit is contained in:
Pepijn van Oort 2023-10-24 13:06:36 +02:00
parent 6eb36b2b4f
commit 6d903c3e9b
3 changed files with 34 additions and 47 deletions

View File

@ -44,25 +44,6 @@ namespace FarmmapsApi.Services
var currentYear = new DateTime(year, 1, 1);
JObject jdata = JObject.Parse(data);
string name = string.Format($"CrpRec Operation, {jdata.GetValue("name")}");
int code022;
string type022;
double quantity;
double applied_kgNha;
double ncontent = 0.0; // for now just any value
//Is it a fertilizer application?
//If the operation contains an element "n" then nothing, use that value (kg N/ha administred)
//Else: look up the N content for the code022, calculate "n" based on fertilizer amount (data) & content (cl022) and add applied_kgNha to the jdata
if (jdata.ContainsKey("n") == false)
{
quantity = jdata.GetValue("quantity").ToObject<double>();
code022 = jdata.GetValue("product").ToObject<int>();
//TODO: Now here look up this code022 in the cl022 and get the ncontent from that list.
//And check the unit in which the ncontent is expressed, e.g. % or kg/ton and check if it is not null
applied_kgNha = quantity * ncontent;
jdata.Add("n", applied_kgNha.ToString()); //all Data elements in Farmmaps code ar strings
};
ItemRequest operationItemRequest = new ItemRequest()
{

View File

@ -38,7 +38,15 @@ namespace FarmmapsKPI
public async Task RunAsync()
{
var fieldsInputJson = File.ReadAllText("KPIInput.json");
// var fieldsInputJson = File.ReadAllText("KPIInputChemie.json"); // hier gebleven, bad gateway?!
string fnKPIinput;
Console.WriteLine("Type name of input json file. Example: KPIinput.json (in same directory as FarmmapsKPI.exe) or C:/temp/KPIinputChemieTmp.json");
fnKPIinput = Console.ReadLine();
if (string.IsNullOrEmpty(fnKPIinput))
fnKPIinput = "KPIinput.json";
var fieldsInputJson = File.ReadAllText(fnKPIinput);
List<KPIInput> fieldsInputs = JsonConvert.DeserializeObject<List<KPIInput>>(fieldsInputJson);
@ -55,8 +63,11 @@ namespace FarmmapsKPI
if (!Directory.Exists(downloadFolder))
Directory.CreateDirectory(downloadFolder);
//Write the same info to a single csv file. Note this means existing file will be overwritten!
StreamWriter sw; string KPIItemPathCsv = Path.Combine(downloadFolder, "KPIItems.csv");
StreamWriter sw;
string KPIItemCsv = Path.GetFileNameWithoutExtension(fnKPIinput) + "_Items.csv";
string KPIItemPathCsv = Path.Combine(downloadFolder, KPIItemCsv);
List<string> headerList = new List<string> { "parentName", "area_ha", "cropTypeCode", "cropTypeName", "KPIid", "KPIvariable", "KPIvalue", "KPIunit", "KPItargetvalue", "KPIthresholdValue" };
//Create a new csv file. Means if existing then overwritten !!!
sw = new StreamWriter(KPIItemPathCsv);
@ -271,19 +282,22 @@ namespace FarmmapsKPI
dataList = new List<string> { };
kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString());
//Seems sometimes duplicate KPI items are returned. So check that here and only write if this kpio is different from previous
if (kpio.id != kpioPrevious.id)
if (kpio.id != null)
{
dataList.Add(kpio.parentName);
dataList.Add(kpio.data.area);
dataList.Add(kpio.data.cropTypeCode);
dataList.Add(kpio.data.cropTypeName);
dataList.Add(kpio.id);
dataList.Add(kpio.quantity);
dataList.Add(kpio.value);
dataList.Add(kpio.unit);
dataList.Add(kpio.targetValue);
dataList.Add(kpio.thresholdValue);
sw.WriteLine(string.Join(",", dataList));
if (kpio.id != kpioPrevious.id)
{
dataList.Add(kpio.parentName);
dataList.Add(kpio.data.area);
dataList.Add(kpio.data.cropTypeCode);
dataList.Add(kpio.data.cropTypeName);
dataList.Add(kpio.id);
dataList.Add(kpio.quantity);
dataList.Add(kpio.value);
dataList.Add(kpio.unit);
dataList.Add(kpio.targetValue);
dataList.Add(kpio.thresholdValue);
sw.WriteLine(string.Join(",", dataList));
}
}
kpioPrevious = kpio;
}
@ -296,8 +310,12 @@ namespace FarmmapsKPI
{
codeOperation = crpOperationItemCodes[i];
crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation);
operationNapplied = crpOperationItem.Data.GetValue("n").ToObject<double>();
totalNapplied = totalNapplied + operationNapplied;
dynamic data = JObject.Parse(crpOperationItem.Data.ToString(Formatting.None));
if (data.n != null)
{
operationNapplied = crpOperationItem.Data.GetValue("n").ToObject<double>();
totalNapplied = totalNapplied + operationNapplied;
}
}
//Also add totalNapplied to the csv
dataList = new List<string> { };

View File

@ -23,17 +23,5 @@ namespace FarmmapsKPI
_farmmapsApiService = farmmapsApiService;
_generalService = generalService;
}
// zoiets kan ik overwegen als ik de itemtype niet kan vinden
//public async Task<Item> CreateTargetKPIItem(Item cropfieldItem)
//{
// var itemRequest = new ItemRequest()
// {
// ParentCode = cropfieldItem.ParentCode,
// ItemType = USERINPUT_ITEMTYPE,
// Name = "kpi"
// };
// return await _farmmapsApiService.CreateItemAsync(itemRequest);
//}
}
}