<commit_before>import React, { Component } from 'react';
import { connect } from 'react-redux';

import { DeviceEventEmitter } from 'react-native';
import Beacons from 'react-native-beacons-android';

import { setBeaconData, beaconError, requestBeaconData } from '../actions/beaconLocationActions';

const FOREGROUND_SCAN_PERIOD = 1000;
const BACKGROUND_SCAN_PERIOD = 1000;

class BeaconController extends Component {
  constructor(props) {
    super(props);

    this.state = {
      beaconDetected: false,
      attempts: 0,
    };

    this.beacons = [{
      id: 'ebefd083-70a2-47c8-9837-e7b5634df524',
      latitude: 60.19942,
      longitude: 24.93461,
    }];
  }

  componentWillMount() {
    this.getBeacons();
  }

  async getBeacons() {
    this.props.requestBeaconData();
    Beacons.detectIBeacons();
    Beacons.setForegroundScanPeriod(FOREGROUND_SCAN_PERIOD);
    Beacons.setBackgroundScanPeriod(BACKGROUND_SCAN_PERIOD);

    try {
      await Beacons.startRangingBeaconsInRegion('REGION1', this.beacons[0].id);
    } catch (error) {
      // Continue regardless of error
    }

    // Print a log of the detected iBeacons (1 per 5 second)
    DeviceEventEmitter.addListener('beaconsDidRange', (data) => {
      if (this.state.attempts === 5) {
        Beacons.stopRangingBeaconsInRegion('REGION1', this.beacons[0].id);
        this.props.beaconError('Beacon not found in 5 seconds');
      }
      if (data.beacons.length > 0) {
        if (!this.state.beaconDetected) {
          let closestBeaconIndex = 0;
          let closestBeaconDistance = 999999;

          for (let beaconIndex = 0; beaconIndex < data.beacons.length; beaconIndex++) {
            if (data.beacons[beaconIndex].distance < closestBeaconDistance) {
              closestBeaconIndex = beaconIndex;
            }
          }

          const beaconData = {
            major: data.beacons[closestBeaconIndex].major,
            minor: data.beacons[closestBeaconIndex].minor,
          };

          this.props.setBeaconData(beaconData);
          Beacons.stopRangingBeaconsInRegion('REGION1', this.beacons[0].id);
          // alert('Olet pysäkillä Pasilan asema (2181)')
          this.setState({ beaconDetected: true });
        }
      } else if (this.state.beaconDetected) {
        this.setState({ beaconDetected: false });
      }

      this.setState({ attempts: this.state.attempts + 1 });
    });
  }
<commit_msg>Update beacon distance when searching for closest beacon<commit_after>import React, { Component } from 'react';
import { connect } from 'react-redux';

import { DeviceEventEmitter } from 'react-native';
import Beacons from 'react-native-beacons-android';

import { setBeaconData, beaconError, requestBeaconData } from '../actions/beaconLocationActions';

const FOREGROUND_SCAN_PERIOD = 1000;
const BACKGROUND_SCAN_PERIOD = 1000;

class BeaconController extends Component {
  constructor(props) {
    super(props);

    this.state = {
      beaconDetected: false,
      attempts: 0,
    };

    this.beacons = [{
      id: 'ebefd083-70a2-47c8-9837-e7b5634df524',
      latitude: 60.19942,
      longitude: 24.93461,
    }];
  }

  componentWillMount() {
    this.getBeacons();
  }

  async getBeacons() {
    this.props.requestBeaconData();
    Beacons.detectIBeacons();
    Beacons.setForegroundScanPeriod(FOREGROUND_SCAN_PERIOD);
    Beacons.setBackgroundScanPeriod(BACKGROUND_SCAN_PERIOD);

    try {
      await Beacons.startRangingBeaconsInRegion('REGION1', this.beacons[0].id);
    } catch (error) {
      // Continue regardless of error
    }

    // Print a log of the detected iBeacons (1 per 5 second)
    DeviceEventEmitter.addListener('beaconsDidRange', (data) => {
      if (this.state.attempts === 5) {
        Beacons.stopRangingBeaconsInRegion('REGION1', this.beacons[0].id);
        this.props.beaconError('Beacon not found in 5 seconds');
      }
      if (data.beacons.length > 0) {
        if (!this.state.beaconDetected) {
          let closestBeaconIndex = 0;
          let closestBeaconDistance = data.beacons[0].distance;

          for (let beaconIndex = 1; beaconIndex < data.beacons.length; beaconIndex++) {
            if (data.beacons[beaconIndex].distance < closestBeaconDistance) {
              closestBeaconIndex = beaconIndex;
              closestBeaconDistance = data.beacons[beaconIndex].distance;
            }
          }

          const beaconData = {
            major: data.beacons[closestBeaconIndex].major,
            minor: data.beacons[closestBeaconIndex].minor,
          };

          this.props.setBeaconData(beaconData);
          Beacons.stopRangingBeaconsInRegion('REGION1', this.beacons[0].id);
          // alert('Olet pysäkillä Pasilan asema (2181)')
          this.setState({ beaconDetected: true });
        }
      } else if (this.state.beaconDetected) {
        this.setState({ beaconDetected: false });
      }

      this.setState({ attempts: this.state.attempts + 1 });
    });
  }

{'fraction_non_alphanumeric': 0.11624621594349142, 'fraction_numerical': 0.026437941473259334, 'mean_word_length': 2.827027027027027, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2344157', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1599, 'n_words': 287}