<html>
<head>
<title>High-Voltage Patch-Panel Configuration</title>
<style type="text/css">
	body      { font-family:Arial; }
	td        { text-align:center; }
	a:link    { text-decoration:none; }
	a:visited { text-decoration:none; }
	a:hover   { text-decoration:none; }
	a:active  { text-decoration:none; }
	a:focus   { text-decoration:none; }
</style>
</head>
<body>



<?php
	// offset between patch-panel channel $ch and adei channel $adei: $adei = $ch + $offset
	$offset = 2;

	// main-spectrometer rings
	$rings = array( "w02o", "w02i", "w03o", "w03i", "w04o", "w04i", "w05o", "w05i", "w06o", "w06i", "w07o", "w07i", "w12o", "w12i", "w13o", "w13i", "w14o", "w14i", "w15o", "w15i", "w16o", "w16i", "e02o", "e02i", "e03o", "e03i", "e04o", "e04i", "e05o", "e05i", "e06o", "e06i", "e07o", "e07i", "e12o", "e12i", "e13o", "e13i", "e14o", "e14i", "e15o", "e15i", "e16o", "e16i" );
?>



<?php

	// submit button

	if ($_POST["ok"] == "submit")
	{
		// write TXT patch-panel configuration

		$time = strtotime($_POST["validfrom"]);
		if ($time === FALSE) $filename = time() . ".txt"; else $filename = $time . ".txt";

		$handle = fopen("data/" . $filename, "w");
		if ($handle)
		{
			$content = "";
			for ($id = 0; $id < 44; $id++)
			{
				if ($_POST[$rings[$id]] == 22) $tempcontent = "IE"; else $tempcontent = floatval($_POST[$rings[$id]]) + $offset;
				$content = $content . $tempcontent . "\r\n";
			}
			fwrite($handle, $content);
			fclose($handle);
		}

		// copy for SDS-STATUS-DISPLAY

		copy($filename, "ringstoadei.txt");

		// write CONF patch-panel configuration

		if ($_POST["configurationname"] != "")
		{
			$filename = $_POST["configurationname"] . ".conf";
			$handle = fopen("data/" . $filename, "w");
			if ($handle)
			{
				$content = "";
				for ($id = 0; $id < 44; $id++)
				{
					$tempcontent = floatval($_POST[$rings[$id]]);
					$content = $content . $tempcontent . "\r\n";
				}
				fwrite($handle, $content);
				fclose($handle);	
			}
		}
	}

?>



<?php

	// load configuration

	if ($_POST["configurationload"] != "" && $_POST["configurationload"] != "new configuration")
	{
		$filename = $_POST["configurationload"] . ".conf";
		$handle = fopen("data/" . $filename, "r");
		if ($handle)
		{
			while (($buffer = fgets($handle, 4096)) !== false)
			{
				$buffer = trim($buffer);
				$check[count($check)] = $buffer;
			}
			fclose($handle);
		}
	}
	else
	{
		$check = array( 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22 );
	}

?>



<?php

	$adeiChannel[0] = array( "hv__katrin-hv_rep__0__95", "hv__katrin-hv_rep__0__71", "hv__katrin-hv_rep__0__79" );
	for ($id = 0; $id < 24; $id++)
	{
		$adeiChannel[1][$id] = "hv__katrin-hv_rep__1__" . (7 * $id + 3 + 7 * $offset);
		$adeiChannel[2][$id] = "hv__katrin-hv_rep__2__" . (7 * $id + 3 + 7 * $offset);
	}


	$username = 'katrin';
	$password = 'neutrino';
	$context = stream_context_create(array(
		'http' => array(
			'header'  => "Authorization: Basic " . base64_encode("$username:$password")
		)
	));


	for ($id = 0; $id < sizeof($adeiChannel); $id++)
	{
		$adeiChannels = "";
		for ($id2 = 0; $id2 < sizeof($adeiChannel[$id]); $id2++)
		{
			$adeiChannels = $adeiChannels . $adeiChannel[$id][$id2] . ",";
		}
		$adeiChannels = substr($adeiChannels, 0, strlen($adeiChannels) - 1);

      		$fileip = file_get_contents("https://adei-katrin.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=60&format=csv", false, $context);
		//echo $fileip . "<br>";

		for ($id2 = sizeof($adeiChannel[$id]) - 1 ; $id2 > -1; $id2--)
		{
			$pos = strrpos($fileip, ",");
			$adeiValue[$id][$id2] = floatval(substr($fileip, $pos + 2));
			$fileip = substr($fileip, 0, $pos);
			//echo $id . "," . $id2 . "," . $adeiValue[$id][$id2] . "<br>";
		}
	}

?>



<form method="post" action="index.php" name="myform">



<font size="6"><b>High-Voltage Patch-Panel Configuration</b></font>



<br><br>



