<commit_before>var noble = require('noble');
var chalk = require('chalk');
var push = require('./push.js');
var urldecode = require('./urldecode.js');
var userdata = require('./userdata.js');
var schedule = require('./schedule.js');
var notify = require('./notify.js');
var test = require('./test.js');
var led = {};
var brew = {};
var resetMachine = {};
var relay = {};
var brewScheduled = false;
var brewDuration = 60;
var resetInterval = 50 * 60;
var objects = [];
var scheduledCoffee = [];
if (process.env.TEST !== '1') {
  var five = require('johnny-five');
  var Edison = require('edison-io');
  var board = new five.Board({ io: new Edison() });
  board.on('ready', function () {
    led = new five.Led('J19-6');
    relay = new five.Relay('J19-10');
    relay.off();
    led.on();
  });
  board.on('warn', function () {
    led.off();
    led.stop();
    relay.off();
  });
} else {
  //test.schedule();
  //test.push();
  //test.notify();
  //test.userdata();
  led.off = function () {
    console.log(chalk.underline.bgRed('LED OFF !'));
  };
  led.on = function () {
    console.log(chalk.underline.bgBlue('LED ON'));
  };
  led.blink = function () {
    console.log(chalk.underline.bgBlue('LED BLINKING'));
  };
  led.stop = function () {
    console.log(chalk.underline.bgRed('LED STOP'));
  };
  relay.on = function () {
    console.log(chalk.underline.bgBlue('Relay ON'));
  };
  relay.off = function () {
    console.log(chalk.underline.bgRed('Relay OFF !'));
  };
}
noble.on('stateChange', function (state) {
  if (state === 'poweredOn') {
    noble.startScanning(['fed8', 'feaa'], true);
  } else {
    noble.stopScanning();
  }
});
noble.on('scanStart', function () {
  console.log(chalk.dim('Scan started...'));
  console.log();
  led.off();
  led.blink();
});
noble.on('scanStop', function () {
  console.log(chalk.dim('Scan stopped...'));
  console.log();
  led.stop();
});

var resetMachine = new Promise(function (resolve, reject) {
  setTimeout(resolve, resetInterval * 1000);
});
resetMachine.then(function () {
  console.log(chalk.underline.bgYellow('Machine reset'));
  brewScheduled = false;
  objects = [];
});

noble.on('discover', function (peripheral) {
  var serviceData = peripheral.advertisement.serviceData;
  var object = {};
  if (serviceData && serviceData.length) {
    for (var i in serviceData) {
      // check if Eddystone-URL
      var hexData = serviceData[i].data.toString('hex');
      if (hexData.substr(0, 2) === '10') {
        var url = urldecode(hexData);
        object = { 'device': peripheral.address, 'url': url, 'brewed': false };
      }
    }
    if (!object.url) {
      console.log(chalk.dim('Device found but not a Eddystone URL: ' + peripheral.address));
      return;
    }
    var found = false;
    for(var j = 0; j < objects.length; j++) {
      if (objects[j].device === object.device) {
          found = true;
          console.log(chalk.dim('Eddystone URL found but allread scheduled: ' + peripheral.address));
          break;
      }
    }
    if (!found) {
      objects.push(object);
      userdata(objects[0]).then(function (user) {
        var coffee = user.coffee;
        console.log(JSON.stringify(coffee));
        var data = coffee;
        if (data.pwEnabled) {
          console.log(chalk.underline.bgGreen('Coffee is brewing !'));
          schedule(user).then(function (data) {
            scheduledCoffee.push(data);
            if (!brewScheduled && process.env.SIM !== '1'){
              processCoffee();
            }
          });
        } else {
          objects.shift();
        }
      }).catch(function (e) {
        objects.pop();
        console.log(chalk.underline.bgRed('Error: ' + e));
        console.log();
      });
    }
  }
});

