All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.ac.ait.lablink.clients.plotter.services.DoubleInputDataNotifier Maven / Gradle / Ivy

Go to download

This package provides Lablink clients that visualize data as plotted graphs in a separate window.

The newest version!
//
// Copyright (c) AIT Austrian Institute of Technology GmbH.
// Distributed under the terms of the Modified BSD License.
//

package at.ac.ait.lablink.clients.plotter.services;

import at.ac.ait.lablink.clients.plotter.PlotterBase;

import at.ac.ait.lablink.core.service.IServiceStateChangeNotifier;
import at.ac.ait.lablink.core.service.LlService;

import java.io.File;


/**
 * Class DoubleInputDataNotifier.
 */
public class DoubleInputDataNotifier
    extends InputDataFileWriterBase
    implements IServiceStateChangeNotifier {

  private final PlotterBase plotter;
  private final int dataset;
  private final boolean writeToFile;

  /**
   * Constructor.
   *
   * @param id input name
   * @param pl associated plotter instance
   * @param ds unique integer ID of associated client input
   * @param wf if true, in addition to plotting also write new values to CSV file
   * @param pf path for writing output file
   * @param ut if true, use timestamps instead of elapsed runtime in CSV files
   * @throws java.io.IOException adding header to the CSV output file failed
   */
  public DoubleInputDataNotifier( String id, PlotterBase pl, int ds,
      boolean wf, String pf, boolean ut ) throws
    java.io.IOException {

    plotter = pl;
    dataset = ds;
    writeToFile = wf;

    if ( writeToFile ) {
      // Initialize CSV output file.
      File outputFile = new File( pf, id + ".csv" );
      initializeFileWriter( id, outputFile, ut );
    }
  }


  /**
   * Whenever a the state of the associated data service changes (i.e., a new
   * input arrives), add a new data point to the associated plotter.
   */
  @Override
  public void stateChanged( LlService service, Double oldVal, Double newVal ) {
    double time = plotter.getElapsedTimeInSeconds();

    if ( false == plotter.isPaused() ) {
      plotter.addPoint( dataset, time, newVal, true );

      if ( true == writeToFile ) {
        try {
          // Write new value to CSV output file.
          writeDataToFile( time, newVal );
        } catch ( java.io.IOException ex ) {
          System.out.println( "Failed to print data to file." );
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy