text: var tcp = require('../../tcp');
  var instance_skel = require('../../instance_skel');
  var TelnetSocket = require('../../telnet');
  var debug;
  var log;
  
  
  function instance(system, id, config) {
  	var self = this;
  
  	// Request id counter
  	self.request_id = 0;
  	self.login = false;
  	// super-constructor
  	instance_skel.apply(this, arguments);
  	self.status(1,'Initializing');
  	self.actions(); // export actions
  
  	return self;
  }
  
  instance.prototype.updateConfig = function(config) {
  	var self = this;
  	self.config = config;
  	self.init_tcp();
  };
  
  instance.prototype.incomingData = function(data) {
  	var self = this;
  	debug("OPTOMA IN", data);
  	if (data.match("Projector")) {
  		self.status(self.STATUS_OK,'OK');
  	}
  
  /*
  	// Match part of the copyright response from unit when a connection is made.
  	if (self.login === false && data.match("Extron Electronics")) {
  		self.status(self.STATUS_WARNING,'Logging in');
  		self.socket.write("2I"+ "\n"); // Query model description
  	}
  
  	if (self.login === false && data.match("Password:")) {
  		self.log('error', "expected no password");
  		self.status(self.STATUS_ERROR, 'expected no password');
  	}
  
  	// Match expected response from unit.
  	else if (self.login === false && data.match("Streaming")) {
  		self.login = true;
  		self.status(self.STATUS_OK);
  		debug("logged in");
  	}
  	// Heatbeat to keep connection alive
  	function heartbeat() {
  		self.login = false;
  		self.status(self.STATUS_WARNING,'Checking Connection');
  		self.socket.write("2I"+ "\n"); // should respond with model description eg: "Streaming Media Processor"
  		debug("Checking Connection");
  		}
  	if (self.login === true) {
  		clearInterval(self.heartbeat_interval);
  		var beat_period = 180; // Seconds
  		self.heartbeat_interval = setInterval(heartbeat, beat_period * 1000);
  	}
  	else {
  		debug("data nologin", data);
  	}*/
  
  };
  
  instance.prototype.init = function() {
  	var self = this;
  
  	debug = self.debug;
  	log = self.log;
  
  	self.init_tcp();
  };
  
  instance.prototype.init_tcp = function() {
  	var self = this;
  	var receivebuffer = '';
  
  	if (self.socket !== undefined) {
  		self.socket.destroy();
  		delete self.socket;
  		self.login = false;
  	}
  
  	if (self.config.host) {
  		self.socket = new TelnetSocket(self.config.host, 23);
  
  		self.socket.on('status_change', function (status, message) {
  			if (status !== self.STATUS_OK) {
  				self.status(status, message);
  			}
  		});
  
  		self.socket.on('error', function (err) {
  			debug("Network error", err);
  			self.log('error',"Network error: " + err.message);
  		});
  
  		self.socket.on('connect', function () {
  			debug("Connected");
  			self.login = false;
  		});
  
  		self.socket.on('error', function (err) {
  			debug("Network error", err);
  			self.log('error',"Network error: " + err.message);
  			self.login = false;
  		});
  
  		// if we get any data, display it to stdout
  		self.socket.on("data", function(buffer) {
  			var indata = buffer.toString("utf8");
  			self.incomingData(indata);
  		});
  
  		self.socket.on("iac", function(type, info) {
  			// tell remote we WONT do anything we're asked to DO
  			if (type == 'DO') {
  				socket.write(Buffer.from([ 255, 252, info ]));
  			}
  
  			// tell the remote DONT do whatever they WILL offer
  			if (type == 'WILL') {
  				socket.write(Buffer.from([ 255, 254, info ]));
  			}
  		});
  	}
  };
  
  
  // Return config fields for web config
  instance.prototype.config_fields = function () {
  	var self = this;
  
  	return [
  		{
  			type: 'text',
  			id: 'info',
  			width: 12,
  			label: 'Information',
  			value: 'This will establish a tcp connection to the projector'
  		},
  		{
  			type: 'textinput',
  			id: 'host',
  			label: 'Projector IP address',
  			width: 12,
  			default: '192.168.0.1',
  			regex: self.REGEX_IP
  		}
  	]
  };
  
  // When module gets deleted
  instance.prototype.destroy = function() {
  	var self = this;
  	//clearInterval (self.heartbeat_interval); //Stop Heartbeat
  
  	if (self.socket !== undefined) {
  		self.socket.destroy();
  	}
  
  	debug("destroy", self.id);
  };
  
  instance.prototype.actions = function(system) {
  	var self = this;
  	var actions = {
  		'power_off': {
  			label: 'Power off',
  			options: []
  		},
  		'power_on': {
  			label: 'Power on',
  			options: []
  		}
  	};
  
  	self.setActions(actions);
  }
  
  instance.prototype.action = function(action) {
  
  	var self = this;
  	var id = action.action;
  	var opt = action.options;
  	var cmd;
  
  	switch (id) {
  		case 'power_off':
  			cmd = "\x7E\x30\x30\x30\x30\x20\x30\x0D";
  			break;
  		case 'power_on':
  			cmd = "\x7E\x30\x30\x30\x30\x20\x31\x0D";
  			break;
  	}
  
  	if (cmd !== undefined) {
  
  		if (self.socket !== undefined && self.socket.connected) {
  			console.log(Buffer.from(cmd));
  			self.socket.write(cmd);
  		} else {
  			debug('Socket not connected :(');
  		}
  
  	}
  };
  
  instance_skel.extendedBy(instance);
  exports = module.exports = instance;
  