<commit_before>var express    = require('express');
var bodyParser = require('body-parser');
var gpio       = require('gpio');
var Slack      = require('slack-node');
var fs         = require('fs');

var app = express();
slack = new Slack();
var port = 3000; // same port as ngrok

// number of milliseconds elapsed since 1 January 1970 
var startTime = 0; // when the user is notified
var endTime   = 0; // when the user returns from opening the door

var messageLink = process.env.LINK;
var token       = process.env.TOKEN;

slack.setWebhook(messageLink);

app.use(bodyParser.urlencoded({extended: true}));

app.get('/', function(req, res) {
  res.status(200).send('Hello World!\n');
});

app.listen(port, function(){
  console.log('Listening on port ' + port);
  console.log(messageLink);
});



//===========================================================

app.post('/', function(req, res, next) {

  var requesterName = req.body.user_name;
  var channel = req.body.trigger_word;
  var tok = req.body.token;   
  // make sure there are no message loops, and only one user is being timed
  if(requesterName === 'slackbot' || requesterName === 'Door' || startTime != 0 || tok !== token){
    console.log('YOU SHALL NOT PASS!\n');
    return res.status(200).end();
  } else {
    console.log('channel: ' + channel);
    // Turn on red LED (TODO: sound buzzer)
    var chosen = OnDoorCall(channel, requesterName);
    
    // checks if button was pressed in 60ms intervals
    firstButtonPressCheck = setInterval(function() {
      if (gpio27.value == 1) { 
        clearInterval(firstButtonPressCheck);
        
        OnFirstButtonPress(requesterName,chosen);
    
        // checks if button was released in 60ms intervals
        firstButtonReleaseCheck = setInterval(function() {
          if (gpio27.value == 0) {
            clearInterval(firstButtonReleaseCheck);
            console.log('Button release');
            secondButtonPressCheck = setInterval(function() {
              if (gpio27.value == 1) {
                clearInterval(secondButtonPressCheck);  
                OnSecondButtonPress();  
                var deltaTime = getDeltaTime();
                var timeString = getTimeString(deltaTime);
  
                var botPayLoad = {
                  text: 'Conseguiste um tempo de ' + timeString + '!'
                }

                // checks if button was released in 60ms intervals
                secondButtonReleaseCheck = setInterval(function() {
                  if (gpio27.value == 0) {
                    clearInterval(secondButtonReleaseCheck);
                    console.log('Second release\n');
                    // send message to slack
                    return res.status(200).json(botPayLoad);
                  }
                },60);
              }
            },10);
          }
        },60);
      }
    },10);
  } 
});


function sendMessage(msgChannel, msgText) {
  slack.webhook({
    channel:msgChannel,
    requesterName:'HODOR',
    text:msgText
  }, function(err, response) {
    //console.log(err);
  });
}

function pickRandom(department) {
  console.log('===PICK RANDOM===');
  console.log(department);
  var members = JSON.parse(fs.readFileSync('members/' + department + '.json', 'utf8'));
  console.log(members);
  var chosenOne = members[Math.floor(Math.random() * members.length)];
  console.log(chosenOne);
  console.log('===END===');
  return chosenOne;
};

// flashing lights if led is on GPIO4
//green led
var gpio4 = gpio.export(4, {
  direction: 'out',
  interval: 200,
  ready: function(){
    intervalTimer = setInterval(function() { 
      gpio4.set();
      setTimeout(function() { 
        gpio4.reset(); 
      },500);
    },500);
  }
});

// red led
var gpio17 = gpio.export(17, {
  direction: 'out',
  interval: 200,
  ready: function() {
    gpio17.reset();
  }
}); 

// button
var gpio27 = gpio.export(27, {
  direction: 'in',
  interval: 200
});

function OnDoorCall(callerChannel, callerName){
  // Green ON
  gpio17.set(1); 
  console.log('green on!');

  // get a random user
  var requesterName = pickRandom(callerChannel);
  console.log('Messaging: ' + callerChannel);
  var channel = '@' + requesterName;
  var message = '@' + callerName + ' pede que abras a porta, por favor!';
  sendMessage(channel,message);
  return requesterName;
}