<table border="0" cellpadding="5" cellspacing="0" bgcolor="#C8C8C8">
	<tr>
		<td><b>configuration</b></td>
		<td>
			<select name="configurationload" size="1" onchange="myform.submit();">
				<option>new configuration</option>
				<?php
					foreach (glob("data/*.conf") as $filename)
					{
						$buffer = substr($filename, 5, strlen($filename) - 10);
						if ($_POST["configurationload"] == $buffer) echo '<option selected>'; else echo '<option>';
						echo $buffer . '</option>';
					}
				?>
			</select>
		</td>
	</tr>
	<tr>
		<td>name</td>
		<td>
			<input type="text" name="configurationname" size="35">
		</td>
	</tr>
</table>



<br>



<table border="0" cellpadding="0" cellspacing="0">

	<tr bgcolor="#000000" height="1px"><td colspan="47"></td></tr>
	<tr>
		<td bgcolor="#C8C8C8" colspan="22"><b>W E S T</b></td>
		<td colspan="3"><b>PATCH PANEL</b></td>
		<td bgcolor="#C8C8C8" colspan="22"><b>E A S T</b></td>
	</tr>
	<tr>
		<?php
			for ($id=2; $id<7; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '" colspan="2">0' . $id . '</td>'; }
			echo '<td colspan="2">07-11</td>';
			for ($id=12; $id<17; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '" colspan="2">' . $id . '</td>'; }
			echo '<td>&nbsp;ADEI&nbsp;</td>';
			echo '<td>&nbsp;CH&nbsp;</td>';
			echo '<td>&nbsp;ADEI&nbsp;</td>';
			for ($id=2; $id<7; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '" colspan="2">0' . $id . '</td>'; }
			echo '<td colspan="2">07-11</td>';
			for ($id=12; $id<17; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '" colspan="2">' . $id . '</td>'; }
		?>
	</tr>
	<tr>
		<?php
			for ($id=0; $id<11; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '">o</td><td bgcolor="' . $color . '">i</td>'; }
			echo '<td></td>';
			echo '<td></td>';
			echo '<td></td>';
			for ($id=0; $id<11; $id++) { if ($id % 2 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8"; echo '<td bgcolor="' . $color . '">o</td><td bgcolor="' . $color . '">i</td>'; }
		?>
	</tr>
	<?php
		for ($ch=0; $ch<23; $ch++)
		{
			if ($ch % 5 == 0) echo '<tr bgcolor="#000000" height="1px"><td colspan="47"></td></tr>';

			$text = str_pad($ch, 2, 0, STR_PAD_LEFT);

			echo '<tr>';

			for ($id=0; $id<22; $id++)
			{
				if ($id % 4 == 2 || $id % 4 == 3) $color = "#FFFFFF"; else $color = "#C8C8C8";

				$checked = "";
				if ($check[$id] == $ch) $checked = 'checked="checked"';
				echo '<td bgcolor="' . $color . '"><input type="radio" name="' . $rings[$id] . '" value="' . $ch . '" ' . $checked . '></td>';
			}

			$value = $adeiValue[1][$ch];
			if ($ch == 22) $value = $adeiValue[0][0] + $adeiValue[0][1];
			echo '<td>' . sprintf('%.0F', $value) . '</td>';

			echo '<td><b>';
			if ($ch == 22) echo 'IE';
			else echo $text;
			echo '</b></td>';

			$value = $adeiValue[2][$ch];
			if ($ch == 22) $value = $adeiValue[0][0] + $adeiValue[0][2];
			echo '<td>' . sprintf('%.0F', $value) . '</td>';

			for ($id=22; $id<44; $id++)
			{
				if ($id % 4 == 0 || $id % 4 == 1) $color = "#FFFFFF"; else $color = "#C8C8C8";

				$checked = "";
				if ($check[$id] == $ch) $checked = 'checked="checked"';
				echo '<td bgcolor="' . $color . '"><input type="radio" name="' . $rings[$id] . '" value="' . $ch . '" ' . $checked . '></td>';
			}

			echo '</tr>';
		}
	?>
	<tr bgcolor="#000000" height="1px"><td colspan="47"></td></tr>

</table>



<br>



<table border="0" cellpadding="5" cellspacing="0" bgcolor="#C8C8C8">
	<tr>
		<td><b>valid from</b></td>
		<td>
			<input type="text" name="validfrom" size="19" maxlength="19"> (YYYY-MM-DD HH:MM:SS)
		</td>
	</tr>
</table>



<br>



<table border="0" cellpadding="5" cellspacing="0" bgcolor="#C8C8C8">
	<tr>
		<td><b>elog entry</b></td>
		<td>
			<input type="checkbox" name="elog" value="yes"> write & submit
		</td>
	</tr>
	<tr>
		<td>author</td>
		<td>
			<select name="author" size="1">
				<option>---</option>
				<option>M. Erhard</option>
				<option>M. Kraus</option>
				<option>K. Schlösser</option>
				<option>T. Thümmler</option>
				<option>S. Wüstling</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>comments</td>
		<td>
			<textarea name="comments" cols="50" rows="5"></textarea>
		</td>
	</tr>
</table>



<br>



<input type="submit" name="ok" value="submit">
<input type="reset" value="reset">



</form>
</body>
</html>
