# HG changeset patch # User Michiel Broek # Date 1660138820 -7200 # Node ID ce5f39b66a517cca5466cd91463adb3a5a29428c # Parent b5e2ff72cc731873d3819b26576c26e796842ee6 Switch co2pressure graph to MySQL diff -r b5e2ff72cc73 -r ce5f39b66a51 www/getco2pressurelog.php --- a/www/getco2pressurelog.php Wed Aug 10 15:33:11 2022 +0200 +++ b/www/getco2pressurelog.php Wed Aug 10 15:40:20 2022 +0200 @@ -3,35 +3,9 @@ require_once('config.php'); if (isset($_GET["code"])) - $code = $_GET["code"] . ' ' . $_GET["name"]; + $code = $_GET["code"]; else - $code = "CB0080 Op stoom"; - -$filename = 'log/co2pressure/' . $code . '.log'; -if (! file_exists($filename)) { - header("Content-type: application/json"); - echo '{}'; - exit; -} - - -/* - * From Stackoverflow, the fastest way to count the lines in a file. - */ -$file = new \SplFileObject($filename, 'r'); -$file->seek(PHP_INT_MAX); - -define ('MAX_INTERVALS', 10); -$GRAPH_INTERVAL = array ( 0, 1, 5, 15, 30, 60, 120, 240, 480, 720 ); -$GRAPH_DATALINES = array ( 0, 800, 3200, 12000, 24000, 48000, 96000, 192000, 384000, 768000 ); - - -for ($graphstep = 1; $graphstep <= MAX_INTERVALS; $graphstep++) { - if ($file->key() < $GRAPH_DATALINES[$graphstep]) - break; -} -if ($graphstep > MAX_INTERVALS) - $graphstep = MAX_INTERVALS; + $code = "CB0080"; /* @@ -43,38 +17,21 @@ * unit uuid --------------------------------+ */ - -$handle = @fopen($filename, "r"); -if ($handle) { - $lines = 0; - while (($buffer = fgets($handle, 4096)) !== false) { - - $buffer = preg_replace( "/\r|\n/", "", $buffer); - $row = explode(",", $buffer); +$connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME); +if (! $connect) { + die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); +} +mysqli_set_charset($connect, "utf8" ); - $hr = intval(substr($buffer, 11, 2)); - if ((($graphstep == 1)) || - (($graphstep == 2) && ((substr($buffer, 15, 1) == '0') || (substr($buffer, 15, 1) == '5'))) || - (($graphstep == 3) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '15') || - (substr($buffer, 14, 2) == '30') || (substr($buffer, 14, 2) == '45'))) || - (($graphstep == 4) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '30'))) || - (($graphstep == 5) && (substr($buffer, 14, 2) == '00')) || - (($graphstep == 6) && (substr($buffer, 14, 2) == '00') && (($hr % 2) == 0)) || - (($graphstep == 7) && (substr($buffer, 14, 2) == '00') && (($hr % 4) == 0)) || - (($graphstep == 8) && (substr($buffer, 14, 2) == '00') && (($hr % 8) == 0)) || - (($graphstep == 9) && (substr($buffer, 14, 2) == '00') && (($hr % 12) == 0))) { - $lines++; - $logs[] = array( - 'date' => $row[0], - 'temperature' => $row[1], - 'pressure' => $row[2] - ); - } - } - if (!feof($handle)) { - echo "Error: unexpected fgets() fail\n"; - } - fclose($handle); +$query = "SELECT * FROM log_co2pressure WHERE code='".$code."' ORDER BY datetime;"; +$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect)); +while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { + + $logs[] = array( + 'date' => substr($row['datetime'],0,16), + 'temperature' => $row['temperature'], + 'pressure' => $row['pressure'] + ); } header("Content-type: application/json"); echo json_encode($logs);