modified: sqlite.php
This commit is contained in:
1 parent
2e5a70d33f
commit
ed4aee284b
1 file changed
+168
-5
@@ -1,10 +1,173 @@
|
|||||||
<?php
|
<?php
|
||||||
class DB
|
class DB {
|
||||||
{
|
private $pdo;
|
||||||
|
|
||||||
function __construct()
|
public function __construct() {
|
||||||
{
|
try {
|
||||||
//echo "Time to set up!";
|
if (!file_exists('db.sqlite') {
|
||||||
|
$this->pdo = new PDO("sqlite:db.sqlite");
|
||||||
|
$this->createTables();
|
||||||
|
syslog(LOG_INFO,"New SQLite Database was created.");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$this->pdo = new PDO("sqlite:db.sqlite");
|
||||||
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
$this->pdo->exec("PRAGMA foreign_keys = ON");
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
syslog(LOG_EMERG,"Could not connect to the database: " . $e->getMessage());
|
||||||
|
die("Could not connect to the database: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createTables() {
|
||||||
|
$sql = "
|
||||||
|
CREATE TABLE IF NOT EXISTS devices (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
device_id TEXT NOT NULL,
|
||||||
|
mac_address TEXT NOT NULL,
|
||||||
|
ip_address TEXT NOT NULL,
|
||||||
|
firmware_version TEXT NOT NULL,
|
||||||
|
temperature_offset INTEGER DEFAULT 0,
|
||||||
|
humidity_offset INTEGER DEFAULT 0,
|
||||||
|
last_updated TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
last_seen TEXT DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS readings (
|
||||||
|
dev_id INTEGER PRIMARY KEY,
|
||||||
|
temperature INTEGER NOT NULL,
|
||||||
|
humidity INTEGER NOT NULL,
|
||||||
|
FOREIGN KEY(dev_id) REFERENCES devices(id) ON UPDATE CASCADE ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
";
|
||||||
|
try {
|
||||||
|
$this->pdo->exec($sql);
|
||||||
|
echo "Table 'devices' and 'readings' created successfully.";
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Error creating table: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addDevice($deviceData) {
|
||||||
|
$sql = "
|
||||||
|
INSERT INTO devices (device_id, mac_address, ip_address, firmware_version, temperature_offset, humidity_offset, last_updated, last_seen)
|
||||||
|
VALUES (:device_id, :mac_address, :ip_address, :firmware_version, :temperature_offset, :humidity_offset, :last_updated, :last_seen)
|
||||||
|
";
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute($deviceData);
|
||||||
|
return $this->pdo->lastInsertId();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Error inserting device: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDevices() {
|
||||||
|
$sql = "SELECT * FROM devices";
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->query($sql);
|
||||||
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Error fetching devices: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDeviceById($id) {
|
||||||
|
$sql = "SELECT * FROM devices WHERE id = :id";
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute(['id' => $id]);
|
||||||
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Error fetching device: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateDevice($id, $deviceData) {
|
||||||
|
$sql = "
|
||||||
|
UPDATE devices
|
||||||
|
SET device_id = :device_id,
|
||||||
|
mac_address = :mac_address,
|
||||||
|
ip_address = :ip_address,
|
||||||
|
firmware_version = :firmware_version,
|
||||||
|
temperature_offset = :temperature_offset,
|
||||||
|
humidity_offset = :humidity_offset,
|
||||||
|
last_updated = :last_updated,
|
||||||
|
last_seen = :last_seen
|
||||||
|
WHERE id = :id
|
||||||
|
";
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$deviceData['id'] = $id;
|
||||||
|
$stmt->execute($deviceData);
|
||||||
|
return $stmt->rowCount();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Error updating device: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteDevice($id) {
|
||||||
|
$sql = "DELETE FROM devices WHERE id = :id";
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute(['id' => $id]);
|
||||||
|
return $stmt->rowCount();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Error deleting device: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createOrUpdateReading($deviceID, $temperature, $humidity) {
|
||||||
|
$sql = "SELECT COUNT(*) FROM readings WHERE dev_id = :dev_id";
|
||||||
|
try {
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute(['dev_id' => $deviceID]);
|
||||||
|
$exists = $stmt->fetchColumn() > 0;
|
||||||
|
|
||||||
|
if ($exists) {
|
||||||
|
$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
|
||||||
|
]);
|
||||||
|
return $stmt->rowCount();
|
||||||
|
} 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();
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Error creating or updating reading: " . $e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
$newDevice = [
|
||||||
|
'device_id' => 'device123',
|
||||||
|
'mac_address' => '00:1B:44:11:3A:B7',
|
||||||
|
'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);
|
||||||
|
|
||||||
|
$devices = $deviceDb->getDevices();
|
||||||
|
print_r($devices);
|
||||||
|
*/
|
||||||
|
?>
|
||||||
Reference in new issue
Block a user