var processCoffee = function() {
  brewScheduled = true;
  console.log(chalk.underline.bgGreen('Processing Coffee'));
    var schedule = scheduledCoffee.shift();
    var data = schedule.user.coffee;<commit_msg>Fix beacon detection when it changes from config to advertising mode<commit_after>// Set this envars to be able to detect the beacon again
// when it changes its apparence and local_name
process.env.NOBLE_REPORT_ALL_HCI_EVENTS=1

var noble = require('noble');
var chalk = require('chalk');
var push = require('./push.js');
var urldecode = require('./urldecode.js');
var userdata = require('./userdata.js');
var schedule = require('./schedule.js');
var notify = require('./notify.js');
var test = require('./test.js');
var led = {};
var brew = {};
var resetMachine = {};
var relay = {};
var brewScheduled = false;
var brewDuration = 60;
var resetInterval = 50 * 60;
var objects = [];
var scheduledCoffee = [];
if (process.env.TEST !== '1') {
  var five = require('johnny-five');
  var Edison = require('edison-io');
  var board = new five.Board({ io: new Edison() });
  board.on('ready', function () {
    led = new five.Led('J19-6');
    relay = new five.Relay('J19-10');
    relay.off();
    led.on();
  });
  board.on('warn', function () {
    led.off();
    led.stop();
    relay.off();
  });
} else {
  //test.schedule();
  //test.push();
  //test.notify();
  //test.userdata();
  led.off = function () {
    console.log(chalk.underline.bgRed('LED OFF !'));
  };
  led.on = function () {
    console.log(chalk.underline.bgBlue('LED ON'));
  };
  led.blink = function () {
    console.log(chalk.underline.bgBlue('LED BLINKING'));
  };
  led.stop = function () {
    console.log(chalk.underline.bgRed('LED STOP'));
  };
  relay.on = function () {
    console.log(chalk.underline.bgBlue('Relay ON'));
  };
  relay.off = function () {
    console.log(chalk.underline.bgRed('Relay OFF !'));
  };
}
noble.on('stateChange', function (state) {
  if (state === 'poweredOn') {
    noble.startScanning(['fed8', 'feaa'], true);
  } else {
    noble.stopScanning();
  }
});
noble.on('scanStart', function () {
  console.log(chalk.dim('Scan started...'));
  console.log();
  led.off();
  led.blink();
});
noble.on('scanStop', function () {
  console.log(chalk.dim('Scan stopped...'));
  console.log();
  led.stop();
});

var resetMachine = new Promise(function (resolve, reject) {
  setTimeout(resolve, resetInterval * 1000);
});
resetMachine.then(function () {
  console.log(chalk.underline.bgYellow('Machine reset'));
  brewScheduled = false;
  objects = [];
});

noble.on('discover', function (peripheral) {
  var serviceData = peripheral.advertisement.serviceData;
  var object = {};
  if (serviceData && serviceData.length) {
    for (var i in serviceData) {
      // check if Eddystone-URL
      var hexData = serviceData[i].data.toString('hex');
      if (hexData.substr(0, 2) === '10') {
        var url = urldecode(hexData);
        object = { 'device': peripheral.address, 'url': url, 'brewed': false };
      }
    }
    if (!object.url) {
      console.log(chalk.dim('Device found but not a Eddystone URL: ' + peripheral.address));
      return;
    }
    var found = false;
    for(var j = 0; j < objects.length; j++) {
      if (objects[j].device === object.device && objects[j].url === object.url) {
          found = true;
          console.log(chalk.dim('Eddystone URL found but allread scheduled: ' + peripheral.address));
          break;
      }
    }
    if (!found) {
      objects.push(object);
      userdata(objects[0]).then(function (user) {
        var coffee = user.coffee;
        console.log(JSON.stringify(coffee));
        var data = coffee;
        if (data.pwEnabled) {
          console.log(chalk.underline.bgGreen('Coffee is brewing !'));
          schedule(user).then(function (data) {
            scheduledCoffee.push(data);
            if (!brewScheduled && process.env.SIM !== '1'){
              processCoffee();
            }
          });
        } else {
          objects.shift();
        }
      }).catch(function (e) {
        objects.shift();
        console.log(chalk.underline.bgRed('Eddystone URL found but unable to retrieve valid coffee settings: ' + e ));
        console.log();
      });
    }
  }
});

var processCoffee = function() {
  brewScheduled = true;
  console.log(chalk.underline.bgGreen('Processing Coffee'));
    var schedule = scheduledCoffee.shift();
    var data = schedule.user.coffee;
{'fraction_non_alphanumeric': 0.18184037100317305, 'fraction_numerical': 0.006468147424945081, 'mean_word_length': 2.9079637577491653, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2471815', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2640, 'n_words': 560}