var util = require('util');
var EventEmitter = require('events').EventEmitter;
var request = require('request');

var Flukso = function(hostname, pathname) {
    var self = this;

    this.pollPower = function() {
        request({url: "http://" + hostname + pathname}, function(error, res, response) {
            if (error) {
                self.emit('failed', error)
            } else {

                try {
                    var jsondata = JSON.parse(response);
                    var power = jsondata[jsondata.length-1][1];

                    self.emit('done', parseInt(power));
                } catch(err) {
                    console.log("error parsing fluxo data");
                }

            }
        });
    };
};

util.inherits(Flukso, EventEmitter);
module.exports = Flukso;
