From bcf508ee3dfe2f0dfb319426a2fb690bd54b2e47 Mon Sep 17 00:00:00 2001
From: Lucio Zambon <lucio.zambon@elettra.eu>
Date: Mon, 29 Apr 2024 03:03:51 +0000
Subject: [PATCH] Update bpm2d.js

---
 bpm2d.js | 171 ++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 106 insertions(+), 65 deletions(-)

diff --git a/bpm2d.js b/bpm2d.js
index 3d1d32a..575cc99 100644
--- a/bpm2d.js
+++ b/bpm2d.js
@@ -2,7 +2,38 @@
 	const bpmData = {};
 	const corr = {};
 	function bpmMenu(lattice, facilities, params) {
-		if (document.location.search.indexOf('new')>-1) {bpmMenuNew(params); return;}
+		if (document.location.search.indexOf('old')>-1) {bpmMenuOld(lattice, facilities, params); return;}
+		params.bpm = '';
+		$('.bpmhor').css('display', 'none');
+		$('.bpmver').css('display', 'none');
+		params.bpm = document.location.search.indexOf('bpm')>-1 && document.location.search.indexOf('=bpm')==-1;
+		gui.add(params, 'bpm').name('bpm 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() {bpmSwitch(bpmData, params);});
+		for (let f in facilities) {
+			const b = facilities[f];
+			if (b=='' || typeof bpmData[b] == 'undefined' || typeof lattice[b].bpm == 'undefined' || typeof lattice[b].bpm.orbitconf == 'undefined') continue;
+			console.log('bpmMenu() - ',b);
+			// bpmData[b].datasrc = lattice[b].bpm.orbithor+';'+lattice[b].bpm.orbitver;
+			bpmData[b].datasrc = lattice[b].bpm.orbithor;
+			bpmData[b].map = [];
+			fetch(conf.rchan+lattice[b].bpm.orbitconf) 
+			.then((response) => {return response.json();})
+			.then((bpmData2) => {			
+				let bpmCounter = 0;
+				let photoCounter = 0;
+				for (let i in bpmData2) {
+					const name = bpmData2[i];
+					if (name.indexOf(bpmData[b].skip)>-1) continue;
+					for (let bl in bpmData[b].obj) {
+						// console.log('b', bl, bpmData[b].obj, bpmData[b].obj[bl]);
+						if (name==bpmData[b].obj[bl]) {bpmData[b].map[bl] = bpmCounter; bpmCounter++;}
+					}
+				}
+				console.log('bpmMenu(), name', b, bpmData[b], lattice[b]);
+			});
+		}
+	}
+	function bpmMenuOld(lattice, facilities, params) {
+		if (document.location.search.indexOf('old')>-1) {bpmMenuOld(lattice, facilities, params); return;}
 		params.bpm = '';
 		$('.bpmhor').css('display', 'none');
 		$('.bpmver').css('display', 'none');
@@ -32,7 +63,7 @@
 			});
 		}
 	}
