pub fn simulate_pv_and_write_results_to_file<U: Into<String>, P: AsRef<Path>>(
    stride: Duration,
    simulation_length: Duration,
    broker_url: U,
    output_path: P,
) {
    // Use two different threads to simulate different, independent components of the system.
    // Variables for moving into the threads are created here.
    let broker_url_meter: String = broker_url.into();
    let broker_url_pv = broker_url_meter.clone();
    let output_pv: PathBuf = output_path.as_ref().into();

    // The first thread is the meter generating random values and passing them to the broker.
    let meter_sample_and_publish = thread::spawn(move || {
        // Create a meter with a range of 0-9000 W.
        // Unwrapping is not problematic as we know the upper bound
        // to be positive and finite.
        let meter = Meter::new(9000.0, broker_url_meter).unwrap();
        // Setup the time frame to be simulated.
        let simulation_time = SimulatedDateTime::new(stride, simulation_length);
        // Run the simulation.
        if let Err(err) = meter.publish_samples_to_broker_until(simulation_time) {
            // Use panic! to simplify function handling by the Python
            // wrapper. If this would be an actual library it should
            // return the result for proper error propagation.
            // This holds true for all subsequent code blocks.
            panic!("The meter simulation failed: {:?}", err);
        }
    });

    // The second thread is the pv simulator that gets the power consumption from
    // the broker, augments it and writes the results to a file.
    let pv_simulate_and_write = thread::spawn(move || {
        let mut simulator = PvSimulator::new(broker_url_pv);
        if let Err(err) = simulator.listen_to_broker() {
            panic!("Listening to the broker failed: {:?}", err);
        }
        if let Err(err) = simulator.write_records_to_file(output_pv) {
            panic!("Writing output to file failed: {:?}", err);
        }
    });

    // Wait for both of the threads to finish.
    if let Err(err) = meter_sample_and_publish.join() {
        panic!("The meter thread paniced: {:?}", err);
    }
    if let Err(err) = pv_simulate_and_write.join() {
        panic!("The pv simulator thread paniced: {:?}", err);
    }
    // Print a small notification that the simulation was finished.
    println!("    Simulation completed!");
}
/// Simulates the `Meter` and photovoltaic component as specified by the exercise's description.
/// The results are written to the specified file.
///
/// # Parameters
/// * `stride` - the simulated time steps
/// * `simulation_length` - the total simulation length
/// * `broker_url` - the URL of the RabbitMQ message broker
/// * `output_path` - the path to the output file
///
/// # Panics
///
/// If any part of the simulation fails.
Simulates the `Meter` and photovoltaic component as specified by the exercise's description.
The results are written to the specified file.
Simulates the ` Meter ` and photovoltaic component as specified by the exercise ' s description . The results are written to the specified file .
pub fn simulate_pv_and_write_results_to_file<U: Into<String>, P: AsRef<Path>>(
    stride: Duration,
    simulation_length: Duration,
    broker_url: U,
    output_path: P,
) {
    // Use two different threads to simulate different, independent components of the system.
    // Variables for moving into the threads are created here.
    let broker_url_meter: String = broker_url.into();
    let broker_url_pv = broker_url_meter.clone();
    let output_pv: PathBuf = output_path.as_ref().into();
    // The first thread is the meter generating random values and passing them to the broker.
    let meter_sample_and_publish = thread::spawn(move || {
        // Create a meter with a range of 0-9000 W.
        // Unwrapping is not problematic as we know the upper bound
        // to be positive and finite.
        let meter = Meter::new(9000.0, broker_url_meter).unwrap();
        // Setup the time frame to be simulated.
        let simulation_time = SimulatedDateTime::new(stride, simulation_length);
        // Run the simulation.
        if let Err(err) = meter.publish_samples_to_broker_until(simulation_time) {
            // Use panic! to simplify function handling by the Python
            // wrapper. If this would be an actual library it should
            // return the result for proper error propagation.
            // This holds true for all subsequent code blocks.
            panic!("The meter simulation failed: {:?}", err);
        }
    });
    // The second thread is the pv simulator that gets the power consumption from
    // the broker, augments it and writes the results to a file.
    let pv_simulate_and_write = thread::spawn(move || {
        let mut simulator = PvSimulator::new(broker_url_pv);
        if let Err(err) = simulator.listen_to_broker() {
            panic!("Listening to the broker failed: {:?}", err);
        }
        if let Err(err) = simulator.write_records_to_file(output_pv) {
            panic!("Writing output to file failed: {:?}", err);
        }
    });
    // Wait for both of the threads to finish.
    if let Err(err) = meter_sample_and_publish.join() {
        panic!("The meter thread paniced: {:?}", err);
    }
    if let Err(err) = pv_simulate_and_write.join() {
        panic!("The pv simulator thread paniced: {:?}", err);
    }
    // Print a small notification that the simulation was finished.
    println!("    Simulation completed!");
}
pub fn simulate_pv_and_write_results_to_file < U : Into < String > , P : AsRef < Path > > ( stride : Duration , simulation_length : Duration , broker_url : U , output_path : P , ) { // Use two different threads to simulate different, independent components of the system. // Variables for moving into the threads are created here. let broker_url_meter : String = broker_url . into ( ) ; let broker_url_pv = broker_url_meter . clone ( ) ; let output_pv : PathBuf = output_path . as_ref ( ) . into ( ) ; // The first thread is the meter generating random values and passing them to the broker. let meter_sample_and_publish = thread :: spawn ( move | | { // Create a meter with a range of 0-9000 W. // Unwrapping is not problematic as we know the upper bound // to be positive and finite. let meter = Meter :: new ( 9000.0 , broker_url_meter ) . unwrap ( ) ; // Setup the time frame to be simulated. let simulation_time = SimulatedDateTime :: new ( stride , simulation_length ) ; // Run the simulation. if let Err ( err ) = meter . publish_samples_to_broker_until ( simulation_time ) { // Use panic! to simplify function handling by the Python // wrapper. If this would be an actual library it should // return the result for proper error propagation. // This holds true for all subsequent code blocks. panic ! ( " " err ) ; } } ) ; // The second thread is the pv simulator that gets the power consumption from // the broker, augments it and writes the results to a file. let pv_simulate_and_write = thread :: spawn ( move | | { let mut simulator = PvSimulator :: new ( broker_url_pv ) ; if let Err ( err ) = simulator . listen_to_broker ( ) { panic ! ( " " err ) ; } if let Err ( err ) = simulator . write_records_to_file ( output_pv ) { panic ! ( " " err ) ; } } ) ; // Wait for both of the threads to finish. if let Err ( err ) = meter_sample_and_publish . join ( ) { panic ! ( " " err ) ; } if let Err ( err ) = pv_simulate_and_write . join ( ) { panic ! ( " " err ) ; } // Print a small notification that the simulation was finished. println ! ( " " ) ; }
Simulates the `Meter` and photovoltaic component as specified by the exercise's description.
Simulates the ` Meter ` and photovoltaic component as specified by the exercise ' s description .
stride
Duration
simulation_length
Duration
broker_url
U
output_path
P
stride
Duration
simulation_length
Duration
broker_url
U
output_path
P
panics
If any part of the simulation fails.
If any part of the simulation fails .