# HG changeset patch # User Michiel Broek # Date 1660138391 -7200 # Node ID b5e2ff72cc731873d3819b26576c26e796842ee6 # Parent 409f8c4974296a08b88370284afbcd95aaa0972b Switch ispindel graph to MySQL diff -r 409f8c497429 -r b5e2ff72cc73 www/getispindellog.php --- a/www/getispindellog.php Wed Aug 10 15:21:06 2022 +0200 +++ b/www/getispindellog.php Wed Aug 10 15:33:11 2022 +0200 @@ -3,48 +3,27 @@ require_once('config.php'); if (isset($_GET["code"])) - $code = $_GET["code"] . ' ' . $_GET["name"]; + $code = $_GET["code"]; else - $code = "CB0081 Keuls"; + $code = "CB0111"; -$filename = 'log/ispindel/' . $code . '.log'; -if (! file_exists($filename)) { - header("Content-type: application/json"); - echo '{}'; - exit; +$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" ); -/* - * 2014-11-15 18:39:12,TEMPERATURE,PLATO,SG,BATTERY,ANGLE,INVERVAL,UUID - * | | | | | - * datetime + | | | | - * temperature ------------+ | | | - * density plato --------------------+ | | - * density sg ----------------------------+ | - * battery ------------------------------------+ - */ -$handle = @fopen($filename, "r"); -if ($handle) { - $lines = 0; - while (($buffer = fgets($handle, 4096)) !== false) { +$query = "SELECT * FROM log_ispindel 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)) { - $buffer = preg_replace( "/\r|\n/", "", $buffer); - $row = explode(",", $buffer); - $lines++; - - $logs[] = array( - 'date' => $row[0], - 'temperature' => $row[1], - 'plato' => $row[2], - 'sg' => $row[3], - 'battery' => $row[4] - ); - } - if (!feof($handle)) { - echo "Error: unexpected fgets() fail\n"; - } - fclose($handle); + $logs[] = array( + 'date' => substr($row['datetime'],0,16), + 'temperature' => $row['temperature'], + 'plato' => $row['plato'], + 'sg' => $row['sg'], + 'battery' => $row['battery'] + ); } header("Content-type: application/json"); echo json_encode($logs);