pub fn register_active_transmitter_if_free(
    conn: &PgConnection,
    worker_name: &String,
    r_type: RadioTypes,
    range: f64,
    _logger: &Logger,
) -> Result<usize, MeshSimError> {
    let query_str = match r_type {
        RadioTypes::ShortRange => String::from(
            "
            INSERT INTO active_wifi_transmitters (worker_id)
            SELECT ID 
                FROM workers 
                WHERE Worker_Name = $1 AND
                    (SELECT COUNT(*)
                        FROM worker_positions a
                        JOIN workers on workers.ID = a.worker_id
                        LEFT JOIN worker_positions b ON a.worker_id <> b.worker_id
                        INNER JOIN active_wifi_transmitters c ON b.worker_id = c.worker_id
                        WHERE workers.Worker_Name = $2
                                    AND distance(b.x, b.y, a.x, a.y) <= $3) = 0
            ON CONFLICT(worker_id)
            DO NOTHING;
            ",
        ),
        RadioTypes::LongRange => String::from(
            "
            INSERT INTO active_lora_transmitters (worker_id)
            SELECT ID 
                FROM workers 
                WHERE Worker_Name = $1 AND
                    (SELECT COUNT(*)
                        FROM worker_positions a
                        JOIN workers on workers.ID = a.worker_id
                        LEFT JOIN worker_positions b ON a.worker_id <> b.worker_id
                        INNER JOIN active_lora_transmitters c ON b.worker_id = c.worker_id
                        WHERE workers.Worker_Name = $2
                                    AND distance(b.x, b.y, a.x, a.y) <= $3) = 0
            ON CONFLICT(worker_id)
            DO NOTHING;
            ",
        ),
    };

    let q = sql_query(query_str)
        .bind::<Text, _>(worker_name)
        .bind::<Text, _>(worker_name)
        .bind::<Double, _>(range);
    // let debug_q = diesel::debug_query::<diesel::pg::Pg, _>(&q);
    // debug!(logger, "Query: {}", &debug_q);

    let rows = q.execute(conn).map_err(|e| {
        let r: String = r_type.into();
        let error_msg = format!(
            "Failed to register worker as an active transmitter. Radio={}",
            r
        );
        MeshSimError {
            kind: MeshSimErrorKind::SQLExecutionFailure(error_msg),
            cause: Some(Box::new(e)),
        }
    })?;

    Ok(rows)
}
/// Function to get the active transmitters that correspond to the radio-type provided
Function to get the active transmitters that correspond to the radio-type provided
Function to get the active transmitters that correspond to the radio - type provided
pub fn register_active_transmitter_if_free(
    conn: &PgConnection,
    worker_name: &String,
    r_type: RadioTypes,
    range: f64,
    _logger: &Logger,
) -> Result<usize, MeshSimError> {
    let query_str = match r_type {
        RadioTypes::ShortRange => String::from(
            "
            INSERT INTO active_wifi_transmitters (worker_id)
            SELECT ID 
                FROM workers 
                WHERE Worker_Name = $1 AND
                    (SELECT COUNT(*)
                        FROM worker_positions a
                        JOIN workers on workers.ID = a.worker_id
                        LEFT JOIN worker_positions b ON a.worker_id <> b.worker_id
                        INNER JOIN active_wifi_transmitters c ON b.worker_id = c.worker_id
                        WHERE workers.Worker_Name = $2
                                    AND distance(b.x, b.y, a.x, a.y) <= $3) = 0
            ON CONFLICT(worker_id)
            DO NOTHING;
            ",
        ),
        RadioTypes::LongRange => String::from(
            "
            INSERT INTO active_lora_transmitters (worker_id)
            SELECT ID 
                FROM workers 
                WHERE Worker_Name = $1 AND
                    (SELECT COUNT(*)
                        FROM worker_positions a
                        JOIN workers on workers.ID = a.worker_id
                        LEFT JOIN worker_positions b ON a.worker_id <> b.worker_id
                        INNER JOIN active_lora_transmitters c ON b.worker_id = c.worker_id
                        WHERE workers.Worker_Name = $2
                                    AND distance(b.x, b.y, a.x, a.y) <= $3) = 0
            ON CONFLICT(worker_id)
            DO NOTHING;
            ",
        ),
    };
    let q = sql_query(query_str)
        .bind::<Text, _>(worker_name)
        .bind::<Text, _>(worker_name)
        .bind::<Double, _>(range);
    // let debug_q = diesel::debug_query::<diesel::pg::Pg, _>(&q);
    // debug!(logger, "Query: {}", &debug_q);
    let rows = q.execute(conn).map_err(|e| {
        let r: String = r_type.into();
        let error_msg = format!(
            "Failed to register worker as an active transmitter. Radio={}",
            r
        );
        MeshSimError {
            kind: MeshSimErrorKind::SQLExecutionFailure(error_msg),
            cause: Some(Box::new(e)),
        }
    })?;
    Ok(rows)
}
pub fn register_active_transmitter_if_free ( conn : & PgConnection , worker_name : & String , r_type : RadioTypes , range : f64 , _logger : & Logger , ) -> Result < usize , MeshSimError > { let query_str = match r_type { RadioTypes :: ShortRange => String :: from ( " " , ) , RadioTypes :: LongRange => String :: from ( " " , ) , } ; let q = sql_query ( query_str ) . bind :: < Text , _ > ( worker_name ) . bind :: < Text , _ > ( worker_name ) . bind :: < Double , _ > ( range ) ; // let debug_q = diesel::debug_query::<diesel::pg::Pg, _>(&q); // debug!(logger, "Query: {}", &debug_q); let rows = q . execute ( conn ) . map_err ( | e | { let r : String = r_type . into ( ) ; let error_msg = format ! ( " " r ) ; MeshSimError { kind : MeshSimErrorKind :: SQLExecutionFailure ( error_msg ) , cause : Some ( Box :: new ( e ) ) , } } ) ? ; Ok ( rows ) }
Function to get the active transmitters that correspond to the radio-type provided
Function to get the active transmitters that correspond to the radio - type provided
conn
&PgConnection
worker_name
&String
r_type
RadioTypes
range
f64
_logger
&Logger