modified: database/sqlite.php

modified:   device-api.php
This commit is contained in:
Sven Luethi committed 2024-07-13 02:37:54 +02:00
1 parent c44e580120
commit 9759ab5ee4
2 files changed
+80 -42

No files matched your search

+29 -20
View File
@@ -1,5 +1,18 @@
<?php
class DB {
interface DBIface
{
public function __construct();
public function addDevice(DeviceData $deviceData);
public function getDevices();
public function getDeviceById($id);
public function updateDevice($id, $deviceData);
public function deleteDevice($id);
public function createOrUpdateReading($deviceID, $temperature, $humidity);
}
class DB implements DBIface {
private $pdo;
public function __construct() {
@@ -51,8 +64,8 @@ class DB {
public function addDevice($deviceData) {
$sql = "
INSERT INTO devices (id, mac_address, ip_address, seen_ip_address, firmware_version, temperature_offset, humidity_offset, last_updated, last_seen)
VALUES (:id, :mac_address, :ip_address, seen_ip_address, :firmware_version, :temperature_offset, :humidity_offset, :last_updated, :last_seen)
INSERT INTO devices (mac_address, ip_address, seen_ip_address, firmware_version, temperature_offset, humidity_offset, last_updated, last_seen)
VALUES (:mac_address, :ip_address, seen_ip_address, :firmware_version, :temperature_offset, :humidity_offset, :last_updated, :last_seen)
";
try {
$stmt = $this->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();
+51 -22
View File
@@ -1,31 +1,60 @@
<?php
// Returns a new ID for device to use for future communication.
$router->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.