API Reference¶
DatabaseInterface¶
The main entry point for the library. Manages the background data daemon and provides query methods.
Constructor¶
DatabaseInterface(
sensors: dict[str, list[str]],
db_host: str,
db_port: str,
db_token: str,
db_org: str,
db_bucket: str,
srv_ip: str,
srv_port: int | str = 80,
handle: str = '',
interval: int = 1,
)
| Parameter | Type | Description |
|---|---|---|
| sensors | dict[str, list[str]] | Sensors and their parameters to track |
| db_host | str | InfluxDB host address |
| db_port | str | InfluxDB port |
| db_token | str | InfluxDB authentication token |
| db_org | str | InfluxDB organization |
| db_bucket | str | InfluxDB bucket name |
| srv_ip | str | Device IP address |
| srv_port | int or str | Device HTTP port (default: 80) |
| handle | str | HTTP endpoint path on device (default: '') |
| interval | int | Seconds between fetch cycles (default: 1) |
Properties¶
daemon¶
Returns the DataDaemon instance. Use it to start/stop data collection:
Methods¶
query_latest() -> pd.DataFrame¶
Query the most recent measurement from InfluxDB (last hour).
query_historical(start_relative, end='') -> pd.DataFrame¶
Query data from a time range. Accepts relative times or absolute timestamps.
# relative - last 30 days
df = interface.query_historical('-30d')
# absolute range
df = interface.query_historical(
'2024-05-16T00:00:00Z',
'2024-05-21T00:00:00Z'
)
query_custom_async(query) -> pd.DataFrame¶
Execute a custom Flux query using the async InfluxDB client. Best for small result sets - large queries may cause session issues.
query_custom_sync(query) -> pd.DataFrame¶
Execute a custom Flux query using the synchronous InfluxDB client. Use this for large queries.
DataDaemon¶
Background process that continuously fetches data from the device and
stores it in InfluxDB. Runs in a separate multiprocessing.Process.
You don't create this directly - access it through DatabaseInterface.daemon.
Methods¶
enable()¶
Start the daemon process. Begins the fetch-store loop.
disable()¶
Stop the daemon process. Terminates and joins the background process.
AsyncFetcher¶
Async HTTP client that fetches JSON readings from the device.
Constructor¶
Methods¶
async request_readings() -> dict¶
Send a GET request to the device and return the JSON response as a dict.
Returns None if the request fails (non-200 status).
AsyncQuery¶
Handles querying InfluxDB. Supports both async and sync clients.
Methods¶
async latest() -> pd.DataFrame¶
Query the last measurement from the past hour.
async historical(start, end='') -> pd.DataFrame¶
Query a time range. Uses the sync client internally for large result sets.
async custom_async(query) -> pd.DataFrame¶
Execute a custom async Flux query.
async custom_sync(query) -> pd.DataFrame¶
Execute a custom Flux query via the synchronous client.
Exceptions¶
InvalidInterval¶
Raised when historical() receives invalid time parameters.
JSONInfluxParser¶
Converts device JSON responses into InfluxDB-compatible records.
Methods¶
parse(json_measurements) -> dict¶
Parse raw JSON into an InfluxDB record with measurement, tags, timestamp, and fields.
If the same parameter appears in multiple sensors, the values are averaged.
DataParser¶
Converts InfluxDB query results into pandas DataFrames.
Methods¶
into_dataframe() -> pd.DataFrame¶
Parse FluxTable results into a DataFrame indexed by local time. Timestamps are converted from UTC to your local timezone.