From 96ac0f5b239da211a0ab25748d11e3adfb6595b6 Mon Sep 17 00:00:00 2001
From: Lucio Zambon <lucio.zambon@elettra.eu>
Date: Thu, 23 May 2024 17:38:24 +0000
Subject: [PATCH] Update panther.js

---
 panther.js | 114 +++++++++++++++++++++++++----------------------------
 1 file changed, 54 insertions(+), 60 deletions(-)

diff --git a/panther.js b/panther.js
index f4f45b8..105ba7a 100644
--- a/panther.js
+++ b/panther.js
@@ -56,7 +56,7 @@
 	const vlv = document.location.search.indexOf('vlv')>-1;
 	const vlvs = [];
 	const ps = document.location.search.indexOf('ps')>-1;
-	const demo = document.location.search.indexOf('demo')>-1 || document.location.search.indexOf('machine=elettra2')>-1;
+	const demo = document.location.search.indexOf('demo')>-1; // || document.location.search.indexOf('machine=elettra2')>-1;
 	if (demo) document.getElementById('demo').style.display = 'block';
 	let camera, controls, scene, renderer;
 	let composer, outlinePass;			
@@ -90,7 +90,8 @@
 		fetch(conf.rchan+'srv-tango-srf-01:20000/f/access_control/safety/Undulator_access_state').then((response) => {return response.json();}).then((fel) => {
 			fel1 = fel[5] == 1;
 			fel2 = fel[6] == 1;
-		});
+		})
+		.catch(error => {console.log("Fetch error", error);});
 	}
 	const gui = new GUI();
 	gui.add(params, 'machine', conf.machineList).onChange(function() {toggleMachine(params.machine);});
@@ -116,7 +117,7 @@
 	}
 	export function bpm_reference(action) {
 		const name = $('.name:contains("bpmReference")').parent().select('.widget').children().eq(1).children('input').val();
-		if (action=='save') fetch(bpm.srv+'?save='+name+'&content='+JSON.stringify(bpmData)).then((response) => {return response.text();}).then((res) =>{alert(res=='OK'? 'BPM reference saved': res);});
+		if (action=='save') fetch(bpm.srv+'?save='+name+'&content='+JSON.stringify(bpmData)).then((response) => {return response.text();}).then((res) =>{alert(res=='OK'? 'BPM reference saved': res);}).catch(error => {console.log("Fetch error", error);});
 		else fetch(bpm.srv+'?open='+name).then((response) => {return response.json();}).then((res) =>{
 			for (let facility in res) {
 				// if (facility=='bts') use https://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/fb/bts/traj/SensorReferenceValues
@@ -128,7 +129,8 @@
 				bpmReferenceMesh[facility] = new THREE.Mesh(bpmGeometry, material);
 				facilities[facility].add(bpmReferenceMesh[facility]);
 			}
-		});
+		})
+		.catch(error => {console.log("Fetch error", error);});
 	}
 	window.bpmreference = bpm_reference;
 	function updateBpm(data, facility) {
@@ -146,23 +148,6 @@
 		bpmMesh[facility].geometry.dynamic = true;
 		data = null;
 	}
-	function readBpm(facility) {
-		console.log('readBpm()', facility,lattice[facility].bpm.pos);
-		if (document.location.search.indexOf('bpm=')>-1) {
-			if(bpmPoints[facility]) {
-				const data = [new Array(bpmPoints[facility].length+bpmSkip[facility].length).fill(1), new Array(bpmPoints[facility].length+bpmSkip[facility].length).fill(1)];
-				data[0][document.location.search.split('bpm=')[1].split('&')[0]] = 8;
-				for (let i=0; i<bpmSkip[facility].length; i++) {data[0].splice(bpmSkip[facility][i]+1, 1); data[1].splice(bpmSkip[facility][i]+1, 1);}
-				updateBpm(data, facility);
-			}
-		}
-		else fetch(lattice[facility].bpm.pos)
-		.then((response) => {return response.json();})
-		.then((data) => {
-			for (let i=0; i<bpmSkip[facility].length; i++) {data[0].splice(bpmSkip[facility][i]+1, 1); data[1].splice(bpmSkip[facility][i]+1, 1);}
-			updateBpm(data, facility);
-		});
-	}
 	function readBpms() {
 		fetch(conf.bpmUrl)
 		.then((response) => {return response.json();})
@@ -178,7 +163,8 @@
 				$('#bpmfreq').val(bpmfreq.toPrecision(2) + ' fps');
 				// console.log(bpmfreq);
 			}
