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
|
<?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;
|
private $pdo;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
@@ -51,8 +64,8 @@ class DB {
|
|||||||
|
|
||||||
public function addDevice($deviceData) {
|
public function addDevice($deviceData) {
|
||||||
$sql = "
|
$sql = "
|
||||||
INSERT INTO devices (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 (:id, :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 {
|
try {
|
||||||
$stmt = $this->pdo->prepare($sql);
|
$stmt = $this->pdo->prepare($sql);
|
||||||
@@ -120,31 +133,27 @@ class DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function createOrUpdateReading($deviceID, $temperature, $humidity) {
|
public function createOrUpdateReading($deviceID, $temperature, $humidity) {
|
||||||
$sql = "SELECT COUNT(*) FROM readings WHERE dev_id = :dev_id";
|
$readingData = [
|
||||||
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,
|
"dev_id" => $deviceID,
|
||||||
"temperature" => $temperature,
|
"temperature" => $temperature,
|
||||||
"humidity" => $humidity
|
"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($readingData);
|
||||||
|
$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($readingData);
|
||||||
return $stmt->rowCount();
|
return $stmt->rowCount();
|
||||||
} else {
|
}
|
||||||
$sql = "
|
else {
|
||||||
INSERT INTO readings (dev_id, temperature, humidity)
|
$sql = "INSERT INTO readings (dev_id, temperature, humidity) VALUES (:dev_id, :temperature, :humidity)";
|
||||||
VALUES (:dev_id, :temperature, :humidity)
|
|
||||||
";
|
|
||||||
$stmt = $this->pdo->prepare($sql);
|
$stmt = $this->pdo->prepare($sql);
|
||||||
$stmt->execute($readingData);
|
$stmt->execute($readingData);
|
||||||
return $this->pdo->lastInsertId();
|
return $this->pdo->lastInsertId();
|
||||||
|
|||||||
+47
-18
@@ -1,31 +1,60 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Returns a new ID for device to use for future communication.
|
function valid($msg, $key, $regex) {
|
||||||
$router->post('/api/register', function($req) {
|
//if(!isset($msg[$key])) {
|
||||||
$data = $req->getJSONBody();
|
/*
|
||||||
//echo "Body: ".var_dump($data)."\n";
|
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...
|
// Update device configuration
|
||||||
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'])) {
|
$router->post('/api/config', function($req) {
|
||||||
echo $data['mac_address']." does not match a mac address.\n";
|
$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);
|
http_response_code(400);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
$newDevice = [
|
// 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.
|
||||||
'id' => 'device123',
|
else if(valid($msg,'id','/^[0-9]+$/')) {
|
||||||
'mac_address' => $data['mac_address'],
|
http_response_code(200);
|
||||||
'ip_address' => '192.168.1.2',
|
return;
|
||||||
'seen_ip_address' => '192.168.1.2',
|
}
|
||||||
'firmware_version' => '1.0.1',
|
|
||||||
'temperature_offset' => 0,
|
// All fields are set and no ID was given. Register new device and return new ID...
|
||||||
'humidity_offset' => 0,
|
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_updated' => date('Y-m-d H:i:s'),
|
||||||
'last_seen' => date('Y-m-d H:i:s')
|
'last_seen' => date('Y-m-d H:i:s')
|
||||||
];
|
];
|
||||||
$deviceDb->addDevice($newDevice);
|
$deviceID = $db->addDevice($config);
|
||||||
*/
|
echo json_encode(['deviceID' => $deviceID]);
|
||||||
http_response_code(200);
|
http_response_code(200);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Device reports temperature back on this endpoint.
|
// Device reports temperature back on this endpoint.
|
||||||
|
|||||||
Reference in new issue
Block a user