error catching and console input to ask which KPIinput file to use
This commit is contained in:
parent
6eb36b2b4f
commit
6d903c3e9b
@ -44,25 +44,6 @@ namespace FarmmapsApi.Services
|
|||||||
var currentYear = new DateTime(year, 1, 1);
|
var currentYear = new DateTime(year, 1, 1);
|
||||||
JObject jdata = JObject.Parse(data);
|
JObject jdata = JObject.Parse(data);
|
||||||
string name = string.Format($"CrpRec Operation, {jdata.GetValue("name")}");
|
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()
|
ItemRequest operationItemRequest = new ItemRequest()
|
||||||
{
|
{
|
||||||
|
@ -38,7 +38,15 @@ namespace FarmmapsKPI
|
|||||||
|
|
||||||
public async Task RunAsync()
|
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);
|
List<KPIInput> fieldsInputs = JsonConvert.DeserializeObject<List<KPIInput>>(fieldsInputJson);
|
||||||
|
|
||||||
@ -55,8 +63,11 @@ namespace FarmmapsKPI
|
|||||||
if (!Directory.Exists(downloadFolder))
|
if (!Directory.Exists(downloadFolder))
|
||||||
Directory.CreateDirectory(downloadFolder);
|
Directory.CreateDirectory(downloadFolder);
|
||||||
|
|
||||||
|
|
||||||
//Write the same info to a single csv file. Note this means existing file will be overwritten!
|
//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" };
|
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 !!!
|
//Create a new csv file. Means if existing then overwritten !!!
|
||||||
sw = new StreamWriter(KPIItemPathCsv);
|
sw = new StreamWriter(KPIItemPathCsv);
|
||||||
@ -271,6 +282,8 @@ namespace FarmmapsKPI
|
|||||||
dataList = new List<string> { };
|
dataList = new List<string> { };
|
||||||
kpio = JsonConvert.DeserializeObject<KPIOutput>(item.Data.ToString());
|
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
|
//Seems sometimes duplicate KPI items are returned. So check that here and only write if this kpio is different from previous
|
||||||
|
if (kpio.id != null)
|
||||||
|
{
|
||||||
if (kpio.id != kpioPrevious.id)
|
if (kpio.id != kpioPrevious.id)
|
||||||
{
|
{
|
||||||
dataList.Add(kpio.parentName);
|
dataList.Add(kpio.parentName);
|
||||||
@ -285,6 +298,7 @@ namespace FarmmapsKPI
|
|||||||
dataList.Add(kpio.thresholdValue);
|
dataList.Add(kpio.thresholdValue);
|
||||||
sw.WriteLine(string.Join(",", dataList));
|
sw.WriteLine(string.Join(",", dataList));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
kpioPrevious = kpio;
|
kpioPrevious = kpio;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,9 +310,13 @@ namespace FarmmapsKPI
|
|||||||
{
|
{
|
||||||
codeOperation = crpOperationItemCodes[i];
|
codeOperation = crpOperationItemCodes[i];
|
||||||
crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation);
|
crpOperationItem = await _farmmapsApiService.GetItemAsync(codeOperation);
|
||||||
|
dynamic data = JObject.Parse(crpOperationItem.Data.ToString(Formatting.None));
|
||||||
|
if (data.n != null)
|
||||||
|
{
|
||||||
operationNapplied = crpOperationItem.Data.GetValue("n").ToObject<double>();
|
operationNapplied = crpOperationItem.Data.GetValue("n").ToObject<double>();
|
||||||
totalNapplied = totalNapplied + operationNapplied;
|
totalNapplied = totalNapplied + operationNapplied;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//Also add totalNapplied to the csv
|
//Also add totalNapplied to the csv
|
||||||
dataList = new List<string> { };
|
dataList = new List<string> { };
|
||||||
//Seems sometimes duplicate KPI items are returned. So check that here and only write if this kpio is different from previous
|
//Seems sometimes duplicate KPI items are returned. So check that here and only write if this kpio is different from previous
|
||||||
|
@ -23,17 +23,5 @@ namespace FarmmapsKPI
|
|||||||
_farmmapsApiService = farmmapsApiService;
|
_farmmapsApiService = farmmapsApiService;
|
||||||
_generalService = generalService;
|
_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);
|
|
||||||
//}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user