<commit_before>var five = require('johnny-five');
var board = new five.Board();
var _ = require('lodash');
var moment = require('moment');
var request = require('request');

// Current weather data (http://openweathermap.org/current)
// http://openweathermap.org/weather-data
//
// Examples:
// http://api.openweathermap.org/data/2.5/weather?units=metric&q=Taipei
var getCurrentWeatherData = function(callback) {
    var querystring = require('querystring');
    var qs = querystring.stringify({
        'units': 'metric', // metric or imperial
        'q': 'Taipei' // city
    });
    var url = 'http://api.openweathermap.org/data/2.5/weather?' + qs;
    request(url, function(err, rsp, body) {
        if (err || rsp.statusCode !== 200) {
            return;
        }

        var data = JSON.parse(body);
        callback(data);
    });
};

board.on('ready', function() {
    // I2C LCD, PCF8574
    var lcd = new five.LCD({
        controller: 'PCF8574',
        rows: 4,
        cols: 20
    });

    this.repl.inject({
        lcd: lcd
    });

    lcd.clear();

    getCurrentWeatherData(function(data) {
        var name = _.get(data, 'name');
        var dt = moment(_.get(data, 'dt') * 1000).format('YYYY-MM-DD hh:mm:ss');
        var weather = _.get(data, 'weather[0].main');
        var temp = _.get(data, 'main.temp').toFixed(1);
            tempMin = _.get(data, 'main.temp_min').toFixed(1),
            tempMax = _.get(data, 'main.temp_max').toFixed(1);
        var humidity = _.get(data, 'main.humidity');
        var windSpeed = _.get(data, 'wind.speed') * 3600 / 1000; // m/s -> km/h

        lcd.cursor(0, 0).print(dt);
        lcd.cursor(1, 0).print(name + ' (' + weather + ')');
        lcd.cursor(2, 0).print('T:' + temp + ' (' + tempMin + '-' + tempMax + ')');
        lcd.cursor(3, 0).print('H:' + humidity + '%  W:' + windSpeed + 'km/h');
    });

});<commit_msg>Refactoring code<commit_after>var five = require('johnny-five');
var board = new five.Board();
var _ = require('lodash');
var moment = require('moment');
var request = require('request');

var LCDWeatherDisplay = function(lcd) {
    this.lcd = lcd;

    // Current weather data (http://openweathermap.org/current)
    // http://openweathermap.org/weather-data
    //
    // Examples:
    // http://api.openweathermap.org/data/2.5/weather?units=metric&q=Taipei
    this._fetchWeatherData = function(callback) {
        var querystring = require('querystring');
        var qs = querystring.stringify({
            'units': 'metric', // metric or imperial
            'q': 'Taipei' // city
        });
        var url = 'http://api.openweathermap.org/data/2.5/weather?' + qs;
        request(url, function(err, rsp, body) {
            if (err || rsp.statusCode !== 200) {
                err = err || new Error(body);
            }

            var data = JSON.parse(body);
            callback(err, data);
        });
    };

};

LCDWeatherDisplay.prototype.load = function() {
    var lcd = this.lcd;

    lcd.clear();
    lcd.cursor(0, 0).print('Loading...');

    this._fetchWeatherData(function(err, data) {
        lcd.clear();

        if (err) {
            lcd.cursor(0, 0).print('Loading data failed.');
            return;
        }

        var name = _.get(data, 'name');
        var dt = moment(_.get(data, 'dt') * 1000).format('YYYY-MM-DD hh:mm:ss');
        var weather = _.get(data, 'weather[0].main');
        var temp = _.get(data, 'main.temp').toFixed(1);
            tempMin = _.get(data, 'main.temp_min').toFixed(1),
            tempMax = _.get(data, 'main.temp_max').toFixed(1);
        var humidity = _.get(data, 'main.humidity');
        var windSpeed = _.get(data, 'wind.speed') * 3600 / 1000; // m/s -> km/h

        lcd.cursor(0, 0).print(dt);
        lcd.cursor(1, 0).print(name + ' (' + weather + ')');
        lcd.cursor(2, 0).print('T:' + temp + ' (' + tempMin + '-' + tempMax + ')');
        lcd.cursor(3, 0).print('H:' + humidity + '%  W:' + windSpeed + 'km/h');
    });
};

board.on('ready', function() {
    // I2C LCD, PCF8574
    var lcd = new five.LCD({
        controller: 'PCF8574',
        rows: 4,
        cols: 20
    });
    var weatherDisplay = new LCDWeatherDisplay(lcd);

    this.repl.inject({
        lcd: lcd,
        weatherDisplay: weatherDisplay
    });

    weatherDisplay.load();
});
{'fraction_non_alphanumeric': 0.18715341959334567, 'fraction_numerical': 0.020794824399260628, 'mean_word_length': 2.297029702970297, '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': '5459714', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1448, 'n_words': 288}