-	function bpmMenuNew(params) {
+	function bpmMenuNew(lattice, facilities, params) {
 		params.bpm = '';
 		bpmSwitchNew(params);
 		fetch(conf.bpmSrcUrl)
@@ -53,28 +84,32 @@
 	}
 	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 bpmInit(facility) {
-		if (bpmData[facility].obj.length>0) {
-			console.log('bpmInit', facility, bpmData[facility]);
-			const dhor = [];
-			const dver = [];
-			for (i=0; i<bpmData[facility].pos.length; i++) {
-				if (i>threshold) break; 
-				const dir = bpmData[facility].dir[i]; 
-				const beta = Math.PI*dir/180;
-				if (threshold<1000) console.log('i', i, 'pos',bpmData[facility].pos[i], 'dir', dir, beta, Math.cos(beta), Math.sin(beta));
-				dhor.push((i==0?'M':'L')+ 
-					  Math.round(bpmData[facility].pos[i][0] - f*Math.sin(beta)) + ' ' + 
-					  Math.round(bpmData[facility].pos[i][1] + f*Math.cos(beta))
-				);
-				dver.push((i==0?'M':'L')+ 
-					  Math.round(bpmData[facility].pos[i][0] - f*1.05*Math.sin(beta)) + ' ' + 
-					  Math.round(bpmData[facility].pos[i][1] + f*1.05*Math.cos(beta))
-				);
+	function bpmInit(facilities) {
+		console.log('bpmData', bpmData);
+		for (let fi in facilities) {
+			const facility = facilities[fi];
+			if (facility.length>0 && bpmData[facility].obj.length>0) {
+				console.log('bpmInit', facility, bpmData[facility]);
+				const dhor = [];
+				const dver = [];
+				for (i=0; i<bpmData[facility].pos.length; i++) {
+					if (i>threshold) break; 
+					const dir = bpmData[facility].dir[i]; 
+					const beta = Math.PI*dir/180;
+					if (threshold<1000) console.log('i', i, 'pos',bpmData[facility].pos[i], 'dir', dir, beta, Math.cos(beta), Math.sin(beta));
+					dhor.push((i==0?'M':'L')+ 
+						  Math.round(bpmData[facility].pos[i][0] - f*Math.sin(beta)) + ' ' + 
+						  Math.round(bpmData[facility].pos[i][1] + f*Math.cos(beta))
+					);
+					dver.push((i==0?'M':'L')+ 
+						  Math.round(bpmData[facility].pos[i][0] - f*1.05*Math.sin(beta)) + ' ' + 
+						  Math.round(bpmData[facility].pos[i][1] + f*1.05*Math.cos(beta))
+					);
+				}
+				appendSvg("path", {id:facility+"_bpmhor", class: "bpmhor "+facility, visibility:"hidden", name:"bpmhor", fill: "none", "stroke-width":"50", "stroke":"red", d: dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')});
+				appendSvg("path", {id:facility+"_bpmver", class: "bpmver "+facility, visibility:"hidden", name:"bpmver", fill: "none", "stroke-width":"50", "stroke":"green", d: dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')});
+				console.log("path", {id:facility+"_bpmhor", name:"bpmhor", d: dver.join(' ')+' Z'});
 			}
-			appendSvg("path", {id:facility+"_bpmhor", class: "bpmhor "+facility, visibility:"hidden", name:"bpmhor", fill: "none", "stroke-width":"50", "stroke":"red", d: dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')});
-			appendSvg("path", {id:facility+"_bpmver", class: "bpmver "+facility, visibility:"hidden", name:"bpmver", fill: "none", "stroke-width":"50", "stroke":"green", d: dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')});
-			console.log("path", {id:facility+"_bpmhor", name:"bpmhor", d: dver.join(' ')+' Z'});
 		}
 	}
 	function bpmSwitch(bpmData, params) {
@@ -85,13 +120,13 @@
 			$('#applicationFrame').removeAttr("src");
 		} 
 		else {
-			if (typeof params.bpm != 'string') return;
-			const bpms = params.bpm.split(',');
-			for (let bs=0; bs<bpms.length; bs++) { 
-				bpmData.oldIndex = bpms[bs];
+			const bpms = 'sr,bts,preinjector'.split(','); // <<<<<<<<<<<<<<<<<<<<<<<<--------------------------------------------------
+			for (let facility in bpmData) {
+				if (typeof bpmData[facility].map == 'undefined') continue;
+				bpmData.oldIndex = facility;
 				bpmRead(bpmData, params);
-				$('#'+bpms[bs]+"_bpmhor").attr('visibility', "visible");
-				$('#'+bpms[bs]+"_bpmver").attr('visibility', "visible");
+				$('#'+facility+"_bpmhor").attr('visibility', "visible");
+				$('#'+facility+"_bpmver").attr('visibility', "visible");
 			}
 			bpmData.reader = setInterval(bpmRead, 3000, bpmData, params);
 			$('#application').show();	
@@ -116,7 +151,7 @@
 			$('#'+params.bpm+"_bpmhor").attr('visibility', "visible");
 			$('#'+params.bpm+"_bpmver").attr('visibility', "visible");
 
-			bpmData.reader = setInterval(bpmRead, 3000, bpmData, params);
+			bpmData.reader = setInterval(bpmRead, 300, bpmData, params);
 			$('#application').show();	
 			$('.bpmhor.'+params.bpm).css('display', 'block');
 			$('.bpmver.'+params.bpm).css('display', 'block');
@@ -124,48 +159,54 @@
 			$("#applicationFrame").height(window.innerHeight); 
 		}		
 	}
-	function bpmRender(bpm, bpmData) {
-		const facility = bpm.split('_')[0];
+	function bpmRender(facility, val) {
+		// console.log('bpmRender()',facility, val);
+/*
+		const bpmMaxX = Math.max.apply(null, val.x.map(Math.abs)); // https://stackoverflow.com/questions/29515761/find-absolute-max-value-in-javascript-array
+		const bpmMaxY = Math.max.apply(null, val.y.map(Math.abs));
+		let bpmPeakX = (Math.log10(bpmMaxX) + 2) * 5000;
+		let bpmPeakY = (Math.log10(bpmMaxY) + 2) * 5000;
+		bpmPeakX = bpmPeakX>20000? 20000: (bpmPeakX<2000? 2000: bpmPeakX);
+		bpmPeakY = bpmPeakY>20000? 20000: (bpmPeakY<2000? 2000: bpmPeakY);
+		const bpmFactorX = bpmPeakX/bpmMaxX;
+		const bpmFactorY = bpmPeakY/bpmMaxY;
+/*
 		console.log('bpmRender(), facility', facility, 'bpmData', bpmData);
 		const bpmd = facility==''? bpmData: bpmData[facility];
 		const bpmMax = Math.max.apply(null, bpmd.val.map(Math.abs)); // https://stackoverflow.com/questions/29515761/find-absolute-max-value-in-javascript-array
 		let bpmPeak = (Math.log10(bpmMax) + 2) * 5000;
 		bpmPeak = bpmPeak>20000? 20000: (bpmPeak<2000? 2000: bpmPeak);
-		const bpmFactor = bpmPeak/bpmMax;
-			const dhor = [];
-			const dver = [];
-			for (i=0; i<bpmd.pos.length; i++) {
-				if (i>threshold) break; 
-				const dir = bpmd.dir[i]; 
-				const beta = Math.PI*dir/180;
-				if (threshold<1000) console.log('i', i, 'pos',bpmd.pos[i], 'dir', dir, beta, Math.cos(beta), Math.sin(beta));
-				dhor.push((i==0?'M':'L')+ 
-					  Math.round(bpmd.pos[i][0] - bpmd.val[0][i]/1000*Math.sin(beta)) + ' ' + 
-					  Math.round(bpmd.pos[i][1] + bpmd.val[0][i]/1000*Math.cos(beta))
-				);
-				dver.push((i==0?'M':'L')+ 
-					  Math.round(bpmd.pos[i][0] - bpmd.val[1][i]/1000*Math.sin(beta)) + ' ' + 
-					  Math.round(bpmd.pos[i][1] + bpmd.val[1][i]/1000*Math.cos(beta))
-				);
-			}
-			$('#'+facility+"_bpmhor").attr('d', dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
-			$('#'+facility+"_bpmver").attr('d', dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
-			console.log("bpmRender(),", {id:facility+"_bpmhor", name:"bpmhor", d: dver.join(' ')+' Z'});
+*/
+		const dhor = [];
+		const dver = [];
+		for (i=0; i<val.Hor.length; i++) {
+			// if (i>threshold) break; 
+			const pos = bpmData[facility].pos[i];
+			if (pos==null) continue;
+			const beta = Math.PI*bpmData[facility].dir[i]/180;
+			// console.log('i', i, 'pos',pos, 'dir', bpmData[facility].dir[i], 'facility', facility, beta, Math.cos(beta), Math.sin(beta));
+			dhor.push((i==0?'M':'L')+ 
+				Math.round(pos[0] - val.Hor[i]/1000*Math.sin(beta)) + ' ' + 
+				Math.round(pos[1] + val.Hor[i]/1000*Math.cos(beta))
+			);
+			dver.push((i==0?'M':'L')+ 
+				Math.round(pos[0] - val.Ver[i]/1000*Math.sin(beta)) + ' ' + 
+				Math.round(pos[1] + val.Ver[i]/1000*Math.cos(beta))
+			);
+		}
+		$('#'+facility+"_bpmhor").attr('d', dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
+		$('#'+facility+"_bpmver").attr('d', dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
+		// console.log("bpmRender(),", {id:facility+"_bpmhor", name:"bpmhor", d: dver.join(' ')+' Z'});
 		// if (params.bpm) {bpmLabel(bpmData, params, camera, THREE);}
 	}
 	function bpmRead(bpmData, params) {
-		const bpms = params.bpm.split(',');
-		console.log('bpms',bpms);
-		for (let bs=0; bs<bpms.length; bs++) { 
-			if (bpmData[bpms[bs]] && bpmData[bpms[bs]].datasrc) {
-				console.log((bpmData[bpms[bs]].datasrc.indexOf('http')>-1? '': conf.rchan)+bpmData[bpms[bs]].datasrc);
-				fetch((bpmData[bpms[bs]].datasrc.indexOf('http')>-1? '': conf.rchan)+bpmData[bpms[bs]].datasrc)
-				.then((response) => {return response.json();})
-				.then((eventData) => {
-					bpmData[bpms[bs]].val = eventData;
-					console.log('bpmRead()', conf.rchan+bpmData[bpms[bs]].datasrc, bpms[bs], bpmData, eventData);
-					bpmRender(bpms[bs], bpmData);
-				});
+		fetch(conf.bpmUrl)
+		.then((response) => {return response.json();})
+		.then((eventData) => {
+			// console.log('bpmRead()', eventData);
+			for (let i in eventData) {
+				// console.log(i, eventData[i]);
+				bpmRender(i, eventData[i]);
 			}
-		}
+		});
 	}
-- 
GitLab