Commit 3b0788d7 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

finisheshed daemon-control javascript

parent 620a1357
Loading
Loading
Loading
Loading
+222 −108
Original line number Original line Diff line number Diff line
@@ -4,13 +4,26 @@
  <style></style>
  <style></style>
<script language="JavaScript">
<script language="JavaScript">
var initialized = false;
var initialized = false;
var seq_counter =  0;

var duration =   100;
var pre_delay;//  =     5.0;
var pre_delay =    5.0;
var duration;//  =    100;
var ffc_period = 30;
var duration_eo;//  = 100;
var run =      true;
var ffc;//  =         false;
var want_run = true;
var ffc_period;//  =   30;
var next_ffc_sec = 0; // will be overdue
var ffc_groups;//  =    2;
var ffc_frames;//  =    8;
var compressor_run;//  =  false;
var debug;//  =        0;

var sequence_num;//  = 0;
var last_ffc;//  =     0;
var time_to_ffc;//  =  0;
var capture_run;//  =  0;
var want_run;
var request_num = 0;
var update_editable = true;
var apply_pending = false;

function parse_response(resp){
function parse_response(resp){
  var result = "";
  var result = "";
  console.log(resp);
  console.log(resp);
@@ -41,134 +54,235 @@ function send_request(rq,callback){
  request.send();
  request.send();
}
}


function sendStatusRequest(){
    var url = "http://192.168.0.41/lwir16/lwir16.php?daemon=status";
    if (apply_pending) {
        var mod_pars =   modParameters();
//    console.log('mod_pars members:');
        for(const p in mod_pars) {
            url +="&"+p+"="+mod_pars[p];
        }
        update_editable = true; // will update edited fields
        apply_pending = false;
    }
    send_request(url,parseStatusResponse);
}

function sendRestartRequest(){
    var url = "http://192.168.0.41/lwir16/lwir16.php?daemon=restart&cmd=STATUS";
    send_request(url,parseStatusResponse);
}





function init_lwir(){
function parseStatusResponse(resp){
    send_request("http://192.168.0.41/lwir16/lwir16.php?cmd=init",init_response);
  var result = "";
    document.getElementById('idpre_delay').value=  ""+pre_delay;
//  console.log(resp);
    document.getElementById('idduration').value=   ""+duration;
  if (update_editable) {
    document.getElementById('idffc_period').value= ""+ffc_period;
    if (resp.getElementsByTagName("pre_delay").length!=0){
        pre_delay = parseFloat(resp.getElementsByTagName("pre_delay")[0].childNodes[0].nodeValue);
        document.getElementById("idpre_delay").value = pre_delay;
    }
    }
function init_response(resp){
    
    initialized = true;
    if (resp.getElementsByTagName("duration").length!=0){
//    alert ("LWIR16 initialized");
        duration = parseInt(resp.getElementsByTagName("duration")[0].childNodes[0].nodeValue);
    document.getElementById('idStartStop').innerHTML= run? 'STOP': 'START';
        document.getElementById("idduration").value = duration;
    console.log("LWIR16 initialized");
    want_run = run;
    captureSet();
    }
    }
    
    
function capture_response(resp){
    if (resp.getElementsByTagName("duration_eo").length!=0){
    document.getElementById('idcount').value=""+seq_counter;
        duration_eo = parseInt(resp.getElementsByTagName("duration_eo")[0].childNodes[0].nodeValue);
//    document.getElementById('idcount').innerHTML=""+seq_counter;
        document.getElementById("idduration_eo").value = duration_eo;
    //id="idcount"
    seq_counter++;
    if (want_run) {
        run = true;
//        send_request("http://192.168.0.41/lwir16/lwir16.php?cmd=capture&duration="+duration+"&pre_delay="+pre_delay,capture_response);
        captureSet();
    } else {
        run = false;
        document.getElementById('idStartStop').innerHTML='START';
    }
    }

    if (resp.getElementsByTagName("ffc").length!=0){
        ffc = parseInt(resp.getElementsByTagName("ffc")[0].childNodes[0].nodeValue);
        document.getElementById("idffc").checked = ffc > 0;
    }

    if (resp.getElementsByTagName("ffc_period").length!=0){
        ffc_period = parseFloat(resp.getElementsByTagName("ffc_period")[0].childNodes[0].nodeValue);
        document.getElementById("idffc_period").value = ffc_period;
    }
    }


function updatePreDelay(field_id){
    if (resp.getElementsByTagName("ffc_groups").length!=0){
  var v=parseDouble(document.getElementById(field_id).value);
        ffc_groups = parseInt(resp.getElementsByTagName("ffc_groups")[0].childNodes[0].nodeValue);
  if (isNaN(v)){
        document.getElementById("idffc_groups").value = ffc_groups;
    v = 5.0;
    }
    }
  pre_delay = v;
    
  document.getElementById(field_id).value=""+v;
    if (resp.getElementsByTagName("ffc_frames").length!=0){
        ffc_frames = parseInt(resp.getElementsByTagName("ffc_frames")[0].childNodes[0].nodeValue);
        document.getElementById("idffc_frames").value = ffc_frames;
    }
    }


    if (resp.getElementsByTagName("compressor_run").length!=0){
        compressor_run = parseInt(resp.getElementsByTagName("compressor_run")[0].childNodes[0].nodeValue);
        document.getElementById("idcompressor_run").checked = compressor_run > 0;
    }
    
    
function updateDuration(field_id){
    if (resp.getElementsByTagName("debug").length!=0){
  var v=parseInt(document.getElementById(field_id).value);
        debug = parseInt(resp.getElementsByTagName("debug")[0].childNodes[0].nodeValue);
  if (isNaN(v)){
        document.getElementById("iddebug").value = debug;
    v = 100;
    }
  }
  }
  duration = v;
  if (resp.getElementsByTagName("sequence_num").length!=0){
  document.getElementById(field_id).value=""+v;
  	sequence_num = parseInt(resp.getElementsByTagName("sequence_num")[0].childNodes[0].nodeValue);
  	document.getElementById("idsequence_num").value = sequence_num;
  }
  }


function updateFFCPeriod(field_id){
  if (resp.getElementsByTagName("last_ffc").length!=0){
  var v=parseInt(document.getElementById(field_id).value);
  	last_ffc = parseFloat(resp.getElementsByTagName("last_ffc")[0].childNodes[0].nodeValue);
  if (isNaN(v)){
  	document.getElementById("idlast_ffc").value = last_ffc;
    v = 100;
  }
  }
  ffc_period = v;
  
  document.getElementById(field_id).value=""+v;
  if (resp.getElementsByTagName("time_to_ffc").length!=0){
  	time_to_ffc = parseFloat(resp.getElementsByTagName("time_to_ffc")[0].childNodes[0].nodeValue);
  	document.getElementById("idtime_to_ffc").value = time_to_ffc;
  }
  }
  
  
  if (resp.getElementsByTagName("capture_run").length!=0){
  	capture_run = parseInt(resp.getElementsByTagName("capture_run")[0].childNodes[0].nodeValue);
  	document.getElementById("idcapture_run").checked = capture_run > 0;
  	if (update_editable) {
        document.getElementById("idStartStop").innerHTML=capture_run?"Stop":"Start";
        document.getElementById("idStartStop").disabled = false;
        want_run = capture_run;
  	}
  }
  if (update_editable) {
       document.getElementById('idApply').innerHTML='Apply';
       document.getElementById('idApply').disabled= false;
       document.getElementById('idRestart').innerHTML='Restart';
       document.getElementById('idRestart').disabled= false;
       
       
function clickedRun(field_id){
    want_run = !run;
//    alert("want_run="+want_run);
    if (want_run){
        run = true;
        document.getElementById(field_id).innerHTML='STOP';
        captureSet();
//        send_request("http://192.168.0.41/lwir16/lwir16.php?cmd=capture&duration="+duration+"&pre_delay="+pre_delay,init_response);
  }
  }
  request_num++;
  document.getElementById("idrequest_num").value = request_num;
  update_editable = false;
  sendStatusRequest();
}
}


function modParameters(){
    var mod_pars={};
    if (document.getElementById("idpre_delay").value != (""+pre_delay)){
        mod_pars['pre_delay'] = document.getElementById("idpre_delay").value;
    }
    if (document.getElementById("idduration").value != (""+duration)){
        mod_pars['duration'] = document.getElementById("idduration").value;
    }
    if (document.getElementById("idduration_eo").value != (""+duration_eo)){
        mod_pars['duration_eo'] = document.getElementById("idduration_eo").value;
    }
    if (document.getElementById("idffc").checked != ffc){
        mod_pars['ffc'] = document.getElementById("idffc").checked?"1":"0";
    }
    if (document.getElementById("idffc_period").value != (""+ffc_period)){
        mod_pars['ffc_period'] = document.getElementById("idffc_period").value;
    }
    if (document.getElementById("idffc_groups").value != (""+ffc_groups)){
        mod_pars['ffc_groups'] = document.getElementById("idffc_groups").value;
    }
    if (document.getElementById("idffc_frames").value != (""+ffc_frames)){
        mod_pars['ffc_frames'] = document.getElementById("idffc_frames").value;
    }
    if (document.getElementById("idcompressor_run").checked != compressor_run){
        mod_pars['compressor_run'] = document.getElementById("idcompressor_run").checked?"1":"0";
    }
    if (want_run != capture_run) {
        mod_pars['cmd'] = want_run? "START":"STOP"; 
    }
    console.log('mod_pars members:');
    for(const p in mod_pars) {
        console.log (p, mod_pars[p]);
    }
    return mod_pars;
}


function captureSet(){
function setPending(){
    var now_sec = 0.001 * ((new Date()).getTime());
    document.getElementById('idApply').innerHTML='Pending';
    var ffc_cmd = "";
    document.getElementById('idApply').disabled= true;
    if (now_sec > next_ffc_sec) {
    document.getElementById("idStartStop").innerHTML="Pending";
        ffc_cmd = "&ffc";
    document.getElementById("idStartStop").disabled = true;
        next_ffc_sec = now_sec + ffc_period;
    document.getElementById("idRestart").innerHTML="Pending";
        document.getElementById("idffc").value="NOW";
    document.getElementById("idRestart").disabled = true;
    } else {
    apply_pending = true;
            document.getElementById('idffc').value=""+ (0.01 * Math.round(100*(next_ffc_sec - now_sec)));
}

function clickedRestart(){
    setPending();
    sendRestartRequest();
}
function clickedRun(){
    want_run = !capture_run;
    setPending();
}
}
    send_request("http://192.168.0.41/lwir16/lwir16.php?cmd=capture&duration="+duration+"&pre_delay="+pre_delay+ffc_cmd,capture_response);

function clickedApply(){
    setPending();
}
}


</script>
</script>
  </head>
  </head>
  
  
  
  
  <body onload='init_lwir()'>
  <body onload='sendStatusRequest()'>
  <table>
  <table>
  <tr>
  <td>Pre-capture delay</td><td>
        <input id="idpre_delay" name="npre_delay" size="10" maxlength="10" value="?" onchange="updatePreDelay('idpre_delay')" type="text">
  </td>
  <td><button id="idStartStop" name = "nStartStop" onclick="clickedRun('idStartStop')">???</button></td>
  </tr>
  <tr>
  <td>Sequence length (@60Hz)</td><td>
        <input id="idduration" name="nduration" size="10" maxlength="10" value="?" onchange="updateDuration('idduration')" type="text">
  </td>
  <td>frames</td>
  </tr>

  <tr>
  <td>FFC period</td><td>
        <input id="idffc_period" name="nffc_period" size="10" maxlength="10" value="?" onchange="updateFFCPeriod('idffc_period')" type="text">
  </td>
  <td>sec</td>
  </tr>

  
  <tr>
    <td>Sequence count</td><td>
       <input id="idcount" name="ncount" size="10" maxlength="10" value="?" type="text" disabled >
    </td>
    <td>captured</td>
  </tr>

  <tr>
    <td>Time to ffc</td><td>
       <input id="idffc" name="ncount" size="10" maxlength="10" value="?" type="text" disabled >
    </td>
  <td>sec</td>
  </tr>
  
  
  <tr><td>
  <button id="idStartStop" name = "nStartStop" onclick="clickedRun()" disabled="true">???</button>
  </td><td>
    <button id="idApply" name = "nApply" onclick="clickedApply()"      disabled = "true">???</button>
</td><td>&nbsp;</td> <td>&nbsp;</td><td>&nbsp;</td>
<td><button id="idRestart" name = "nRestart" onclick="clickedRestart()" >Restart</button></td></tr>
  
  <tr><td>Pre-capture delay</td><td>
        <input id="idpre_delay" name="npre_delay" size="10" maxlength="10" value="?" type="text">
  </td><td> sec</td><td>Sequence count</td><td>
       <input id="idsequence_num" name="nsequence_num" size="10" maxlength="10" value="?" type="text" disabled >
  </td><td>captured</td></tr>

  <tr><td>Sequence length (@60Hz)</td><td>
        <input id="idduration" name="nduration" size="10" maxlength="10" value="?" type="text">
  </td><td>frames</td><td>Request count</td><td>
       <input id="idrequest_num" name="nrequest_num" size="10" maxlength="10" value="?" type="text" disabled >
  </td><td>&nbsp;</td></tr>

  <tr><td>Sequence length (@10Hz)</td><td>
        <input id="idduration_eo" name="nduration_eo" size="10" maxlength="10" value="?"  type="text">
  </td><td>frames</td><td>Capture run</td><td>
        <input id="idcapture_run" name="ncapture_run" type="checkbox" disabled>
  </td><td>&nbsp;</td></tr>
  
  <tr><td>FFC</td><td>
        <input id="idffc" name="nffc" type="checkbox">
  </td><td>&nbsp;</td><td>Last FFC</td><td>
       <input id="idlast_ffc" name="nlast_ffc" size="10" maxlength="10" value="?" type="text" disabled >
  </td><td>sec</td></tr>

  <tr><td>FFC period</td><td>
        <input id="idffc_period" name="nffc_period" size="10" maxlength="10" value="?" type="text">
  </td><td>sec</td><td>Time to FFC</td><td>
       <input id="idtime_to_ffc" name="ntime_to_ffc" size="10" maxlength="10" value="?" type="text" disabled >
  </td><td>sec</td></tr>

  <tr><td>FFC groups</td><td>
        <select id="idffc_groups" name="nffc_groups"  value="?">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="4" selected="selected">4</option>
        </select>
  </td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>

  <tr><td>FFC frames</td><td>
        <input id="idffc_frames" name="nffc_frames" size="10" maxlength="10" value="?" type="text">
  </td><td>frames</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
  
  <tr><td>Compress after Init</td><td>
        <input id="idcompressor_run" name="ncompressor_run" type="checkbox">
  </td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>


  <tr><td>Debug level</td><td>
        <input id="iddebug" name="ndebug" size="10" maxlength="10" value="?" type="text">
  </td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
  </table>
  </table>
  </body>
  </body>
</html>
</html>
+2 −2
Original line number Original line Diff line number Diff line
ips =        "192.168.0.41,192.168.0.42,192.168.0.43,192.168.0.44,192.168.0.45"
ips =        "192.168.0.41,192.168.0.42,192.168.0.43,192.168.0.44,192.168.0.45"
duration =   100
duration =   100
pre_delay =  5.0
pre_delay =  3.0
ffc_period = 300.0
ffc_period = 30.0
ffc_groups = 2
ffc_groups = 2
ffc_frames = 8
ffc_frames = 8
ffc =        1
ffc =        1
+92 −30
Original line number Original line Diff line number Diff line
@@ -63,12 +63,14 @@
   define('DAEMON_CTRL',        'daemon');
   define('DAEMON_CTRL',        'daemon');
   define('DAEMON_CTRL_CMDS',   'cmd'); //comma-separated commands to be sent to the daemon
   define('DAEMON_CTRL_CMDS',   'cmd'); //comma-separated commands to be sent to the daemon
   
   
//   print_r($_GET); // magic
//   exit(0); // magic
   
   
   // initializations before reading lwir16.ini
   // initializations before reading lwir16.ini
   $GLOBALS[COMPRESSOR_RUN] = 0;
   $GLOBALS[COMPRESSOR_RUN] = 0;
   $GLOBALS[DURATION] = 100;
   $GLOBALS[DURATION] = 100;
   $GLOBALS[PRE_DELAY] = 5.0; // seconds
   $GLOBALS[PRE_DELAY] = 5.0; // seconds
   $GLOBALS[FFC] =       false; // perform FFC before starting a sequence (and before delay? reduce delay ?)
   $GLOBALS[FFC] =        0; // perform FFC before starting a sequence (and before delay? reduce delay ?)
//   $ffc =        false; 
//   $ffc =        false; 
   $GLOBALS[FFC_GROUPS] = 2; // 1/2/4 - do not run FFC on all channels simultaneously (43 failed)
   $GLOBALS[FFC_GROUPS] = 2; // 1/2/4 - do not run FFC on all channels simultaneously (43 failed)
   $GLOBALS[FFC_FRAMES] = 8; // read actual?
   $GLOBALS[FFC_FRAMES] = 8; // read actual?
@@ -108,7 +110,7 @@
       } else if ($key == 'run'){
       } else if ($key == 'run'){
           $GLOBALS[COMPRESSOR_RUN] = 2;
           $GLOBALS[COMPRESSOR_RUN] = 2;
       } else if ($key == 'ffc'){
       } else if ($key == 'ffc'){
           $GLOBALS[FFC] = true;
           $GLOBALS[FFC] = 1;
           if ($value) { // string "0" will also be false
           if ($value) { // string "0" will also be false
               $v = (int) $value;
               $v = (int) $value;
               if (($v == 1) || ($v == 2) || ($v == 4)){
               if (($v == 1) || ($v == 2) || ($v == 4)){
@@ -117,6 +119,10 @@
           }
           }
       }
       }
   }
   }
