Skip to content
Snippets Groups Projects
gof2d.js 25.3 KiB
Newer Older
Lucio Zambon's avatar
Lucio Zambon committed
// jshint esversion: 6
	// sr/feedback/profile_s/CorrHorFast
	// sr/feedback/profile_s->GetSrCorrNames
	// sr/feedback/profile_s/OrbitHorFast
Lucio Zambon's avatar
Lucio Zambon committed
	// ./bpm.php?machine=elettra&src&sr
Lucio Zambon's avatar
Lucio Zambon committed
	let gofFactorsHor, gofFactorsVer;
Lucio Zambon's avatar
Lucio Zambon committed
	let gofSt, gofFps, gofDataTs, gPos;
	let gofMaxFps = 25;
Lucio Zambon's avatar
Lucio Zambon committed
	function lilclick(par) {
		if (par!=123) setTimeout(lilclick, 500, 123);
		gPos = $("#fps").children().eq(1).children()[0].getBoundingClientRect();
		console.log('lilclick', gPos);
		$('#fpsr').css('left', gPos.left);
		$('#fpsr').css('top', gPos.top+gPos.height-3);
	}
	$(".lil-gui.root").click(lilclick);
Lucio Zambon's avatar
Lucio Zambon committed
	function getParam(param, defaultValue) {
		if (document.location.search.indexOf(param+'=')>-1) return document.location.search.split(param+'=')[1].split('&')[0];
		return defaultValue;
	}
	if (document.location.search.indexOf('machine=esrf')>-1 || document.location.search.indexOf('machine=gof_esrf')>-1) {
Lucio Zambon's avatar
Lucio Zambon committed
		gofFactorsHor = {"": 1, "corr": 100000, 'bpm': getParam('positionScaleH', 0.00002)};
		gofFactorsVer = {"": 1, "corr": 1, 'bpm': getParam('positionScaleV', 0.00002)};
Lucio Zambon's avatar
Lucio Zambon committed
	}
	else if (document.location.search.indexOf('machine=gof_soleil')>-1) {
Lucio Zambon's avatar
Lucio Zambon committed
		gofFactorsHor = {"": 1, "corr": 50000, 'bpm': getParam('positionScaleH', 0.00002)};
		gofFactorsVer = {"": 1, "corr": 10000, 'bpm': getParam('positionScaleV', 0.00002)};
Lucio Zambon's avatar
Lucio Zambon committed
	}
	else if (document.location.search.indexOf('machine=elettra2')>-1) {
Lucio Zambon's avatar
Lucio Zambon committed
		gofFactorsHor = {"": 1, "corr": 500000, 'bpm': getParam('positionScaleH', 0.00002)};
		gofFactorsVer = {"": 1, "corr": 500000, 'bpm': getParam('positionScaleV', 0.00002)};
Lucio Zambon's avatar
Lucio Zambon committed
	}
	else {
Lucio Zambon's avatar
Lucio Zambon committed
		gofFactorsHor = {"": 1, "corr": getParam('positionScaleH', 0.005), 'bpm': getParam('positionScaleH', 0.00002)};
		gofFactorsVer = {"": 1, "corr": getParam('positionScaleV', 0.005), 'bpm': getParam('positionScaleV', 0.00002)};
Lucio Zambon's avatar
Lucio Zambon committed
	}
	let gof = document.location.search.indexOf('gof=')>-1? document.location.search.split('gof=')[1].split('&')[0]: '';
Lucio Zambon's avatar
Lucio Zambon committed
	const gofTimeout = document.location.search.indexOf('gofTimeout=')>-1? document.location.search.split('gofTimeout=')[1].split('&')[0]: 10;
Lucio Zambon's avatar
Lucio Zambon committed
	const gofPeriod = document.location.search.indexOf('gofPeriod=')>-1? document.location.search.split('gofPeriod=')[1].split('&')[0]: 200;
Lucio Zambon's avatar
Lucio Zambon committed
	const gofDisplayerPeriod = document.location.search.indexOf('gofDisplayerPeriod=')>-1? document.location.search.split('gofDisplayerPeriod=')[1].split('&')[0]: 40;
