Source: views/SimulationModeView.js

/**
 * @author Serge Babayan
 * @module views/ProbeDropView
 * @requires SimulationManager
 * @requires util/Logger
 * @requires models/TelemetryData
 * @requires util/Template
 * @requires fast-csv
 * @requires electron
 * @copyright Waterloo Aerial Robotics Group 2016
 * @licence https://raw.githubusercontent.com/UWARG/WARG-Ground-Station/master/LICENSE
 * @description Simulation view that allows the user to select and run a simulation file at a specified speed
 */

var remote = require('electron').remote;

var TelemetryData = remote.require('./app/models/TelemetryData');
var Logger = remote.require('./app/util/Logger');
var SimulationManager = remote.require('./app/SimulationManager');
var Template = require('../util/Template');
var dialog = remote.dialog;

var csv = require('fast-csv');

module.exports = function (Marionette) {

  return Marionette.ItemView.extend({
    template: Template('SimulationView'),
    className: 'simulationView',

    ui: {
      file_path: '#selected-file-path',
      transmission_speed: '#selected_speed',
      start_button: '#toggle-simulation-button',
      change_speed_slider: '#change-speed-slider'
    },

    events: {
      'click #toggle-simulation-button': 'toggleSimulation',
      'click #select-new-file-button': 'openSimulationFile',
      'change #change-speed-slider': 'changeTransmissionSpeed'
    },

    onRender: function () {
      this.ui.file_path.text(SimulationManager.default_simulation_path);
      this.ui.transmission_speed.text(SimulationManager.transmission_frequency);

      if (SimulationManager.simulationActive) {
        this.changeToStopButton();
      }
      else {
        this.changeToStartButton();
      }

      csv.fromPath(SimulationManager.default_simulation_path, {
        headers: false, //Set to true if you expect the first line of your CSV to contain headers
        ignoreEmpty: true, //If you wish to ignore empty rows.
        discardUnmappedColumns: true, //If you want to discard columns that do not map to a header.
        delimiter: ',',
        trim: true, //If you want to trim all values parsed set to true.
        objectMode: true //if we want to return the stringified version of the data, set to false
      }).on('data-invalid', function () {
        Logger.warn('Invalid data detected in simulation file');
      }).on("data", function (data) {
        SimulationManager.addDataEntry(data);
      });
    },

    toggleSimulation: function () {
      SimulationManager.toggleSimulation();
      if (SimulationManager.simulationActive) {
        this.changeToStopButton();
      }
      else {
        this.changeToStartButton();
      }
    },

    openSimulationFile: function () {
      dialog.showOpenDialog({
        properties: ['openFile'],
        title: 'Select Simulation File',
        defaultPath: SimulationManager.default_simulation_path,
        buttonLabel: 'Open',
        filters: [
          {name: 'CSV Simulation File', extensions: ['csv']},
          {name: 'All Files', extensions: ['*']}
        ]
      }, function (file_path) {
        file_path = file_path[0];
        this.ui.file_path.text(file_path);
        SimulationManager.clearData();

        csv.fromPath(file_path, {
          headers: false, //Set to true if you expect the first line of your CSV to contain headers
          ignoreEmpty: true, //If you wish to ignore empty rows.
          discardUnmappedColumns: true, //If you want to discard columns that do not map to a header.
          delimiter: ',',
          trim: true, //If you want to trim all values parsed set to true.
          objectMode: true //if we want to return the stringified version of the data, set to false
        }).on("data", function (data) {
          SimulationManager.addDataEntry(data);
        }).on('data-invalid', function () {
          Logger.error('Invalid data detected in simulation file');
        });
      }.bind(this));
    },

    changeTransmissionSpeed: function () {
      SimulationManager.changeTransmissionFrequency(this.ui.change_speed_slider.val());
      this.ui.transmission_speed.text(SimulationManager.transmission_frequency);
    },

    changeToStartButton: function () {
      this.ui.start_button.text('Start Simulation');
      this.ui.start_button.removeClass('button-error');
      this.ui.start_button.addClass('button-success');
    },
    changeToStopButton: function () {
      this.ui.start_button.text('Stop Simulation');
      this.ui.start_button.addClass('button-error');
      this.ui.start_button.removeClass('button-success');
    }
  });
};