Struct SqlInterfaceBalling

Source
pub struct SqlInterfaceBalling {
    pub conn: PooledConn,
}
Expand description

Contains the configuration and the implementation of the SQL interface for Balling.

Fields§

§conn: PooledConn

Connection to the database

Implementations§

Source§

impl SqlInterfaceBalling

Source

pub fn new( conn: PooledConn, max_rows_balling_set_values: u64, max_rows_balling_dosing_log: u64, ) -> Result<Self, SqlInterfaceError>

Creates a new SqlInterfaceBalling instance.

This constructor initializes the Balling SQL interface with an established database connection. It performs several pre-flight checks to ensure data integrity and adherence to configured limits, such as verifying that the Balling set value table contains no NULL values and that table row counts are within their specified maximums.

§Arguments
  • conn - An active, pooled database connection.
  • max_rows_balling_set_values - Maximum allowed number of rows in the Balling set value table.
  • max_rows_balling_dosing_log - Maximum allowed number of rows in the Balling dosing log table.
§Returns

A Result containing a new SqlInterfaceBalling instance on success.

§Errors

This function will return an error if:

  • Any of the initial database queries to get table counts fail (DatabaseCheckBallingSetValsFailure).
  • Any of the retrieved counts are negative, indicating a database issue (DatabaseBallingSetValTableNegativeValue).
  • The ballingsetvals table contains entries with NULL values (DatabaseBallingSetValTableContainsNull).
  • The number of rows in the ballingsetvals table exceeds max_rows_balling_set_values (DatabaseBallingSetValTableContainsTooManyRows).
  • The number of rows in the ballingdosinglog table exceeds max_rows_balling_dosing_log (DatabaseBallingDosingLogTableContainsTooManyRows).
Source

fn get_last_balling_dosing_timestamp( &mut self, pump_id: i64, ) -> Result<Option<NaiveDateTime>, SqlInterfaceError>

Retrieves the timestamp of the last Balling dosing event for a specific pump.

This function queries the database for the most recent dosing timestamp associated with the given pump_id.

§Arguments
  • pump_id - The unique identifier of the pump.
§Returns

A Result containing an Option<NaiveDateTime>:

  • Ok(Some(NaiveDateTime)) if a last dosing timestamp is found for the pump.
  • Ok(None) if no previous dosing events are recorded for the specified pump.
§Errors

This function will return an error if the underlying database query fails. This could be due to a connection issue, a syntax error, or if the query unexpectedly returns more than one row. The specific error is propagated from the underlying get_optional_timestamp call.

Source

pub fn get_duration_since_last_balling_dosing( &mut self, pump_id: i64, ) -> Result<Option<Duration>, SqlInterfaceError>

Calculates the duration in seconds since the last Balling dosing event for a given pump.

This function retrieves both the current database timestamp and the last dosing timestamp for the specified pump_id. It then computes the time difference.

§Arguments
  • pump_id - The unique identifier of the pump.
§Returns

A Result containing an Option<i64>:

  • Ok(Some(Duration::new)): The duration that has passed since the last dosing event.
  • Ok(None): If no previous dosing event is recorded for this pump.
§Errors

This function will return an error if it fails to retrieve either the current timestamp or the last dosing timestamp from the database. This could be due to a connection loss or other database-side issues. It also will return an error if the database contains any entries that are in the future.

Source

pub fn insert_balling_event( &mut self, timestamp: NaiveDateTime, pump_id: i64, dosing_volume: f64, ) -> Result<(), SqlInterfaceError>

Inserts a new Balling dosing event record into the SQL database.

This function logs details of a Balling dosing operation, including the exact time, the pump used, and the volume of fluid dispensed.

§Arguments
  • timestamp - The NaiveDateTime when the dosing event occurred.
  • pump_id - The identifier of the pump that performed the dosing.
  • dosing_volume - The volume (in milliliters) of fluid dispensed.
§Returns

An empty Result (Ok(())) if the event record was successfully inserted.

§Errors

Returns SqlInterfaceError::InsertBallingEventFailure if the INSERT query fails. This can happen due to a lost connection, constraint violation (e.g., foreign key), or incorrect data types. The original mysql::Error is included as the source.

Source

pub fn get_single_balling_setval_from_database( &mut self, pump_id: i64, ) -> Result<BallingSetVal, SqlInterfaceError>

Retrieves a single set of Balling dosing parameters for a specified pump from the database.

This function queries the database for the Balling set values (e.g., dosing speed, volume, label) associated with a given pump_id. It strictly expects to find exactly one matching dataset.

§Arguments
  • pump_id - The unique identifier of the pump for which to retrieve the set values.
§Returns

A Result containing the BallingSetVal for the specified pump on success.

§Errors

This function will return an error if:

  • The database query fails for any reason (SingleBallingSetValRequestFailure).
  • The query returns zero rows (SingleBallingSetValEmptyResponse).
  • The query returns more than one row (SingleBallingSetValNoSingleResponse), which indicates a data consistency issue.

Trait Implementations§

Source§

impl Debug for SqlInterfaceBalling

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Pingable for SqlInterfaceBalling

Source§

fn ping(&mut self)

Pings the database to check connection health and prevent timeouts.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T