<commit_before>'use strict';

const alt = require('../alt');
const _ = require('lodash');

const { rx, tx } = require('../actions/transmission');
const { hideDownload, showDownload } = require('../actions/overlay');
const { clearOutput, output } = require('../actions/console');
const { disableAuto, reloadDevices, updateSelected } = require('../actions/device');
const { handleSuccess, handleError } = require('../actions/file');

class DeviceStore {
  constructor() {

    this.bindListeners({
      onReloadDevices: [reloadDevices, showDownload],
      onDisableAuto: disableAuto,
      onUpdateSelected: updateSelected
    });

    this.state = {
      auto: true,
      devices: [],
      devicePath: null,
      message: null,
      searching: true,
      selectedDevice: null,
      progress: 0
    };

    this.messages = {
      none: 'No BASIC Stamps found.',
      noneMatched: 'No matching BASIC Stamps found.',
      multiple: 'Please select which module to download to.'
    };
  }

  onDisableAuto() {
    this.setState({ auto: false });
  }

  onReloadDevices(){

    const { scanBoards, workspace } = this.getInstance();
    const source = workspace.current.deref();

    const scanOpts = {
      reject: [
        /Bluetooth-Incoming-Port/,
        /Bluetooth-Modem/,
        /dev\/cu\./
      ],
      source: source
    };
    this.setState({
      devicePath: null,
      message: null,
      searching: true
    });
    scanBoards(scanOpts)
    .then((devices) => this.setState({ devices: devices, searching: false }))
    .then(() => this._checkDevices());

  }

  onUpdateSelected(device) {

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

    this._download();
  }

  _checkDevices(){
    const { auto, devices } = this.state;
    const { none, noneMatched, multiple } = this.messages;
    let matchedDevices = [];
    let exists = false;

    _.forEach(devices, (device) => {
      if (device.match) {
        matchedDevices.push(device);
      }
      if (device.name) {
        exists = true;
      }
    });

    if (!exists) {

      this.setState({ message: none });

    } else if (matchedDevices.length === 0) {

      //TODO: setup for part 4 of auto download issue
      this.setState({ message: noneMatched });

    } else if(matchedDevices.length === 1) {

      this.setState({
        message: null,
        selectedDevice: matchedDevices[0]
      });

      if (auto) {
        this._download();
      }

    } else {

      this.setState({ message: multiple });

    }

    this.setState({ auto: true });

  }

  _download() {
<commit_msg>only check devices on download, not identify<commit_after>'use strict';

const alt = require('../alt');
const _ = require('lodash');

const { rx, tx } = require('../actions/transmission');
const { hideDownload, showDownload } = require('../actions/overlay');
const { clearOutput, output } = require('../actions/console');
const { disableAuto, reloadDevices, updateSelected } = require('../actions/device');
const { handleSuccess, handleError } = require('../actions/file');

class DeviceStore {
  constructor() {

    this.bindListeners({
      onReloadDevices: [reloadDevices, showDownload],
      onDisableAuto: disableAuto,
      onUpdateSelected: updateSelected
    });

    this.state = {
      auto: true,
      devices: [],
      devicePath: null,
      message: null,
      searching: true,
      selectedDevice: null,
      progress: 0
    };

    this.messages = {
      none: 'No BASIC Stamps found.',
      noneMatched: 'No matching BASIC Stamps found.',
      multiple: 'Please select which module to download to.'
    };
  }

  onDisableAuto() {
    this.setState({ auto: false });
  }

  onReloadDevices(){

    const { scanBoards, workspace } = this.getInstance();
    const { auto } = this.state;
    const source = workspace.current.deref();

    const scanOpts = {
      reject: [
        /Bluetooth-Incoming-Port/,
        /Bluetooth-Modem/,
        /dev\/cu\./
      ],
      source: source
    };

    this.setState({
      devicePath: null,
      message: null,
      searching: true
    });

    scanBoards(scanOpts)
      .then((devices) => this.setState({ devices: devices, searching: false }))
      .then(() => {
        if(auto) {
          this._checkDevices();
        }
    });

    this.setState({ auto: true });
  }

  onUpdateSelected(device) {

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

    this._download();
  }

  _checkDevices(){
    const { auto, devices } = this.state;
    const { none, noneMatched, multiple } = this.messages;
    let matchedDevices = [];
    let exists = false;

    _.forEach(devices, (device) => {
      if (device.match) {
        matchedDevices.push(device);
      }
      if (device.name) {
        exists = true;
      }
    });

    if (!exists) {

      this.setState({ message: none });

    } else if (matchedDevices.length === 0) {

      //TODO: setup for part 4 of auto download issue
      this.setState({ message: noneMatched });

    } else if(matchedDevices.length === 1) {

      this.setState({
        message: null,
        selectedDevice: matchedDevices[0]
      });

      if (auto) {
        this._download();
      }

    } else {

      this.setState({ message: multiple });

    }

  }

  _download() {

{'fraction_non_alphanumeric': 0.14461653293524912, 'fraction_numerical': 0.001866019779809666, 'mean_word_length': 2.3458177278402, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6471233', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1576, 'n_words': 370}