// var parameterPath = local.parameters;
// var valuesPath = local.values;


function init() {
	//loadFixtures();
	script.log("hello VISCA");
}

function setZoom(ID, position){

	var idHex = (parseInt("0x80") + ID);


	var pos = Math.round(4000 * position);
	var posString = ""+pos;
	var pad = "0000";
	posString = pad.substring(0, pad.length - posString.length) + posString;
	script.log(posString);
	// script.log(posString.charAt(1));
	var posCommand = [parseInt("0x0"+posString.charAt(0)),
										parseInt("0x0"+posString.charAt(1)),
										parseInt("0x0"+posString.charAt(2)),
										parseInt("0x0"+posString.charAt(3))
	];
	// script.log(posCommand[0]);
	// script.log(posCommand[1]);
	// script.log(posCommand[2]);
	// script.log(posCommand[3]);
	// var packet = [idHex, , 00, 00, 00, 00, "ff"]
	// local.send(String.fromCharCode.apply(null, posCommand));
	// local.sendBytes(129, 1, 4, 7, 48, 255);
	// local.send()
	local.sendBytes(idHex, 1, 4, 71,
										posCommand, 255);


}

var buffer=[];
var bufferIndex = 0 ;
function dataReceived(data)
{
	//If mode is "Lines", you can expect data to be a single line String
	// script.log("Data received : " +data);

	//If mode is anything else, you can expect data to be an array of bytes
	// script.log(" Bytes received : "+data.length);
	// for(var i=0; i < data.length; i++)
	// {
	// 	script.log(" > " + parseInt((""+data[i]),16));
	// }

	//fill buffer with bytes until you find a 255
	for (var i = 0 ; i<data.length ; i++){
		if (data[i]!=255){
			buffer[bufferIndex]=data[i];
			bufferIndex += 1;

		}

		else{
			if(buffer[0]==144){	//0x90, you've got a message
				if (buffer[1]==65){script.log("received command");} //0x41
				if (buffer[1]==81){script.log("complete command");} //0x51
				if (buffer[1]==97){//0x61
					var msg = "error :";
					if(buffer[2]==1){msg=msg+"message length";}
					if(buffer[2]==2){msg=msg+"syntax";}
					if(buffer[2]==3){msg=msg+"command buffer full";}
					if(buffer[2]==4){msg=msg+"command cancelled";}
					if(buffer[2]==5){msg=msg+"no socket";}
					if(buffer[2]==65){msg=msg+"command not executable";}
					else{
							msg=msg+"unknow error";
							for(var j=2; j < buffer.length; j++)
							{
								script.log(" > " +data[i]);
							}
						}
						script.log(msg);
					}

			}
			bufferIndex = 0;
			buffer=[];
		}

	}
	//then process buffer

}