PDO::CASE_NATURAL, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_EMULATE_PREPARES => false, PDO::MYSQL_ATTR_INIT_COMMAND => " SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION', time_zone = '+00:00'; ", ]); } catch(Exception $ex) { http_response_code(500); echo '

Unable to connect to database

'; die($ex->getMessage()); } if(isset($_POST['temp']) && isset($_POST['hash']) && isset($_POST['time'])) { $temp = (string)filter_input(INPUT_POST, 'temp', FILTER_SANITIZE_STRING); $hash = (string)filter_input(INPUT_POST, 'hash', FILTER_SANITIZE_STRING); $time = (string)filter_input(INPUT_POST, 'time', FILTER_SANITIZE_NUMBER_INT); if(!hash_equals(hash_hmac('sha256', $temp . '|' . $time, FM_TEMP_KEY), $hash)) return; $time = floor((int)json_decode($time) / FM_TEMP_INT); if($time !== floor(time() / FM_TEMP_INT)) return; $insert = $pdo->prepare('INSERT INTO `fm_temperature` (`temp_celcius`) VALUES (:temp)'); $insert->bindValue('temp', (string)json_decode($temp)); $insert->execute(); return; } if(isset($_GET['since'])) { $since = (int)filter_input(INPUT_GET, 'since', FILTER_SANITIZE_NUMBER_INT); $temps = $pdo->prepare('SELECT `temp_celcius`, UNIX_TIMESTAMP(`temp_datetime`) AS `temp_datetime` FROM `fm_temperature` WHERE `temp_datetime` > FROM_UNIXTIME(:since) AND `temp_datetime` > NOW() - INTERVAL 1 DAY ORDER BY `temp_datetime` ASC LIMIT 288'); $temps->bindValue('since', $since); $temps->execute(); header('Content-Type: application/json; charset=utf-8'); echo json_encode($temps->fetchAll(PDO::FETCH_ASSOC)); return; } if(isset($_GET['since_3mo'])) { $since = (int)filter_input(INPUT_GET, 'since_3mo', FILTER_SANITIZE_NUMBER_INT); $temps = $pdo->prepare('SELECT MAX(`temp_celcius`) AS `temp_celcius_max`, AVG(`temp_celcius`) AS `temp_celcius_avg`, MIN(`temp_celcius`) AS `temp_celcius_min`, UNIX_TIMESTAMP(DATE(`temp_datetime`)) AS `temp_datetime` FROM `fm_temperature` WHERE `temp_datetime` > DATE(FROM_UNIXTIME(:since)) AND `temp_datetime` > NOW() - INTERVAL 3 MONTH GROUP BY DATE(`temp_datetime`) ORDER BY `temp_datetime` ASC LIMIT 92'); $temps->bindValue('since', $since); $temps->execute(); header('Content-Type: application/json; charset=utf-8'); echo json_encode($temps->fetchAll(PDO::FETCH_ASSOC)); return; } if(isset($_GET['since_6mo'])) { $since = (int)filter_input(INPUT_GET, 'since_6mo', FILTER_SANITIZE_NUMBER_INT); $temps = $pdo->prepare('SELECT MAX(`temp_celcius`) AS `temp_celcius_max`, AVG(`temp_celcius`) AS `temp_celcius_avg`, MIN(`temp_celcius`) AS `temp_celcius_min`, UNIX_TIMESTAMP(DATE(`temp_datetime`)) AS `temp_datetime` FROM `fm_temperature` WHERE `temp_datetime` > DATE(FROM_UNIXTIME(:since)) AND `temp_datetime` > NOW() - INTERVAL 6 MONTH GROUP BY DATE(`temp_datetime`) ORDER BY `temp_datetime` ASC LIMIT 92'); $temps->bindValue('since', $since); $temps->execute(); header('Content-Type: application/json; charset=utf-8'); echo json_encode($temps->fetchAll(PDO::FETCH_ASSOC)); return; } if(!empty($_GET['siri'])) { header('Content-Type: text/plain; charset=utf-8'); $temps = $pdo->prepare('SELECT `temp_celcius`, UNIX_TIMESTAMP(`temp_datetime`) AS `temp_datetime` FROM `fm_temperature` ORDER BY `temp_datetime` DESC LIMIT 1'); $temps->execute(); $temps = $temps->fetch(PDO::FETCH_ASSOC); date_default_timezone_set('Europe/Amsterdam'); printf('It was %2$.1f°C at %1$s.', date('H:i:s', $temps['temp_datetime']), $temps['temp_celcius']); return; } ?> Room Temperature
-- °C
--:--:--