/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@function: Controller for iTach Global Cache
*******************************************************************************/
	
	module_command_list = [
		{name: 'Constructor', func: 'myModule = Modules.iTach.Global_Cache("169.254.1.2", 4998);', 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 Relay 1', func: 'myModule.setRelay1(1);', comment: '//set state to 1 or 0'},
		{name: 'Set Relay 2', func: 'myModule.setRelay2(1);', comment: '//set state to 1 or 0'},
		{name: 'Set Relay 3', func: 'myModule.setRelay3(1);', comment: '//set state to 1 or 0'},
		{name: 'Up', func: 'myModule.up();', comment: '//raise projector'},
		{name: 'Down', func: 'myModule.down();', comment: '//lower projector'},
		{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.iTach) Modules.iTach = {};
	
	Modules.iTach.Global_Cache = function(ip, port, onError){
			
		var obj = {};
		
		obj._ip = ip;
		obj.ip = ip;
		obj.error = false;
		obj.port = !undef(port) ? port : '4998';
		obj.onError = !undef(onError) ? onError : noop;
		
		obj.setPort = function(port){
			obj.port = port;
		}
		
		obj._updateInterval = false;
		obj.connection = false;
		
		obj.getStatus = function(onComplete){
			
			if (!obj.connection) obj.connection = tcpopen(obj.ip, obj.port);
			
			obj.connection.send(asciiToHex('getstate,'+'1:1'+'\r')+' '+
					asciiToHex('getstate,'+'1:2'+'\r')+' '+
					asciiToHex('getstate,'+'1:3'+'\r'), function(data){
				obj.status = {};
				obj.status.relay1state = parseInt(data.substring(10,11));
				obj.status.relay2state = parseInt(data.substring(22,23));
				obj.status.relay3state = parseInt(data.substring(data.length-2));
				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.setRelay1 = function(state){
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:1,'+state+'\r'));
		}
		obj.setRelay2 = function(state){
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:2,'+state+'\r'));
		}
		obj.setRelay3 = function(state){
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:3,'+state+'\r'));
		}
		
		obj.up = function(){
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:1,1\r'));
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:2,1\r'));
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:3,0\r'));
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:2,0\r'));
		}
		
		obj.down = function(){
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:3,1\r'));
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:1,1\r'));
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:2,0\r'));
			tcpclient(obj.ip, obj.port, asciiToHex('setstate,1:3,0\r'));
		}
		
		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);
		}
		
		return obj;
		
	}