From 3cd459a6832aca96520e51b829dc6e32455e155c Mon Sep 17 00:00:00 2001 From: Ivan <39461389+AlcibiadesCleinias@users.noreply.github.com> Date: Fri, 23 Jul 2021 19:41:32 +0300 Subject: [PATCH] add timeout option (#1) --- .gitignore | 3 +++ PIconnect/PI.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/.gitignore b/.gitignore index 5c01fda1..6d5946a9 100644 --- a/.gitignore +++ b/.gitignore @@ -74,3 +74,6 @@ target/ # MyPy cache .mypy_cache/ + +# IDE confs +.idea diff --git a/PIconnect/PI.py b/PIconnect/PI.py index edd6e05d..68fa8efa 100644 --- a/PIconnect/PI.py +++ b/PIconnect/PI.py @@ -58,12 +58,18 @@ from PIconnect.PIData import PISeries, PISeriesContainer from PIconnect.PIConsts import AuthenticationMode +from System import TimeSpan + class PIServer(object): # pylint: disable=useless-object-inheritance """PIServer is a connection to an OSIsoft PI Server Args: server (str, optional): Name of the server to connect to, defaults to None + username (str, optional): can be used only with password as well + password (str, optional): -//- + todo: domain, auth + timeout (int, optional): the maximum seconds an operation can take .. note:: If the specified `server` is unknown a warning is thrown and the connection @@ -85,6 +91,7 @@ def __init__( password=None, domain=None, authentication_mode=AuthenticationMode.PI_USER_AUTHENTICATION, + timeout=None, ): if server and server not in self.servers: message = 'Server "{server}" not found, using the default server.' @@ -108,8 +115,12 @@ def __init__( self._credentials = (NetworkCredential(*cred), int(authentication_mode)) else: self._credentials = None + self.connection = self.servers.get(server, self.default_server) + if timeout: + self.connection.ConnectionInfo.OperationTimeOut = TimeSpan(0, 0, timeout) # hour, min, sec + def __enter__(self): if self._credentials: self.connection.Connect(*self._credentials)