Exemplo n.º 1
0
function processRadio($radioID)
{
    global $db;
    $reachedTheEnd = 0;
    // try 5 times to get all the lines.
    for ($tries = 0; $tries < 5; $tries++) {
        $response = radioCommand($radioID, "du");
        foreach ($response as $line) {
            // get rid of first line
            if (strpos($line, "Transm") === 0) {
                continue;
            }
            $lineArray = explode("-", $line);
            $thisNumber = hexdec(substr($lineArray[0], -2));
            $radioOutput[$thisNumber] = $line;
            if (strpos($lineArray[0], "END") === 0 and $thisNumber + 1 == count($radioOutput)) {
                $reachedTheEnd = 1;
                $tries = 5;
            }
        }
    }
    if ($reachedTheEnd == 0) {
        echo "Didn't receive all lines<br/>";
        return false;
    }
    // K we got all the lines in order. Lets process them
    $misc = $switches = $programs = $inputs = $timeLimits = $colorChanges = "";
    foreach ($radioOutput as $line) {
        if (strpos($line, "Transm") === 0) {
            continue;
        }
        $lineArray = explode("-", $line);
        $letter = substr($lineArray[0], 0, 1);
        if ($letter == "M") {
            $misc .= $lineArray[1];
        } else {
            if ($letter == "S") {
                $switches .= $lineArray[1];
            } else {
                if ($letter == "P") {
                    $programs .= $lineArray[1];
                } else {
                    if ($letter == "I") {
                        $inputs .= $lineArray[1];
                    } else {
                        if ($letter == "T") {
                            $timeLimits .= $lineArray[1];
                        } else {
                            if ($letter == "C") {
                                $colorChanges .= $lineArray[1];
                            } else {
                                $radioTime = $lineArray[1];
                            }
                        }
                    }
                }
            }
        }
    }
    // TODO: use bind to make these sql entries safe
    $stuff = explode("|", $misc);
    $sql = "update radios set clockTweak = '{$stuff[0]}', daylightSavingsStartMonth = '{$stuff[1]}', " . "daylightSavingsStartDay = '{$stuff[2]}', daylightSavingsStopMonth = '{$stuff[3]}', " . "daylightSavingsStopDay = '{$stuff[4]}', pwmDirection = '{$stuff[5]}', " . "inputMessageTiming = '{$stuff[6]}', hueSpeed = '{$stuff[7]}', " . "colorChangeSpeed = '{$stuff[8]}' where id = {$radioID}";
    if (!$db->exec($sql)) {
        echo $db->lastErrorMsg();
        echo " updating radios<br>";
    }
    $switchDetails = str_split($switches, 4);
    for ($x = 0; $x < count($switchDetails); $x++) {
        $thisSwitch = str_split($switchDetails[$x], 2);
        $thisSwitchStuff = hexdec($thisSwitch[0]);
        $thisSwitchPWM = hexdec($thisSwitch[1]);
        $sql = "insert or ignore into switches (radioID, switchNumber, switchStuff, switchPWM) values " . "({$radioID}, {$x}, {$thisSwitchStuff}, {$thisSwitchPWM})";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at switches 1<br/>{$sql}<br/>";
        }
        $sql = "update switches set switchStuff = {$thisSwitchStuff}, " . "switchPWM = {$thisSwitchPWM} where radioID = {$radioID} and switchNumber = {$x}";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at switches 2<br/>{$sql}<br/>";
        }
    }
    $programDetails = str_split($programs, 20);
    for ($x = 0; $x < count($programDetails); $x++) {
        $thisProgram = str_split($programDetails[$x], 2);
        $thisDays = hexdec($thisProgram[0]);
        $thisStart = hexdec($thisProgram[1] . $thisProgram[2]);
        $thisDuration = hexdec($thisProgram[3] . $thisProgram[4]);
        $thisSwitches = "";
        for ($y = 5; $y <= 8; $y++) {
            if ($thisProgram[$y] != "FF") {
                if ($thisSwitches != "") {
                    $thisSwitches .= ",";
                }
                $thisSwitches .= $thisProgram[$y];
            }
        }
        $thisRollover = hexdec($thisProgram[9]);
        $sql = "insert or ignore into programs (radioID, programNumber, days, time, duration, switches, rollover) values " . "('{$radioID}', '{$x}', '{$thisDays}', '{$thisStart}', '{$thisDuration}', '{$thisSwitches}', '{$thisRollover}')";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at programs 1<br/>{$sql}<br/>";
        }
        $sql = "update programs set days = '{$thisDays}', time = '{$thisStart}', duration = '{$thisDuration}', switches = " . "'{$thisSwitches}', rollover = '{$thisRollover}' where radioID = '{$radioID}' and programNumber = '{$x}'";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at programs 2<br/>{$sql}<br/>";
        }
    }
    $inputDetails = str_split($inputs, 16);
    for ($x = 0; $x < count($inputDetails); $x++) {
        $thisInput = str_split($inputDetails[$x], 2);
        $thisPinStuff = hexdec($thisInput[0]);
        $thisLowPercent = hexdec($thisInput[1]);
        $thisHighPercent = hexdec($thisInput[2]);
        $thisWhichSwitchOrProgram = hexdec($thisInput[3]);
        $thisDuration = hexdec($thisInput[4] . $thisInput[5]);
        $thisPollTime = hexdec($thisInput[6]);
        $thisWhichRGB = hexdec($thisInput[7]);
        $sql = "insert or ignore into inputs (radioID, inputNumber, pinStuff, lowPercent, highPercent, " . "whichSwitchOrProgram, duration, pollTime, whichRGB) values " . "('{$radioID}', '{$x}', '{$thisPinStuff}', '{$thisLowPercent}', '{$thisHighPercent}', " . "'{$thisWhichSwitchOrProgram}', '{$thisDuration}', '{$thisPollTime}', '{$thisWhichRGB}')";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at inputs 1<br/>{$sql}<br/>";
        }
        $sql = "update inputs set pinStuff = '{$thisPinStuff}', lowPercent = '{$thisLowPercent}', highPercent = " . "'{$thisHighPercent}', whichSwitchOrProgram = {$thisWhichSwitchOrProgram}, duration = {$thisDuration}, " . "pollTime = '{$thisPollTime}', whichRGB = '{$thisWhichRGB}' where radioID = '{$radioID}' and inputNumber = '{$x}'";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at inputs 2<br/>{$sql}<br/>";
        }
    }
    $timeLimitsDetails = str_split($timeLimits, 14);
    for ($x = 0; $x < count($timeLimitsDetails); $x++) {
        $thisStartTime = hexdec(substr($timeLimitsDetails[$x], 0, 6));
        $thisStopTime = hexdec(substr($timeLimitsDetails[$x], 6, 6));
        $thisDays = hexdec(substr($timeLimitsDetails[$x], 12, 2));
        $sql = "insert or ignore into timeLimits (radioID, limitNumber, startTime, stopTime, " . "days) values ('{$radioID}', '{$x}', '{$thisStartTime}', '{$thisStopTime}', '{$thisDays}')";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at timeLimits 1<br/>{$sql}<br/>";
        }
        $sql = "update timeLimits set startTime = '{$thisStartTime}', stopTime = '{$thisStopTime}', " . "days = '{$thisDays}' where radioID = '{$radioID}' and limitNumber = '{$x}'";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at timeLimits 2<br/>{$sql}<br/>";
        }
    }
    $colorChangeDetails = str_split($colorChanges, 7);
    for ($x = 0; $x < count($colorChangeDetails); $x++) {
        $thisColorChange = str_split($colorChangeDetails[$x]);
        $thisRed = hexdec($thisColorChange[0] . $thisColorChange[1]);
        $thisGreen = hexdec($thisColorChange[2] . $thisColorChange[3]);
        $thisBlue = hexdec($thisColorChange[4] . $thisColorChange[5]);
        if ($thisColorChange[6] == "Y") {
            $thisChangeable = 1;
        } else {
            $thisChangeable = 0;
        }
        $sql = "insert or ignore into colorChanges (radioID, colorChangeNumber, red, green, blue, " . "ifChangeable) values ({$radioID}, {$x}, {$thisRed}, {$thisGreen}, {$thisBlue}, {$thisChangeable})";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at colorchanges 1<br/>{$sql}<br/>";
        }
        $sql = "update colorchanges set red = {$thisRed}, green = {$thisGreen}, blue = {$thisBlue}, " . "ifChangeable = {$thisChangeable} where radioID = {$radioID} and colorChangeNumber = {$x}";
        if (!$db->exec($sql)) {
            echo $db->lastErrorMsg();
            echo " at colorchanges 2<br/>{$sql}<br/>";
        }
    }
    return "ok";
}
Exemplo n.º 2
0
function updateTime()
{
    $radioID = intval($_POST['radioID']);
    include_once "functions.php";
    $response = radioCommand($radioID, "", "/");
    if ($response == false) {
        return "fail";
    }
    return "ok";
}