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
+59
-12
No files matched your search
@@ -4,3 +4,4 @@
|
||||
firmware/build/*
|
||||
firmware/sdkconfig.old
|
||||
firmware/sdkconfig
|
||||
software/database/db.sqlite
|
||||
+2
-2
@@ -2,8 +2,8 @@
|
||||
|
||||
## API definition
|
||||
|
||||
- id
|
||||
- GET: Returns a new ID for device to use for future communication.
|
||||
- register
|
||||
- POST: Returns a new ID for device to use for future communication.
|
||||
- metrics
|
||||
- GET: Prometheus metrics endpoint for temperatures.
|
||||
- report
|
||||
|
||||
+1
-1
@@ -17,5 +17,5 @@ $router->get('/favicon.ico', function() {
|
||||
});
|
||||
|
||||
require_once('device-api.php');
|
||||
require_once('integration.php');
|
||||
//require_once('integration.php');
|
||||
|
||||
@@ -4,13 +4,13 @@ class DB {
|
||||
|
||||
public function __construct() {
|
||||
try {
|
||||
if (!file_exists('db.sqlite') {
|
||||
$this->pdo = new PDO("sqlite:db.sqlite");
|
||||
if (!file_exists(__DIR__ . '/db.sqlite')) {
|
||||
$this->pdo = new PDO('sqlite:'.__DIR__.'/db.sqlite');
|
||||
$this->createTables();
|
||||
syslog(LOG_INFO,"New SQLite Database was created.");
|
||||
}
|
||||
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->exec("PRAGMA foreign_keys = ON");
|
||||
}
|
||||
@@ -24,9 +24,9 @@ class DB {
|
||||
$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,
|
||||
seen_ip_address TEXT NOT NULL,
|
||||
firmware_version TEXT NOT NULL,
|
||||
temperature_offset INTEGER DEFAULT 0,
|
||||
humidity_offset INTEGER DEFAULT 0,
|
||||
@@ -51,8 +51,8 @@ class DB {
|
||||
|
||||
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)
|
||||
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)
|
||||
";
|
||||
try {
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
@@ -87,9 +87,10 @@ class DB {
|
||||
public function updateDevice($id, $deviceData) {
|
||||
$sql = "
|
||||
UPDATE devices
|
||||
SET device_id = :device_id,
|
||||
SET id = :id,
|
||||
mac_address = :mac_address,
|
||||
ip_address = :ip_address,
|
||||
seen_ip_address = :seen_ip_address,
|
||||
firmware_version = :firmware_version,
|
||||
temperature_offset = :temperature_offset,
|
||||
humidity_offset = :humidity_offset,
|
||||
@@ -156,9 +157,10 @@ class DB {
|
||||
|
||||
/*
|
||||
$newDevice = [
|
||||
'device_id' => 'device123',
|
||||
'id' => 'device123',
|
||||
'mac_address' => '00:1B:44:11:3A:B7',
|
||||
'ip_address' => '192.168.1.2',
|
||||
'seen_ip_address' => '212.71.123.123',
|
||||
'firmware_version' => '1.0.1',
|
||||
'temperature_offset' => 0,
|
||||
'humidity_offset' => 0,
|
||||
|
||||
+25
-1
@@ -1,7 +1,30 @@
|
||||
<?php
|
||||
|
||||
// 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);
|
||||
});
|
||||
|
||||
@@ -35,3 +58,4 @@ $router->get('/api/update', function() {
|
||||
|
||||
http_response_code(400);
|
||||
});
|
||||
|
||||
@@ -122,5 +122,25 @@ class Request implements IRequest
|
||||
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