<commit_before>var UI = require('ui');
var ajax = require('ajax');

var activeThing;
var thingList;

var activeTimeout;
var mainTimeout;

var main = new UI.Card({
  title: 'Home Pebble',
  subtitle: 'Control your home from your wrist!'
});

var activeCard = new UI.Card();
activeCard.on('click', function(e) {
  activeCard.body('Status: ...');
  takeAction(activeThing);
});

main.show();

// Retrieve things to control
ajax(
  {url: 'http://10.0.0.16:3000/things', type: 'json', cache: false},
  function(data){
    thingList = new UI.Menu({
      sections: [{
        items: data.things.map(function(thing) {
          return {title: thing.name, subtitle: thing.action};
        })
      }]
    });
    thingList.on('select', function(e) {
      activeThing = {
        index: e.itemIndex,
        title: e.item.title,
        subtitle: e.item.subtitle
      };
      takeAction(activeThing);
    });

    // Now that thingList is ready, add main click handler
    main.on('click', function(e) {
      thingList.show();
    });

  }, function(error) {
    console.log('error: ', error);
  }
);

function takeAction(activeThing) {
  clearTimeout(activeTimeout);
  clearTimeout(mainTimeout);

  ajax(
    {url: 'http://10.0.0.16:3000/action/'+activeThing.index, type: 'json', cache: false },
    function(data){
      activeCard.title(activeThing.title);
      activeCard.subtitle(activeThing.subtitle);
      activeCard.body('Status: ' + data.status);
      activeCard.show();

      // close active thing window
      activeTimeout = setTimeout(function() {
        activeCard.hide();
      }, 5000);

      // close whole app if no action
      mainTimeout = setTimeout(function() {
        thingList.hide();
        main.hide();
      }, 15000);

    }, function(error) {
      console.log('error: ', error);
      activeCard.body('Communication Error');
    }
  );  
}<commit_msg>Simplify UI, dynamic menu items<commit_after>var UI = require('ui');
var ajax = require('ajax');

var thingList;
var things;

// Retrieve things to control and start UI
ajax(
  {url: 'http://10.0.0.16:3000/things', type: 'json'},
  function(data){
    things = data.things.map(function(thing) {
      return {
        title: thing.name,
        subtitle: 'Status: ' + thing.status
      };
    });

    thingList = new UI.Menu({
      sections: [{
        items: things
      }]
    });

    thingList.on('select', takeAction);
    thingList.show();

  }, function(error) {
    var errorCard = new UI.Card({
      title: 'Error',
      subtitle: 'There was an error communicating with Home Pebble'
    });
    errorCard.show();
  }
);

function takeAction(e) {
  ajax(
    {url: 'http://10.0.0.16:3000/action/'+e.itemIndex, type: 'json', cache: false },
    function(data){
      things[e.itemIndex].subtitle = 'Status: ' + data.status;
      thingList.items(0, things);
    }, function(error) {
      things[e.itemIndex].subtitle = 'Status: Error communicating';
      thingList.items(0, things);
    }
  );  
}
{'fraction_non_alphanumeric': 0.16116116116116116, 'fraction_numerical': 0.01701701701701702, 'mean_word_length': 2.756892230576441, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '6919064', 'n_tokens_mistral': 1008, 'n_tokens_neox': 956, 'n_words': 209}