eta_utility.connectors.base_classes module
Base classes for the connectors
- class eta_utility.connectors.base_classes.SubscriptionHandler(write_interval: TimeStep = 1)[source]
Bases:
ABC
Subscription handlers do stuff to subscribed data points after they are received. Every handler must have a push method which can be called when data is received.
- Parameters:
write_interval – Interval for writing data to csv file.
- abstract push(node: Node, value: Any, timestamp: datetime | None = None) None [source]
Receive data from a subscription. This should contain the node that was requested, a value and a timestamp when data was received. If the timestamp is not provided, current time will be used.
- Parameters:
node – Node object the data belongs to.
value – Value of the data.
timestamp – Timestamp of receiving the data.
- class eta_utility.connectors.base_classes.Connection(url: str, usr: str | None = None, pwd: str | None = None, *, nodes: Nodes[N] | None = None)[source]
-
Base class with a common interface for all connection objects
The URL may contain the username and password (schema://username:password@hostname:port/path). In this case, the parameters usr and pwd are not required. The keyword parameters of the function will take precedence over username and password configured in the url.
- Parameters:
url – URL of the server to connect to.
usr – Username for login to server.
pwd – Password for login to server.
nodes – List of nodes to select as a standard case.
- selected_nodes
Preselected nodes which will be used for reading and writing, if no other nodes are specified
- classmethod from_node(node: Nodes[Node], usr: str | None = None, pwd: str | None = None, **kwargs: Any) Connection [source]
- Return a single connection for nodes with the same url netloc.
Initialize the connection object from a node object. When a list of Node objects is provided, from_node checks if all nodes match the same connection; it throws an error if they don’t. A node matches a connection if it has the same url netloc.
- Parameters:
node – Node to initialize from.
kwargs – Other arguments are ignored.
- Raises:
ValueError: if not all nodes match the same connection.
- Returns:
Connection object
- classmethod from_nodes(nodes: Nodes[Node], **kwargs: Any) dict[str, Connection[Node]] [source]
- Returns a dictionary of connections for nodes with the same url netloc.
This method handles different Connections, unlike from_node(). The keys of the dictionary are the netlocs of the nodes and each connection contains the nodes with the same netloc. (Uses from_node to initialize connections from nodes.)
- Parameters:
nodes – List of nodes to initialize from.
kwargs – Other arguments are ignored.
- Returns:
Dictionary of Connection objects with the netloc as key.
- abstract read(nodes: Nodes[N] | None = None) pd.DataFrame [source]
Read data from nodes
- Parameters:
nodes – List of nodes to read from.
- Returns:
Pandas DataFrame with resulting values.
- abstract write(values: Mapping[N, Any]) None [source]
Write data to a list of nodes
- Parameters:
values – Dictionary of nodes and data to write {node: value}.
- abstract subscribe(handler: SubscriptionHandler, nodes: Nodes[N] | None = None, interval: TimeStep = 1) None [source]
Subscribe to nodes and call handler when new data is available.
- Parameters:
nodes – Identifiers for the nodes to subscribe to.
handler – Function to be called upon receiving new values, must accept attributes: node, val.
interval – Interval for receiving new data. Interpreted as seconds when given as integer.
- class eta_utility.connectors.base_classes.BaseConnection(url: str, usr: str | None = None, pwd: str | None = None, *, nodes: Nodes[N] | None = None)[source]
Bases:
Connection
[N
],ABC
Deprecated BaseConnection class. Use Connection instead.
- class eta_utility.connectors.base_classes.SeriesConnection(url: str, usr: str | None = None, pwd: str | None = None, *, nodes: Nodes[N] | None = None)[source]
Bases:
Connection
[N
],ABC
Connection object for protocols with the ability to provide access to timeseries data.
- Parameters:
url – URL of the server to connect to.
- abstract read_series(from_time: datetime, to_time: datetime, nodes: Nodes[N] | None = None, interval: TimeStep = 1, **kwargs: Any) pd.DataFrame [source]
Read time series data from the connection, within a specified time interval (from_time until to_time).
- Parameters:
nodes – List of nodes to read values from.
from_time – Starting time to begin reading (included in output).
to_time – Time to stop reading at (not included in output).
interval – interval between time steps. It is interpreted as seconds if given as integer.
kwargs – additional argument list, to be defined by subclasses.
- Returns:
pandas.DataFrame containing the data read from the connection.
- subscribe_series(handler: SubscriptionHandler, req_interval: TimeStep, offset: TimeStep | None = None, nodes: Nodes[N] | None = None, interval: TimeStep = 1, data_interval: TimeStep = 1, **kwargs: Any) None [source]
Subscribe to nodes and call handler when new data is available. This will always return a series of values. If nodes with different intervals should be subscribed, multiple connection objects are needed.
- Parameters:
handler – SubscriptionHandler object with a push method that accepts node, value pairs.
req_interval – Duration covered by requested data (time interval). Interpreted as seconds if given as int
offset – Offset from datetime.now from which to start requesting data (time interval). Interpreted as seconds if given as int. Use negative values to go to past timestamps.
data_interval – Time interval between values in returned data. Interpreted as seconds if given as int.
interval – interval (between requests) for receiving new data. It is interpreted as seconds when given as an integer.
nodes – Identifiers for the nodes to subscribe to.
kwargs – Any additional arguments required by subclasses.