def default_server(self): if self._default_server is _NOTHING: self._default_server = None if AF.PISystems().DefaultPISystem: self._default_server = self.servers[ AF.PISystems().DefaultPISystem.Name] elif len(self.servers) > 0: self._default_server = self.servers[list(self.servers)[0]] else: self._default_server = None return self._default_server
class PIAFDatabase(object): """Context manager for connections to the PI Asset Framework database.""" version = '0.1.0' servers = {x.Name: {'server': x, 'databases': {}} for x in AF.PISystems()} default_server = servers[AF.PISystems().DefaultPISystem.Name] def __init__(self, server=None, database=None): server = self.servers.get(server, self.default_server) self.server = server['server'] if not server['databases']: server['databases'] = {x.Name: x for x in self.server.Databases} self.database = server['databases'].get( database, self.server.Databases.DefaultDatabase) def __enter__(self): self.server.Connect() return self def __exit__(self, *args): self.server.Disconnect() def __repr__(self): return u'%s(\\\\%s\\%s)' % (self.__class__.__name__, self.server_name, self.database_name) @property def server_name(self): """Return the name of the connected PI AF server.""" return self.server.Name @property def database_name(self): """Return the name of the connected PI AF database.""" return self.database.Name @property def children(self): """Return a dictionary of the direct child elements of the database.""" return {c.Name: PIAFElement(c) for c in self.database.Elements} def descendant(self, path): """Return a descendant of the database from an exact path.""" return PIAFElement(self.database.Elements.get_Item(path))
def servers(self): if self._servers is _NOTHING: i, j, failed_servers, failed_databases = 0, 0, 0, 0 self._servers = {} from System import Exception as dotNetException # type: ignore for i, s in enumerate(AF.PISystems(), start=1): try: self._servers[s.Name] = {"server": s, "databases": {}} for j, d in enumerate(s.Databases, start=1): try: self._servers[s.Name]["databases"][d.Name] = d except Exception: failed_databases += 1 except dotNetException: failed_databases += 1 except Exception: failed_servers += 1 except dotNetException: failed_servers += 1 if failed_servers or failed_databases: warn("Failed loading {}/{} servers and {}/{} databases".format( failed_servers, i, failed_databases, j)) return self._servers
class PIAFDatabase(object): """Context manager for connections to the PI Asset Framework database.""" version = "0.1.0" servers = {x.Name: {"server": x, "databases": {}} for x in AF.PISystems()} default_server = servers[AF.PISystems().DefaultPISystem.Name] def __init__(self, server=None, database=None): self.server = None self.database = None self._initialise_server(server) self._initialise_database(database) def _initialise_server(self, server): if server and server not in self.servers: message = 'Server "{server}" not found, using the default server.' warn(message=message.format(server=server), category=UserWarning) server = self.servers.get(server, self.default_server) self.server = server["server"] def _initialise_database(self, database): server = self.servers.get(self.server.Name) if not server["databases"]: server["databases"] = {x.Name: x for x in self.server.Databases} if database and database not in server["databases"]: message = 'Database "{database}" not found, using the default database.' warn(message=message.format(database=database), category=UserWarning) default_db = self.server.Databases.DefaultDatabase self.database = server["databases"].get(database, default_db) def __enter__(self): self.server.Connect() return self def __exit__(self, *args): self.server.Disconnect() def __repr__(self): return "%s(\\\\%s\\%s)" % ( self.__class__.__name__, self.server_name, self.database_name, ) @property def server_name(self): """Return the name of the connected PI AF server.""" return self.server.Name @property def database_name(self): """Return the name of the connected PI AF database.""" return self.database.Name @property def children(self): """Return a dictionary of the direct child elements of the database.""" return {c.Name: PIAFElement(c) for c in self.database.Elements} def descendant(self, path): """Return a descendant of the database from an exact path.""" return PIAFElement(self.database.Elements.get_Item(path))
""" PIconnect Connector to the OSISoft PI and PI-AF databases. """ # pragma pylint: disable=unused-import from PIconnect.AFSDK import AF, AF_SDK_VERSION from PIconnect.config import PIConfig from PIconnect.PI import PIServer from PIconnect.PIAF import PIAFDatabase # pragma pylint: enable=unused-import __version__ = "0.9.1" __sdk_version = tuple(int(x) for x in AF.PISystems().Version.split("."))