<html>
<head>
<link rel="shortcut icon" href="../pics/KATRINlogo.ico" />
<title>High-Voltage Patch-Panel Configuration</title>
<style type="text/css">
	body      { font-family:Arial; }
	td        { text-align:center; }
    td.left   { text-align:left; }
	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; }

    td.even {
        background-color: #C8C8C8;
    }
    td.odd {
        background-color: #FFFFFF;
    }

    tr.highlight:hover,
    tr.highlight:hover td {
        background-color: #F0E68C !important;
        transition: .1s;
    }

    .container  {
      display: block;
      position: relative;
      margin: 5px;
      margin-top: -9px;
      padding-left: 17px;
      cursor: default;
      font-size: 22px;
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }

    /* Hide the browser's default checkbox */
    .container input {
      position: absolute;
      opacity: 0;
      cursor: pointer;
      height: 0;
      width: 0;
    }

    /* Create a custom checkbox */
    .checkmark {
      position: absolute;
      top: 0;
      left: 0;
      height: 12px;
      width: 12px;
      background-color: #eee;
      border: 2px solid #ddd;
      border-radius: 8px;
      transition: .2s;
    }

    /* On mouse-over, add a grey background color */
    .container:hover input ~ .checkmark {
      background-color: #FF8C00;
      border: 2px solid black;
    }

    /* On mouse-over, add a grey background color */
    .container:hover input:checked ~ .checkmark {
      background-color: #0000CD;
      border: 2px solid #0000CD;
    }

    /* When the checkbox is checked, add a blue background */
    .container input:checked ~ .checkmark {
      background-color: #0000CD;
      border: 2px solid #eee;
    }

    /* On mouse-over, add a grey background color */
    .common .container:hover input:checked ~ .checkmark {
      background-color: #CD0000;
      border: 2px solid #CD0000;
    }

    .common .container input:checked ~ .checkmark {
      background-color: #CD0000;
      border: 2px solid #eee;
    }

    /* Create the checkmark/indicator (hidden when not checked) */
    .checkmark:after {
      content: "";
      position: absolute;
      display: none;
    }

    /* Show the checkmark when checked */
    .container input:checked ~ .checkmark:after {
      display: block;
    }

    /* Style the checkmark/indicator */
    .container .checkmark:after {
      left: 3px;
      top: 3px;
      width: 2px;
      height: 2px;
      border: solid white;
      border-width: 1px 2px 3px 2px;
      border-radius: 4px;
    }
</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

        $config = $_POST["configurationname"] != "" ? $_POST["configurationname"] : $_POST["configurationload"];
        $author = $_POST["author"] != "" ? $_POST["author"] : "(anonymous)";

        //$time = strtotime($_POST["validfrom"]);
		$time = strtotime($_POST["validfrom-date"] . " " . $_POST["validfrom-time"]);
		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("data/" . $filename, "data/activeIEconfig.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);
			}
		}

        // update KDB database

        if (isset($_POST["database"])) {
            sleep(1);
            //require '../update_database.php';
            if ($time === FALSE) {
                shell_exec('cd .. ; php ./update_database.php "' . $config . '" "' . $author . '"');
            }
            else {
                shell_exec('cd .. ; php ./update_database.php "' . $config . '" "' . $author . '" "' . date('Y-m-d H:i:s', $time) . '"');
            }
        }

        // submit message to elog (currently main spec elog)

        if (isset($_POST["elog"])) {
            sleep(1);
	
    	    // apache web server runs on UTF-8, but elog runs on ISO 8859-1, so all stings posted to the elog need to be converted via utf8_decode();
        	$ElogAuthor = utf8_decode($author); 
        	$ElogConfig = utf8_decode($config);
        	$ElogTime = utf8_decode(date('Y-m-d H:i:s', $time));
            $ElogEntry = 'Patch panel changed to ' . $ElogConfig . ' on ' . $ElogTime; 
           
            $ToBeExec = 'echo "' . $ElogEntry . '" | elog -h localhost -p 8080 -l main-spectrometer -v -u sds-operator mainspec.13 -a Author="' . $ElogAuthor . '" -a Email=katrin-sds-status@lists.kit.edu -a Type=Routine -a Category=General -a Subject="' . $ElogConfig . '" -x -n 2'; 
          
          	echo "Message for Elog: ".$ToBEExec.$PHP_EOL;
          
            // currently not working due to server change 
			//shell_exec($ToBeExec); 
        }
      
	}

?>



<?php

	// load configuration

    $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 );
    if (!array_key_exists("configurationload", $_POST))
        $_POST["configurationload"] = "(active configuration)";

    #echo $_POST["configurationload"];

    if ($_POST["configurationload"] == "(active configuration)")
	{
        shell_exec('cd .. ; php ./poll_database.php');

        $handle = fopen("data/activeIEconfig.txt", "r");
        if ($handle)
        {
            $id = 0;
            while (($buffer = fgets($handle, 4096)) !== false)
            {
                $buffer = trim($buffer);
                if ($buffer == 'IE')
                    $check[$id++] = 22;
                else
                    $check[$id++] = intval($buffer) - 2;
            }
            fclose($handle);
        }
	}
	else if ($_POST["configurationload"] != "(new configuration)")
	{
		$filename = $_POST["configurationload"] . ".conf";
		$handle = fopen("data/" . $filename, "r");
		if ($handle)
		{
            $id = 0;
			while (($buffer = fgets($handle, 4096)) !== false)
			{
				$buffer = trim($buffer);
				$check[$id++] = $buffer;
			}
			fclose($handle);
		}
	}

