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
+51
-22
@@ -1,31 +1,60 @@
|
||||
<?php
|
||||
|
||||
// Returns a new ID for device to use for future communication.
|
||||
$router->post('/api/register', function($req) {
|
||||
$data = $req->getJSONBody();
|
||||
//echo "Body: ".var_dump($data)."\n";
|
||||
function valid($msg, $key, $regex) {
|
||||
//if(!isset($msg[$key])) {
|
||||
/*
|
||||
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...
|
||||
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";
|
||||
// Update device configuration
|
||||
$router->post('/api/config', function($req) {
|
||||
$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);
|
||||
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);
|
||||
|
||||
// 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]+$/')) {
|
||||
http_response_code(200);
|
||||
return;
|
||||
}
|
||||
|
||||
// All fields are set and no ID was given. Register new device and return new ID...
|
||||
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_seen' => date('Y-m-d H:i:s')
|
||||
];
|
||||
$deviceID = $db->addDevice($config);
|
||||
echo json_encode(['deviceID' => $deviceID]);
|
||||
http_response_code(200);
|
||||
}
|
||||
});
|
||||
|
||||
// Device reports temperature back on this endpoint.
|
||||
|
||||
Reference in new issue
Block a user