<?php
$position=$_GET["p"];
$sensorType=$_GET["sensor"];
$nite=$_GET['nite'];
if ($position==0)
{
    echo "error";
    return;
}
set_time_limit(0);
ob_end_clean();//清空（擦除）缓冲区并关闭输出缓冲
ob_implicit_flush(1);//将打开或关闭绝对（隐式）刷送。绝对（隐式）刷送将导致在每次输出调用后有一次刷送操作，以便不再需要对 flush() 的显式调用
//echo  shell_exec("D:\\03MyGit\\linux\\movingliner\\cmake-build-debug-mingw\\movingliner.exe");
echo "application beging<br>";
//$cmd = 'ping 127.0.0.1';
$ini=parse_ini_file("/home/data/Setting/DeviceSettings.ini",true);

$numberofsensor=intval($ini['DEVICE INFO']['TotalSpectrometer']);
$numberofFS=0;
    for($i=0;$i<$numberofsensor;$i++){
        $tempsens=$ini['FS'.($i+1)]['Model'];
        if ($sensorType==$tempsens)
        {
            $numberofFS=$i+1;
            break;
        }
        if ($i==$numberofsensor-1)
        {
            echo "cannot find the sensor";
            return;
        }
    }



system("sudo killall Mywathdog.sh");
system("sudo killall TowerOptoSifAndSpectral");
system("sudo pkill ocean_optics_calibration_console");
system("sudo gpio write 1 1");
$cmd="";




if ($position==1)
{
    if($sensorType=="OSIFAlpha"||$sensorType=="OSIFBeta")
    {
      $cmd = 'sudo /home/pi/bin/ocean_optics_calibration_console --deviceType OceanOptics -t 30 -slfs IrrLamp.txt --cfon FSN'.$sensorType.$position.'.data --position '.$position.' --integratingSphereDetectorValue '.$nite;
    }
    else{
        $port=$ini['FS'.($i+1)]['Port'];
        getonestring($port,'/');
        getonestring($port,'/');
        $cmd = 'sudo /home/pi/bin/ocean_optics_calibration_console --deviceType OPTOSKY --serialPort '.$port.' -t 30 -slfs IrrLamp.txt --cfon FSN'.$sensorType.$position.'.data --position '.$position.' --integratingSphereDetectorValue '.$nite;

    }


}
else
{
    if($sensorType=="OSIFAlpha"||$sensorType=="OSIFBeta")
    {
      $cmd = 'sudo /home/pi/bin/ocean_optics_calibration_console --deviceType OceanOptics -t 30 -slfs RadLamp.txt --cfon FSN'.$sensorType.$position.'.data --position '.$position.' --integratingSphereDetectorValue '.$nite;
    }else{
        $port=$ini['FS'.($i+1)]['Port'];
        getonestring($port,'/');
        getonestring($port,'/');
        $cmd = 'sudo /home/pi/bin/ocean_optics_calibration_console --deviceType OPTOSKY --serialPort '.$port.' -t 30 -slfs RadLamp.txt --cfon FSN'.$sensorType.$position.'.data --position '.$position.' --integratingSphereDetectorValue '.$nite;

    }
}


echo $cmd;

while(@ ob_end_flush());

$proc = popen($cmd, 'r');


while(!feof($proc)){
    echo fread($proc, 4096);
    @ flush();
}
echo 'FINISH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ';
echo '<a href=/config/position.html>回到首页</a>';

function getonestring(&$bytes,$fenge)
{
    $pattern = '{^.*?'.$fenge.'}';
    $lenth=strlen($fenge);
    preg_match($pattern, $bytes, $aa, PREG_OFFSET_CAPTURE);
    $bytes = substr($bytes, strlen($aa[0][0]));
    $bb = substr($aa[0][0], 0, -$lenth);
    return $bb;
}