BAC0.db package

Submodules

BAC0.db.sql module

sql.py -

class BAC0.db.sql.SQLMixin[source]

Bases: object

Use SQL to persist a device’s contents. By saving the device contents to an SQL database, you can work with the device’s data while offline, or while the device is not available.

backup_histories_df(resampling='1s')[source]

Build a dataframe of the point histories By default, dataframe will be resampled for 1sec intervals, NaN will be forward filled then backward filled. This way, no NaN values will remains and analytics will be easier.

Please note that this can be disabled using resampling=False

In the process of building the dataframe, analog values are resampled using the mean() function. So we have intermediate results between to records.

For binary values, we’ll use .last() so we won’t get a 0.5 value which means nothing in this context.

If saving a DB that already exists, previous resampling will survive the merge of old data and new data.

dev_properties_df()[source]
async his_from_sql(db_name, point)[source]

Retrive point histories from SQL database

async points_from_sql(db_name)[source]

Retrieve point list from SQL database

points_properties_df()[source]

Return a dictionary of point/point_properties in preparation for storage in SQL.

read_dev_prop(device_name)[source]

Device properties retrieved from pickle

read_point_prop(device_name, point)[source]

Points properties retrieved from pickle

async save(filename=None, resampling=None)[source]

Save the point histories to sqlite3 database. Save the device object properties to a pickle file so the device can be reloaded.

Resampling : valid Pandas resampling frequency. If 0 or False, dataframe will not be resampled on save.

async value_from_sql(db_name, point)[source]

Take last known value as the value

BAC0.db.influxdb module

exception BAC0.db.influxdb.ConnectionError[source]

Bases: Exception

class BAC0.db.influxdb.InfluxDB(*args, **kwargs)[source]

Bases: object

Factory proxy: calling this returns an instance of InfluxDBv2 or InfluxDBv3.

clear_notes()

Clear notes object

log(note, *, level: str | int = 10)

Add a log entry…no note

log_subtitle(subtitle, args=None, width=35)
log_title(title, args=None, width=35)
logname = 'BAC0.db.influxdb | InfluxDB'
note(note, *, level=20, log=True)

Add note to the object. By default, the note will also be logged :param note: (str) The note itself :param level: (logging.level) :param log: (boolean) Enable or disable logging of note

property notes

Retrieve notes list as a Pandas Series

class BAC0.db.influxdb.InfluxDBClient[source]

Bases: object

class BAC0.db.influxdb.InfluxDBCommon[source]

Bases: object

clean_value(object_type, val, units_state)[source]

Cleans and formats the value based on the object type.

This method checks the object type and formats the value accordingly. If the object type contains “analog”, the value is formatted to a string with three decimal places and the units state. If the object type contains “multi”, the value is split on “:” and the second part is used.

Parameters: object_type (str): The type of the object. val: The value to be cleaned. units_state: The units state of the object.

Returns: tuple: A tuple containing the cleaned string value and the original value.

Raises: Exception: If an error occurs while cleaning the value.

points: List[Any]
prepare_point(list_of_points)[source]
read_flux(request, params)[source]
async write_points_lastvalue_to_db(list_of_points) None[source]

Writes a list of points to the InfluxDB database.

Args:

list_of_points (list): A list of points to be written to the database.

Returns:

None

class BAC0.db.influxdb.InfluxDBMeta[source]

Bases: type

Metaclass that dispatches construction to InfluxDBv2 or InfluxDBv3 based on params.

class BAC0.db.influxdb.InfluxDBv2(params)[source]

Bases: InfluxDBCommon

This class provides a connection to an InfluxDB database version 2.

It allows for writing to and reading from the database. The connection parameters such as the URL, port, token, organization, and bucket are specified as class attributes.

Attributes: url (str): The URL of the InfluxDB server. port (int): The port on which the InfluxDB server is listening. token (str): The token for authentication with the InfluxDB server. org (str): The organization for the InfluxDB server. timeout (int): The timeout for requests to the InfluxDB server, in milliseconds. bucket (str): The default bucket to use for operations. tags_file (str): The file containing tags for the InfluxDB server. username (str): The username for authentication with the InfluxDB server. password (str): The password for authentication with the InfluxDB server. client (InfluxDBClientAsync): The client for interacting with the InfluxDB server.