function OnFirstButtonPress(requester, buttonPresser) {
  console.log('First button press!');

  // Green OFF
  gpio17.set(0);
  console.log('green off!');

  // start the timer
  startTime = getTime();
  console.log('start time = ' + startTime);

  // at this time, the user should be on his way to get the door
  var msgChannel = '@' + requester;
  var msgText = '@' + buttonPresser + ' esta a caminho!';<commit_msg>Send public message and more console logs<commit_after>var express    = require('express');
var bodyParser = require('body-parser');
var gpio       = require('gpio');
var Slack      = require('slack-node');
var fs         = require('fs');

var app = express();
slack = new Slack();
var port = 3000; // same port as ngrok

// number of milliseconds elapsed since 1 January 1970 
var startTime = 0; // when the user is notified
var endTime   = 0; // when the user returns from opening the door

var messageLink = process.env.LINK;
var token       = process.env.TOKEN;

slack.setWebhook(messageLink);

app.use(bodyParser.urlencoded({extended: true}));

app.get('/', function(req, res) {
  res.status(200).send('Hello World!\n');
});

app.listen(port, function(){
  console.log('Listening on port ' + port);
  console.log(messageLink);
});



//===========================================================

app.post('/', function(req, res, next) {

  var requesterName = req.body.user_name;
  var channel = req.body.trigger_word;
  var tok = req.body.token;   
  // make sure there are no message loops, and only one user is being timed
  if(requesterName === 'slackbot' || requesterName === 'Door' || startTime != 0 || tok !== token){
    console.log('YOU SHALL NOT PASS!\n');
    return res.status(200).end();
  } else {
    console.log('channel: ' + channel);
    // Turn on red LED (TODO: sound buzzer)
    var chosen = OnDoorCall(channel, requesterName);
    
    // checks if button was pressed in 60ms intervals
    firstButtonPressCheck = setInterval(function() {
      if (gpio27.value == 1) { 
        clearInterval(firstButtonPressCheck);
        
        OnFirstButtonPress(requesterName,chosen);
    
        // checks if button was released in 60ms intervals
        firstButtonReleaseCheck = setInterval(function() {
          if (gpio27.value == 0) {
            clearInterval(firstButtonReleaseCheck);
            console.log('Button release');
            secondButtonPressCheck = setInterval(function() {
              if (gpio27.value == 1) {
                clearInterval(secondButtonPressCheck);  
                OnSecondButtonPress();  
                var deltaTime = getDeltaTime();
                var timeString = getTimeString(deltaTime);
  
                var botPayLoad = {
                  text: 'Conseguiste um tempo de ' + timeString + '!'
                }

                // checks if button was released in 60ms intervals
                secondButtonReleaseCheck = setInterval(function() {
                  if (gpio27.value == 0) {
                    clearInterval(secondButtonReleaseCheck);
                    console.log('Second release\n');
                    // send message to slack
                    return res.status(200).json(botPayLoad);
                  }
                },60);
              }
            },10);
          }
        },60);
      }
    },10);
  } 
});


function sendMessage(msgChannel, msgText) {
  slack.webhook({
    channel:msgChannel,
    user_name:'HODOR',
    text:msgText
  }, function(err, response) {
    console.log('[MESSAGE]\n' + msgChannel + ' -> ' + msgText);
  });
}

function pickRandom(department) {
  var members = JSON.parse(fs.readFileSync('members/' + department + '.json', 'utf8'));
  var chosenOne = members[Math.floor(Math.random() * members.length)];
  console.log('pickRandom -> ' + chosenOne);
  return chosenOne;
};

// flashing lights if led is on GPIO4
//green led
var gpio4 = gpio.export(4, {
  direction: 'out',
  interval: 200,
  ready: function(){
    intervalTimer = setInterval(function() { 
      gpio4.set();
      setTimeout(function() { 
        gpio4.reset(); 
      },500);
    },500);
  }
});

// red led
var gpio17 = gpio.export(17, {
  direction: 'out',
  interval: 200,
  ready: function() {
    gpio17.reset();
  }
}); 

// button
var gpio27 = gpio.export(27, {
  direction: 'in',
  interval: 200
});

function OnDoorCall(callerChannel, callerName){
  // Green ON
  gpio17.set(1); 
  console.log('green on!');

  // get a random user
  var chosenName = pickRandom(callerChannel);
  var channel = '@' + chosenName;
  var privateMessage = '@' + callerName + ' pede que abras a porta, por favor!';
  var publicMessage = '@' + chosenName + ' é a tua vez de abrir a porta :grin:';
  sendMessage(channel, privateMessage);
  sendMessage('#door-channel',publicMessage);
  return chosenName;
}

function OnFirstButtonPress(requester, buttonPresser) {
  console.log('First button press!');

  // Green OFF
  gpio17.set(0);
  console.log('green off!');

  // start the timer
  startTime = getTime();
  console.log('start time = ' + startTime);

  // at this time, the user should be on his way to get the door
  var msgChannel = '@' + requester;
  var msgText = '@' + buttonPresser + ' está a caminho!';
{'fraction_non_alphanumeric': 0.14681585276586845, 'fraction_numerical': 0.017567708877967166, 'mean_word_length': 2.630979498861048, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2670341', 'n_tokens_mistral': 3007, 'n_tokens_neox': 2794, 'n_words': 733}