//   print_r($lswir16cmds);
//   exit(0);
   
   
   if (isset($duration) && !isset($duration_eo)){
   if (isset($duration) && !isset($duration_eo)){
       $duration_eo = (int) ($duration/EO_DECIMATE + 1);
       $duration_eo = (int) ($duration/EO_DECIMATE + 1);
   }
   }
@@ -125,6 +131,7 @@
   if ($GLOBALS[DURATION] < 1)    $GLOBALS[DURATION] = 1;
   if ($GLOBALS[DURATION] < 1)    $GLOBALS[DURATION] = 1;
   if ($GLOBALS[DURATION_EO] < 1) $GLOBALS[DURATION_EO] = 1;
   if ($GLOBALS[DURATION_EO] < 1) $GLOBALS[DURATION_EO] = 1;
   
   
   
   if (isset($lswir16cmds)){
   if (isset($lswir16cmds)){
       $lwir_trig_dly =       0;
       $lwir_trig_dly =       0;
       $eo_quality =         97;
       $eo_quality =         97;
@@ -180,7 +187,7 @@
           } else if ($cmd == 'capture'){
           } else if ($cmd == 'capture'){
               $results =  runCapture($GLOBALS[FFC], $nowait); // runCapture($run_ffc, $nowait = 0, $debug=0)
               $results =  runCapture($GLOBALS[FFC], $nowait); // runCapture($run_ffc, $nowait = 0, $debug=0)
               $xml = new SimpleXMLElement("<?xml version='1.0'  standalone='yes'?><capture_range/>");
               $xml = new SimpleXMLElement("<?xml version='1.0'  standalone='yes'?><capture_range/>");
               $xml->addChild ('ffc', $GLOBALS[FFC] ? 'true':'false');
               $xml->addChild ('ffc', $GLOBALS[FFC]);
               $xml->addChild (FFC_GROUPS,$GLOBALS[FFC_GROUPS]);
               $xml->addChild (FFC_GROUPS,$GLOBALS[FFC_GROUPS]);
               $xml->addChild (FFC_FRAMES,$GLOBALS[FFC_FRAMES]);
               $xml->addChild (FFC_FRAMES,$GLOBALS[FFC_FRAMES]);
               for ($i = 0; $i<count($GLOBALS[IPS]); $i++){
               for ($i = 0; $i<count($GLOBALS[IPS]); $i++){
@@ -591,11 +598,9 @@ EOT;
                               $xml_state->addChild (trim($kv[0]),trim($kv[1]));
                               $xml_state->addChild (trim($kv[0]),trim($kv[1]));
                           }
                           }
                       }
                       }
                       $xml->addChild (SECUENCE_NUM, $GLOBALS[SECUENCE_NUM]);
                       if ($GLOBALS[FFC]) {
                       $GLOBALS[TIME_TO_FFC] = $GLOBALS[LAST_FFC] + $GLOBALS[FFC_PERIOD] - time();
                       $GLOBALS[TIME_TO_FFC] = $GLOBALS[LAST_FFC] + $GLOBALS[FFC_PERIOD] - time();
                       $xml->addChild (SECUENCE_NUM,    $GLOBALS[SECUENCE_NUM]);
                       $xml->addChild (TIME_TO_FFC,     $GLOBALS[TIME_TO_FFC]);
                       $xml->addChild (TIME_TO_FFC,     $GLOBALS[TIME_TO_FFC]);
                       }
                       $xml->addChild (IPS, implode(',',$GLOBALS[IPS]));
                       $xml->addChild (IPS, implode(',',$GLOBALS[IPS]));
                       $xml->addChild (DURATION,        $GLOBALS[DURATION]);
                       $xml->addChild (DURATION,        $GLOBALS[DURATION]);
                       $xml->addChild (DURATION_EO,     $GLOBALS[DURATION_EO]);
                       $xml->addChild (DURATION_EO,     $GLOBALS[DURATION_EO]);
