-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example notebook for authentication approach.
- Loading branch information
Showing
4 changed files
with
396 additions
and
685 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,292 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import Libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Import the required packages and libraries\n", | ||
"import datetime\n", | ||
"import os\n", | ||
"from dotenv import load_dotenv \n", | ||
"from pathlib import Path" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import Scripts" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pyprediktormapclient.opc_ua import OPC_UA\n", | ||
"from pyprediktormapclient.model_index import ModelIndex\n", | ||
"from pyprediktormapclient.auth_client import AUTH_CLIENT\n", | ||
"from pyprediktormapclient.analytics_helper import AnalyticsHelper\n", | ||
"from pyprediktormapclient.shared import *" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import Envrionment Variables" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Consider obtaining the envrionment variables from .env file if you are running this locally from source.\n", | ||
"dotenv_path = Path(\"../.env\")\n", | ||
"load_dotenv(dotenv_path=dotenv_path)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"username = os.environ[\"USERNAME\"]\n", | ||
"password = os.environ[\"PASSWORD\"]\n", | ||
"opcua_rest_url = os.environ[\"OPC_UA_REST_URL\"]\n", | ||
"opcua_server_url = os.environ[\"OPC_UA_SERVER_URL\"]\n", | ||
"model_index_url = os.environ[\"MODEL_INDEX_URL\"]\n", | ||
"ory_url = os.environ[\"ORY_URL\"]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Getting ory bearer token\n", | ||
"auth_client = AUTH_CLIENT(rest_url=ory_url, username=username, password=password)\n", | ||
"auth_client.request_new_ory_token()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Download data from modelindex api" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Connecting to ModelIndex APIs \n", | ||
"model_data = ModelIndex(url=model_index_url, auth_client=auth_client, session=auth_client.session)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Listed sites on the model index api server\n", | ||
"namespaces = model_data.get_namespace_array()\n", | ||
"namespaces" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Types of Objects\n", | ||
"object_types_json = model_data.get_object_types()\n", | ||
"object_types = AnalyticsHelper(object_types_json)\n", | ||
"object_types.dataframe" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Unique types of Objects\n", | ||
"object_types_unique = object_types.dataframe[[\"Id\", \"Name\"]].drop_duplicates()\n", | ||
"object_types_unique" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# To get typeId by type name of an object\n", | ||
"object_type_id = model_data.get_object_type_id_from_name(\"SiteType\")\n", | ||
"object_type_id" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# To get the objects of a type\n", | ||
"sites_json = model_data.get_objects_of_type(\"SiteType\")\n", | ||
"\n", | ||
"# Send the returned JSON into a normalizer to get Id, Type, Name, Props and Vars as columns\n", | ||
"sites = AnalyticsHelper(sites_json)\n", | ||
"sites.list_of_names()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Analytics helper\n", | ||
"sites.variables_as_dataframe()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"sites.list_of_ids()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Selecting the single site\n", | ||
"site_id = sites.list_of_ids()[1]\n", | ||
"site_id" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Get all stringsets for one park\n", | ||
"string_sets_for_first_park_as_json = model_data.get_object_descendants(\n", | ||
" \"StringSetType\", [site_id], \"PV_Assets\"\n", | ||
")\n", | ||
"string_sets_for_first_park = AnalyticsHelper(string_sets_for_first_park_as_json)\n", | ||
"string_sets_for_first_park.dataframe" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Ancestors of an object type, get all trackers that are ancestor of the parks string sets\n", | ||
"\n", | ||
"trackers_as_json = model_data.get_object_ancestors(\n", | ||
" \"TrackerType\", string_sets_for_first_park.list_of_ids(), \"PV_Serves\"\n", | ||
")\n", | ||
"trackers = AnalyticsHelper(trackers_as_json)\n", | ||
"trackers.variables_as_dataframe()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Download data from the opc ua api" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"namespace_list = object_types.namespaces_as_list(namespaces)\n", | ||
"\n", | ||
"# Initating the OPC UA API with a fixed namespace list\n", | ||
"opc_data = OPC_UA(rest_url=opcua_rest_url, opcua_url=opcua_server_url, namespaces=namespace_list, auth_client=auth_client)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Live value data of trackers\n", | ||
"live_value = opc_data.get_values(\n", | ||
" trackers.variables_as_list([\"AngleMeasured\", \"AngleSetpoint\"])\n", | ||
")\n", | ||
"live_value" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Historic value data of trackers, 1 days worth of data 30 days ago\n", | ||
"one_day_historic_tracker_data = opc_data.get_historical_aggregated_values(\n", | ||
" start_time=(datetime.datetime.now() - datetime.timedelta(30)),\n", | ||
" end_time=(datetime.datetime.now() - datetime.timedelta(29)),\n", | ||
" pro_interval=3600000,\n", | ||
" agg_name=\"Average\",\n", | ||
" variable_list=trackers.variables_as_list([\"AngleMeasured\"]),\n", | ||
")\n", | ||
"one_day_historic_tracker_data" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".venv_auth", | ||
"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.11.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.