Lucio Zambon's avatar
Lucio Zambon committed
	const gofVisibility = gof==''? 'hidden': 'visible';
	const gofScaleType = 'slider';
	const gofSimfacilities = [];
	let gofDataHor = [];
	let gofDataVer = [];
	let gofScaleHor;
	let gofScaleVer;
	let gofBuffer = false;
	let gofLogScaleHor = false;
	let gofLogScaleVer = false;
	let gofFactorHor = document.location.search.indexOf('gofFactorHor=')>-1? document.location.search.split('gofFactorHor=')[1].split('&')[0]*1000: localStorage.getItem('reference'+gof+'hor')!=null? localStorage.getItem('reference'+gof+'hor'): gofFactorsHor[gof]*1000;
	let gofFactorVer = document.location.search.indexOf('gofFactorVer=')>-1? document.location.search.split('gofFactorVer=')[1].split('&')[0]*1000: localStorage.getItem('reference'+gof+'ver')!=null? localStorage.getItem('reference'+gof+'ver'): gofFactorsVer[gof]*1000;
	const gofScaleSteps = 10;
	// let ruler = document.location.search.indexOf('ruler')>-1;
	rulerInit();
Lucio Zambon's avatar
Lucio Zambon committed
	// main.addEventListener('mousemove', function(e) {getRulerposition(main, e, 'sr');});