@@ -653,6 +658,8 @@ EOT;
                   print_r($ini);
                   print_r($ini);
               }
               }
               unset ($GLOBALS[DAEMON_CMD]);
               unset ($GLOBALS[DAEMON_CMD]);
               
               
               applyConf ($ini); // update $GLOBALS
               applyConf ($ini); // update $GLOBALS
               $from_pipe = true;
               $from_pipe = true;
               if ($GLOBALS[DEBUG] > 1){
               if ($GLOBALS[DEBUG] > 1){
@@ -726,7 +733,7 @@ EOT;
       if (isset($arr[DAEMON_CMD]))      $GLOBALS[DAEMON_CMD] =             $arr[DAEMON_CMD];
       if (isset($arr[DAEMON_CMD]))      $GLOBALS[DAEMON_CMD] =             $arr[DAEMON_CMD];
       if (isset($arr[DEBUG]))           $GLOBALS[DEBUG] = (int)            $arr[DEBUG];
       if (isset($arr[DEBUG]))           $GLOBALS[DEBUG] = (int)            $arr[DEBUG];
       if (isset($arr[COMPRESSOR_RUN]))  $GLOBALS[COMPRESSOR_RUN] = (int)   $arr[COMPRESSOR_RUN]; // only after INIT
       if (isset($arr[COMPRESSOR_RUN]))  $GLOBALS[COMPRESSOR_RUN] = (int)   $arr[COMPRESSOR_RUN]; // only after INIT
       if (isset($arr[FFC]))             $GLOBALS[FFC] =                    $arr[FFC]?true:false; // only after INIT
       if (isset($arr[FFC]))             $GLOBALS[FFC] =                    $arr[FFC]?1:0;
   }
   }


