modified: .gitignore
modified: software/README.md modified: software/app.php modified: software/database/sqlite.php modified: software/device-api.php modified: software/router.php
This commit is contained in:
1 parent
50eec7e284
commit
c44e580120
6 files changed
+62
-15
No files matched your search
@@ -4,3 +4,4 @@
|
|||||||
firmware/build/*
|
firmware/build/*
|
||||||
firmware/sdkconfig.old
|
firmware/sdkconfig.old
|
||||||
firmware/sdkconfig
|
firmware/sdkconfig
|
||||||
|
software/database/db.sqlite
|
||||||
+2
-2
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
## API definition
|
## API definition
|
||||||
|
|
||||||
- id
|
- register
|
||||||
- GET: Returns a new ID for device to use for future communication.
|
- POST: Returns a new ID for device to use for future communication.
|
||||||
- metrics
|
- metrics
|
||||||
- GET: Prometheus metrics endpoint for temperatures.
|
- GET: Prometheus metrics endpoint for temperatures.
|
||||||
- report
|
- report
|
||||||
|
|||||||
+1
-1
@@ -17,5 +17,5 @@ $router->get('/favicon.ico', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
require_once('device-api.php');
|
require_once('device-api.php');
|
||||||
require_once('integration.php');
|
//require_once('integration.php');
|
||||||
|
|
||||||
@@ -4,13 +4,13 @@ class DB {
|
|||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
try {
|
try {
|
||||||
if (!file_exists('db.sqlite') {
|
if (!file_exists(__DIR__ . '/db.sqlite')) {
|
||||||
$this->pdo = new PDO("sqlite:db.sqlite");
|
$this->pdo = new PDO('sqlite:'.__DIR__.'/db.sqlite');
|
||||||
$this->createTables();
|
$this->createTables();
|
||||||
syslog(LOG_INFO,"New SQLite Database was created.");
|
syslog(LOG_INFO,"New SQLite Database was created.");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->pdo = new PDO("sqlite:db.sqlite");
|
$this->pdo = new PDO('sqlite:'.__DIR__.'/db.sqlite');
|
||||||
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
$this->pdo->exec("PRAGMA foreign_keys = ON");
|
$this->pdo->exec("PRAGMA foreign_keys = ON");
|
||||||
}
|
}
|
||||||
@@ -24,9 +24,9 @@ class DB {
|
|||||||
$sql = "
|
$sql = "
|
||||||
CREATE TABLE IF NOT EXISTS devices (
|
CREATE TABLE IF NOT EXISTS devices (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
device_id TEXT NOT NULL,
|
|
||||||
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,
|
||||||
firmware_version TEXT NOT NULL,
|
firmware_version TEXT NOT NULL,
|
||||||
temperature_offset INTEGER DEFAULT 0,
|
temperature_offset INTEGER DEFAULT 0,
|
||||||
humidity_offset INTEGER DEFAULT 0,
|
humidity_offset INTEGER DEFAULT 0,
|
||||||
@@ -51,8 +51,8 @@ class DB {
|
|||||||
|
|
||||||
public function addDevice($deviceData) {
|
public function addDevice($deviceData) {
|
||||||
$sql = "
|
$sql = "
|
||||||
INSERT INTO devices (device_id, mac_address, ip_address, firmware_version, temperature_offset, humidity_offset, last_updated, last_seen)
|
INSERT INTO devices (id, mac_address, ip_address, seen_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)
|
VALUES (:id, :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);
|
||||||
@@ -87,9 +87,10 @@ class DB {
|
|||||||
public function updateDevice($id, $deviceData) {
|
public function updateDevice($id, $deviceData) {
|
||||||
$sql = "
|
$sql = "
|
||||||
UPDATE devices
|
UPDATE devices
|
||||||
SET device_id = :device_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,
|
||||||
firmware_version = :firmware_version,
|
firmware_version = :firmware_version,
|
||||||
temperature_offset = :temperature_offset,
|
temperature_offset = :temperature_offset,
|
||||||
humidity_offset = :humidity_offset,
|
humidity_offset = :humidity_offset,
|
||||||
@@ -156,9 +157,10 @@ class DB {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
$newDevice = [
|
$newDevice = [
|
||||||
'device_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',
|
||||||
'firmware_version' => '1.0.1',
|
'firmware_version' => '1.0.1',
|
||||||
'temperature_offset' => 0,
|
'temperature_offset' => 0,
|
||||||
'humidity_offset' => 0,
|
'humidity_offset' => 0,
|
||||||
|
|||||||
+25
-1
@@ -1,7 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Returns a new ID for device to use for future communication.
|
// Returns a new ID for device to use for future communication.
|
||||||
$router->get('/api/id', function($req) {
|
$router->post('/api/register', function($req) {
|
||||||
|
$data = $req->getJSONBody();
|
||||||
|
//echo "Body: ".var_dump($data)."\n";
|
||||||
|
|
||||||
|
// 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";
|
||||||
|
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);
|
http_response_code(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -35,3 +58,4 @@ $router->get('/api/update', function() {
|
|||||||
|
|
||||||
http_response_code(400);
|
http_response_code(400);
|
||||||
});
|
});
|
||||||
|
|
||||||
+23
-3
@@ -9,7 +9,7 @@ class Router
|
|||||||
|
|
||||||
function __construct(IRequest $request)
|
function __construct(IRequest $request)
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
function __call($name, $args)
|
function __call($name, $args)
|
||||||
@@ -100,8 +100,8 @@ class Request implements IRequest
|
|||||||
|
|
||||||
foreach($matches[0] as $match)
|
foreach($matches[0] as $match)
|
||||||
{
|
{
|
||||||
$c = str_replace('_', '', strtoupper($match));
|
$c = str_replace('_', '', strtoupper($match));
|
||||||
$result = str_replace($match, $c, $result);
|
$result = str_replace($match, $c, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
@@ -122,5 +122,25 @@ class Request implements IRequest
|
|||||||
return $body;
|
return $body;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getJSONBody()
|
||||||
|
{
|
||||||
|
if($this->requestMethod === "GET") { return; }
|
||||||
|
|
||||||
|
if($this->requestMethod === "POST")
|
||||||
|
{
|
||||||
|
//return filter_input(INPUT_POST, $_POST, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||||
|
$json = file_get_contents('php://input');
|
||||||
|
|
||||||
|
// Decode the JSON data into a PHP array
|
||||||
|
$data = json_decode($json, true);
|
||||||
|
|
||||||
|
// Check for JSON parsing errors
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
die('Invalid JSON received');
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user