From 9759ab5ee4d5a7333e263b0961e97a29ec3f676d Mon Sep 17 00:00:00 2001 From: Sven Luethi Date: Sat, 13 Jul 2024 02:37:54 +0200 Subject: [PATCH] modified: database/sqlite.php modified: device-api.php --- software/database/sqlite.php | 49 ++++++++++++++---------- software/device-api.php | 73 +++++++++++++++++++++++++----------- 2 files changed, 80 insertions(+), 42 deletions(-) diff --git a/software/database/sqlite.php b/software/database/sqlite.php index d1f65c0..8a9f8f7 100644 --- a/software/database/sqlite.php +++ b/software/database/sqlite.php @@ -1,5 +1,18 @@ pdo->prepare($sql); @@ -120,31 +133,27 @@ class DB { } public function createOrUpdateReading($deviceID, $temperature, $humidity) { + $readingData = [ + "dev_id" => $deviceID, + "temperature" => $temperature, + "humidity" => $humidity + ]; + $sql = "SELECT COUNT(*) FROM readings WHERE dev_id = :dev_id"; try { $stmt = $this->pdo->prepare($sql); - $stmt->execute(['dev_id' => $deviceID]); + //$stmt->execute(['dev_id' => $deviceID]); + $stmt->execute($readingData); $exists = $stmt->fetchColumn() > 0; if ($exists) { - $sql = " - UPDATE readings - SET temperature = :temperature, - humidity = :humidity - WHERE dev_id = :dev_id - "; + $sql = "UPDATE readings SET temperature = :temperature, humidity = :humidity WHERE dev_id = :dev_id"; $stmt = $this->pdo->prepare($sql); - $stmt->execute([ - "dev_id" => $deviceID, - "temperature" => $temperature, - "humidity" => $humidity - ]); + $stmt->execute($readingData); return $stmt->rowCount(); - } else { - $sql = " - INSERT INTO readings (dev_id, temperature, humidity) - VALUES (:dev_id, :temperature, :humidity) - "; + } + else { + $sql = "INSERT INTO readings (dev_id, temperature, humidity) VALUES (:dev_id, :temperature, :humidity)"; $stmt = $this->pdo->prepare($sql); $stmt->execute($readingData); return $this->pdo->lastInsertId(); diff --git a/software/device-api.php b/software/device-api.php index 2ebdb70..1fa6a97 100644 --- a/software/device-api.php +++ b/software/device-api.php @@ -1,31 +1,60 @@ post('/api/register', function($req) { - $data = $req->getJSONBody(); - //echo "Body: ".var_dump($data)."\n"; +function valid($msg, $key, $regex) { + //if(!isset($msg[$key])) { + /* + if(!array_key_exists($key,$msg)) { + echo "{\"error_message\": \"Key " . $key . " does not exist in msg.\"}"; + return false; + } + else if(!preg_match($regex,$msg[$key])) { + echo "{\"error_message\": \"" . $msg[$key] . " does not match regex " . $regex . "\"}"; + return false; + } + else { + return true; + } + */ + return array_key_exists($key,$msg) && preg_match($regex,$msg[$key]); +} - // Verify a mac address was sent... - if(!isset($data['mac_address']) || !preg_match("/^[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]$/i",$data['mac_address'])) { - echo $data['mac_address']." does not match a mac address.\n"; +// Update device configuration +$router->post('/api/config', function($req) { + $msg = $req->getJSONBody(); + + if(!valid($msg,'mac_address', '/^[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]$/i') || + !valid($msg,'ip_address', '/^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/i') || + !valid($msg,'firmware_version', '/^[0-9]+\.[0-9]+$/') || + !valid($msg,'temperature_offset', '/^[0-9]+\.*[0-9]*$/') || + !valid($msg,'humidity_offset', '/^[0-9]+\.*[0-9]*$/') + ) { + echo '{"error_message": "Field verification failed."}'; http_response_code(400); return; } - /* - $newDevice = [ - 'id' => 'device123', - 'mac_address' => $data['mac_address'], - 'ip_address' => '192.168.1.2', - 'seen_ip_address' => '192.168.1.2', - 'firmware_version' => '1.0.1', - 'temperature_offset' => 0, - 'humidity_offset' => 0, - 'last_updated' => date('Y-m-d H:i:s'), - 'last_seen' => date('Y-m-d H:i:s') - ]; - $deviceDb->addDevice($newDevice); - */ - http_response_code(200); + + // If device is already registered (it sends an ID) then figure out what values changed and send those back for the device to re-configure itself. + else if(valid($msg,'id','/^[0-9]+$/')) { + http_response_code(200); + return; + } + + // All fields are set and no ID was given. Register new device and return new ID... + else { + $config = [ + 'mac_address' => $msg['mac_address'], + 'ip_address' => $msg['ip_address'], + 'seen_ip_address' => $_SERVER['REMOTE_ADDR'], + 'firmware_version' => $msg['firmware_version'], + 'temperature_offset' => $msg['temperature_offset'], + 'humidity_offset' => $msg['humidity_offset'], + 'last_updated' => date('Y-m-d H:i:s'), + 'last_seen' => date('Y-m-d H:i:s') + ]; + $deviceID = $db->addDevice($config); + echo json_encode(['deviceID' => $deviceID]); + http_response_code(200); + } }); // Device reports temperature back on this endpoint.