add.php 2.66 KB
<?php
session_start();
?>
<!DOCTYPE html>

<html>
        <head>
                <?php include("head.php"); ?>
        </head>

        <!-- Contenu du site -->
        <body>
                <?php 
                include("header.php");
                if(isset($_SESSION['Login'], $_SESSION['Password']))
                {
                        ?>
			<div class="row">
			        <div class="col-6 col-md-6 offset-md-3 offset-3">
                			<br/>
                			<h1 style="margin-bottom:20px; margin-top:70px; border-bottom:1px solid #CCC; padding-bottom:20px;"><i class="fas fa-history"></i> Ajout d'un capteur</h1>
					<?php
					if(add($_POST['nom'], $_POST['type'], $_POST['raspberry'], $_POST['unite'], $_POST['version']))
					{
						$arduino_simple = "[Arduino_init]\r\n";
						$arduino_spi = "[Arduino_init]\r\n";
						$nucleo = "[Nucleo_init]\r\n";

						$setup = getSetup($_POST['raspberry']);

						if($_POST['version'] == 0)
						{
							$ini = $arduino_simple;
						}
						elseif($_POST['version'] == 1)
						{
							$ini = $arduino_spi;
						}
						elseif($_POST['version'] == 2)
						{
							$ini = $nucleo;
						}

						$numero = getNumero($_POST['nom']);
						$ini = $ini.$_POST['raspberry']." capteur=".$numero."-".$_POST['version']." setup=".$setup."\n";
						file_put_contents('upload/inventaire.ini', $ini);
						setup($_POST['raspberry']);

						$temps = getTime($_POST['type']);
						file_put_contents('upload/temps.txt', $temps);

						$retour = array();
						$statut;
						exec("super cmd.sh", $retour, $statut);
					
						if($statut == 0)
						{
							$message = "Le capteur a bien été ajouté !";
							$color = "alert-success";
						}
						else
						{
							$ansible = "Code retour : ".$statut."<br/>";
							$message = "Echec lors de l'ajout. Vérifiez vos informations !";
							$color = "alert-danger";
						}

						$size = count($retour);
						for($i = 0; $i<$size; $i++)
						{
							$ansible = $ansible.$retour[$i]."<br/>";
						}

						?>
						<div class="alert <?php echo $color;?>" role="alert">
							<?php echo $message;?>
						</div>
						<div class="alert alert-secondary" role="alert">
							<?php echo $ansible;?>
						</div>
						<?php
					}
					else
					{
						?>
						<div class="alert alert-danger" role="alert">
					     		Echec lors de l'ajout. Vérifiez vos informations !
						</div>
						<meta http-equiv="Refresh" content="3;url=gestionCapteur.php" />
						<?php
					}
					?>
        			</div>
			</div>
			<?php
                }
                else
                {
                        include("formConnexion.php");
                }
                ?>
        </body>
</html>