?>



<?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 = getenv('PHP_ADEI_USERNAME');
    $password = getenv('PHP_ADEI_PASSWORD');
	$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.kaas.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">
<a href="../index.php" title="Back to main page">
<img style="margin-right: .5em; margin-bottom: -8px" src="../pics/katrin_logo_round.png" width="40px" height="40px">
<a>
<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>(active configuration)</option>
				<?php
                    if ($_POST["configurationload"] == "(new configuration)") echo '<option selected>'; else echo '<option>';
                    echo '(new configuration)</option>';

					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>
		<th bgcolor="#C8C8C8" colspan="22"><b>W E S T</b></th>
		<th colspan="3"><b>PATCH PANEL</b></th>
		<th bgcolor="#C8C8C8" colspan="22"><b>E A S T</b></th>
	</tr>
	<tr>
		<?php
			for ($id=2; $id<7; $id++) {
                if ($id % 2 == 1) $class = "odd"; else $class = "even";
                echo '<td class="highlight ' . $class . '" colspan="2">0' . $id . '</td>';
            }
			echo '<td colspan="2">07-11</td>';
			for ($id=12; $id<17; $id++) {
                if ($id % 2 == 1) $class = "odd"; else $class = "even";
                echo '<td class="highlight ' . $class . '" 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) $class = "odd"; else $class = "even";
                echo '<td class="highlight ' . $class . '" colspan="2">0' . $id . '</td>';
            }
			echo '<td colspan="2">07-11</td>';
			for ($id=12; $id<17; $id++) {
                if ($id % 2 == 1) $class = "odd"; else $class = "even";
                echo '<td class="highlight ' . $class . '" colspan="2">' . $id . '</td>';
            }
		?>
	</tr>
	<tr>
		<?php
			for ($id=0; $id<11; $id++) {
                if ($id % 2 == 1) $class = "odd"; else $class = "even";
                echo '<td class="highlight ' . $class . '">o</td><td class="highlight ' . $class . '">i</td>';
            }
			echo '<td></td>';
			echo '<td></td>';
			echo '<td></td>';
			for ($id=0; $id<11; $id++) {
                if ($id % 2 == 1) $class = "odd"; else $class = "even";
                echo '<td class="highlight ' . $class . '">o</td><td class="highlight ' . $class . '">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 class="highlight">';

			for ($id=0; $id<22; $id++)
			{
				//if ($id % 4 == 2 || $id % 4 == 3) $color = "#FFFFFF"; else $color = "#C8C8C8";
                if ($id % 4 == 2 || $id % 4 == 3) $class = "odd"; else $class = "even";
                if ($ch == 22) $class .= " common";

				$checked = "";
				if ($check[$id] == $ch) $checked = 'checked="checked"';
				echo '<td class="highlight ' . $class . '"><label class="container"><input type="radio" name="' . $rings[$id] . '" value="' . $ch . '" ' . $checked . '><span class="checkmark"></span></label></td>';
			}

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

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

			$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";
                if ($id % 4 == 2 || $id % 4 == 3) $class = "even"; else $class = "odd";
                if ($ch == 22) $class .= " common";

				$checked = "";
				if ($check[$id] == $ch) $checked = 'checked="checked"';
				echo '<td class="highlight ' . $class . '"><label class="container"><input type="radio" name="' . $rings[$id] . '" value="' . $ch . '" ' . $checked . '><span class="checkmark"></span></label></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) -->
            <input type="date" name="validfrom-date">
            <input type="time" name="validfrom-time">
		</td>
	</tr>
</table>



<br>



<table border="0" cellpadding="5" cellspacing="0" bgcolor="#C8C8C8">
	<tr>
		<td width="200px"><b>elog entry</b></td>
		<td class="left">
			<input type="checkbox" name="elog" value="yes"> submit to ELOG
		</td>
	</tr>
    <tr>
		<td><b>database entry</b></td>
		<td class="left">
            <input type="checkbox" name="database" value="yes"> submit to KDB
		</td>
	</tr>
	<tr>
		<td>author<br/><sup>(click twice to open drop-down)</sup></td>
		<td class="left">
			<!--<select name="author" size="1">-->
            <input type="text" list="authorlist" name="author" size="50"/>
            <datalist id="authorlist">
				<option>---</option>
				<option>B. Bieringer</option>
                <option>C. Rodenbeck</option>
				<option>T. Thuemmler</option>
				<option>S. Wuestling</option>
            </datalist>
			<!--</select>-->
		</td>
	</tr>
	<tr>
		<td>comments</td>
		<td class="left">
			<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>
