{"size": 2588, "ext": "js", "max_stars_repo_path": "r-server.js", "max_stars_repo_name": "inNETMonitoring/node-red-contrib-R-nodes", "max_stars_repo_stars_event_min_datetime": "2019-06-04T16:48:03.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-04T19:42:25.000Z", "max_issues_repo_path": "r-server.js", "max_issues_repo_name": "inNETMonitoring/node-red-contrib-R-nodes", "max_issues_repo_issues_event_min_datetime": "2019-06-03T17:24:53.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-22T09:43:43.000Z", "max_forks_repo_path": "r-server.js", "max_forks_repo_name": "inNETMonitoring/node-red-contrib-R-nodes", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 33.1794871795, "max_line_length": 127, "alphanum_fraction": 0.4126738794}
module.exports = function(RED) {
    "use strict"

    var r = require('rio')

    function RServer(config) {
        RED.nodes.createNode(this, config)

        this.host = config.host || '127.0.0.1'
        this.port = config.port || 6311

        this.user = this.credentials.username || ''
        this.password = this.credentials.password || ''

        this.connected = false
        this.nodes = {}

        this.register = (rNode) => {
            this.nodes[rNode.id] = rNode
            if (Object.keys(this.nodes).length === 1) {
                // Connection test
                this.evaluate('version', (err, msg) => {})
            }
        }

        this.deregister = (rNode, done) => {
            delete this.nodes[rNode.id];
            done()
        }

        this.evaluate = (cmd, cb) =>  {
            config = {
                command: cmd,
                callback: (err, message) => {
                    if (!err && !this.connected) {
                        this.connected = true
                        for (var id in this.nodes) {
                            if (this.nodes.hasOwnProperty(id)) {
                                this.nodes[id].status({fill: "green", shape: "dot", text: "node-red:common.status.connected"});
                            }
                        }
                    } else if (err && err.code === "ECONNREFUSED") {
                        this.connected = false
                        for (var id in this.nodes) {
                            if (this.nodes.hasOwnProperty(id)) {
                                this.nodes[id].status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
                            }
                        }
                    } else if (!this.connected) {
                        for (var id in this.nodes) {
                            if (this.nodes.hasOwnProperty(id)) {
                                this.nodes[id].status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
                            }
                        }
                        console.error("Unknown error.");
                    }
                    cb(err, message)
                },

                host: this.host,
                port: this.port,

                user: this.user,
                password: this.password
            }

            r.evaluate(config)
        }
    }

    RED.nodes.registerType("R Server", RServer, {
        credentials: {
            username: {type: "text"},
            password: {type: "password"}
        }
    })
}
