modified: app.php
modified: database/sqlite.php modified: device-api.php
This commit is contained in:
1 parent
9759ab5ee4
commit
7f93508eb3
3 files changed
+31
-15
No files matched your search
+2
-2
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
require_once('debug.php');
|
require_once('debug.php');
|
||||||
require_once('router.php');
|
require_once('router.php');
|
||||||
$router = new Router(new Request);
|
|
||||||
|
|
||||||
require_once('database/sqlite.php');
|
require_once('database/sqlite.php');
|
||||||
|
|
||||||
|
$router = new Router(new Request);
|
||||||
$db = new DB();
|
$db = new DB();
|
||||||
|
|
||||||
$router->get('/', function($req) {
|
$router->get('/', function($req) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
interface DBIface
|
interface DBIface
|
||||||
{
|
{
|
||||||
public function __construct();
|
public function __construct();
|
||||||
public function addDevice(DeviceData $deviceData);
|
public function addDevice($deviceData);
|
||||||
public function getDevices();
|
public function getDevices();
|
||||||
public function getDeviceById($id);
|
public function getDeviceById($id);
|
||||||
public function updateDevice($id, $deviceData);
|
public function updateDevice($id, $deviceData);
|
||||||
@@ -39,17 +39,17 @@ class DB implements DBIface {
|
|||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
mac_address TEXT NOT NULL,
|
mac_address TEXT NOT NULL,
|
||||||
ip_address TEXT NOT NULL,
|
ip_address TEXT NOT NULL,
|
||||||
seen_ip_address TEXT NOT NULL,
|
ip_address_seen TEXT NOT NULL,
|
||||||
firmware_version TEXT NOT NULL,
|
firmware_version INTEGER NOT NULL,
|
||||||
temperature_offset INTEGER DEFAULT 0,
|
temperature_offset INTEGER DEFAULT 0,
|
||||||
humidity_offset INTEGER DEFAULT 0,
|
humidity_offset INTEGER DEFAULT 0,
|
||||||
last_updated TEXT DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
last_seen TEXT DEFAULT CURRENT_TIMESTAMP
|
last_seen TEXT DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
CREATE TABLE IF NOT EXISTS readings (
|
CREATE TABLE IF NOT EXISTS readings (
|
||||||
dev_id INTEGER PRIMARY KEY,
|
dev_id INTEGER PRIMARY KEY,
|
||||||
temperature INTEGER NOT NULL,
|
temperature INTEGER NOT NULL,
|
||||||
humidity INTEGER NOT NULL,
|
humidity INTEGER NOT NULL,
|
||||||
|
updated_at TEXT DEFAULT CURRENT_TIMESTAMP
|
||||||
FOREIGN KEY(dev_id) REFERENCES devices(id) ON UPDATE CASCADE ON DELETE CASCADE
|
FOREIGN KEY(dev_id) REFERENCES devices(id) ON UPDATE CASCADE ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -64,8 +64,8 @@ class DB implements DBIface {
|
|||||||
|
|
||||||
public function addDevice($deviceData) {
|
public function addDevice($deviceData) {
|
||||||
$sql = "
|
$sql = "
|
||||||
INSERT INTO devices (mac_address, ip_address, seen_ip_address, firmware_version, temperature_offset, humidity_offset, last_updated, last_seen)
|
INSERT INTO devices (mac_address, ip_address, ip_address_seen, firmware_version, temperature_offset, humidity_offset, last_seen)
|
||||||
VALUES (:mac_address, :ip_address, seen_ip_address, :firmware_version, :temperature_offset, :humidity_offset, :last_updated, :last_seen)
|
VALUES (:mac_address, :ip_address, :ip_address_seen, :firmware_version, :temperature_offset, :humidity_offset, :last_seen)
|
||||||
";
|
";
|
||||||
try {
|
try {
|
||||||
$stmt = $this->pdo->prepare($sql);
|
$stmt = $this->pdo->prepare($sql);
|
||||||
@@ -103,11 +103,10 @@ class DB implements DBIface {
|
|||||||
SET id = :id,
|
SET id = :id,
|
||||||
mac_address = :mac_address,
|
mac_address = :mac_address,
|
||||||
ip_address = :ip_address,
|
ip_address = :ip_address,
|
||||||
seen_ip_address = :seen_ip_address,
|
ip_address_seen = :ip_address_seen,
|
||||||
firmware_version = :firmware_version,
|
firmware_version = :firmware_version,
|
||||||
temperature_offset = :temperature_offset,
|
temperature_offset = :temperature_offset,
|
||||||
humidity_offset = :humidity_offset,
|
humidity_offset = :humidity_offset,
|
||||||
last_updated = :last_updated,
|
|
||||||
last_seen = :last_seen
|
last_seen = :last_seen
|
||||||
WHERE id = :id
|
WHERE id = :id
|
||||||
";
|
";
|
||||||
@@ -169,7 +168,7 @@ $newDevice = [
|
|||||||
'id' => 'device123',
|
'id' => 'device123',
|
||||||
'mac_address' => '00:1B:44:11:3A:B7',
|
'mac_address' => '00:1B:44:11:3A:B7',
|
||||||
'ip_address' => '192.168.1.2',
|
'ip_address' => '192.168.1.2',
|
||||||
'seen_ip_address' => '212.71.123.123',
|
'ip_address_seen' => '212.71.123.123',
|
||||||
'firmware_version' => '1.0.1',
|
'firmware_version' => '1.0.1',
|
||||||
'temperature_offset' => 0,
|
'temperature_offset' => 0,
|
||||||
'humidity_offset' => 0,
|
'humidity_offset' => 0,
|
||||||
|
|||||||
+21
-4
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
function valid($msg, $key, $regex) {
|
function valid($msg, $key, $regex) {
|
||||||
//if(!isset($msg[$key])) {
|
//if(!isset($msg[$key])) {
|
||||||
/*
|
/*
|
||||||
@@ -19,12 +20,12 @@ function valid($msg, $key, $regex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update device configuration
|
// Update device configuration
|
||||||
$router->post('/api/config', function($req) {
|
$router->post('/api/config', function($req) use($db) {
|
||||||
$msg = $req->getJSONBody();
|
$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') ||
|
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,'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,'firmware_version', '/^[0-9]+$/') ||
|
||||||
!valid($msg,'temperature_offset', '/^[0-9]+\.*[0-9]*$/') ||
|
!valid($msg,'temperature_offset', '/^[0-9]+\.*[0-9]*$/') ||
|
||||||
!valid($msg,'humidity_offset', '/^[0-9]+\.*[0-9]*$/')
|
!valid($msg,'humidity_offset', '/^[0-9]+\.*[0-9]*$/')
|
||||||
) {
|
) {
|
||||||
@@ -35,6 +36,23 @@ $router->post('/api/config', function($req) {
|
|||||||
|
|
||||||
// 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.
|
// 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]+$/')) {
|
else if(valid($msg,'id','/^[0-9]+$/')) {
|
||||||
|
$deviceData = $db->getDeviceByID($msg['id']);
|
||||||
|
// If device is not registered yet...
|
||||||
|
if($deviceData == null) {
|
||||||
|
echo '{"error_message": "Device by that ID not found."}';
|
||||||
|
http_response_code(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Return stored device data so device may update its settings.
|
||||||
|
echo json_encode($deviceData);
|
||||||
|
// Update database entry based on post data received.
|
||||||
|
$db->updateDevice([
|
||||||
|
'mac_address' => $msg['mac_address'],
|
||||||
|
'ip_address' => $msg['ip_address'],
|
||||||
|
'ip_address_seen' => $_SERVER['REMOTE_ADDR'],
|
||||||
|
'firmware_version' => $msg['firmware_version'],
|
||||||
|
'last_seen' => date('Y-m-d H:i:s')
|
||||||
|
]);
|
||||||
http_response_code(200);
|
http_response_code(200);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -44,11 +62,10 @@ $router->post('/api/config', function($req) {
|
|||||||
$config = [
|
$config = [
|
||||||
'mac_address' => $msg['mac_address'],
|
'mac_address' => $msg['mac_address'],
|
||||||
'ip_address' => $msg['ip_address'],
|
'ip_address' => $msg['ip_address'],
|
||||||
'seen_ip_address' => $_SERVER['REMOTE_ADDR'],
|
'ip_address_seen' => $_SERVER['REMOTE_ADDR'],
|
||||||
'firmware_version' => $msg['firmware_version'],
|
'firmware_version' => $msg['firmware_version'],
|
||||||
'temperature_offset' => $msg['temperature_offset'],
|
'temperature_offset' => $msg['temperature_offset'],
|
||||||
'humidity_offset' => $msg['humidity_offset'],
|
'humidity_offset' => $msg['humidity_offset'],
|
||||||
'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')
|
||||||
];
|
];
|
||||||
$deviceID = $db->addDevice($config);
|
$deviceID = $db->addDevice($config);
|
||||||
|
|||||||
Reference in new issue
Block a user