create testbed file

This commit is contained in:
2024-09-28 13:37:12 +02:00
parent 73c7d813cb
commit 8fd17d00d0

84
testbed.py Normal file
View File

@@ -0,0 +1,84 @@
from farmOS import farmOS
hostname = "farmos.wagframe.duckdns.org"
username = "matze"
password = "m6ChEHx5gMqgctr8dpb3fhATZbQS8hv4"
# Create the client.
farm_client = farmOS(
hostname=hostname,
client_id = "jupyter", # Optional. The default oauth client_id "farm" is enabled on all farmOS servers.
)
import json
# Authorize the client, save the token.
# A scope can be specified, but will default to the default scope set when initializing the client.
token = farm_client.authorize(username, password)
import pandas as pd
def get_related_logs_of_type(asset, type):
response = farm_client.resource.get('log', type)
ret = []
for log in response['data']:
assets = log['relationships']['asset']['data']
for asset in assets:
if asset['id'] == id:
ret.append(log)
return ret
def get_related_quantities_of_measure(log, measure):
quantities = log['relationships']['quantity']['data']
ret = []
for quant_ref in quantities:
quant_id = quant_ref['id']
quant = farm_client.resource.get_id('quantity', 'standard', quant_id)
if quant['data']['attributes']['measure'] == measure:
ret.append(quant)
return ret
# Beispiel für eine API-Abfrage mit farm_client
response = farm_client.resource.get('log', 'harvest')
logs = []
for item in response['data']:
# plant name und fläche holen
assets = item['relationships']['asset']['data']
plant_name = ''
plant_area = 0.0
for asset in assets:
if asset['type'] == 'asset--plant':
plant_id = asset['id']
seedings = get_related_logs_of_type(plant_id, 'seeding')
for seeding in seedings:
area_quants = get_related_quantities_of_measure(seeding, 'area')
for area_quant in area_quants:
plant_area += float(area_quant['data']['attributes']['value']['decimal'])
plant = farm_client.asset.get_id('plant', plant_id)
plant_name = plant['data']['attributes']['name']
# log aufbauen
log = {
'ID': item['id'],
'Name': item['attributes']['name'],
'Timestamp': item['attributes']['timestamp'],
'Plant': plant_name,
'Area': plant_area,
'Status': item['attributes']['status'],
'Revision Created': item['attributes']['revision_created'],
'Link': item['links']['self']['href']
}
logs.append(log)
# Erstelle ein DataFrame aus den Harvest Logs
df = pd.DataFrame(logs)
# Zeige die Tabelle an
print(df)
# Speichere die Tabelle in einer CSV-Datei
csv_file_path = 'resources/harvest_logs.csv'
df.to_csv(csv_file_path, index=False)
print(f"Die Tabelle wurde erfolgreich in der Datei '{csv_file_path}' gespeichert.")