bucket = None
clear_notes()

Clear notes object

client: InfluxDBClient
async delete(predicate: str, value: str, start: datetime = datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc), stop: datetime = datetime.datetime(2025, 9, 19, 23, 38, 21, 666337, tzinfo=datetime.timezone.utc), bucket: str | None = None) bool[source]

Asynchronously delete data from the specified bucket in the InfluxDB database.

This method deletes all records that match the specified predicate and value within the given time range (start and stop).

Parameters: predicate (str): The field to match for deletion. value (str): The value that the predicate field should have for a record to be deleted. start (datetime, optional): The start of the time range for deletion. Defaults to the Unix epoch. stop (datetime, optional): The end of the time range for deletion. Defaults to the current time. bucket (str, optional): The name of the bucket from which to delete. If not provided, defaults to the instance’s bucket.

Example: await bacnet.database.delete(predicate=”object”, value=”virtual:73195493”, bucket=”BAC0_Test”)

Returns: bool: True if the deletion was successful, False otherwise.

Raises: Exception: If an error occurs while deleting.

log(note, *, level: str | int = 10)

Add a log entry…no note

log_subtitle(subtitle, args=None, width=35)
log_title(title, args=None, width=35)
logname = 'BAC0.db.influxdb | InfluxDBv2'
note(note, *, level=20, log=True)

Add note to the object. By default, the note will also be logged :param note: (str) The note itself :param level: (logging.level) :param log: (boolean) Enable or disable logging of note

property notes

Retrieve notes list as a Pandas Series

org = None
password = None
port = 8086
async query(query: str) AsyncIterator[source]
read_last_value_from_db(id=None)[source]
tags_file = None
timeout = 6000
token = None
url = None
username = None
async write(record) bool[source]

Asynchronously writes a record to the specified bucket in the InfluxDB database.

This method establishes a connection with the InfluxDB client and attempts to write the provided record to the specified bucket.

Parameters: bucket (str): The name of the bucket to which the record will be written. record: The record to be written to the bucket. The record should be in a format acceptable by the InfluxDB write API.

Example: await bacnet.database.write(record=my_record)

Raises: Exception: If an error occurs while writing to the database.

class BAC0.db.influxdb.InfluxDBv3(params)[source]

Bases: InfluxDBCommon

This class provides a connection to an InfluxDB database version 3.

It allows for writing to and reading from the database. The connection parameters such as the URL, port, token, organization, and bucket are specified as class attributes.

Attributes: url (str): The URL of the InfluxDB server. port (int): The port on which the InfluxDB server is listening. token (str): The token for authentication with the InfluxDB server. org (str): The organization for the InfluxDB server. timeout (int): The timeout for requests to the InfluxDB server, in milliseconds. database (str): The default bucket to use for operations. tags_file (str): The file containing tags for the InfluxDB server. username (str): The username for authentication with the InfluxDB server. password (str): The password for authentication with the InfluxDB server. client (InfluxDBClientAsync): The client for interacting with the InfluxDB server.

bucket = None
clear_notes()

Clear notes object

client: InfluxDBClient
database = None
log(note, *, level: str | int = 10)

Add a log entry…no note

log_subtitle(subtitle, args=None, width=35)
log_title(title, args=None, width=35)
logname = 'BAC0.db.influxdb | InfluxDBv3'
note(note, *, level=20, log=True)

Add note to the object. By default, the note will also be logged :param note: (str) The note itself :param level: (logging.level) :param log: (boolean) Enable or disable logging of note

property notes

Retrieve notes list as a Pandas Series

org = None
port = 8181
async query(query: str) AsyncIterator[source]
table: str
tags_file = None
timeout = 6000
token = None
url = None
async write(record) bool[source]
class BAC0.db.influxdb.Point(*args, **kwargs)[source]

Bases: object

class BAC0.db.influxdb.WriteOptions(*args, **kwargs)[source]

Bases: object

Module contents