<commit_before>var _ = require('lodash');

var check_period = 500;

var mock_flow = true;
var mock_count = 27;

var sensor_0_gpio = 17;
var sensor_1_gpio = 27;
var sensor_2_gpio = 22;

var states = [
  {
    id: sensor_0_gpio,
    color: "orange",
    count: 0,
    millis: 0,
    pouring: false
  },
  {
    id: sensor_1_gpio,
    color: "green",
    count: 0,
    millis: 0,
    pouring: false
  },
  {
    id: sensor_2_gpio,
    color: "yellow",
    count: 0,
    millis: 0,
    pouring: false
  }
];

if( mock_flow ){
  function pouring_mock(c){
    count = [c,c*.75,c*.125];
    states[0].pouring = false;
    states[0].count = c;
    states[1].pouring = false;
    states[1].count = c*0.75;
    states[2].pouring = false;
    states[2].count = c*1.25;
    setTimeout(pouring_mock,check_period,mock_count);
  }
  setTimeout(pouring_mock,check_period,mock_count);
} else {
  var Gpio = require('onoff').Gpio,
      sensor0 = new Gpio(states[0].id, 'in', 'both'),
      sensor1 = new Gpio(states[1].id, 'in', 'both'),
      sensor2 = new Gpio(states[2].id, 'in', 'both');

  function pouring_check(c,id){
    states[id].pouring = (c != states[id].count);
  }
  
  sensor0.watch(function(err, value) {
    if (err) exit();
    if( value ){
      states[0].count++;
      setTimeout(pouring_check,check_period,states[0].count,0);
    }
  });  
  
  sensor1.watch(function(err, value) {
    if (err) exit();
    if( value ){
      states[1].count++;
      setTimeout(pouring_check,check_period,states[1].count,1);
    }
  });  

  sensor2.watch(function(err, value) {
    if (err) exit();
    if( value ){
      states[2].count++;
      setTimeout(pouring_check,check_period,states[2].count,2);
    }
  });  

  process.on('SIGINT', exit);
}

function exit() {
    process.exit();
}

function milliliters(count) {
  return count * 2.25;
}

function init(s){
  states = s;
  states.forEach( function(state){
    state.count = 0;
  });
}

function check(data, callback){
  // if(!_.isEmpty(data)){
  //   console.log('old states : '+JSON.stringify(states));
  //   // states = data;

  // }

  states.forEach( function(state){
    if( !state.pouring ) {
      state.millis += milliliters(state.count);
      state.count = 0;
    }
  });
  callback(states);
}

module.exports = {
  check: check,
  init: init
};


<commit_msg>lets go live gpio<commit_after>// var _ = require('lodash');

var check_period = 500;

var mock_flow = false;
var mock_count = 27;

var sensor_0_gpio = 17;
var sensor_1_gpio = 27;
var sensor_2_gpio = 22;

var states = [
  {
    id: sensor_0_gpio,
    color: "orange",
    count: 0,
    millis: 0,
    pouring: false
  },
  {
    id: sensor_1_gpio,
    color: "green",
    count: 0,
    millis: 0,
    pouring: false
  },
  {
    id: sensor_2_gpio,
    color: "yellow",
    count: 0,
    millis: 0,
    pouring: false
  }
];

if( mock_flow ){
  function pouring_mock(c){
    count = [c,c*.75,c*.125];
    states[0].pouring = false;
    states[0].count = c;
    states[1].pouring = false;
    states[1].count = c*0.75;
    states[2].pouring = false;
    states[2].count = c*1.25;
    setTimeout(pouring_mock,check_period,mock_count);
  }
  setTimeout(pouring_mock,check_period,mock_count);
} else {
  var Gpio = require('onoff').Gpio,
      sensor0 = new Gpio(states[0].id, 'in', 'both'),
      sensor1 = new Gpio(states[1].id, 'in', 'both'),
      sensor2 = new Gpio(states[2].id, 'in', 'both');

  function pouring_check(c,id){
    states[id].pouring = (c != states[id].count);
  }
  
  sensor0.watch(function(err, value) {
    if (err) exit();
    if( value ){
      states[0].count++;
      setTimeout(pouring_check,check_period,states[0].count,0);
    }
  });  
  
  sensor1.watch(function(err, value) {
    if (err) exit();
    if( value ){
      states[1].count++;
      setTimeout(pouring_check,check_period,states[1].count,1);
    }
  });  

  sensor2.watch(function(err, value) {
    if (err) exit();
    if( value ){
      states[2].count++;
      setTimeout(pouring_check,check_period,states[2].count,2);
    }
  });  

  process.on('SIGINT', exit);
}

function exit() {
    process.exit();
}

function milliliters(count) {
  return count * 2.25;
}

function init(s){
  states = s;
  states.forEach( function(state){
    state.count = 0;
  });
}

function check(data, callback){

  states.forEach( function(state){
    if( !state.pouring ) {
      state.millis += milliliters(state.count);
      state.count = 0;
    }
  });
  callback(states);
}

module.exports = {
  check: check,
  init: init
};



{'fraction_non_alphanumeric': 0.1709307454786061, 'fraction_numerical': 0.027790030877812087, 'mean_word_length': 2.892703862660944, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2084572', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1719, 'n_words': 326}