<commit_before>/**
 * created by stfnhmplr on 11.01.17
 * control your Homee via websocket with node.js
 */
const WebSocket = require("ws");
const hash = require("crypto-toolkit").Hash("hex");
const request = require("request");

const Homee = function(host, user, pass) {
    this.host = host;
    this.user = user;
    this.pass = <PASSWORD>;
    this.ws = null;
    this.connected = false;
};

/**
 * [getAccesToken description]
 * @type {Promise}
 */
Homee.prototype.getAccessToken = function() {
    let options = {
        url: "http://" + this.host + ":7681/access_token",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        form: {
            device_name: "iPhone",
            device_hardware_id: "rrr",
            device_os: 2,
            device_type: 1,
            device_app: 1,
            device_push_registration_id: 1284
        },
        auth: {
            user: this.user,
            pass: <PASSWORD>(<PASSWORD>)
        }
    };

    return new Promise(function(resolve, reject) {
        request.post(options, function(err, res, body) {
            if (!err) {
                let token = body.split("&")[0].split("=")[1];
                resolve(token);
            } else {
                reject(
                    new Error(
                        "Homee: Error while receiving AccessToken: " + err
                    )
                );
            }
        });
    });
};

/**
 * [connect description]
 * @type {Promise}
 */
Homee.prototype.connect = function() {
    let that = this;

    return new Promise(function(resolve, reject) {
        that
            .getAccessToken()
            .then((token) => {
                that.ws = new WebSocket(
                    "ws://" +
                        that.host +
                        ":7681/connection?access_token=" +
                        token,
                    {
                        protocol: "v2",
                        protocolVersion: 13,
                        origin: "http://" + that.host + ":7681"
                    },
                    function(err) {
                        reject(err);
                    }
                );

                that.ws.on("open", function open() {
                    that.connected = true;
                    that.startHeartbeatHandler();
                    resolve();
                });
            })
            .catch(function(err) {
                reject(err);
            });
    });
};

/**
 * [listen description]
 * @return {[type]} [description]
 */
Homee.prototype.listen = function(callback) {
    this.ws.on('message', function(message) {
        callback(JSON.parse(message));
    });
};

/**
 * sends a message via websocket
 * @param  {String} message the message, i.e. 'GET:nodes'
 */
Homee.prototype.send = function(message) {
    this.ws.send(message);
};


Homee.prototype.startHeartbeatHandler = function () {
    let that = this;

    this.ws.on('pong', function () {
        that.connected = true;
    });

    setInterval(function ping() {
        if (that.ws && that.connected === false) return that.ws.terminate();
        that.connected = false;
        that.ws.ping('', false, true);
    }, 30000);
}

module.exports = Homee;
<commit_msg>added auto reconnect after loss of connection, refactoring<commit_after>/**
 * created by stfnhmplr on 11.01.17
 * control your Homee via websocket with node.js
 */
const WebSocket = require("ws");
const hash = require("crypto-toolkit").Hash("hex");
const request = require("request");

const Homee = function(host, user, pass) {
    this.host = host;
    this.user = user;
    this.pass = pass;
    this.ws = null;
    this.token = '';
    this.expires = '';
    this.connected = false;
};


/**
 * retrieve access token
 * @type {Promise}
 */
Homee.prototype.getAccessToken = function() {
    let that = this;

    let options = {
        url: "http://" + this.host + ":7681/access_token",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        form: {
            device_name: "iPhone",
            device_hardware_id: "rrr",
            device_os: 2,
            device_type: 1,
            device_app: 1,
            device_push_registration_id: 1284
        },
        auth: {
            user: this.user,
            pass: <PASSWORD>(this.<PASSWORD>)
        }
    };

    return new Promise(function(resolve, reject) {
        if (that.token && that.expires > Date.now()) resolve(that.token);

        request.post(options, function(err, res, body) {
            if (!err) {
                that.token = body.split("&")[0].split("=")[1];
                that.expires = Date.now() + parseInt(body.split("&")[3].split("=")[1]);
                resolve(that.token);
            } else {
                reject(
                    new Error(
                        "Homee: Error while receiving AccessToken: " + err
                    )
                );
            }
        });
    });
};


/**
 * Establish websocket connection
 * @type {Promise}
 */
Homee.prototype.connect = function() {
    let that = this;

    return new Promise(function(resolve, reject) {
        that.getAccessToken()
            .then((token) => {
                that.ws = new WebSocket(
                    "ws://" +
                        that.host +
                        ":7681/connection?access_token=" +
                        token,
                    {
                        protocol: "v2",
                        protocolVersion: 13,
                        origin: "http://" + that.host + ":7681"
                    },
                    function(err) {
                        reject(err);
                    }
                );

                that.ws.on("open", function open() {
                    that.connected = true;
                    that.startHeartbeatHandler();
                    resolve();
                });
            })
            .catch(function(err) {
                reject(err);
            });
    });
};


/**
 * Listen to messages of homee
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
Homee.prototype.listen = function(callback) {
    this.ws.on('message', function(message) {
        callback(JSON.parse(message));
    });
};


/**
 * sends a message via websocket
 * @param  {String} message the message, i.e. 'GET:nodes'
 */
Homee.prototype.send = function(message) {
    this.ws.send(message);
};


/**
 * Heartbeathandler to detect connection status
 */
Homee.prototype.startHeartbeatHandler = function () {
    let that = this;

    this.ws.on('pong', function () {
        that.connected = true;
    });

    setInterval(function ping() {
        if (that.ws && that.connected === false) that.reconnect();
        that.connected = false;
        that.ws.ping('', false, true);
    }, 30000);
}


/**
 * connects again after connection is lost
 */
Homee.prototype.reconnect = function () {
    this.ws.terminate();
    this.connect();
}


module.exports = Homee;

{'fraction_non_alphanumeric': 0.14078149245309635, 'fraction_numerical': 0.010156580617858655, 'mean_word_length': 1.5204408105225737, 'pattern_counts': {'":': 8, '<': 8, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2913817', 'n_tokens_mistral': 2072, 'n_tokens_neox': 1940, 'n_words': 413}