<commit_before>'use strict';

const _ = require('lodash');
const React = require('react');
const through = require('through2');
const Card = require('react-material/components/Card');
const Button = require('react-material/components/Button');
const Loader = require('react-loader');
const Progress = require('./progress');

const styles = require('../styles');

class DownloadOverlay extends React.Component {
  constructor(){
    this.state = {
      devicePath: null,
      searching: false,
      selectedDevice: null,
      devices: [],
      progress: 0
    };

    this.onAccept = this.onAccept.bind(this);
    this.onCancel = this.onCancel.bind(this);
    this.reloadDevices = this.reloadDevices.bind(this);
    this.updateSelected = this.updateSelected.bind(this);
    this.updateProgress = this.updateProgress.bind(this);
  }

  componentDidMount(){
    this.reloadDevices();
  }

  onAccept(evt){
    this.download(this.state.selectedDevice);
  }

  onCancel(evt){
    if(typeof this.props.onCancel === 'function'){
      this.props.onCancel(evt);
    }
  }

  download(device){

    const { irken, handleError, handleSuccess } = this.props;

    const { toast, workspace, logger, overlay } = irken;

    const name = workspace.filename.deref();
    const source = workspace.current.deref();

    if(!device){
      return;
    }

    const board = irken.getBoard(device);

    board.removeListener('terminal', logger);

    board.on('progress', this.updateProgress);

    board.compile(source)
      .tap(() => logger.clear())
      .then((memory) => board.bootload(memory))
      .then(() => board.on('terminal', logger))
      .tap(() => toast.clear())
      .tap(() => handleSuccess(`'${name}' downloaded successfully`))
      .catch(handleError)
      .finally(() => {
        overlay.hide();
        board.removeListener('progress', this.updateProgress);
        this.setState({ progress: 0 });
      });

  }

  updateProgress(progress){
    this.setState({ progress: progress});
  }

  updateSelected(device){
    this.setState({
      devicePath: device.path,
      selectedDevice: device
    });
  }

  reloadDevices(){
    const irken = this.props.irken;
    this.setState({ devicePath: null, searching: true });
    irken.scanBoards()
      .then((devices) => this.setState({ devices: devices, searching: false }));
  }

  componentizeDevice(device, selectedPath){
    const highlight = device.path === selectedPath ? 'active' : 'inactive';
    return (
      <tr style={styles[highlight]} onClick={this.updateSelected.bind(this, device)}>
        <td style={styles.deviceTd}>{device.name}</td>
        <td style={styles.deviceTd}>{device.path}</td>
      </tr>
      );
  }
<commit_msg>Add serial blacklist to device scan.<commit_after>'use strict';

const _ = require('lodash');
const React = require('react');
const through = require('through2');
const Card = require('react-material/components/Card');
const Button = require('react-material/components/Button');
const Loader = require('react-loader');
const Progress = require('./progress');

const styles = require('../styles');

class DownloadOverlay extends React.Component {
  constructor(){
    this.state = {
      devicePath: null,
      searching: false,
      selectedDevice: null,
      devices: [],
      progress: 0
    };

    this.onAccept = this.onAccept.bind(this);
    this.onCancel = this.onCancel.bind(this);
    this.reloadDevices = this.reloadDevices.bind(this);
    this.updateSelected = this.updateSelected.bind(this);
    this.updateProgress = this.updateProgress.bind(this);
  }

  componentDidMount(){
    this.reloadDevices();
  }

  onAccept(evt){
    this.download(this.state.selectedDevice);
  }

  onCancel(evt){
    if(typeof this.props.onCancel === 'function'){
      this.props.onCancel(evt);
    }
  }

  download(device){

    const { irken, handleError, handleSuccess } = this.props;

    const { toast, workspace, logger, overlay } = irken;

    const name = workspace.filename.deref();
    const source = workspace.current.deref();

    if(!device){
      return;
    }

    const board = irken.getBoard(device);

    board.removeListener('terminal', logger);

    board.on('progress', this.updateProgress);

    board.compile(source)
      .tap(() => logger.clear())
      .then((memory) => board.bootload(memory))
      .then(() => board.on('terminal', logger))
      .tap(() => toast.clear())
      .tap(() => handleSuccess(`'${name}' downloaded successfully`))
      .catch(handleError)
      .finally(() => {
        overlay.hide();
        board.removeListener('progress', this.updateProgress);
        this.setState({ progress: 0 });
      });

  }

  updateProgress(progress){
    this.setState({ progress: progress});
  }

  updateSelected(device){
    this.setState({
      devicePath: device.path,
      selectedDevice: device
    });
  }

  reloadDevices(){
    const irken = this.props.irken;
    const scanOpts = {
      blacklist: [
        /Bluetooth-Incoming-Port/,
        /Bluetooth-Modem/,
        /cu\./
      ]
    };
    this.setState({ devicePath: null, searching: true });
    irken.scanBoards(scanOpts)
      .then((devices) => this.setState({ devices: devices, searching: false }));
  }

  componentizeDevice(device, selectedPath){
    const highlight = device.path === selectedPath ? 'active' : 'inactive';
    return (
      <tr style={styles[highlight]} onClick={this.updateSelected.bind(this, device)}>
        <td style={styles.deviceTd}>{device.name}</td>
        <td style={styles.deviceTd}>{device.path}</td>
      </tr>
      );
  }

{'fraction_non_alphanumeric': 0.15917636526410026, 'fraction_numerical': 0.0010743061772605193, 'mean_word_length': 3.1780104712041886, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6066980', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1644, 'n_words': 315}