-		});
+		})
+		.catch(error => {console.log("Fetch error", error);});
 	}
 	function bpmPath(data, facility) {
 		let len = lattice[facility].bpm.length - bpmSkip[facility].length;
@@ -256,7 +242,7 @@
 				if (i=='servicearea' && document.location.search.indexOf(i)==-1) params[i] = false;
 				machineFolder.add(params, i).onChange(mytoggleFacility);
 				if (lattice[i].blm) {blmfacilities.push(i);}
-				console.log('fetch()', latticeFile, i, params[i]);
+				// console.log('fetch()', latticeFile, i, params[i]);
 				if (lattice[i].bpm && (typeof params.bpm == 'undefined')) {
 					params.bpm = document.location.search.indexOf('&bpm')>-1 || document.location.search.indexOf('?bpm')>-1; gui.add(params, 'bpm').onChange(function() {toggleParam('bpm');});
 					if (params.bpm) {
@@ -297,24 +283,25 @@
 		$('#ui-id-1').css('z-index', 10000000);
 		if (document.location.search.indexOf('highlight=')>-1) {$('<div><iframe style="width: 100%;height:250px;" src="../misc/gauge.html?dark&r2only=1&r=100&ringwidth=58&max=1&throttlingPeriod=50&apply=shrink&val='+params.highlightShrink+'"></iframe></div>').insertBefore('.function');}
 		for (let i in lattice) {if (i!='conf') initLattice(lattice[i].sections, i);}
-			if (vlv) {
-				fetch(conf.vlvSrcUrl, {cache: "no-store"}).then((response) => {return response.text();}).then((data) => {
-					const vlvSrc = data.toUpperCase().substring(14).split(',');
-					for (let i=0; i<vlvs.length; i++) { 
-						for (let j=0; j<vlvSrc.length; j++) {
-							const name = vlvSrc[j].split('/')[3];
-							if (vlvs[i].name.indexOf(name)>-1) {vlvs[i].vlvsrc = vlv; vlvs[i].vlvindex = j;}
-							// conf.bstmap.base
-							if (vlvSrc[j].split('/')[2]=="ACCESS_CONTROL" && i>39 && i<41) console.log('bst', j, vlvSrc[j], conf.bstmap.base, vlvs[i].name);
-							if (vlvSrc[j].split('/')[2].indexOf(conf.bstmap.base)>-1) {
-								if (conf.bstmap[vlvs[i].name] && conf.bstmap[vlvs[i].name].indexOf(vlvSrc[j].split('/')[4])>-1) {vlvs[i].vlvsrc = 'bst'; vlvs[i].vlvindex = j;}
-							}
+		if (vlv) {
+			fetch(conf.vlvSrcUrl, {cache: "no-store"}).then((response) => {return response.text();}).then((data) => {
+				const vlvSrc = data.toUpperCase().substring(14).split(',');
+				for (let i=0; i<vlvs.length; i++) { 
+					for (let j=0; j<vlvSrc.length; j++) {
+						const name = vlvSrc[j].split('/')[3];
+						if (vlvs[i].name.indexOf(name)>-1) {vlvs[i].vlvsrc = vlv; vlvs[i].vlvindex = j;}
+						// conf.bstmap.base
+						if (vlvSrc[j].split('/')[2]=="ACCESS_CONTROL" && i>39 && i<41) console.log('bst', j, vlvSrc[j], conf.bstmap.base, vlvs[i].name);
+						if (vlvSrc[j].indexOf('/')>-1 && vlvSrc[j].split('/')[2].indexOf(conf.bstmap.base)>-1) {
+							if (conf.bstmap[vlvs[i].name] && conf.bstmap[vlvs[i].name].indexOf(vlvSrc[j].split('/')[4])>-1) {vlvs[i].vlvsrc = 'bst'; vlvs[i].vlvindex = j;}
 						}
 					}
-					console.log('vlvSrc', data, vlvSrc, conf.bstmap, vlvs);
-				});
-				setInterval(updateVlv, 1000);
-			}
+				}
+				console.log('vlvSrc', data, vlvSrc, conf.bstmap, vlvs);
+			})
+			.catch(error => {console.log("Fetch error", error);});
+			setInterval(updateVlv, 1000);
+		}
 		params.goto2D = function() {document.location = './panther2d.php?machine='+params.machine;};
 		gui.add(params, 'goto2D').name('2D');
 		if (document.location.search.indexOf('search=')>-1) findComponent(document.location.search.split('search=')[1].split('&')[0], false);
@@ -333,10 +320,12 @@
 					}
 				}
 				console.log('statSrc', statSrc, status);
-			});
+			})
+			.catch(error => {console.log("Fetch error", error);});
 			setInterval(updateStatus, 1000);
 		}
-	});
+	})
+	.catch(error => {console.log("Fetch error", error);});
 	function showStatus(i, stat) {
 		if (stat == 0 || stat == 'null' || stat == '' || stat == 'ON' || stat == 'RUNNING' || (!fel1 && status[i].facility=='fel1') || (!fel2 && status[i].facility=='fel2')) {status[i].visible = false;}
 		else {status[i].visible = true; status[i].material.color.set(conf.stateLabelColor[stat]);}
@@ -358,7 +347,8 @@
 				}
 			}
 			setTimeout(clearStatus, 600);
