Database Manager API¶
The DBManager orchestrates all database interactions. It serves as a facade for various specialized logger engines (Alarms, Events, Tags) and handles the database connection lifecycle.
automation.managers.db.DBManager(*args, **kwargs)Central manager for database operations and historical data logging.
It manages the connection to the database (SQLite, PostgreSQL, MySQL), registers database models, and initializes specific logging engines for Alarms, Events, Users, etc.
get_queue(self)Retrieves the internal queue used for tag updates.
set_db(self, db, is_history_logged=False)Configures the database connection for all logging engines.
Parameters:
- db (Database): The Peewee database instance (SqliteDatabase, PostgresqlDatabase, MySQLDatabase).
- is_history_logged (bool, optional): Enables or disables historical data logging.
get_db(self)Retrieves the current database connection object.
set_dropped(self, drop_tables)Sets the flag to drop tables on initialization.
Parameters:
- drop_tables (bool): If True, tables will be dropped and recreated on startup.
get_dropped(self)Gets the drop tables flag status.
register_table(self, cls)Registers a new database model (table) to be managed by the system.
Parameters:
- cls (BaseModel): A class inheriting from
BaseModel.
get_db_table(self, tablename)Retrieves a registered table model by its table name.
Parameters:
- tablename (str): The name of the table in the database.
Returns:
- Model: The Peewee model class if found, else None.
create_tables(self)Creates all registered tables in the database.
drop_tables(self)Drops all registered tables from the database.
clear_default_tables(self)Clears the list of default tables. Useful for custom applications that don't need the standard schema.
get_tags(self)Retrieves all tags configured in the database logger.
get_alarms(self)Retrieves all alarms from the alarm logger.
set_tag(self, tag, unit, data_type, description, display_name='', min_value=None, max_value=None, tcp_source_address=None, node_namespace=None)Registers a tag in the database logger configuration.
Parameters:
- tag (str): Tag name.
- unit (str): Tag unit.
- data_type (str): Data type (float, int, bool).
- description (str): Description.
- tcp_source_address (str, optional): OPC UA server address.
- node_namespace (str, optional): OPC UA Node ID.
init_database(self)Initializes the database schema. Drops tables if configured, then creates them.
stop_database(self)Closes the database connection.
get_opcua_clients(self)Retrieves all OPC UA client configurations from the database.
set_role(self, name, level, identifier)Creates a new user role in the database.
set_user(self, user)Creates a new user in the database.
login(self, password, username='', email='')Authenticates a user against the database.
summary(self)Generates a summary of the database manager configuration.
Returns:
- dict: Summary including period, configured tags, and delay.
attach(self, tag_name)Attaches an observer to a tag for database logging purposes.