Skip to content
Snippets Groups Projects
bl2d.js 8.18 KiB
Newer Older
Lucio Zambon's avatar
Lucio Zambon committed
// jshint esversion: 6
Lucio Zambon's avatar
Lucio Zambon committed
	const data_stroke = {};
Lucio Zambon's avatar
Lucio Zambon committed
	const data_color = {};
	const data_value = {};
Lucio Zambon's avatar
Lucio Zambon committed
	const data_interlock = {};
	let alarms = -1;
	let interlocks = -1;
	let logs = -1;
	let summary;
	const bl = document.location.search.split('machine=')[1].split('&')[0].split('_')[1];
	function myid(name) {return name.split(' ').join('_').split('.').join('_');}
Lucio Zambon's avatar
Lucio Zambon committed
	function updateBl(blName) {
Lucio Zambon's avatar
Lucio Zambon committed
		// console.log('./updater.php?read&filename=elettrabl'+bl+'.1000.'+blName);
		fetch('./updater.php?read&filename=elettrabl'+bl+'.1000.'+blName).then((response) => {return response.text();}).then((data) => {
			const values = data.split(':').slice(1).join(':').split(';');
			for (let j=0; j<data_stroke[blName].length; j++) {
				// console.log(data_stroke[blName][j], values[data_stroke[blName][j][2]], values[data_stroke[blName][j][2]]==1);
				$('#'+myid(data_stroke[blName][j][0])).css('stroke', values[data_stroke[blName][j][2]]==1? 'yellow': 'black');
			}
Lucio Zambon's avatar
Lucio Zambon committed
			for (let j=0; j<data_color[blName].length; j++) {
				// console.log("$('#"+data_color[blName][j][0]+"').css('fill',values[data_color[blName][j][2]]=='1'? 'red': 'limegreen')", data_color[blName][j][2], values[data_color[blName][j][2]]);
Lucio Zambon's avatar
Lucio Zambon committed
				$('#'+myid(data_color[blName][j][0])).css('fill', (values[data_color[blName][j][2]]=='1' || values[data_color[blName][j][2]]=='9' || values[data_color[blName][j][2]]=='true')? 'red': (values[data_color[blName][j][2]]==''|| values[data_color[blName][j][2]]==null? 'orange': 'limegreen'));
Lucio Zambon's avatar
Lucio Zambon committed
			}
Lucio Zambon's avatar
Lucio Zambon committed
			// console.log(data_value);
Lucio Zambon's avatar
Lucio Zambon committed
			for (let j=0; j<data_value[blName].length; j++) {
Lucio Zambon's avatar
Lucio Zambon committed
				const id = myid(data_value[blName][j][0])+'value';
Lucio Zambon's avatar
Lucio Zambon committed
				if ($('#'+id)[0]) $('#'+id)[0].innerHTML = '';
			}
			for (let j=0; j<data_value[blName].length; j++) {
Lucio Zambon's avatar
Lucio Zambon committed
				const id = myid(data_value[blName][j][0])+'value';
Lucio Zambon's avatar
Lucio Zambon committed
				for (let i=0; i<data_value[blName][j][2].length; i++) {
Lucio Zambon's avatar
Lucio Zambon committed
					// console.log('#'+id);
					if ($('#'+id)[0]) $('#'+id)[0].innerHTML = $('#'+id)[0].innerHTML+
						'<tspan x="0" dy="1.2em">'+formatBl(values[data_value[blName][j][2][i]]-0, data_value[blName][j][3]? data_value[blName][j][3][i]: '')+'</tspan>';
Lucio Zambon's avatar
Lucio Zambon committed
				}
			}
Lucio Zambon's avatar
Lucio Zambon committed
			for (let j=0; j<data_interlock[blName].length; j++) {
				const id = myid(data_interlock[blName][j][0])+'_interlock';
				// if (id=='VALVE_L_03_interlock') console.log(id, data_interlock[blName][j][0], data_interlock[blName][j][2], values[data_interlock[blName][j][2]], values[data_interlock[blName][j][2]]=='true', $('#'+id));
				if (values[data_interlock[blName][j][2]]=='true') $('#'+id).show(); else $('#'+id).hide();
			}
			$('#alarms').html(values[alarms]==''? '---': values[alarms]);
			$('#interlocks').html(values[interlocks]==''? '---': values[interlocks]);
			const lv = values[logs].split(',');
			let log = '';
			for (let i=0; i*3<lv.length; i++) log = log + lv.slice(i*3, (i+1)*3).join(',')+'\n';
			$('#logs').html(log); 
Lucio Zambon's avatar
Lucio Zambon committed
		});
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function formatBl(value, format) { 
		if (format=='temperature') {return value.toFixed(2)+' °C';}
		if (format=='pressure') {/*console.log(value);*/ return '<tspan x="0" dy="1.2em">'+Number.parseFloat(value).toExponential(2)+'</tspan> <tspan x="-1em" dy="1.2em">mbar  </tspan>';}
		if (format=='current') {return value.toFixed(2)+' mA';}
		if (format=='energy') {return value.toFixed(2)+' GeV';}
		else {return value.toFixed(2);}
	}
Lucio Zambon's avatar
Lucio Zambon committed
	function initBl(blName) {
Lucio Zambon's avatar
Lucio Zambon committed
		fetch('./updater.php?read&filename=elettrabl'+bl+'.1000.'+blName+'.src').then((response) => {return response.text();}).then((data) => {
			const names = data.substr(14).split('nullosp').join('nanosp').split(',');
			// console.log('names', names, './updater.php?read&filename=elettrabl'+bl+'.1000.'+blName+'.src');
Lucio Zambon's avatar
Lucio Zambon committed
			for (let i=0; i<names.length; i++) {
Lucio Zambon's avatar
Lucio Zambon committed
				for (let j=0; j<data_stroke[blName].length; j++) {
					if (data_stroke[blName][j][1] == names[i]) {
						data_stroke[blName][j].push(i);
						if (data_stroke[blName][j][0]) $('#'+myid(data_stroke[blName][j][0])).children()[0].textContent = data_stroke[blName][j][1];
					}
				}
Lucio Zambon's avatar
Lucio Zambon committed
				for (let j=0; j<data_color[blName].length; j++) {
Lucio Zambon's avatar
Lucio Zambon committed
					if (data_color[blName][j][1] == names[i]) {
						data_color[blName][j].push(i);
						if (data_color[blName][j][0]) $('#'+myid(data_color[blName][j][0])).children()[0].textContent = data_color[blName][j][1];
					}
Lucio Zambon's avatar
Lucio Zambon committed
				}
				for (let j=0; j<data_value[blName].length; j++) {
					if (typeof data_value[blName][j][2]=='undefined') data_value[blName][j][2] = [];
					for (let k=0; k<data_value[blName][j][1].length; k++) {
Lucio Zambon's avatar
Lucio Zambon committed
						// if (names[i].indexOf('Pressure')>-1) console.log(data_value[blName][j][1][k] == names[i], i, j, names[i], data_value[blName][j][1][k], data_value[blName]);
Lucio Zambon's avatar
Lucio Zambon committed
						if (data_value[blName][j][1][k] == names[i]) {
							data_value[blName][j][2].push(i);
Lucio Zambon's avatar
Lucio Zambon committed
							// console.log(i, j, names[i], data_value[blName][j][1][k], data_value[blName]);
							if (data_value[blName][j][0]) $('#'+myid(data_value[blName][j][0])+'_g').children()[0].innerHTML = data_value[blName][j][1];
Lucio Zambon's avatar
Lucio Zambon committed
						}
					}
				}
Lucio Zambon's avatar
Lucio Zambon committed
				for (let j=0; j<data_interlock[blName].length; j++) {
					if (data_interlock[blName][j][1] == names[i]) {
						data_interlock[blName][j].push(i);
						if (data_interlock[blName][j][0]) {
							const id = myid(data_interlock[blName][j][0]);
							const transform = $('#'+id+'_g').children()[1].outerHTML.split('transform="')[1].split('">')[0];
							appendSvg('rect', {id: id+'_interlock', fill: 'orange', x: '-290', y: '-200', height: "30", width:"220", transform: transform}, false, '<title>'+data_interlock[blName][j][1]+'</title>', false, '#'+id+'_g');
						}
					}
				}
				if (names[i].indexOf('/geco/alarms')>-1) alarms = i;
				if (names[i].indexOf('/geco/interlocks')>-1) interlocks = i;
				if (names[i].indexOf('/geco/logs')>-1) logs = i;
Lucio Zambon's avatar
Lucio Zambon committed
			}
Lucio Zambon's avatar
Lucio Zambon committed
			// console.log('initBl()', data.split(','), data_color, data_value);
Lucio Zambon's avatar
Lucio Zambon committed
			updateBl(blName);
Lucio Zambon's avatar
Lucio Zambon committed
			colorReader = setInterval(updateBl, 1000, blName);
Lucio Zambon's avatar
Lucio Zambon committed
		});
	}
	function bl2d(lattice) {
		// console.log('bl2d');
		for (let facility in lattice) {
			if (facility == 'conf') continue;
Lucio Zambon's avatar
Lucio Zambon committed
			data_stroke[facility] = [];
Lucio Zambon's avatar
Lucio Zambon committed
			data_color[facility] = [];
			data_value[facility] = [];
Lucio Zambon's avatar
Lucio Zambon committed
			data_interlock[facility] = [];
Lucio Zambon's avatar
Lucio Zambon committed
			const sections = lattice[facility].sections;
			for (let section=0; section< sections.length; section++) {
				const components = sections[section].components;
				// console.log(components, facility, section, sections[section]);
				if (components) for (let i=0; i<components.length; i++) {
Lucio Zambon's avatar
Lucio Zambon committed
					// console.log(i, components[i]);
					if (components[i]['data-stroke']) data_stroke[facility].push([components[i].name, components[i]['data-stroke']]);
					if (components[i]['data-interlock']) data_interlock[facility].push([components[i].name, components[i]['data-interlock']]);
Lucio Zambon's avatar
Lucio Zambon committed
					if (components[i]['data-color']) data_color[facility].push([components[i].name, components[i]['data-color']]);
					if (components[i]['data-value']) {
Lucio Zambon's avatar
Lucio Zambon committed
						data_value[facility].push([components[i].name, components[i]['data-value'], [], components[i]['data-unit']]);
						const id = myid(components[i].name)+'value';
						console.log("$('#"+id+")[0].style.display");
Lucio Zambon's avatar
Lucio Zambon committed
						if ($('#'+id)[0]) $('#'+id)[0].style.display = 'block';
					}
				}
			}
			if (data_color[facility].length > 0) initBl(facility);
Lucio Zambon's avatar
Lucio Zambon committed
			if (typeof lattice.conf.summary != undefined) {
				summary = lattice.conf.summary;
				let st = '<div style="position: fixed; bottom: 5px; left: 5px;color: white; background-color: #333; border-radius: 5px; padding: 5px;"><div style="cursor:pointer;" onClick="$(\'#summary\').toggle()">Summary</div><div id="summary" style="display:none;"><table><tr>';
				for (let i in summary) {
					// console.log(i, summary[i]);
					st = st + '<td>' + i.toUpperCase() + '</td>';
				}
				st = st + '</tr><tr valign="top">';
				for (let i in summary) {
					st = st + '<td><textarea id="'+i+'" title="'+summary[i]['data-value']+'" cols="'+summary[i].cols+'" rows="'+summary[i].rows+'" style="color: white; background-color: #222; border: 0;"></textarea></td>';
				}
				const sm = document.createElement("div");
				sm.innerHTML = st + '</tr></table></div>';
				document.body.appendChild(sm);
			}
		}
		$('#talk').hide();
		for (let i=4; i<=10; i++) {
			$('#lil-gui-name-'+i).parent().hide();
Lucio Zambon's avatar
Lucio Zambon committed
		}
	}