From 6d903c3e9be91a5d630c132e4511febb0d56059c Mon Sep 17 00:00:00 2001 From: Pepijn van Oort Date: Tue, 24 Oct 2023 13:06:36 +0200 Subject: [PATCH] error catching and console input to ask which KPIinput file to use --- FarmmapsApi/Services/GeneralService.cs | 19 ---------- FarmmapsKPI/KPIApplication.cs | 50 +++++++++++++++++--------- FarmmapsKPI/KPIService.cs | 12 ------- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/FarmmapsApi/Services/GeneralService.cs b/FarmmapsApi/Services/GeneralService.cs index d95380c..6b452a0 100644 --- a/FarmmapsApi/Services/GeneralService.cs +++ b/FarmmapsApi/Services/GeneralService.cs @@ -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(); - code022 = jdata.GetValue("product").ToObject(); - //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() { diff --git a/FarmmapsKPI/KPIApplication.cs b/FarmmapsKPI/KPIApplication.cs index 5fec1ad..91f0a9e 100644 --- a/FarmmapsKPI/KPIApplication.cs +++ b/FarmmapsKPI/KPIApplication.cs @@ -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 fieldsInputs = JsonConvert.DeserializeObject>(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 headerList = new List { "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 { }; kpio = JsonConvert.DeserializeObject(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(); - totalNapplied = totalNapplied + operationNapplied; + dynamic data = JObject.Parse(crpOperationItem.Data.ToString(Formatting.None)); + if (data.n != null) + { + operationNapplied = crpOperationItem.Data.GetValue("n").ToObject(); + totalNapplied = totalNapplied + operationNapplied; + } } //Also add totalNapplied to the csv dataList = new List { }; diff --git a/FarmmapsKPI/KPIService.cs b/FarmmapsKPI/KPIService.cs index 846bce7..4e0614c 100644 --- a/FarmmapsKPI/KPIService.cs +++ b/FarmmapsKPI/KPIService.cs @@ -23,17 +23,5 @@ namespace FarmmapsKPI _farmmapsApiService = farmmapsApiService; _generalService = generalService; } - // zoiets kan ik overwegen als ik de itemtype niet kan vinden - //public async Task CreateTargetKPIItem(Item cropfieldItem) - //{ - // var itemRequest = new ItemRequest() - // { - // ParentCode = cropfieldItem.ParentCode, - // ItemType = USERINPUT_ITEMTYPE, - // Name = "kpi" - // }; - // return await _farmmapsApiService.CreateItemAsync(itemRequest); - //} - } } \ No newline at end of file