modified: database/sqlite.php
modified: device-api.php
This commit is contained in:
1 parent
c44e580120
commit
9759ab5ee4
2 files changed
+80
-42
No files matched your search
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user