RPR/02_Software/02_RaspberryPi/updateBallDatabase.php

103 lines
3.2 KiB
PHP
Raw Permalink Normal View History

<?php
include('db.inc.php');
$q = intval($_GET['q']);
// Load Plate with balls and pass the result to html-file
if($q == 0) {
$result = $conn->query("SELECT pos_id, pos_x, pos_y, ball_color FROM ball_positions WHERE pos_isEmpty = 0 AND ball_targetPos = 0");
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
$conn->close();
}
// Load Shelf with balls and pass the result to html-file
if($q == 3) {
$result = $conn->query("SELECT pos_id, ball_color, ball_targetPos FROM ball_positions WHERE ball_targetPos != 0 ORDER BY ball_prio");
$outp = array();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
$conn->close();
}
// Write to database, if a ball has been moved to the shelf
if($q == 1) {
$ballID = intval($_GET['ballID']);
$shelfID = intval($_GET['shelfID']);
$prio = intval($_GET['prio']);
$live = intval($_GET['live']);
// Create instructions only if webside is in live-mode
if ($live == 1) {
$conn->query("UPDATE ball_positions SET ball_targetPos = $shelfID, ball_prio = $prio, sorted_by_robot = 1 WHERE pos_id = $ballID");
//Get infos for python-script
$result = $conn->query("SELECT pos_x, pos_y, ball_targetPos FROM ball_positions WHERE pos_id = $ballID");
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$posX = $row['pos_x'];
$posY = $row['pos_y'];
$target = $row['ball_targetPos'];
passthru('python create_instructions.py '.$posX.' '.$posY.' '.$target.'');
} else {
$conn->query("UPDATE ball_positions SET ball_targetPos = $shelfID, ball_prio = $prio, sorted_by_robot = 0 WHERE pos_id = $ballID");
}
$conn->close();
}
// Empty the shelf -> Reset target-positions and priorities
if($q == 2) {
$conn->query("UPDATE ball_positions SET pos_isEmpty = 0, ball_targetPos = 0, ball_prio = -1, sorted_by_robot = 0");
$conn->query("UPDATE ball_positions SET pos_isEmpty = 1 WHERE pos_id = 29");
$conn->close();
echo json_encode("done");
}
// Autosort balls in Database
if($q == 4) {
$result = $conn->query("SELECT pos_id, ball_color FROM ball_positions WHERE pos_isEmpty = 0 AND ball_targetPos = 0 ORDER BY ball_color, pos_id");
$numColor = array(0, 0, 0, 0); // Store Blue, Red, Green, Yellow
$actColor = "undefined";
$i = 0;
$prio = 0;
$shelfID = 10;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$actID = $row['pos_id'];
$actColor = $row['ball_color'];
if ($i == 0) $lastColor = $actColor; // Don't go to next shelf at first run
if (($i % 5 == 0 && $i != 0) || $actColor != $lastColor) { // If the shelf is full or color changes
$shelfID++;
$i = 0;
}
if ($shelfID % 5 == 0 && $shelfID % 10 != 0 && $shelfID != 10) {// If the last shelf in a row is reached
$shelfID = $shelfID + 5;
$i = 0;
}
$conn->query("UPDATE ball_positions SET ball_targetPos = $shelfID, ball_prio = $prio WHERE pos_id = $actID");
$lastColor = $actColor;
$i++;
$prio++;
}
$conn->close();
echo json_encode("done");
}
// Call python-script for refreshing the plate (scanning the positions)
if($q == 5) {
passthru('python update_VarAngle.py');
}
// Call python-script for doing the sorting of the balls which are in the shelf in Not-Live-Mode
if($q == 6) {
passthru('python readBalls.py');
}
?>