<?php
session_start();
require("accesBase.php");
$err = false;
require("erreurs.php");

if(isset($_POST['sensor']))
{
	$ini = "";
	$arduino_simple = "[Arduino]\r\n";
	$arduino_spi = "[Arduino]\r\n";
	$nucleo = "[Nucleo]\r\n";

	$a_simple = false;
	$a_spi = false;
	$n = false;
	$ordre = " ordre=0";

	foreach($_POST['sensor'] as $sensor => $value)
	{
		$version = -1;
		$val = explode(":", $value);

		if(count($types)<>0 && !in_array($val[2], $types)) {$err = true;}
		else {$types[] = $val[2];}

		$version = getSensorVersion($val[0], $val[1]);
		if($version == 0)
		{
			$a_simple = true;
			$arduino_simple = $arduino_simple.$val[0]." capteur=".$val[1]."-".$version.$ordre."\n";
		}
		elseif($version == 1)
		{
			$a_spi = true;
			$arduino_spi = $arduino_spi.$val[0]." capteur=".$val[1]."-".$version.$ordre."\n";
		}
		elseif($version == 2)
		{
			$n = true;
			$nucleo = $nucleo.$val[0]." capteur=".$val[1]."-".$version.$ordre."\n";
		}
	}

	if($a_simple) {$ini = $ini.$arduino_simple."\n";}
	if($a_spi) {$ini = $ini.$arduino_spi."\n";}
	if($n) {$ini = $ini.$nucleo."\n";}

	$retour = array();
	$statut;

	if(!$err)
	{
		file_put_contents('upload/inventaire.ini', $ini);
		$upload = load($_FILES['fichier']['tmp_name'], $_FILES['fichier']['name']);
		if($upload)
		{
			exec("super cmd.sh", $retour, $statut);
			$size = count($retour);

			if($statut == 0)
			{
				$message = "Upload du fichier réussi !";
				$color = "alert-success";
			}
			else
			{
				$ansible = "Code retour : ".$statut." => ".$errors[$statut]."<br/>";
				$message = "Erreur lors du transfert du fichier...";
				$color = "alert-danger";
			}

			for($i = 0; $i<$size; $i++)
			{
				$ansible = $ansible.$retour[$i]."<br/>";
			}
		}
		else
		{
			$message = "Erreur lors du transfert du fichier...";
			$color = "alert-danger";
		}
	}
	else
	{
		$message = "Veuillez choisir des capteurs de même type !";
		$color = "alert-danger";
	}
}
?>

<!DOCTYPE html>

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

	<body>
		<?php 
		include("header.php"); 
		include("reponse.php");
		if(!$err) {include("tableau.php");}
		else {include("capteurs.php");}
		?>
	</body>
</html>
