2023-03-22 13:48:13 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.IO.Compression ;
using System.Linq ;
using System.Threading.Tasks ;
using FarmmapsApi ;
using FarmmapsApi.Models ;
using FarmmapsApi.Services ;
using FarmmapsKPI.Models ;
using Microsoft.Extensions.Logging ;
using Newtonsoft.Json ;
using Newtonsoft.Json.Linq ;
using static FarmmapsApiSamples . Constants ;
namespace FarmmapsKPI
{
public class KPIApplication : IApplication
{
//private const string DownloadFolder = "Downloads";
private const string SettingsFile = "settings.json" ;
private readonly ILogger < KPIApplication > _logger ;
private readonly FarmmapsApiService _farmmapsApiService ;
2023-10-04 15:21:05 +00:00
private readonly KPIService _kpiService ;
2023-03-22 13:48:13 +00:00
private readonly GeneralService _generalService ;
private Settings _settings ;
public KPIApplication ( ILogger < KPIApplication > logger , FarmmapsApiService farmmapsApiService ,
2023-10-04 15:21:05 +00:00
GeneralService generalService , KPIService kpiService )
2023-03-22 13:48:13 +00:00
{
_logger = logger ;
_farmmapsApiService = farmmapsApiService ;
_generalService = generalService ;
2023-10-04 15:21:05 +00:00
_kpiService = kpiService ;
2023-03-22 13:48:13 +00:00
}
public async Task RunAsync ( )
{
2023-10-24 11:06:36 +00:00
// 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 ) ;
2023-03-22 13:48:13 +00:00
List < KPIInput > fieldsInputs = JsonConvert . DeserializeObject < List < KPIInput > > ( fieldsInputJson ) ;
// !! this call is needed the first time an api is called with a fresh clientid and secret !!
await _farmmapsApiService . GetCurrentUserCodeAsync ( ) ;
var roots = await _farmmapsApiService . GetCurrentUserRootsAsync ( ) ;
2023-10-13 14:37:50 +00:00
//Where to write the output
string downloadFolder = fieldsInputs [ 0 ] . DownloadFolder ;
if ( string . IsNullOrEmpty ( downloadFolder ) )
{
downloadFolder = "Downloads" ;
}
if ( ! Directory . Exists ( downloadFolder ) )
Directory . CreateDirectory ( downloadFolder ) ;
2023-10-24 11:06:36 +00:00
2023-10-13 14:37:50 +00:00
//Write the same info to a single csv file. Note this means existing file will be overwritten!
2023-10-24 11:06:36 +00:00
StreamWriter sw ;
string KPIItemCsv = Path . GetFileNameWithoutExtension ( fnKPIinput ) + "_Items.csv" ;
string KPIItemPathCsv = Path . Combine ( downloadFolder , KPIItemCsv ) ;
2023-10-13 14:37:50 +00:00
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 ) ;
sw . WriteLine ( string . Join ( "," , headerList ) ) ;
//Now loop through the list of cropfields in KPIinput.json and get the KPI's for each cropfield
2023-03-22 13:48:13 +00:00
foreach ( var input in fieldsInputs )
{
try
{
2023-10-13 14:37:50 +00:00
await Process ( roots , input , sw ) ;
2023-03-22 13:48:13 +00:00
}
catch ( Exception ex )
{
_logger . LogError ( ex . Message ) ;
}
}
2023-10-13 14:37:50 +00:00
//Close the csv file, write message to screen
sw . Close ( ) ;
_logger . LogInformation ( $"Done! Written all KPI for all fields in 'KPIinput.json' to output file '{KPIItemPathCsv}'" ) ;
2023-03-22 13:48:13 +00:00
}
2023-10-13 14:37:50 +00:00
private async Task Process ( List < UserRoot > roots , KPIInput input , StreamWriter sw )
2023-03-22 13:48:13 +00:00
{
2023-10-13 14:37:50 +00:00
KPIOutput kpio ;
2023-10-16 11:55:12 +00:00
KPIOutput kpioPrevious = new KPIOutput ( ) ; //creates a new empty
2023-10-13 14:37:50 +00:00
2023-03-22 13:48:13 +00:00
string downloadFolder = input . DownloadFolder ;
if ( string . IsNullOrEmpty ( downloadFolder ) ) {
downloadFolder = "Downloads" ;
}
if ( ! Directory . Exists ( downloadFolder ) )
Directory . CreateDirectory ( downloadFolder ) ;
// !!specify if you are using an already created cropfield:
2023-10-13 14:37:50 +00:00
bool useExistingCropfieldWithChildren = input . UseExistingCropfieldWithChildren ;
int cropYear = input . CropYear ;
string fieldName = input . fieldName ;
string fieldGeom = input . GeometryJson . ToString ( Formatting . None ) ;
2023-03-22 13:48:13 +00:00
2023-10-13 14:37:50 +00:00
//Settings
2023-03-22 13:48:13 +00:00
string settingsfile = $"Settings_{fieldName}.json" ;
LoadSettings ( settingsfile ) ;
var uploadedRoot = roots . SingleOrDefault ( r = > r . Name = = "USER_IN" ) ;
if ( uploadedRoot = = null )
{
_logger . LogError ( "Could not find a needed root item" ) ;
return ;
}
var myDriveRoot = roots . SingleOrDefault ( r = > r . Name = = "USER_FILES" ) ;
if ( myDriveRoot = = null )
{
_logger . LogError ( "Could not find a needed root item" ) ;
return ;
}
2023-04-25 14:14:43 +00:00
// Use already created cropfield or create new one, added a Data input, with field specific data for the KPI calculation
2023-03-22 13:48:13 +00:00
Item cropfieldItem ;
2023-10-13 14:37:50 +00:00
//1 useExistingCropfieldWithChildren = false -> create new
//2 useExistingCropfieldWithChildren = true && input.CropfieldItemCode = "" or absent -> read from settings json
//3 useExistingCropfieldWithChildren = true && input.CropfieldItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
if ( useExistingCropfieldWithChildren = = false )
2023-03-22 13:48:13 +00:00
{
2023-06-27 12:08:27 +00:00
_logger . LogInformation ( "Creating cropfield, writting to settings file" ) ;
2023-03-22 13:48:13 +00:00
cropfieldItem = await _generalService . CreateCropfieldItemAsync ( myDriveRoot . Code ,
2023-10-13 14:37:50 +00:00
$"{fieldName}" , cropYear , input . GeometryJson . ToString ( Formatting . None ) , input . DataCropfield . ToString ( Formatting . None ) ) ;
2023-03-22 13:48:13 +00:00
_settings . CropfieldItemCode = cropfieldItem . Code ;
SaveSettings ( settingsfile ) ;
}
2023-06-27 12:08:27 +00:00
else if ( string . IsNullOrEmpty ( input . CropfieldItemCode ) )
2023-03-22 13:48:13 +00:00
{
2023-10-04 15:21:05 +00:00
_logger . LogInformation ( "reading CropfieldItemCode from settings file" ) ;
2023-03-22 13:48:13 +00:00
cropfieldItem = await _farmmapsApiService . GetItemAsync ( _settings . CropfieldItemCode ) ;
}
2023-06-27 12:08:27 +00:00
else
{
2023-10-04 15:21:05 +00:00
_logger . LogInformation ( "reading CropfieldItemCode from KPIinput.json" ) ;
2023-06-27 12:08:27 +00:00
cropfieldItem = await _farmmapsApiService . GetItemAsync ( input . CropfieldItemCode ) ;
2023-10-13 14:37:50 +00:00
SaveSettings ( settingsfile ) ;
2023-06-27 12:08:27 +00:00
}
2023-03-22 13:48:13 +00:00
2023-10-04 15:21:05 +00:00
// Use already created croprecording or create new one, added a Data input, with field specific data for the KPI calculation
Item crprecItem ;
2023-10-13 14:37:50 +00:00
//1 useExistingCropfieldWithChildren = false -> create new
//2 useExistingCropfieldWithChildren = true && input.CropRecordingItemCode = "" or absent -> read from settings json
//3 useExistingCropfieldWithChildren = true && input.CropRecordingItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
if ( useExistingCropfieldWithChildren = = false )
2023-10-04 15:21:05 +00:00
{
_logger . LogInformation ( "RunCropRecordingTask ..." ) ;
crprecItem = await _generalService . RunCropRecordingTask ( cropfieldItem ) ;
_settings . CropRecordingItemCode = crprecItem . Code ;
SaveSettings ( settingsfile ) ;
}
else if ( string . IsNullOrEmpty ( input . CropfieldItemCode ) )
{
_logger . LogInformation ( "reading CropRecordingItemCode from settings file" ) ;
crprecItem = await _farmmapsApiService . GetItemAsync ( _settings . CropRecordingItemCode ) ;
}
else
2023-03-22 13:48:13 +00:00
{
2023-10-04 15:21:05 +00:00
_logger . LogInformation ( "reading CropRecordingItemCode from KPIinput.json" ) ;
crprecItem = await _farmmapsApiService . GetItemAsync ( input . CropRecordingItemCode ) ;
2023-10-13 14:37:50 +00:00
SaveSettings ( settingsfile ) ;
2023-10-04 15:21:05 +00:00
}
2023-03-22 13:48:13 +00:00
2023-10-13 14:37:50 +00:00
// Use already created operations or create new one,s added a Data input, with field specific data for the KPI calculation
List < Item > crpOperationItems = new List < Item > { } ;
List < string > crpOperationItemCodes = new List < string > { } ;
2023-10-04 15:21:05 +00:00
Item crpOperationItem ;
2023-10-13 14:37:50 +00:00
string dataOperation ;
string codeOperation ;
//1 useExistingCropfieldWithChildren = false -> create new
//2 useExistingCropfieldWithChildren = true && input.OperationItemCode = "" or absent -> read from settings json
//3 useExistingCropfieldWithChildren = true && input.OperationItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
if ( useExistingCropfieldWithChildren = = false )
2023-10-04 15:21:05 +00:00
{
2023-10-13 14:37:50 +00:00
for ( int i = 0 ; i < input . DataOperations . Length ; i + + )
{
dataOperation = input . DataOperations [ i ] . ToString ( Formatting . None ) ;
dynamic data = JObject . Parse ( dataOperation ) ;
_logger . LogInformation ( $"CreateOperationItemAsync ... for operation {i}: '{data.name}', {data.n} kg N/ha, on date '{data.from}'" ) ;
crpOperationItem = await _generalService . CreateOperationItemAsync ( crprecItem . Code , cropYear , fieldGeom , dataOperation ) ;
crpOperationItems . Add ( crpOperationItem ) ;
crpOperationItemCodes . Add ( crpOperationItem . Code ) ;
}
_settings . OperationItemCodes = crpOperationItemCodes . ToArray ( ) ;
2023-10-04 15:21:05 +00:00
SaveSettings ( settingsfile ) ;
}
else if ( string . IsNullOrEmpty ( input . CropfieldItemCode ) )
{
_logger . LogInformation ( "reading OperationItemCode from settings file" ) ;
2023-10-13 14:37:50 +00:00
for ( int i = 0 ; i < _settings . OperationItemCodes . Length ; i + + )
{
codeOperation = _settings . OperationItemCodes [ i ] ;
crpOperationItem = await _farmmapsApiService . GetItemAsync ( codeOperation ) ;
crpOperationItems . Add ( crpOperationItem ) ;
crpOperationItemCodes . Add ( crpOperationItem . Code ) ;
}
2023-10-04 15:21:05 +00:00
}
else
{
2023-10-13 14:37:50 +00:00
_logger . LogInformation ( "reading OperationItemCodes from KPIinput.json" ) ;
for ( int i = 0 ; i < input . OperationItemCodes . Length ; i + + )
{
codeOperation = input . OperationItemCodes [ i ] ;
crpOperationItem = await _farmmapsApiService . GetItemAsync ( codeOperation ) ;
crpOperationItems . Add ( crpOperationItem ) ;
crpOperationItemCodes . Add ( crpOperationItem . Code ) ;
}
_settings . OperationItemCodes = crpOperationItemCodes . ToArray ( ) ;
SaveSettings ( settingsfile ) ;
2023-10-04 15:21:05 +00:00
}
2023-03-22 13:48:13 +00:00
2023-10-04 15:21:05 +00:00
// The cropfieldCharacteristicItem is used to enter crop yields
// So once we have added an operation for fertilizer application and a crop yield, then KPIapp can calculate
// Nutrient balance.
// Use already created cropfieldCharacteristicItem or create new one, added a Data input, with field specific data for the KPI calculation
Item cropfieldCharacteristicItem ;
2023-10-13 14:37:50 +00:00
//1 useExistingCropfieldWithChildren = false -> create new
//2 useExistingCropfieldWithChildren = true && input.CropfieldCharacteristicItemCode = "" or absent -> read from settings json
//3 useExistingCropfieldWithChildren = true && input.CropfieldCharacteristicItemCode like "deb48a74c5b54299bb852f17288010e9" in KPIinput -> use this one
if ( useExistingCropfieldWithChildren = = false )
2023-10-04 15:21:05 +00:00
{
_logger . LogInformation ( "CreateCropfieldCharacteristicItemAsync ..." ) ;
2023-10-13 14:37:50 +00:00
cropfieldCharacteristicItem = await _generalService . CreateCropfieldCharacteristicItemAsync ( cropfieldItem . Code , cropYear , fieldGeom , input . DataCropfieldCharacteristic . ToString ( Formatting . None ) ) ;
2023-10-04 15:21:05 +00:00
_settings . CropfieldCharacteristicItemCode = cropfieldCharacteristicItem . Code ;
SaveSettings ( settingsfile ) ;
}
2023-10-13 14:37:50 +00:00
else if ( string . IsNullOrEmpty ( input . CropfieldItemCode ) )
2023-10-04 15:21:05 +00:00
{
_logger . LogInformation ( "reading OperationItemCode from settings file" ) ;
cropfieldCharacteristicItem = await _farmmapsApiService . GetItemAsync ( _settings . CropfieldCharacteristicItemCode ) ;
2023-03-22 13:48:13 +00:00
}
else
{
2023-10-04 15:21:05 +00:00
_logger . LogInformation ( "reading CropfieldCharacteristicItemCode from KPIinput.json" ) ;
cropfieldCharacteristicItem = await _farmmapsApiService . GetItemAsync ( input . CropfieldCharacteristicItemCode ) ;
2023-10-13 14:37:50 +00:00
SaveSettings ( settingsfile ) ;
2023-03-22 13:48:13 +00:00
}
2023-10-13 14:37:50 +00:00
// Inspect the children and grandchildren. If all is well, cropfield will have:
// one crprec, with 0-many operations as children. And the Data of an operation will have specification of how much fertilizer was applied
// one edicrop.characteristic (with yield in the data)
2023-10-04 15:21:05 +00:00
var cropfieldChildren = await _farmmapsApiService . GetItemChildrenAsync ( cropfieldItem . Code ) ;
var crprecChildren = await _farmmapsApiService . GetItemChildrenAsync ( crprecItem . Code ) ;
//Now get the KPIs for this cropfield, mounted with operations & cropyield
2023-06-27 10:58:36 +00:00
// Get KPI data for saving it in a file, here the generalsedrvice is called to get the KPI data
2023-10-11 15:00:25 +00:00
_logger . LogInformation ( $"GetKpiItemsForCropField('{cropfieldItem.Code}')" ) ;
2023-10-13 14:37:50 +00:00
List < Item > KPIItems = await _generalService . GetKpiItemsForCropField ( cropfieldItem ) ;
_logger . LogInformation ( $"Found {KPIItems.Count} KPI items" ) ;
2023-04-25 10:00:08 +00:00
2023-10-04 15:21:05 +00:00
//Download KPI's into a json output file for this specific cropfield (with unique cropfieldItem.Code)
2023-10-13 14:37:50 +00:00
string KPIItemPathJson = Path . Combine ( downloadFolder , $"KPIItems_{cropfieldItem.Code}.json" ) ;
2023-04-25 10:00:08 +00:00
var count = 0 ;
2023-10-13 14:37:50 +00:00
foreach ( Item item in KPIItems )
2023-03-22 13:48:13 +00:00
{
2023-10-13 14:37:50 +00:00
//Console.WriteLine($"KPI item #{count}: {item.Name}");
File . AppendAllText ( KPIItemPathJson , item . Data + Environment . NewLine ) ;
2023-04-25 10:00:08 +00:00
count + + ;
2023-03-22 13:48:13 +00:00
}
2023-10-13 14:37:50 +00:00
_logger . LogInformation ( $"Downloaded file {KPIItemPathJson}" ) ;
2023-04-25 10:00:08 +00:00
2023-10-13 14:37:50 +00:00
//Write to the csv file that collects all KPI's for all the crop fields
List < string > dataList ;
foreach ( Item item in KPIItems )
{
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
2023-10-24 11:06:36 +00:00
if ( kpio . id ! = null )
2023-10-13 14:37:50 +00:00
{
2023-10-24 11:06:36 +00:00
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 ) ) ;
}
2023-10-13 14:37:50 +00:00
}
kpioPrevious = kpio ;
}
2023-10-16 13:34:07 +00:00
//Total N applied
double totalNapplied = 0.0 ;
double operationNapplied ;
JObject opData ;
for ( int i = 0 ; i < crpOperationItemCodes . Count ; i + + )
{
codeOperation = crpOperationItemCodes [ i ] ;
crpOperationItem = await _farmmapsApiService . GetItemAsync ( codeOperation ) ;
2023-10-24 11:06:36 +00:00
dynamic data = JObject . Parse ( crpOperationItem . Data . ToString ( Formatting . None ) ) ;
if ( data . n ! = null )
{
operationNapplied = crpOperationItem . Data . GetValue ( "n" ) . ToObject < double > ( ) ;
totalNapplied = totalNapplied + operationNapplied ;
}
2023-10-16 13:34:07 +00:00
}
//Also add totalNapplied to the csv
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
dataList . Add ( kpioPrevious . parentName ) ;
dataList . Add ( kpioPrevious . data . area ) ;
dataList . Add ( kpioPrevious . data . cropTypeCode ) ;
dataList . Add ( kpioPrevious . data . cropTypeName ) ;
dataList . Add ( "" ) ;
dataList . Add ( "totalNapplied" ) ;
dataList . Add ( totalNapplied . ToString ( ) ) ;
dataList . Add ( kpioPrevious . unit ) ;
dataList . Add ( "" ) ;
dataList . Add ( "" ) ;
sw . WriteLine ( string . Join ( "," , dataList ) ) ;
2023-10-13 14:37:50 +00:00
//Clean up. Only newly created fields
//Look up instruction in Swagger / api / v1 / items /{ code}
if ( useExistingCropfieldWithChildren = = false & & input . DeleteNewlyCreatedAfterCalc = = true )
{
// Tested and found that if I delete the parent (cropfieldItem) then
// automatically deletes children (crprecItem, cropfieldCharacteristicItem, KPIcontainer)
// and automatically deletes grand children (crprecItem: operations, KPIcontainer: KPI items)
await _farmmapsApiService . DeleteItemAsync ( cropfieldItem . Code ) ;
//Do we need to wait here for the task to be completed? Or does it continue in the background?
//await Task.Delay(60000); // wait 60 secs for task to be completed
//Before the GetItem you will see cropfieldItem, crprecItem etc exist (with data).
//After the GetItem these cropfieldItem, crprecItem etc are null which means they have been succesfully deleted.
//// Check if below works, i.e. is item really deleted?
//cropfieldItem = await _farmmapsApiService.GetItemAsync(cropfieldItem.Code);
//// Check if croprecording has also been deleted. If not also delete it
//crprecItem = await _farmmapsApiService.GetItemAsync(crprecItem.Code);
//// Check if 1 operation has also been deleted. If not also delete it
//crpOperationItem = await _farmmapsApiService.GetItemAsync(crpOperationItemCodes[0]);
//// Check if 1 cropfieldCharacteristicItem has also been deleted. If not also delete it
//cropfieldCharacteristicItem = await _farmmapsApiService.GetItemAsync(cropfieldCharacteristicItem.Code);
}
2023-03-22 13:48:13 +00:00
}
// Functions to save previously created cropfields
private void LoadSettings ( string file )
{
if ( File . Exists ( file ) )
{
var jsonText = File . ReadAllText ( file ) ;
_settings = JsonConvert . DeserializeObject < Settings > ( jsonText ) ;
}
else
{
_settings = new Settings ( ) ;
}
}
private void SaveSettings ( string file )
{
if ( _settings = = null )
return ;
var json = JsonConvert . SerializeObject ( _settings ) ;
File . WriteAllText ( file , json ) ;
}
private void SaveInfo ( string file )
{
if ( _settings = = null )
return ;
var json = JsonConvert . SerializeObject ( _settings ) ;
File . WriteAllText ( file , json ) ;
}
}
}