function mainCLI()
{
    global $schedulePars;
    writeLog("RaspiCam support started");
    $captureStart = 0;
    $pipeIn = openPipe($schedulePars[SCHEDULE_FIFOIN]);
    $lastDayPeriod = -1;
    $cmdPeriod = -1;
    $lastOnCommand = -1;
    $timeout = 0;
    $timeoutMax = 0;
    //Loop test will terminate after this (seconds) (used in test), set to 0 forever
    while ($timeoutMax == 0 || $timeout < $timeoutMax) {
        writeLog("Scheduler loop is started");
        $pollTime = $schedulePars[SCHEDULE_CMDPOLL];
        $slowPoll = 0;
        $managechecktime = time();
        $modechecktime = $managechecktime;
        if ($schedulePars[SCHEDULE_AUTOCAPTUREINTERVAL] > $schedulePars[SCHEDULE_MAXCAPTURE]) {
            $autocapturetime = $managechecktime;
            $autocapture = 2;
        } else {
            $autocapturetime = 0;
            $autocapture = 0;
        }
        while ($timeoutMax == 0 || $timeout < $timeoutMax) {
            usleep($pollTime * 1000000);
            //Check for incoming motion capture requests
            $cmd = "";
            $cmd = checkMotion($pipeIn);
            if ($cmd == SCHEDULE_STOP && $autocapture == 0) {
                if ($lastOnCommand >= 0) {
                    writeLog('Stop capture requested');
                    $send = $schedulePars[SCHEDULE_COMMANDSOFF][$lastOnCommand];
                    if ($send) {
                        sendCmds($send);
                        $lastOnCommand = -1;
                    }
                } else {
                    writeLog('Stop capture request ignored, already stopped');
                }
            } else {
                if ($cmd == SCHEDULE_START || $autocapture == 1) {
                    if ($lastOnCommand < 0 && $lastDayPeriod >= 0) {
                        if ($autocapture == 1) {
                            $autocapture = 2;
                            writeLog('Start triggered by autocapture');
                        } else {
                            writeLog('Start capture requested from Pipe');
                        }
                        $send = $schedulePars[SCHEDULE_COMMANDSON][$lastDayPeriod];
                        if ($send) {
                            sendCmds($send);
                            $lastOnCommand = $lastDayPeriod;
                            $captureStart = time();
                        }
                    } else {
                        writeLog('Start capture request ignored, already started');
                    }
                } else {
                    if ($cmd == SCHEDULE_RESET) {
                        writeLog("Reload parameters command requested");
                        $schedulePars = loadPars(BASE_DIR . '/' . SCHEDULE_CONFIG);
                        //start outer loop
                        break;
                    } else {
                        if ($cmd != "") {
                            writeLog("Ignore FIFO char {$cmd}");
                        }
                    }
                }
            }
            //slow Poll actions done every 10 fast loops times
            $slowPoll--;
            if ($slowPoll < 0) {
                $slowPoll = 10;
                $timenow = time();
                //Action period time change checks at MODE_POLL intervals
                if ($timenow > $modechecktime) {
                    //Set next period check time
                    $modechecktime = $timenow + $schedulePars[SCHEDULE_MODEPOLL];
                    if ($lastOnCommand < 0) {
                        //No capture in progress, Check if day period changing
                        $newDayPeriod = dayPeriod();
                        if ($newDayPeriod != $lastDayPeriod) {
                            writeLog("New period detected {$newDayPeriod}");
                            sendCmds($schedulePars[SCHEDULE_MODES][$newDayPeriod]);
                            $lastDayPeriod = $newDayPeriod;
                        }
                    }
                }
                if ($lastOnCommand >= 0) {
                    //Capture in progress, Check for maximum
                    if ($schedulePars[SCHEDULE_MAXCAPTURE] > 0) {
                        if ($timenow - $captureStart >= $schedulePars[SCHEDULE_MAXCAPTURE]) {
                            writeLog("Maximum Capture reached. Sending off command");
                            sendCmds($schedulePars[SCHEDULE_COMMANDSOFF][$lastOnCommand]);
                            $lastOnCommand = -1;
                            $autocapture = 0;
                        }
                    }
                }
                if ($timenow > $managechecktime) {
                    // Run management tasks
                    //Set next check time
                    $managechecktime = $timenow + $schedulePars[SCHEDULE_MANAGEMENTINTERVAL];
                    writeLog("Scheduled management tasks. Next at {$managechecktime}");
                    purgeFiles();
                    $cmd = $schedulePars[SCHEDULE_MANAGEMENTCOMMAND];
                    if ($cmd != '') {
                        writeLog("exec_macro: {$cmd}");
                        sendCmds("sy {$cmd}");
                    }
                }
                if ($autocapturetime > 0 && $timenow > $autocapturetime) {
                    // Request autocapture and set next interval
                    $autocapturetime = $timenow + $schedulePars[SCHEDULE_AUTOCAPTUREINTERVAL];
                    writeLog("Autocapture request.");
                    $autocapture = 1;
                }
            }
        }
    }
}
function mainCLI()
{
    global $schedulePars;
    writeLog("RaspiCam support started");
    $captureStart = 0;
    $pipeIn = openPipe($schedulePars[SCHEDULE_FIFOIN]);
    $lastDayPeriod = -1;
    $cmdPeriod = -1;
    $lastOnCommand = -1;
    $timeout = 0;
    $timeoutMax = 0;
    //Loop test will terminate after this (seconds) (used in test), set to 0 forever
    while ($timeoutMax == 0 || $timeout < $timeoutMax) {
        writeLog("Scheduler loop is started");
        $pollTime = $schedulePars[SCHEDULE_CMDPOLL];
        $slowPoll = 0;
        $managechecktime = time();
        $autocameratime = $managechecktime;
        $modechecktime = $managechecktime;
        if ($schedulePars[SCHEDULE_AUTOCAPTUREINTERVAL] > $schedulePars[SCHEDULE_MAXCAPTURE]) {
            $autocapturetime = $managechecktime;
            $autocapture = 2;
        } else {
            $autocapturetime = 0;
            $autocapture = 0;
        }
        $lastStatusTime = filemtime(BASE_DIR . "/status_mjpeg.txt");
        while ($timeoutMax == 0 || $timeout < $timeoutMax) {
            usleep($pollTime * 1000000);
            //Check for incoming motion capture requests
            $cmd = "";
            $cmd = checkMotion($pipeIn);
            if ($cmd == SCHEDULE_STOP && $autocapture == 0) {
                if ($lastOnCommand >= 0) {
                    writeLog('Stop capture requested');
                    $send = $schedulePars[SCHEDULE_COMMANDSOFF][$lastOnCommand];
                    if ($send) {
                        sendCmds($send);
                        $lastOnCommand = -1;
                    }
                } else {
                    writeLog('Stop capture request ignored, already stopped');
                }
            } else {
                if ($cmd == SCHEDULE_START || $autocapture == 1) {
                    if ($lastDayPeriod >= 0) {
                        if ($autocapture == 1) {
                            $autocapture = 2;
                            writeLog('Start triggered by autocapture');
                        } else {
                            writeLog('Start capture requested from Pipe');
                        }
                        $send = $schedulePars[SCHEDULE_COMMANDSON][$lastDayPeriod];
                        if ($send) {
                            sendCmds($send);
                            $lastOnCommand = $lastDayPeriod;
                            $captureStart = time();
                        }
                    } else {
                        writeLog('Start capture request ignored, day period not initialised yet');
                    }
                } else {
                    if ($cmd == SCHEDULE_RESET) {
                        writeLog("Reload parameters command requested");
                        $schedulePars = loadPars(BASE_DIR . '/' . SCHEDULE_CONFIG);
                        //start outer loop
                        break;
                    } else {
                        if ($cmd != "") {
                            writeLog("Ignore FIFO char {$cmd}");
                        }
                    }
                }
            }
            //slow Poll actions done every 10 fast loops times
            $slowPoll--;
            if ($slowPoll < 0) {
                $slowPoll = 10;
                $timenow = time();
                //Action period time change checks at MODE_POLL intervals
                if ($timenow > $modechecktime) {
                    //Set next period check time
                    $modechecktime = $timenow + $schedulePars[SCHEDULE_MODEPOLL];
                    if ($lastOnCommand < 0) {
                        //No capture in progress, Check if day period changing
                        $newDayPeriod = dayPeriod();
                        if ($newDayPeriod != $lastDayPeriod) {
                            writeLog("New period detected {$newDayPeriod}");
                            sendCmds($schedulePars[SCHEDULE_MODES][$newDayPeriod]);
                            $lastDayPeriod = $newDayPeriod;
                        }
                    }
                }
                if ($lastOnCommand >= 0) {
                    //Capture in progress, Check for maximum
                    if ($schedulePars[SCHEDULE_MAXCAPTURE] > 0) {
                        if ($timenow - $captureStart >= $schedulePars[SCHEDULE_MAXCAPTURE]) {
                            writeLog("Maximum Capture reached. Sending off command");
                            sendCmds($schedulePars[SCHEDULE_COMMANDSOFF][$lastOnCommand]);
                            $lastOnCommand = -1;
                            $autocapture = 0;
                        }
                    }
                }
                if ($timenow > $managechecktime) {
                    // Run management tasks
                    //Set next check time
                    $managechecktime = $timenow + $schedulePars[SCHEDULE_MANAGEMENTINTERVAL];
                    writeLog("Scheduled management tasks. Next at {$managechecktime}");
                    purgeFiles();
                    $cmd = $schedulePars[SCHEDULE_MANAGEMENTCOMMAND];
                    if ($cmd != '') {
                        writeLog("exec_macro: {$cmd}");
                        sendCmds("sy {$cmd}");
                    }
                }
                if ($autocapturetime > 0 && $timenow > $autocapturetime) {
                    // Request autocapture and set next interval
                    $autocapturetime = $timenow + $schedulePars[SCHEDULE_AUTOCAPTUREINTERVAL];
                    writeLog("Autocapture request.");
                    $autocapture = 1;
                }
                //Check for auto camera on/off based on status update timing (active browser)
                if ($schedulePars[SCHEDULE_AUTOCAMERAINTERVAL] > 0 && $timenow > $autocameratime) {
                    // 2 seconds between tests to allow time for commands to take effect
                    $autocameratime = $timenow + 2;
                    clearstatcache();
                    $modTime = filemtime(BASE_DIR . "/status_mjpeg.txt");
                    if (file_get_contents(BASE_DIR . "/status_mjpeg.txt") == 'halted') {
                        if ($modTime > $lastStatusTime) {
                            writeLog("autocamera startup");
                            sendCmds('ru 1');
                        }
                    } else {
                        if ($timenow - $modTime > $schedulePars[SCHEDULE_AUTOCAMERAINTERVAL]) {
                            writeLog("autocamera shutdown");
                            sendCmds('md 0;ru 0');
                            //allow a bit of time to ensure it doesn't switch straight back on
                            $lastStatusTime = $timenow + 5;
                        } else {
                            $lastStatusTime = $timenow;
                        }
                    }
                }
            }
        }
    }
}