Lucio Zambon's avatar
Lucio Zambon committed
	if (ruler) {
		main.style.cursor = 'crosshair';
	}
	else {
		$('#rulerg').hide();
		$('#trajectoryWidth').hide();
	}
	function myreference(param) {
		const dir = param.split('_')[0];
		const action = param.split('_')[1];
		if (action=='set') {
			localStorage.setItem('reference'+gof+dir, gofBuffer[dir=='hor'? 0: 1]);
Lucio Zambon's avatar
Lucio Zambon committed
			$("#sr_gofreference"+dir).attr('visibility', "visible");
Lucio Zambon's avatar
Lucio Zambon committed
		}
		if (action=='clear') {
			localStorage.removeItem('reference'+gof+dir);
			localStorage.removeItem('scale'+gof+dir);
Lucio Zambon's avatar
Lucio Zambon committed
			$("#sr_gofreference"+dir).attr('visibility', "hidden");
Lucio Zambon's avatar
Lucio Zambon committed
		}
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function gofElapseHor(step, up) {
Lucio Zambon's avatar
Lucio Zambon committed
		// console.log('elapseHor()',step,gofScaleHor.$fill.style.width, (gofScaleSteps-step)/gofScaleSteps*90);
		if (up) {
			if (step<gofScaleSteps) {const pc = Math.round((gofScaleSteps-step)/gofScaleSteps*90); gofScaleHor.$fill.style.width=pc+"%";setTimeout(elapseHor, 20, step+1, up); return;}
			const buf = gofScaleHor._max; 
			gofScaleHor.max(buf*10); 
			gofScaleHor.min(buf/10); 
			gofScaleHor.step(buf/100);
		}
		else {
			if (step<gofScaleSteps) {const pc = Math.round(step/gofScaleSteps*10); gofScaleHor.$fill.style.width=pc+"%";setTimeout(elapseHor, 20, step+1, up); return;}
			const buf = gofScaleHor._max/10; 
			gofScaleHor.max(buf); 
			gofScaleHor.min(buf/100);
			gofScaleHor.step(buf/1000);
		}
		gofScaleHor.$fill.style.width="10%";
		gofScaleHor.$fill.style.borderColor = '#2cc9ff';
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function gofElapseVer(step, up) {
Lucio Zambon's avatar
Lucio Zambon committed
		if (up) {
			if (step<gofScaleSteps) {const pc = Math.round((gofScaleSteps-step)/gofScaleSteps*90); gofScaleVer.$fill.style.width=pc+"%";setTimeout(elapseVer, 20, step+1, up); return;}
			const buf = gofScaleVer._max; 
			gofScaleVer.max(buf*10); 
			gofScaleVer.min(buf/10); 
			gofScaleVer.step(buf/100);
		}
		else {
			if (step<gofScaleSteps) {const pc = Math.round(step/gofScaleSteps*10); gofScaleVer.$fill.style.width=pc+"%";setTimeout(elapseVer, 20, step+1, up); return;}
			const buf = gofScaleVer._max/10; 
			gofScaleVer.max(buf); 
			gofScaleVer.min(buf/100);
			gofScaleVer.step(buf/1000);
		}
		gofScaleVer.$fill.style.width="10%";
		gofScaleVer.$fill.style.borderColor = '#2cc9ff';
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function gofFinishHor() {
		// console.log('gofFinishHor()', gofScaleHor.$fill.style.width);
Lucio Zambon's avatar
Lucio Zambon committed
		if (gofScaleHor.$fill.style.width=="100%" || gofScaleHor.$fill.style.width=="0%") {
			// gofScaleHor.$fill.style.borderColor = 'yellow';
Lucio Zambon's avatar
Lucio Zambon committed
			setTimeout(gofElapseHor, 200, 1, gofScaleHor.$fill.style.width=="100%");
Lucio Zambon's avatar
Lucio Zambon committed
		}
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function gofFinishVer() {
Lucio Zambon's avatar
Lucio Zambon committed
		if (gofScaleVer.$fill.style.width=="100%" || gofScaleVer.$fill.style.width=="0%") {
Lucio Zambon's avatar
Lucio Zambon committed
			setTimeout(gofElapseVer, 200, 1, gofScaleVer.$fill.style.width=="100%");
Lucio Zambon's avatar
Lucio Zambon committed
		}
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function gofSetfactorHor(v) {
		// console.log('gofSetfactorHor(v)', v, gofScaleHor.$fill.style.width);
Lucio Zambon's avatar
Lucio Zambon committed
		gofScaleHor.$fill.style.borderColor = (gofScaleHor.$fill.style.width=="100%" || gofScaleHor.$fill.style.width=="0%")? 'yellow': '#2cc9ff';
		gofFactorHor = v * 1000;
		if (localStorage.getItem('reference'+gof+'hor')!=null) localStorage.setItem('scale'+gof+'hor', gofFactorHor);
		rulerScale('');
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function gofSetfactorVer(v) {
		console.log('gofSetfactorVer(v)', v, gofScaleVer.$fill.style.width);
Lucio Zambon's avatar
Lucio Zambon committed
		gofScaleVer.$fill.style.borderColor = (gofScaleVer.$fill.style.width=="100%" || gofScaleVer.$fill.style.width=="0%")? 'yellow': '#2cc9ff';
		gofFactorVer = v * 1000;
		if (localStorage.getItem('reference'+gof+'ver')!=null) localStorage.setItem('scale'+gof+'ver', gofFactorVer);
		rulerScale('');
	}
	function logscale(dirNum) {
		if (dirNum==0) gofLogScaleHor = $('#logHor').attr('checked')=='checked';
		else gofLogScaleVer = $('#logVer').attr('checked')=='checked';
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function fpsSet(v) {
		gofMaxFps = v;
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function gofMenu(lattice, facilities, params) {
		console.log('gofMenu',lattice, facilities, params, gui);
		$('#notice').hide();
		if (['elettra2', 'esrf', 'soleil'].indexOf(machine)>-1) for (let i=0; i<gui.children.length; i++) {
			if (['vlv', 'ps', 'bpm', 'blm'].indexOf(gui.children[i].property)>-1) gui.children[i].domElement.style.display = 'none';
		}
		// params.gof = false;
		$('.gofhor').css('display', 'none');
		$('.gofver').css('display', 'none');
Lucio Zambon's avatar
Lucio Zambon committed
		params.gof = document.location.search.indexOf('gof')>-1 && document.location.search.indexOf('=gof')==-1? 'corr': '';
Lucio Zambon's avatar
Lucio Zambon committed
		if (document.location.search.indexOf('gof=')>-1) params.gof = document.location.search.split('gof=')[1].split('&')[0];
Lucio Zambon's avatar
Lucio Zambon committed
		const options = ['', 'corr', 'bpm'];
Lucio Zambon's avatar
Lucio Zambon committed
		if (options.indexOf(params.gof)==-1) params.gof = 'corr';
Lucio Zambon's avatar
Lucio Zambon committed
		gui.add(params, 'gof', options).name('gof H <img id="h" src="red.svg" style="margin-bottom: -4px;">&nbsp;&nbsp;&nbsp;V <img id="v" src="green.svg" style="margin-bottom: -4px;"> ').onChange(function() {gofSwitch(compData, params);});
		const maxVer = Math.pow(10, Math.floor(Math.log10(gofFactorVer/1000))+2);
		const maxHor = Math.pow(10, Math.floor(Math.log10(gofFactorHor/1000))+2);
Lucio Zambon's avatar
Lucio Zambon committed
		menuParams['fps'] = gofMaxFps;
		gofFpsMenu = gui.add(menuParams, 'fps', 0.5, 25, 0.5).onChange(function() {fpsSet(menuParams['fps']);}); // .title('fps (max)')
Lucio Zambon's avatar
Lucio Zambon committed
		guiconf(gui, 'fps');
Lucio Zambon's avatar
Lucio Zambon committed
		gPos = $("#fps").children().eq(1).children()[0].getBoundingClientRect();
		jQuery('<div>', {id: 'fpsr',    style: 'z-index: 1000; min-width: 45px; min-height: 3px; position: absolute; background-color: yellow; left: '+(gPos.left+0)+'px; top: '+(gPos.top+gPos.height-3)+'px;', title: 'fps',}).appendTo('body');
		// $('#fpsr').hide();
Lucio Zambon's avatar
Lucio Zambon committed
		if (gofScaleType == 'slider') {
			/*menuParams['ruler'] = ruler;
			gui.add(menuParams, 'ruler').onChange(function() {rulerSwitch();});
			guiconf(gui, 'ruler');*/
			menuParams['scale H'] = gofFactorHor/1000;
Lucio Zambon's avatar
Lucio Zambon committed
			gofScaleHor = gui.add(menuParams, 'scale H', maxVer/100, maxVer, Math.round(maxVer/1000)).onChange(function() {gofSetfactorHor(menuParams['scale H'], gui);}).onFinishChange(function() {gofFinishHor(gui);});
Lucio Zambon's avatar
Lucio Zambon committed
			guiconf(gui, 'scaleh');
			$('#scaleh').children().eq(0).css('color','red');
			menuParams['scale V'] = gofFactorHor/1000;
Lucio Zambon's avatar
Lucio Zambon committed
			gofScaleVer = gui.add(menuParams, 'scale V', maxHor/100, maxHor, Math.round(maxHor/1000)).onChange(function() {gofSetfactorVer(menuParams['scale V'], gui);}).onFinishChange(function() {gofFinishVer(gui);});
Lucio Zambon's avatar
Lucio Zambon committed
			guiconf(gui, 'scalev');
			$('#scalev').children().eq(0).css('color','green');
		}
		else {
			const controllerOption = $('.controller.option');
			for (let i=0; i<controllerOption.length; i++) {
				if (controllerOption.eq(i).children()[0].innerText.indexOf("gof H") > -1) {
					$('<div class="gofgauge"><iframe id="vergauge" style="width: 100%;height:250px;" src="../misc/gauge.html?dark&r=115&ringwidth=30&max='+maxVer+'&throttlingPeriod=50&apply=factorVer&extbackground=green&exthighlight=darkgreen&intbackground=green&inthighlight=darkgreen&val='+gofFactorVer/1000+'"></iframe></div>').insertAfter(controllerOption.eq(i));
					$('<div class="gofgauge"><iframe id="horgauge" style="width: 100%;height:250px;" src="../misc/gauge.html?dark&r=115&ringwidth=30&max='+maxHor+'&throttlingPeriod=50&apply=factorHor&extbackground=red&exthighlight=darkred&intbackground=red&inthighlight=darkred&val='+gofFactorHor/1000+'"></iframe></div>').insertAfter(controllerOption.eq(i));
					$('<div style="padding: 8px;" class="gofgauge">log scale  H <img id="h" src="red.svg" style="margin-bottom: -2px;"> <input type="checkbox" id="logHor" onChange="logscale(0)">&nbsp;&nbsp;&nbsp;log V <img id="h" src="green.svg" style="margin-bottom: -2px;"> <input type="checkbox" id="logVer" onChange="logscale(1)"></div>').insertAfter(controllerOption.eq(i));
					if (gof=='') $('.gofgauge').hide();
				}
			}
		}
		for (let f in facilities) {
			const b = facilities[f];
			if (b!='sr') continue;
			console.log('gofMenu() - ', b, lattice[b], conf.gofSrcUrl);
Lucio Zambon's avatar
Lucio Zambon committed
			compData[b].bpmMap = [];
			fetch('./bpm.php?machine=elettra&src&sr', {cache: "no-store"})
			.then((response) => {return response.json();})
			.then((gofNaming) => {
				console.log('gofBpmNaming', gofNaming, compData[b].bpmMap);
				for (let i in gofNaming) {
					const name = gofNaming[i];
					for (let bl in compData[b].obj) {
						if (name==compData[b].obj[bl] || name==compData[b].obj[bl].replace('.','_')) {compData[b].bpmMap[i] = bl;}
					}
				}
				for (let fi in facilities) {
					const facility = facilities[fi];
					if (facility!='sr') continue;
					console.log(fi, facility, compData);
					if (facility.length>0 && typeof compData[facility]!='undefined' && compData[facility].obj && compData[facility].obj.length>0) {
						console.log('gofBpmInit', facility, compData[facility]);
						const dhor = [];
						const dver = [];
						for (i=0; i<compData[facility].pos.length; i++) {
							// if (i>threshold) break; 
							const dir = compData[facility].dir[i]; 
							const beta = Math.PI*dir/180;
							if (threshold<1000) console.log('i', i, 'pos',compData[facility].pos[i], 'dir', dir, beta, Math.cos(beta), Math.sin(beta));
							dhor.push((i==0?'M':'L')+ 
								  Math.round(compData[facility].pos[i][0] - f*Math.sin(beta)) + ' ' + 
								  Math.round(compData[facility].pos[i][1] + f*Math.cos(beta))
							);
							dver.push((i==0?'M':'L')+ 
								  Math.round(compData[facility].pos[i][0] - f*1.05*Math.sin(beta)) + ' ' + 
								  Math.round(compData[facility].pos[i][1] + f*1.05*Math.cos(beta))
							);
						}
						console.log(dhor);
						appendSvg("path", {id:facility+"_gofbpmhor", class: "gofbpmhor trajectory "+facility, visibility:gofVisibility, name:"gofbpmhor", fill: "none", "stroke-width":"150", "stroke":"red", "stroke-opacity":"0.6", d: dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, gof_clicked_hor, false, false, '.svg-pan-zoom_viewport');
						appendSvg("path", {id:facility+"_gofbpmver", class: "gofbpmver trajectory "+facility, visibility:gofVisibility, name:"gofbpmver", fill: "none", "stroke-width":"150", "stroke":"limegreen", "stroke-opacity":"0.6", d: dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, gof_clicked_ver, false, false, '.svg-pan-zoom_viewport');
					}
				}
			});
Lucio Zambon's avatar
Lucio Zambon committed
			compData[b].corrMap = [];
Lucio Zambon's avatar
Lucio Zambon committed
			console.log((conf.gofSrcUrl+'&machine='+machine).replace('?&','?'));
			fetch((conf.gofSrcUrl+'&machine='+machine).replace('?&','?'), {cache: "no-store"})
			.then((response) => {return response.json();})
			.then((gofNaming) => {
Lucio Zambon's avatar
Lucio Zambon committed
				console.log('gofNaming', gofNaming, compData[b].corrMap);
Lucio Zambon's avatar
Lucio Zambon committed
				for (let i in gofNaming) {
					const name = gofNaming[i];
					for (let bl in compData[b].obj) {
Lucio Zambon's avatar
Lucio Zambon committed
						if (name==compData[b].obj[bl].replace('CHV', 'CORR') || name==compData[b].obj[bl].replace('CHV', 'CORR').replace('.','_')) {compData[b].corrMap[i] = bl;}
Lucio Zambon's avatar
Lucio Zambon committed
					}
				}
Lucio Zambon's avatar
Lucio Zambon committed
				console.log('gofMenu(), name', b, compData[b].corrMap);
Lucio Zambon's avatar
Lucio Zambon committed
				for (let fi in facilities) {
					const facility = facilities[fi];
					if (facility!='sr') continue;
					console.log(fi, facility, compData);
					if (facility.length>0 && typeof compData[facility]!='undefined' && compData[facility].obj && compData[facility].obj.length>0) {
						console.log('gofInit', facility, compData[facility]);
						const dhor = [];
						const dver = [];
						for (i=0; i<compData[facility].pos.length; i++) {
							// if (i>threshold) break; 
							const dir = compData[facility].dir[i]; 
							const beta = Math.PI*dir/180;
							if (threshold<1000) console.log('i', i, 'pos',compData[facility].pos[i], 'dir', dir, beta, Math.cos(beta), Math.sin(beta));
							dhor.push((i==0?'M':'L')+ 
								  Math.round(compData[facility].pos[i][0] - f*Math.sin(beta)) + ' ' + 
								  Math.round(compData[facility].pos[i][1] + f*Math.cos(beta))
							);
							dver.push((i==0?'M':'L')+ 
								  Math.round(compData[facility].pos[i][0] - f*1.05*Math.sin(beta)) + ' ' + 
								  Math.round(compData[facility].pos[i][1] + f*1.05*Math.cos(beta))
							);
						}
Lucio Zambon's avatar
Lucio Zambon committed
						appendSvg("path", {id:facility+"_gofhor", class: "gofhor trajectory "+facility, visibility:gofVisibility, name:"gofhor", fill: "none", "stroke-width":"150", "stroke":"red", "stroke-opacity":"0.6", d: dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, gof_clicked_hor, false, false, '.svg-pan-zoom_viewport');
						appendSvg("path", {id:facility+"_gofver", class: "gofver trajectory "+facility, visibility:gofVisibility, name:"gofver", fill: "none", "stroke-width":"150", "stroke":"limegreen", "stroke-opacity":"0.6", d: dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, gof_clicked_ver, false, false, '.svg-pan-zoom_viewport');
Lucio Zambon's avatar
Lucio Zambon committed
					}
				}
				rulerScale('');
			});
		}
		if (document.location.search.indexOf('gof')>-1) {/*params.gof=true;*/ setTimeout(gofSwitch, 500, compData, params);}
	}
	// const threshold = document.location.search.indexOf('threshold=')>-1? document.location.search.split('threshold=')[1].split('&')[0]: 1000;
	// const f = document.location.search.indexOf('factor=')>-1? document.location.search.split('factor=')[1].split('&')[0]: 3000;
	function gof_clicked_hor(event) {
		// console.log('gof hor', event);
		openTooltip('trajectory hor '+event.clientX+' '+event.clientY);
	}
	function gof_clicked_ver(lattice, params) {
		// console.log('gof ver');
		openTooltip('trajectory ver'+event.clientX+' '+event.clientY);
	}
	function gof2d(lattice, params) {
		console.log('gof2d',lattice, params);
		for (let i in lattice) {if (i!='conf') gofSimfacilities.push(i);}
		gofMenu(lattice, gofSimfacilities, params);
	}
	function gofSwitch(gofData, params) {
		gof = params.gof;
		gofScaleHor.$fill.style.width = "50%";
		gofScaleVer.$fill.style.width = "50%";
		if (gofData.reader !== false) {
			clearInterval(gofData.reader);
		}
		gofData.reader = false;
		if (params.gof=='') {
			for (let facility in gofData) {
Lucio Zambon's avatar
Lucio Zambon committed
				if (typeof $('#'+facility+"_gofhor").attr('visibility') != 'undefined') {
					$('#'+facility+"_gofhor").attr('visibility', "hidden");
					$('#'+facility+"_gofver").attr('visibility', "hidden");
Lucio Zambon's avatar
Lucio Zambon committed
				}
Lucio Zambon's avatar
Lucio Zambon committed
				if (typeof $('#'+facility+"_gofbpmhor").attr('visibility') != 'undefined') {
					$('#'+facility+"_gofbpmhor").attr('visibility', "hidden");
					$('#'+facility+"_gofbpmver").attr('visibility', "hidden");
				}
Lucio Zambon's avatar
Lucio Zambon committed
			}
			$('.gofgauge').hide();
Lucio Zambon's avatar
Lucio Zambon committed
			$('#fps').hide();
Lucio Zambon's avatar
Lucio Zambon committed
			$('#fpsr').hide();
Lucio Zambon's avatar
Lucio Zambon committed
			$('#scaleh').hide();
			$('#scalev').hide();
			$('#ruler').hide();
			$('#rulerg').hide();
			$('#notice').hide();
			$('#trajectoryWidth').hide();
			$('#application').hide();
			$('#applicationReference').hide();
Lucio Zambon's avatar
Lucio Zambon committed
			gofData.reader.clearInterval();
			gofMaxFps = 0;
Lucio Zambon's avatar
Lucio Zambon committed
		} 
		else {
Lucio Zambon's avatar
Lucio Zambon committed
			$('#fps').show();
Lucio Zambon's avatar
Lucio Zambon committed
			$('#fpsr').show();
Lucio Zambon's avatar
Lucio Zambon committed
			$('#scaleh').show();
			$('#scalev').show();
			$('#trajectoryWidth').show();
			$('#ruler').show();
			$('#notice').show();
			for (let facility in gofData) {
Lucio Zambon's avatar
Lucio Zambon committed
				if (typeof $('#'+facility+"_gofhor").attr('visibility') != 'undefined') {
Lucio Zambon's avatar
Lucio Zambon committed
					$('#'+facility+"_gof"+(params.gof=='corr'? '': params.gof)+"hor").attr('visibility', "visible");
					$('#'+facility+"_gof"+(params.gof=='corr'? '': params.gof)+"ver").attr('visibility', "visible");
Lucio Zambon's avatar
Lucio Zambon committed
				}
Lucio Zambon's avatar
Lucio Zambon committed
				if (typeof gofData[facility] == 'undefined' || typeof gofData[facility][params.gof+'Map'] == 'undefined') continue;
Lucio Zambon's avatar
Lucio Zambon committed
				gofData.oldIndex = facility;
				gofRead(gofData, params);
			}
			displayer = setInterval(gofDisplay, gofDisplayerPeriod, params);
			if (gofTimeout) {
				setTimeout(gofRead, gofTimeout, gofData, params);
			}
			else {
				gofData.reader = setInterval(gofRead, gofPeriod, gofData, params);
			}
			$('.gofhor').css('display', 'block');
			$('.gofver').css('display', 'block');
			gofFactorHor = localStorage.getItem('scale'+gof+'hor')!=null? localStorage.getItem('scale'+gof+'hor')-0: gofFactorsHor[menuParams.gof]*1000;
			gofFactorVer = localStorage.getItem('scale'+gof+'ver')!=null? localStorage.getItem('scale'+gof+'ver')-0: gofFactorsVer[menuParams.gof]*1000;
			if (gofScaleType == 'slider') {
				const fHor = gofFactorHor/1000;
				menuParams['scale H'] = fHor;
				const mxHor = Math.pow(10, Math.floor(Math.log10(fHor))+1);
				gofScaleHor.max(mxHor);
				gofScaleHor.min(mxHor/100);
				gofScaleHor.step(mxHor/1000);
				gofScaleHor.setValue(fHor);
				const fVer = gofFactorVer/1000;
				menuParams['scale V'] = fVer;
				const mxVer = Math.pow(10, Math.floor(Math.log10(fVer))+1);
				gofScaleVer.max(mxVer);
				gofScaleVer.min(mxVer/100);
				gofScaleVer.step(mxVer/1000);
				gofScaleVer.setValue(fVer);
			}
			else {
				document.getElementById("horgauge").contentWindow.maxval = Math.pow(10, Math.floor(Math.log10(gofFactorsHor[params.gof]))+2);
				document.getElementById("horgauge").contentWindow.reset();
				document.getElementById("horgauge").contentWindow.setVal(gofFactorsHor[params.gof]);
				document.getElementById("vergauge").contentWindow.maxval = Math.pow(10, Math.floor(Math.log10(gofFactorsVer[params.gof]))+2);
				document.getElementById("vergauge").contentWindow.reset();
				document.getElementById("vergauge").contentWindow.setVal(gofFactorsVer[params.gof]);
				$('.gofgauge').show();
			}
Lucio Zambon's avatar
Lucio Zambon committed
			gPos = $("#fps").children().eq(1).children()[0].getBoundingClientRect();
			$('#fpsr').css('left', gPos.left);
			$('#fpsr').css('top', gPos.top+gPos.height-3);
Lucio Zambon's avatar
Lucio Zambon committed
		}		
	}
	function gofRender(facility, gb, gof) {
Lucio Zambon's avatar
Lucio Zambon committed
		// console.log('gofRender(), gb', gb, gof);
Lucio Zambon's avatar
Lucio Zambon committed
		const horIndex = gof=='corr'? 0: 2;
		const verIndex = gof=='corr'? 1: 3;
Lucio Zambon's avatar
Lucio Zambon committed
		gofDataHor = [];
		gofDataVer = [];
		let refHor, refVer;
		const referencehor = JSON.parse(localStorage.getItem('reference'+gof+'hor'));
		const referencever = JSON.parse(localStorage.getItem('reference'+gof+'ver'));
		$('#hor_set').css('background-color',localStorage.getItem('reference'+gof+'hor')!=null?'red':'');
		$('#ver_set').css('background-color',localStorage.getItem('reference'+gof+'ver')!=null?'green':'');
Lucio Zambon's avatar
Lucio Zambon committed
		if ((gof!='') && gb[horIndex]) {
			const valh = gb[horIndex].split(',');
			const valv = gb[verIndex].split(',');
			console.log('gofRender(), valh, valv', valh, valv, gof, compData[facility].bpmMap);
Lucio Zambon's avatar
Lucio Zambon committed
			for (i=0; i<valh.length; i++) {
Lucio Zambon's avatar
Lucio Zambon committed
				if (gof=='corr' && typeof compData[facility].corrMap[i] == 'undefined') continue;
Lucio Zambon's avatar
Lucio Zambon committed
				if (gof=='bpm' && typeof compData[facility].bpmMap[i] == 'undefined') continue;
				const j = gof=='bpm'? compData[facility].bpmMap[i]: compData[facility].corrMap[i];
Lucio Zambon's avatar
Lucio Zambon committed
				if (document.location.search.indexOf('pulse=')>-1 && i!=document.location.search.split('pulse=')[1].split('&')[0]) {valh[i] = 0;}
				const pos = compData[facility].pos[j];
				if (pos==null) continue;
				const beta = Math.PI*compData[facility].dir[j]/180;
				const vh = localStorage.getItem('reference'+gof+'hor')==null? valh[i]-0: valh[i] - referencehor[i];
				// https://en.wikipedia.org/wiki/Logarithmic_scale#Extensions
				const valHor = gofLogScaleHor? Math.sign(vh)*Math.log10(1+ Math.abs(vh*Math.LN10)): vh;
Lucio Zambon's avatar
Lucio Zambon committed
				// console.log(i, j, pos, beta, vh, valHor);  
Lucio Zambon's avatar
Lucio Zambon committed
				const vv = localStorage.getItem('reference'+gof+'ver')==null? valv[i]-0: valv[i] - referencever[i];
				const valVer = gofLogScaleVer? Math.sign(vv)*Math.log10(1+ Math.abs(vv*Math.LN10)): vv;
				gofDataHor.push((gofDataHor.length==0?'M':'L')+ 
					Math.round(pos[0] - valHor*gofFactorHor*Math.sin(beta)) + ' ' + 
					Math.round(pos[1] + valHor*gofFactorHor*Math.cos(beta))
				);
				gofDataVer.push((gofDataVer.length==0?'M':'L')+ 
					Math.round(pos[0] - valVer*gofFactorVer*Math.sin(beta)) + ' ' + 
					Math.round(pos[1] + valVer*gofFactorVer*Math.cos(beta))
				);
			}
Lucio Zambon's avatar
Lucio Zambon committed
			// console.log('Hor', gofDataHor.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
Lucio Zambon's avatar
Lucio Zambon committed
			if (gof=='bpm') {
Lucio Zambon's avatar
Lucio Zambon committed
				$('#'+facility+"_gofbpmhor").attr('d', gofDataHor.join(' ').replaceAll('LNaN NaN L','L')+(lattice[facility].sections[0].chamber?' Z':''));
				$('#'+facility+"_gofbpmver").attr('d', gofDataVer.join(' ').replaceAll('LNaN NaN L','L')+(lattice[facility].sections[0].chamber?' Z':''));
Lucio Zambon's avatar
Lucio Zambon committed
			}
			else {
				$('#'+facility+"_gofhor").attr('d', gofDataHor.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
				$('#'+facility+"_gofver").attr('d', gofDataVer.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
			}
Lucio Zambon's avatar
Lucio Zambon committed
		}
	}
	function gofDisplay(params) {
		if (gofBuffer==false) return;
		gofRender('sr', gofBuffer, params.gof);
		$('#delay').html((new Date() - gofDataTs));
	}
	if (document.location.search.indexOf('nostats')==-1) {
		$('#notice').show(); 
		$('#notice').css('color', 'orange');												  
		$('#notice').html('<table style="font-size: 60%"><tr><td colspan="4" style="font-size: 150%" id="lt"></td></tr><tr><td style="width: 50%;">last refresh</td><td style="width: 50%; text-align: right;" id="requiredt"></td></tr><tr><td style="width: 50%;">delay</td><td style="width: 50%; text-align: right;" id="delay"></td></tr></table>');
	}
	function gofRead(gofData, params) {
Lucio Zambon's avatar
Lucio Zambon committed
		// console.log('fetch()',conf.gofUrl+'&param='+params.gof);
		const gofOldSt = gofSt;
Lucio Zambon's avatar
Lucio Zambon committed
		gofSt = new Date();
Lucio Zambon's avatar
Lucio Zambon committed
		gofFps = 1000/(gofSt-gofOldSt);
Lucio Zambon's avatar
Lucio Zambon committed
		if (gofFps<30) $('#fpsr').css('min-width', gofFps/20*gPos.width);
Lucio Zambon's avatar
Lucio Zambon committed
		// console.log(1000/(gofSt-gofOldSt));
Lucio Zambon's avatar
Lucio Zambon committed
		$('#lt').html(gofSt.getFullYear()+'-'+pad(gofSt.getMonth()+1, 2)+'-'+pad(gofSt.getDate(), 2)+' '+pad(gofSt.getHours(), 2)+':'+pad(gofSt.getMinutes(), 2)+':'+pad(gofSt.getSeconds(), 2));
		fetch((conf.gofUrl+'&machine='+machine+'&param='+params.gof).replace('?&','?'), {cache: "no-store"})
		.then((response) => {return response.text();})
		.then((eventData) => {
			if (document.location.search.indexOf('nostats')==-1) {
Lucio Zambon's avatar
Lucio Zambon committed
				// console.log('fetch(eventData)',eventData);
Lucio Zambon's avatar
Lucio Zambon committed
				const lt = new Date();
				const requiredt = new Date() - gofSt;
				gofDataTs = eventData.split(':')[0];
				$('#requiredt').html(requiredt);
				$('#delay').html(lt - gofDataTs);
			}
			gofBuffer = eventData.split(':')[1].split(';');
Lucio Zambon's avatar
Lucio Zambon committed
			const gofMinPeriod = 1000/gofMaxFps;
			const elapsed = new Date() - gofSt;
			const gp = gofMinPeriod-elapsed > gofTimeout? gofMinPeriod-elapsed: gofTimeout;
Lucio Zambon's avatar
Lucio Zambon committed
			if (params.gof!='') setTimeout(gofRead, gp, gofData, params);
		})
		.catch((err) => {
			const gofMinPeriod = 1000/gofMaxFps;
			const elapsed = new Date() - gofSt;
			const gp = gofMinPeriod-elapsed > gofTimeout? gofMinPeriod-elapsed: gofTimeout;
			if (params.gof!='') setTimeout(gofRead, gp, gofData, params);
Lucio Zambon's avatar
Lucio Zambon committed
		});
	}