-		});
+		})
+		.catch(error => {console.log("Fetch error", error);});
 	}
 	for (let i=0; i<envelopeNum; i++) { envelopeSize[i] = 500;}
 	function updateEnvelopeCenters(data) {
@@ -399,7 +389,8 @@
 			.then((response) => {return response.json();})
 			.then((eventData) => {
 			updateEnvelopeCenters(eventData);
-		});
+		})
+		.catch(error => {console.log("Fetch error", error);});
 	}
 	if (document.location.search.indexOf('envelope')>-1) {
 		readEnvelope();
@@ -466,27 +457,28 @@
 			if (magnet=='blm') {blmres.append(blm, facility, mycomp, direction);}
 			if (magnet=='vlv') {vlvs.push({'comp': mycomp, 'name': mycomp.name, 'type': magnet});}
 			if (magnet=='bst') {vlvs.push({'comp': mycomp, 'name': mycomp.name, 'type': magnet});}
-
-			if (mycomp.ps) {
-				for (let pi=0; pi<mycomp.ps.length; pi++) {
+			if (Math.abs(den)>0.00001) {
+				if (mycomp.ps) {
+					for (let pi=0; pi<mycomp.ps.length; pi++) {
+						const sphere = new THREE.Mesh(sphereGeometry,normalMaterial.clone());
+						sphere.position.set(params.highlightScale*(lattice[facility].sections[i].start.x + tang*d / den) - 50 + 50*pi, y+350, params.highlightScale*(lattice[facility].sections[i].start.z + d / den));
+						sphere.name = mycomp.ps[pi].replace('PS','') + '_status';
+						sphere.facility = facility;
+						sphere.visible = false;
+						status.push(sphere);
+						facilities[facility].add(sphere);
+					}
+				}
+				else {
 					const sphere = new THREE.Mesh(sphereGeometry,normalMaterial.clone());
-					sphere.position.set(params.highlightScale*(lattice[facility].sections[i].start.x + tang*d / den) - 50 + 50*pi, y+350, params.highlightScale*(lattice[facility].sections[i].start.z + d / den));
-					sphere.name = mycomp.ps[pi].replace('PS','') + '_status';
+					sphere.position.set(params.highlightScale*(lattice[facility].sections[i].start.x + tang*d / den), y+350, params.highlightScale*(lattice[facility].sections[i].start.z + d / den));
+					sphere.name = mycomp.name+'_status';
 					sphere.facility = facility;
 					sphere.visible = false;
 					status.push(sphere);
 					facilities[facility].add(sphere);
 				}
 			}
-			else {
-				const sphere = new THREE.Mesh(sphereGeometry,normalMaterial.clone());
-				sphere.position.set(params.highlightScale*(lattice[facility].sections[i].start.x + tang*d / den), y+350, params.highlightScale*(lattice[facility].sections[i].start.z + d / den));
-				sphere.name = mycomp.name+'_status';
-				sphere.facility = facility;
-				sphere.visible = false;
-				status.push(sphere);
-				facilities[facility].add(sphere);
-			}
 			return mycomp.position;
 		}
 	}
@@ -499,7 +491,7 @@
 	}
 
 	function initLattice(flattice, facility) {
-		console.log('initLattice()', flattice, facility);
+		// console.log('initLattice()', flattice, facility);
 		if (lattice[facility] && lattice[facility].bpm) {bpmPoints[facility] = []; bpmSkip[facility] = []; bpmData[facility] = [];}
 		facilities[facility] = new THREE.Object3D();
 		for (let i=0; i<flattice.length; i++) {
@@ -545,8 +537,9 @@
 				}
 				window.names.push(flattice[j].bending.name);
 			}
-			const direction = (flattice[j].start.z<flattice[i].start.z /*!= flattice[j].start.x<=flattice[i].start.x*/)? -1: 1;
+			const direction = (facility=='servicearea'? 1: 1)*((flattice[j].start.z<flattice[i].start.z /*!= flattice[j].start.x<=flattice[i].start.x*/)? -1: 1);
 			if (document.location.search.indexOf('components=hide')==-1) if (flattice[i].components) for (let m=0; m<flattice[i].components.length; m++) {
+				// if (facility=='servicearea') console.log(facility, i, m, tang, direction);
 				const position = appendComponent(facility, i, m, tang, direction, y);
 				if (flattice[i].components[m].type=='bpm' && (document.location.search.indexOf('&bpm')>-1 || document.location.search.indexOf('?bpm')>-1) && lattice[facility].bpm) {
 					if (lattice[facility].bpm.skip && (lattice[facility].bpm.skip.indexOf(flattice[i].components[m].name)>-1)) {bpmSkip[facility].push(bpmPoints[facility].length);}
@@ -576,7 +569,8 @@
 					componentCreator.bstUpdate(vlvs[i].comp, val[0]=='true'? 'limegreen': (val[1]=='true'? 'yellow': 'grey'));
 				}
 			}
-		});
+		})
+		.catch(error => {console.log("Fetch error", error);});
 	}
 	let cameraStep=0;
 	let cameraUpdate = null;
-- 
GitLab