Skip to content
Snippets Groups Projects
updater.php 8.83 KiB
Newer Older
Lucio Zambon's avatar
Lucio Zambon committed
<?php
function logdata($request, $filename) {
	$timestamp = isset($_REQUEST['timestamp'])? $_REQUEST['timestamp']: time();
	$data = $timestamp.':'.strtr($_REQUEST[$request],array('nan'=>'null'));
	$f = file_put_contents('/run/panther/'.$filename, $data);
	$debug = '';
	if (isset($_REQUEST['debug'])) $debug = ", filename: $filename, written $f bytes\n";
    if ($f==strlen($data)) die("OK$debug");
    die("ERROR: cannot write data, written $f bytes VS ".strlen($data));
}
if (isset($_REQUEST['check']) && !empty($_REQUEST['filename'])) {
echo '<!doctype html>
<html lang="en">
	<head>
		<title>PAnTHer admin</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="../lib/bootstrap/bootstrap.min.css">
		<script src="../lib/bootstrap/jquery.js"></script>
		<script src="../lib/bootstrap/bootstrap.min.js"></script>	
	</head>
	<body>
<style>
	td {vertical-align: top;}
</style>
';
	list($ts, $srcStr) = explode(':', file_get_contents('http://puma-01.elettra.eu/panther/updater.php?read&filename='.$_REQUEST['filename'].'.src'),2);
	$src = explode(',', $srcStr);
	if (isset($_REQUEST['debug'])) {echo "<pre>"; print_r($src); echo "</pre><br>";}
	list($ts, $dataStr) = explode(':', file_get_contents('http://puma-01.elettra.eu/panther/updater.php?read&filename='.$_REQUEST['filename']),2);
	$data = explode(';', $dataStr);
	if (!empty($_REQUEST['check'])) {
		$period = $_REQUEST['check'] - 0;
		if ($period < 1) $period = 2;
		echo "<meta http-equiv='refresh' content='$period' >\n";
	}
	echo date('Y-m-d H:i:s', $ts/1000)."<br>\n<table class='table table-striped table-hover'>\n";
	foreach ($data as $i=>$v) {
		echo "<tr><td> $i </td><td> ".strtr($v, [','=>'<br>'])." </td><td> {$src[$i]} </td></tr>\n";
	}
	die('</table>');
}
Lucio Zambon's avatar
Lucio Zambon committed
$base = (strpos($_SERVER['SERVER_NAME'], 'puma-01')!==false)? 'http://puma-01.elettra.eu/panther/updater.php?read&filename=': '/run/panther/';
Lucio Zambon's avatar
Lucio Zambon committed
if (isset($_REQUEST['read']) && !empty($_REQUEST['filename'])) {
	if ($_REQUEST['filename']=='preinjector.100.bpm') {
Lucio Zambon's avatar
Lucio Zambon committed
		list($trash, $f) = explode(':', file_get_contents($base.'elettra.100.bpm'));
Lucio Zambon's avatar
Lucio Zambon committed
		$d = explode(';', $f);
		die("[[".implode(',', array_slice($d, 0, 7)).'],['.implode(',', array_slice($d, 7, 7))."]]");
	}
	if ($_REQUEST['filename']=='preinjector.100.bpm.src') {
Lucio Zambon's avatar
Lucio Zambon committed
		list($trash, $f) = explode(':', file_get_contents($base.'elettra.100.bpm.src'), 2);
Lucio Zambon's avatar
Lucio Zambon committed
		$d = explode(',', $f);
		die("[[".implode(',', array_slice($d, 0, 7)).'],['.implode(',', array_slice($d, 7, 7))."]]");
	}
	if ($_REQUEST['filename']=='bts.100.bpm') {
Lucio Zambon's avatar
Lucio Zambon committed
		list($trash, $f) = explode(':', file_get_contents($base.'elettra.100.bpm'));
Lucio Zambon's avatar
Lucio Zambon committed
		$d = explode(';', $f);
		die("[[".implode(',', array_slice($d, 14, 10)).'],['.implode(',', array_slice($d, 24, 10))."]]");
	}
	if ($_REQUEST['filename']=='bts.100.bpm.src') {
Lucio Zambon's avatar
Lucio Zambon committed
		list($trash, $f) = explode(':', file_get_contents($base.'elettra.100.bpm.src'), 2);
Lucio Zambon's avatar
Lucio Zambon committed
		$d = explode(',', $f);
		die("[[".implode(',', array_slice($d, 14, 10)).'],['.implode(',', array_slice($d, 24, 10))."]]");
	}
	if ($_REQUEST['filename']=='sr.100.bpm') {
Lucio Zambon's avatar
Lucio Zambon committed
		list($trash, $f) = explode(':', file_get_contents($base.'elettra.100.bpm'));
Lucio Zambon's avatar
Lucio Zambon committed
		$d = explode(';', $f);
		die("[[".$d[34].'],['.$d[35]."]]");
	} 
	if ($_REQUEST['filename']=='sr.100.bpm.src') {
Lucio Zambon's avatar
Lucio Zambon committed
		// $f = file_get_contents('http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/sr/feedback/profile_s-%3EGetGofBpmNames');
		$f = file_get_contents('http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/sr/DIAGNOSTICS/bpm_s(BpmLabel)');
Lucio Zambon's avatar
Lucio Zambon committed
		if (isset($_REQUEST['debug'])) print_r($f);
Lucio Zambon's avatar
Lucio Zambon committed
		// $d = json_decode(strtr($f, [',""'=>'', '"LGBPM_S2.1","LGBPM_S2.2",'=>'', '"LGBPM_S7.1","LGBPM_S7.2",'=>'']));
		$d = json_decode($f, true);
Lucio Zambon's avatar
Lucio Zambon committed
		$h = $v = [];
		foreach ($d as $name) {
Lucio Zambon's avatar
Lucio Zambon committed
			$h[] = "\"srv-tango-sre-01:20000/sr/feedback/$name/HorPos\"";
			$v[] = "\"srv-tango-sre-01:20000/sr/feedback/$name/VerPos\"";
Lucio Zambon's avatar
Lucio Zambon committed
		}
		die('[['.implode(',', $h).'],['.implode(',', $v).']]');
	}
	if ($_REQUEST['filename']=='elettra.100.bpm') {
		header('Content-Type: application/json');
Lucio Zambon's avatar
Lucio Zambon committed
		while (!is_numeric(substr($f, -1))) $f = substr($f, 0, strlen($f)-1);
		list($trash, $f) = explode(':', strtr(file_get_contents($base.'elettra.100.bpm'), array('e '=>'e')));
Lucio Zambon's avatar
Lucio Zambon committed
		$d = explode(';', $f);
Lucio Zambon's avatar
Lucio Zambon committed
		die("{\"preinjector\":{\"Hor\":[".implode(',', array_slice($d, 0, 7)).'],"Ver":['.implode(',', array_slice($d, 7, 7))."]},\"bts\":{\"Hor\":[".implode(',', array_slice($d, 14, 10)).'],"Ver":['.implode(',', array_slice($d, 24, 10))."]},\"sr\":{\"Hor\":[".$d[34].'],"Ver":['.$d[35]."]}}");
Lucio Zambon's avatar
Lucio Zambon committed
	}
	if ($_REQUEST['filename']=='elettra.100.bpm.src') {
Lucio Zambon's avatar
Lucio Zambon committed
		header('Content-Type: application/json');
		// $f = file_get_contents('http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/sr/feedback/profile_s-%3EGetGofBpmNames');
		$f = file_get_contents('http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/sr/DIAGNOSTICS/bpm_s(BpmLabel)');
		if (isset($_REQUEST['debug'])) print_r($f);
		// $d = json_decode(strtr($f, [',""'=>'', '"LGBPM_S2.1","LGBPM_S2.2",'=>'', '"LGBPM_S7.1","LGBPM_S7.2",'=>'']));
		$d = json_decode($f, true);
Lucio Zambon's avatar
Lucio Zambon committed
		$h = $v = [];
		foreach ($d as $name) {
Lucio Zambon's avatar
Lucio Zambon committed
			$h[] = "\"srv-tango-sre-01:20000/sr/feedback/$name/HorPos\"";
			$v[] = "\"srv-tango-sre-01:20000/sr/feedback/$name/VerPos\"";
Lucio Zambon's avatar
Lucio Zambon committed
		}
Lucio Zambon's avatar
Lucio Zambon committed
		list($trash, $f) = explode(':', file_get_contents($base.'elettra.100.bpm.src'), 2);
Lucio Zambon's avatar
Lucio Zambon committed
		$d = explode(',', $f);
Lucio Zambon's avatar
Lucio Zambon committed
		die("{\"preinjector\":{\"Factor\":1000,\"Hor\":\n[\"".implode("\",\n\"", array_slice($d, 0, 7))."\"], \"Ver\":\n[\"".implode("\",\n\"", array_slice($d, 7, 7)).
			"\"]},\n\"bts\":{\"Factor\":0.001,\"Hor\":\n[\"".implode("\",\n\"", array_slice($d, 14, 10))."\"],\"Ver\":\n[\"".implode("\",\n\"", array_slice($d, 24, 10)).
			"\"]},\n\"sr\":{\"Factor\":1000,\"Hor\":[\n".implode(",\n", $h).'],"Ver":['.implode(',', $v)."\n]\n}\n}");
Lucio Zambon's avatar
Lucio Zambon committed
	}
	if ($_REQUEST['filename']=='fermi.1000.vlv') {
Lucio Zambon's avatar
Lucio Zambon committed
		list($trash, $f) = explode(':', file_get_contents($base.'padres.1000.vlv'));
		die(file_get_contents($base.'fermi.1000.vlv').';'.$f);
Lucio Zambon's avatar
Lucio Zambon committed
	}
	if ($_REQUEST['filename']=='fermi.1000.vlv.src') {
Lucio Zambon's avatar
Lucio Zambon committed
		list($trash, $f) = explode(':', file_get_contents($base.'padres.1000.vlv.src'),2);
		die(file_get_contents($base.'fermi.1000.vlv.src').','.$f);
Lucio Zambon's avatar
Lucio Zambon committed
	}
	if ($_REQUEST['filename']=='elettra.1000.vlv') {
Lucio Zambon's avatar
Lucio Zambon committed
		$f = explode(';', file_get_contents($base.'elettra.1000.vlv'));
Lucio Zambon's avatar
Lucio Zambon committed
		foreach ($f as $i=>$l) {
			if (substr_count($l, ',')>15) {
				$k = explode(',', $l);
				$f[$i] = "UNKNOWN";
				if ($k[0]=='true') $f[$i] = "OPENED";
				if ($k[1]=='true') $f[$i] = "CLOSED";
			}
		}
		die(implode(';', $f));
	}
	if ($_REQUEST['filename']=='elettra.1000.vlv.src') {
Lucio Zambon's avatar
Lucio Zambon committed
		die(strtr(file_get_contents($base.'elettra.1000.vlv.src'), array('rpcbridge/rpc2tangobridge'=>'sr', '_STAT_RF'=>'/state')));
Lucio Zambon's avatar
Lucio Zambon committed
	}
	if (!empty($_REQUEST['read']) || $_REQUEST['read']==='0') {
Lucio Zambon's avatar
Lucio Zambon committed
		$f = explode(';',file_get_contents($base.$_REQUEST['filename']));
Lucio Zambon's avatar
Lucio Zambon committed
		if ($_REQUEST['read']==='0') list($trash, $f[0]) = explode(':', $f[0]);
		header('Content-Type: application/json');
		$factor = 2000000000000;
		$x = $z = [];
		$v = explode(',', $f[$_REQUEST['read']-0]);
		for ($i=0; $i<96; $i++) {$x[] = $v[$i*15] * $factor + (rand(0,2000)-1000)/10000; $z[] = ($v[$i*15] * $factor + (rand(0,2000)-1000)/10000)/4;}
		die("[[".implode(',', $x).'],['.implode(',', $z)."]]");
		die("[[{$f[$_REQUEST['read']-0]}],[{$f[$_REQUEST['read']+1]}]]");
	}
Lucio Zambon's avatar
Lucio Zambon committed
	readfile($base.$_REQUEST['filename']);
Lucio Zambon's avatar
Lucio Zambon committed
	exit();
}
if ($_SERVER['REMOTE_ADDR'] == '192.168.205.106' || $_SERVER['REMOTE_ADDR']=='192.168.205.25' || $_SERVER['REMOTE_ADDR'] == '192.168.205.149' || $_SERVER['REMOTE_ADDR'] == '192.168.205.157' || $_SERVER['REMOTE_ADDR'] == '192.168.205.158' || $_SERVER['REMOTE_ADDR'] == '192.168.205.159' || $_SERVER['REMOTE_ADDR'] == '192.168.205.166') {
	if (empty($_REQUEST['data']) && $_REQUEST['data']!=='0' && empty($_REQUEST['src']) && empty($_REQUEST['stat'])) die("ERROR: empty data\n");
    if (strlen($_REQUEST['data'])>2000000) die("ERROR: data too long: ".strlen($_REQUEST['data'])." bytes\n");
	$filename = isset($_REQUEST['filename'])? strtr($_REQUEST['filename'],array("'"=>'')): 'data.txt';
	if (!empty($_REQUEST['stat'])) {logdata('stat', $filename.'.stat');}
	if (!empty($_REQUEST['src'])) {logdata('src', $filename.'.src');}
	if ($filename=='stat' || $filename=='src') die("ERROR: invalid filename: $filename\n");
	logdata('data', $filename);
	exit();
}
function multiply($factor, $array) {
	foreach ($array as $key => $value) {
		$array[$key]=$value*$factor;
	}
	return $array;
}
Lucio Zambon's avatar
Lucio Zambon committed
$d = scandir('/run/panther');
echo "<table>\n";
foreach ($d as $srv) {
	if ($srv=='.' || $srv=='..' || strpos($srv, '.src')) continue;
	echo "<tr><td> $srv </td><td> <a href='./updater.php?read&filename=$srv'>data</a> </td><td> <a href='./updater.php?read&filename={$srv}.src'>src</a> </td><td> <a href='./updater.php?check&filename={$srv}'>check</a> </td></tr>\n";
}
echo "</table>\n";
Lucio Zambon's avatar
Lucio Zambon committed
?>