/**
/**
@@ -736,41 +743,88 @@ EOT;
 */
 */
    function daemon_control($cmd)
    function daemon_control($cmd)
    {
    {
       /*
//        echo "<pre>\n";
        echo "<pre>\n";
//        print_r($_SERVER);
        print_r($_SERVER);
//        echo "</pre>\n";
        echo "</pre>\n";
//        exit(0);
        exit(0);
        */
        // see if it already running
        // see if it already running
        $debug = 0;
        $max_wait = 10; // seconds
        $max_wait = 10; // seconds
//        $sript_name = substr($_SERVER['SCRIPT_NAME'], 1); // remove leading '/'
//        $sript_name = substr($_SERVER['SCRIPT_NAME'], 1); // remove leading '/'
        $sript_path = $_SERVER['SCRIPT_FILENAME'];
        $sript_path = $_SERVER['SCRIPT_FILENAME'];
        $sript_name = basename($sript_path);
        $sript_name = basename($sript_path);
        $pids = getPIDByName($sript_name, 'php', $active_only = false);
        $pids = getPIDByName($sript_name, 'php', $active_only = false);
        
//        echo "<pre>0:\n";
//        print_r($pids);
//        echo "</pre>\n";
        
        // Stop if needed
        // Stop if needed
        if ($pids && (($cmd == 'restart') || ($cmd == 'stop'))) {
        if ($pids && (($cmd == 'restart') || ($cmd == 'stop'))) {
            $mode = 0600;
            // below - does not work with non-blocking "w+", will take care later
            if (! file_exists(PIPE_CMD)) {
            /*
                // create the pipe
         * $mode = 0600;
                umask(0);
         * if (! file_exists(PIPE_CMD)) {
                posix_mkfifo(PIPE_CMD, $mode);
         * // create the pipe
         * umask(0);
         * posix_mkfifo(PIPE_CMD, $mode);
         * }
         * $f = fopen(PIPE_CMD, "w+"); // make it non-blocking as the receiver may be hang
         * fwrite($f, "CMD=EXIT");
         * fclose($f);
         * for ($i = 0; $pids && ($i < $max_wait); $i ++) {
         * sleep (1);
         * $pids = getPIDByName($sript_name, 'php', $active_only = false);
         * }
         */

            // if(file_exists(PIPE_CMD)) unlink(PIPE_CMD); //delete pipe
            // if(file_exists(PIPE_RESPONSE)) unlink(PIPE_RESPONSE); //delete pipe

            // print_r($pids);
        if ($debug) {
            echo "<pre>1:\n";
            print_r($pids);
            echo "</pre>\n";
        }
        }
            $f = fopen(PIPE_CMD, "w+"); // make it non-blocking as the receiver may be hang
            
            fwrite($f, "CMD=EXIT");
            if ($pids) { // did not exit, ask politely
            fclose($f);
                foreach ($pids as $proc) {
            for ($i = 0; $i < $max_wait; $i ++) {
                    exec('kill ' . $proc['pid'], $output, $retval);
                }
            }
            if ($debug) {
                echo "<pre>2:\n";
                print_r($pids);
                echo "</pre>\n";
            }
            
            for ($i = 0; $pids && ($i < $max_wait); $i ++) {
                sleep (1);
                $pids = getPIDByName($sript_name, 'php', $active_only = false);
                $pids = getPIDByName($sript_name, 'php', $active_only = false);
                if (! $pid)
                    break;
            }
            }
            if ($pids) { // did not exit
            if ($debug) {
                echo "<pre>3:\n";
                print_r($pids);
                echo "</pre>\n";
            }
            if ($pids) { // did not exit - just kill them
                foreach ($pids as $proc) {
                foreach ($pids as $proc) {
                    exec('kill -9 ' . $proc['pid'], $output, $retval);
                    exec('kill -9 ' . $proc['pid'], $output, $retval);
                }
                }
            }
            }
            $pids = getPIDByName($sript_name, 'php', $active_only = false);
            if ($debug) {
                echo "<pre>4\n";
                print_r($pids);
                echo "</pre>\n";
            }
            
        }
        }
        $pids = getPIDByName($sript_name, 'php', $active_only = false);
        $pids = getPIDByName($sript_name, 'php', $active_only = false);
        
//        print_r($pids);
        
        if (!$pids && ($cmd != 'stop')) {
        if (!$pids && ($cmd != 'stop')) {
            exec($sript_path . ' > /dev/null 2>&1 &'); // "> /dev/null 2>&1 &" makes sure it is really really run as a background job that does not wait for input
            exec($sript_path . ' > /dev/null 2>&1 &'); // "> /dev/null 2>&1 &" makes sure it is really really run as a background job that does not wait for input
// wait it to run
// wait it to run
@@ -780,8 +834,16 @@ EOT;
            }
            }
            
            
        }
        }
        /*
        $exit_ps = ($cmd == 'stop') || !pids ; // nothing else to do if it is not running
        if (($cmd == 'restart') || ($cmd == 'start')){
            if (!isset ($_GET[DAEMON_CTRL_CMDS]) && !isset ($_GET[DAEMON_CMD])){ // no other commands (like "status")
                $exit_ps = true;
            }
        }
        */
        if (($cmd == 'restart') || ($cmd == 'start') || ($cmd == 'stop') || !pids) { // nothing else to do if it is not running
        if (($cmd == 'restart') || ($cmd == 'start') || ($cmd == 'stop') || !pids) { // nothing else to do if it is not running
            // just respond with $pids xml
//        if ($exit_ps) { // nothing else to do if it is not running just respond with $pids xml
            $xml = new SimpleXMLElement("<?xml version='1.0'  standalone='yes'?><lwir16_daemon_status/>");
            $xml = new SimpleXMLElement("<?xml version='1.0'  standalone='yes'?><lwir16_daemon_status/>");
            if ($pids) {
            if ($pids) {
                /*
                /*