Files
farmos.py-wagner/Filter.ipynb
2024-09-28 13:19:29 +02:00

198 lines
8.4 KiB
Plaintext
Executable File

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "2257ee1d-bcc1-483c-bb74-106e52aa7b46",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ID Name \\\n",
"0 ae417c7e-e1f2-4184-b6f9-e5c8027a2fc4 Harvest log 40 \n",
"1 f873e803-f20a-460e-8c2c-d10835d6b2c7 Harvest log 42 \n",
"2 97f1f184-20ce-44da-bc70-134e3917a701 Harvest log 43 \n",
"3 fa5c7900-ef46-44fb-a160-2594ca00d65b Harvest log 44 \n",
"4 fa014b11-25ef-4a49-9406-0f3900952a2c Harvest log 46 \n",
"5 05fd5f47-15df-4be0-8197-a4927ab87a88 test log \n",
"6 50a16f30-79f6-4c3b-9815-3af6cf107cf3 Harvest log 48 \n",
"7 7159b3fe-bf3b-4fcc-9438-1ed0b1971937 Harvest log 49 \n",
"8 16a3cdd0-a9fb-403a-8e0b-1f390cefe0cd Harvest log 64 \n",
"9 c39d4bc4-4bcb-408c-b2b6-018901dd7e74 Harvest log 65 \n",
"10 ff8a1498-8057-4ede-be2a-a82b88f893ba Harvest log 67 \n",
"11 e8c4b960-6947-426c-8d07-4ab4a1f11578 Harvest log 69 \n",
"12 a1c2eae4-5543-40ee-98b5-6ca92fc54b5a Harvest log 71 \n",
"13 2058bca2-537b-49cb-bcc0-c33496384c2b Harvest log 73 \n",
"\n",
" Timestamp Plant Area Status \\\n",
"0 2024-09-23T09:13:35+00:00 23/24 Grosser Acker Weizen Plant 0.0 done \n",
"1 2024-09-24T14:13:40+00:00 0.0 done \n",
"2 2024-09-24T17:30:47+00:00 0.0 done \n",
"3 2024-09-24T17:33:27+00:00 0.0 done \n",
"4 2024-09-24T17:47:52+00:00 0.0 done \n",
"5 2024-09-24T17:48:49+00:00 0.0 done \n",
"6 2024-09-24T18:10:27+00:00 23/24 Grosser Acker Weizen Plant 0.0 done \n",
"7 2024-09-24T18:15:59+00:00 23/24 Grosser Acker Weizen Plant 0.0 done \n",
"8 2024-09-27T13:52:31+00:00 23/24 Grosser Acker Weizen Plant 0.0 done \n",
"9 2024-09-27T13:52:34+00:00 23/24 Grosser Acker Weizen Plant 0.0 done \n",
"10 2024-09-27T13:56:30+00:00 23/24 Grosser Acker Raps Plant 0.0 done \n",
"11 2024-09-27T13:57:00+00:00 23/24 Grosser Acker Raps Plant 0.0 done \n",
"12 2024-09-27T13:58:03+00:00 23/24 Grosser Acker Raps Plant 0.0 done \n",
"13 2024-09-27T15:29:38+00:00 23/24 Loehle Mais Plant 0.0 done \n",
"\n",
" Revision Created \\\n",
"0 2024-09-23T09:15:08+00:00 \n",
"1 2024-09-24T14:14:12+00:00 \n",
"2 2024-09-24T17:30:52+00:00 \n",
"3 2024-09-24T17:35:08+00:00 \n",
"4 2024-09-24T17:48:16+00:00 \n",
"5 2024-09-24T17:49:12+00:00 \n",
"6 2024-09-24T18:10:47+00:00 \n",
"7 2024-09-25T09:34:35+00:00 \n",
"8 2024-09-27T13:52:33+00:00 \n",
"9 2024-09-27T13:53:09+00:00 \n",
"10 2024-09-27T13:56:59+00:00 \n",
"11 2024-09-27T13:57:37+00:00 \n",
"12 2024-09-27T13:58:36+00:00 \n",
"13 2024-09-27T15:30:13+00:00 \n",
"\n",
" Link \n",
"0 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"1 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"2 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"3 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"4 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"5 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"6 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"7 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"8 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"9 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"10 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"11 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"12 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"13 https://farmos.wagframe.duckdns.org/api/log/ha... \n",
"Die Tabelle wurde erfolgreich in der Datei 'harvest_logs.csv' gespeichert.\n"
]
}
],
"source": [
"from farmOS import farmOS\n",
"\n",
"hostname = \"farmos.wagframe.duckdns.org\"\n",
"username = \"matze\"\n",
"password = \"m6ChEHx5gMqgctr8dpb3fhATZbQS8hv4\"\n",
"\n",
"# Create the client.\n",
"farm_client = farmOS(\n",
" hostname=hostname,\n",
" client_id = \"jupyter\", # Optional. The default oauth client_id \"farm\" is enabled on all farmOS servers.\n",
")\n",
"import json\n",
"# Authorize the client, save the token.\n",
"# A scope can be specified, but will default to the default scope set when initializing the client\n",
"token = farm_client.authorize(username, password)\n",
"\n",
"import pandas as pd\n",
"\n",
"\n",
"def get_related_logs_of_type(asset, type): \n",
" response = farm_client.resource.get('log', type)\n",
" ret = []\n",
" for log in response['data']:\n",
" assets = log['relationships']['asset']['data']\n",
" for asset in assets:\n",
" if asset['id'] == id:\n",
" ret.append(log)\n",
" return ret\n",
"\n",
"def get_related_quantities_of_measure(log, measure):\n",
" quantities = log['relationships']['quantity']['data']\n",
" ret = []\n",
" for quant_ref in quantities:\n",
" quant_id = quant_ref['id']\n",
" quant = farm_client.resource.get_id('quantity', 'standard', quant_id)\n",
" if quant['data']['attributes']['measure'] == measure:\n",
" ret.append(quant)\n",
" return ret\n",
"\n",
"# Beispiel für eine API-Abfrage mit farm_client\n",
"response = farm_client.resource.get('log', 'harvest')\n",
"\n",
"logs = []\n",
"for item in response['data']:\n",
" # plant name und fläche holen\n",
" assets = item['relationships']['asset']['data']\n",
" plant_name = ''\n",
" plant_area = 0.0\n",
" for asset in assets:\n",
" if asset['type'] == 'asset--plant': \n",
" plant_id = asset['id']\n",
" seedings = get_related_logs_of_type(plant_id, 'seeding')\n",
" for seeding in seedings:\n",
" area_quants = get_related_quantities_of_measure(seeding, 'area')\n",
" for area_quant in area_quants:\n",
" plant_area += float(area_quant['data']['attributes']['value']['decimal'])\n",
" plant = farm_client.asset.get_id('plant', plant_id)\n",
" plant_name = plant['data']['attributes']['name']\n",
" \n",
" \n",
" # log aufbauen\n",
" log = {\n",
" 'ID': item['id'],\n",
" 'Name': item['attributes']['name'],\n",
" 'Timestamp': item['attributes']['timestamp'],\n",
" 'Plant': plant_name,\n",
" 'Area': plant_area,\n",
" 'Status': item['attributes']['status'],\n",
" 'Revision Created': item['attributes']['revision_created'],\n",
" 'Link': item['links']['self']['href']\n",
" }\n",
" logs.append(log)\n",
"\n",
"# Erstelle ein DataFrame aus den Harvest Logs\n",
"df = pd.DataFrame(logs)\n",
"\n",
"# Zeige die Tabelle an\n",
"print(df)\n",
"\n",
"# Speichere die Tabelle in einer CSV-Datei\n",
"csv_file_path = 'harvest_logs.csv'\n",
"df.to_csv(csv_file_path, index=False)\n",
"\n",
"print(f\"Die Tabelle wurde erfolgreich in der Datei '{csv_file_path}' gespeichert.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ddcc922-e7c4-4cb8-8414-11c49e11fc6d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}