Ejemplo n.º 1
0
self.connect = function (options) {
    /*
      No options required if you only have one receiver on your network. We will find it and connect to it!
      options.host            - Hostname/IP
      options.port            - Port (default: 60128)
      options.send_delay      - Delay in milliseconds between each command sent to receiver (default: 500)
      options.model           - Should be discovered automatically but if you want to override it you can
      options.reconnect       - Try to reconnect if connection is lost (default: false)
      options.reconnect_sleep - Time in seconds to sleep between reconnection attempts (default: 5)
    */
    var connection_properties;

    options = options || {};
	config.host = options.host || config.host;
	config.port = options.port || config.port;
	config.model = options.model || config.model;
	config.reconnect = options.reconnect || config.reconnect;
	config.reconnect_sleep = options.reconnect_sleep || config.reconnect_sleep;

    connection_properties = {
        host: config.host,
        port: config.port
    };

    // If no host is configured - we connect to the first device to answer
    if (typeof config.host === 'undefined' || config.host === '') {
        self.discover(function (err, hosts) {
            if (!err && hosts.length > 0) {
                self.connect(hosts[0]);
            }
            return;
        });
        return;
    }

    // If host is configured but no model is set - we send a discover directly to this receiver
    if (typeof config.model === 'undefined' || config.model === '') {
        self.discover({address: config.host}, function (err, hosts) {
            if (!err && hosts.length > 0) {
                self.connect(hosts[0]);
            }
            return;
        });
        return;
    }

    /*
	  Compute modelsets for this model (so commands which are possible on this model are allowed)
      Note that this is not an exact match, model only has to be part of the modelname
    */
    Object.keys(MODELSETS).forEach(function (set) {
        MODELSETS[set].forEach(function (models) {
            if (models.indexOf(config.model) !== -1) {
                config.modelsets.push(set);
            }
        });
    });

    self.emit('debug', util.format("INFO (connecting) Connecting to %s:%s", config.host, config.port));

	// Reconnect if we have previously connected
    if (typeof eiscp !== 'undefined') {
		eiscp.connect(connection_properties);
		return;
    }

	// Connecting the first time
	eiscp = net.connect(connection_properties);

	eiscp.
	on('connect', function () {

		self.is_connected = true;
		self.emit('debug', util.format("INFO (connected) Connected to %s:%s", config.host, config.port));
		self.emit('connect');
	}).

	on('close', function () {

		self.is_connected = false;
		self.emit('debug', util.format("INFO (disconnected) Disconnected from %s:%s", config.host, config.port));
		self.emit('close');

		if (config.reconnect) {

			setTimeout(self.connect, reconnect_sleep * 1000);
		}
	}).

	on('error', function (err) {

		self.emit('error', util.format("ERROR (server_error) Server error on %s:%s - %s", config.host, config.port, err));
		eiscp.destroy();
	}).

	on('data', function (data) {

		var iscp_message = eiscp_packet_extract(data),
			result = iscp_to_command(iscp_message);

		result.iscp_command = iscp_message;

		self.emit('debug', util.format("DEBUG (received_data) Recevied data from %s:%s - %j", config.host, config.port, result));
		self.emit('data', result);

		// If the command is supported we emit it as well
		if (typeof result.command !== 'undefined') {
			if (Array.isArray(result.command)) {
				result.command.forEach(function (cmd) {
					self.emit(cmd, result.argument);
				});
			} else {
				self.emit(result.command, result.argument);
			}
		}
	});
};
Ejemplo n.º 2
0
self.connect = function (options) {

    var connection_properties;

    if (typeof options !== 'undefined') {
        config.host = options.host || config.host;
        config.port = options.port || config.port;
        config.reconnect = options.reconnect || config.reconnect;
        config.reconnect_sleep = options.reconnect_sleep || config.reconnect_sleep;
        config.send_delay = options.send_delay || config.send_delay;
    }

    // If no host is configured we connect to the first device to answer
    if (typeof config.host === 'undefined' || config.host === '') {
        self.discover(function (hosts) {
            if (hosts.length > 0) {
                config.host = hosts[0].host;
                self.connect();
            }
            return;
        });
        return;
    }

    connection_properties = {
        host: config.host,
        port: config.port
    };

    self.emit("debug", 'Connecting to ' + config.host + ':' + config.port);

    if (typeof itach === 'undefined') {

        itach = net.connect(connection_properties);

        queue = new MessageQueue({
            send_delay: config.send_delay
        });

    } else {
        
        itach.connect(connection_properties);
    }

    itach.on('connect', function () {

        self.is_connected = true;
        self.emit("debug", 'Connected to ' + config.host + ':' + config.port);
        self.emit('connect');
    });

    itach.on('close', function () {

        self.is_connected = false;
        self.emit("debug", 'Disconnected from ' + config.host + ':' + config.port);
        self.emit('close', false);

        if (config.reconnect) {

            setTimeout(self.connect, config.reconnect_sleep * 1000);
        }
    });

    itach.on('error', function (err) {

        self.emit('error', err);
    });

    itach.on('data', function (data) {

        var parts, id, result;

        data = data.toString().replace(/[\n\r]$/, "");

        self.emit("debug", "received data: " + data);

        parts = data.split(',');
        id = parts[2];

        if (parts[0] === 'completeir') {
            queue.onComplete(id);
        }

        if (parts[0] === 'busyIR') {

            // This shound not happen if this script is the only device connected to the itach
            return;

        } else if (parts[0].match(/^ERR/)) {

            self.emit("error", "itach error " + parts[1] + ": " + ERRORCODES[parts[1]]);
            queue.onError();
        }
    });

};