var request = require('request');
var noble = require('noble');
var request = require('request-json');

//var apiURL = process.env.APIURL;
var LOCATION = process.env.ENVIRONMENT;
var apiURL = 'http://requestb.in/11gduk11'
var client = request.createClient(apiURL);

// Variables to help us increase accuracy
var RSSI_THRESHOLD    = -90;
var EXIT_GRACE_PERIOD = 2000; // milliseconds

var inRange = [];

// Currently hard-coded list of dogs and their device IDs
var dogs = [
  "c3:63:96:6d:58:99"
]

noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    console.log('scanning...');
    noble.startScanning([], true);
  }
  else {
    console.log("Error...not connected or cannot connect");
    noble.stopScanning();
  }
});

noble.on('discover', function(peripheral) {
  if (peripheral.rssi < RSSI_THRESHOLD) {
    return;
  }

  var id = peripheral.address;
  updateAPI(id);

});


function updateAPI(deviceID) {

  payload = {
    event: {
      device: deviceID,
      location: LOCATION
    }
  }

  if (dogs.indexOf(deviceID) > -1) {
    console.log("POSTING EVENT");
    //client.post('event/', payload, function(err, res, body) {
    client.post(apiURL, payload, function(err, res, body) {
        return console.log(res.statusCode);
    });
  }
}

@@ -1,38 +1,4 @@
-var request = require('request');%0A
 var 
@@ -117,75 +117,70 @@
 N = 
-process.env.ENVIRONMENT;%0Avar apiURL = 'http://requestb.in/11gduk11'
+'kitchen';%0Avar apiURL = 'http://47b7979c.ngrok.com/api/event';
 %0Avar
@@ -341,19 +341,94 @@
 nds%0A
-%0Avar inRang
+var API_UPDATE_INTERVAL = 5000; // milliseconds%0A%0Avar seenDeviceIds = %5B%5D;%0Avar eventQueu
 e = 
@@ -498,19 +498,30 @@
 var 
-dog
+listenDeviceId
 s = %5B%0A  
 %22c3:
@@ -516,17 +516,17 @@
 s = %5B%0A  
-%22
+'
 c3:63:96
@@ -538,11 +538,291 @@
 8:99
-%22
+'
 %0A%5D
+;%0A%0Afunction updateAPI() %7B%0A  // send event queue to server%0A  console.log('POSTing sending events', eventQueue);%0A  client.post(apiURL, %7B events: eventQueue %7D, function(err, res, body) %7B%0A    console.log(res.statusCode)%0A    eventQueue.length = 0;%0A    seenDeviceIds.length = 0;%0A  %7D);%0A%7D
 %0A%0Ano
@@ -962,14 +962,48 @@
 ue);
+%0A%0A    setInterval(updateAPI, 10000);
 %0A  %7D
-%0A 
  els
@@ -1210,17 +1210,23 @@
 %0A%0A  var 
-i
+deviceI
 d = peri
@@ -1247,398 +1247,363 @@
 ;%0A  
-updateAPI(id);%0A%0A%7D);%0A%0A%0Afunction updateAPI(deviceID) %7B%0A%0A  payload = %7B%0A    event: %7B%0A      device: deviceID,%0A      location: LOCATION%0A    %7D%0A  %7D%0A%0A  if (dogs.indexOf(deviceID) %3E -1) %7B%0A    console.log(%22POSTING EVENT%22);%0A    //client.post('event/', payload, function(err, res, body) %7B%0A    client.post(apiURL, payload, function(err, res, body) %7B
+if (listenDeviceIds.indexOf(deviceId) %3E -1 && seenDeviceIds.indexOf(deviceId) === -1) %7B%0A    // queue event to be sent if we recognize the device and have not queued an event yet%0A    seenDeviceIds.push(deviceId);%0A%09 var eventPayload = %7B%0A      device: deviceId,%0A      location: LOCATION,%0A      time: Date.now()
 %0A    
+%7D;%0A
     
-return console.log(res.statusCode);%0A    %7D
+eventQueue.unshift(eventPayload
 );%0A  %7D%0A%7D
 %0A
@@ -1601,9 +1601,11 @@
 );%0A  %7D%0A%7D
+);
 %0A

Update index.js