/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@function: Controller for Pulsar P02C1_750HS_ETHERNET Light Dimmer
*******************************************************************************/

	module_command_list = [
		{name: 'Constructor', func: 'myModule = Pulsar.P02C1_750HS_ETHERNET("169.254.1.2", 2101);', comment: '//port is optional'},
		{name: 'Get Status', func: 'myModule.getStatus(function onComplete(){});', comment: '//function passed in will run on complete'},
		{name: 'Start Updating', func: 'myModule.startUpdating(2);', comment: '//will begin updating module object\'s status'},
		{name: 'Stop Updating', func: 'myModule.stopUpdating();', comment: '//will stop updating module object\'s status'},
		{name: 'Set Brightness', func: 'myModule.setBrightness("00", "00", "01");', comment: '//arguments are brightness, channel, buffer'},
		{name: 'Print', func: 'myModule.print();', comment: '//Prints this module object and all of its properties to the Javascript console.'},
		{name: 'Popup', func: 'myModule.popup();', comment: '//Displays an alert dialog with this module object and all of its properties.'},
	];

	if (!Modules.Pulsar) Modules.Pulsar = {};

	Modules.Pulsar.P02C1_750HS_ETHERNET = function(ip, port, onError){
			
		var obj = {};
		
		obj._ip = ip;
		obj.ip = ip;
		obj.error = false;
		obj.port = !undef(port) ? port : '2101';
		obj.onError = !undef(onError) ? onError : noop;
		
		obj.setPort = function(port){
			obj.port = port;
		}
		
		obj._updateInterval = false;
		obj.connection = false;
		
		//gets the status of the unit and fills it in as properties on the object
		obj.getStatus = function(onComplete, delay){
		
			if (!obj.connection) obj.connection = tcpopen(obj.ip, obj.port);
			
			obj.connection.send('fd 01 00 01 20', function(data){
				if (data.substr(0,1) == 'U') data = data.substring(1);
				obj.status = {};
				obj.status.channel1 = asciiToHex(data.substr(0,1));
				obj.status.channel2 = asciiToHex(data.substr(1,1));
				obj.status.channel3 = asciiToHex(data.substr(2,1));
				console.log('brightstatus: ', asciiToHex(data.substr(0,1))+' '
					+asciiToHex(data.substr(1,1))+' '+asciiToHex(data.substr(2,1)));
				console.log('bs2: ', data);
				if (onComplete) onComplete(data);
				obj.connection.destroy(); obj.connection = false;
			});
			
			setTimeout(function(obj){
				if (obj.connection){
					obj.connection.destroy(); obj.connection = false;
				}
			}.bindArgs(obj), 1500);
			
		}
		
		//gets the status of the unit and fills it in as properties on the object
		//also sets a timeout to call the function again and calls onUpdate if specified
		obj.startUpdating = function(interval, onUpdate, delay){
			if (undef(interval)) var interval = 3;
			obj._updateInterval = setInterval(function(onUpdate, delay){
				obj.getStatus(onUpdate, delay);
			}.bindArgs(onUpdate, delay), interval*1000);
		}
		
		obj.stopUpdating = function(){
			clearInterval(obj._updateInterval);
		}
		
		obj.setBrightness = function(brightness, channel, buffer){
			var brightness = !undef(brightness) ? brightness : '00';
			var channel = !undef(channel) ? channel : '00';
			var buffer = !undef(buffer) ? buffer : '01';
			if (brightness.length === 1) brightness = '0' + brightness;
			if (channel.length === 1) channel = '0' + channel;
			if (buffer.length === 1) buffer = '0' + buffer;
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+'fd '+channel+' '+brightness+' '+buffer);
		}
		
		obj.effect = function(){
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+'fd a0 fd 04');
		}
		
		obj.stopeffect = function(){
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+'fd 96');
		}

		obj.print = function(){
			console.log(obj);
		}
		
		obj.popup = function(){
			var output = '';
			for (var property in obj) {
				if (typeof obj[property] != 'function') output += property + ': ' + obj[property]+'; ';
			}
			alert(output);
		}
		
		obj.getStatus();
		
		return obj;
		
	}
	