Add function to get information about a single device

This commit is contained in:
Paul Gear
2014-09-27 15:25:43 +10:00
parent a58c857ed2
commit 76f5ab87a9
3 changed files with 62 additions and 0 deletions

View File

@ -247,6 +247,49 @@ function get_graph_generic_by_hostname()
require("includes/graphs/graph.inc.php");
}
function get_device()
{
// return details of a single device
$app = \Slim\Slim::getInstance();
$app->response->headers->set('Content-Type', 'application/json');
$router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname'];
// if the hostname is all digits, use the device_id instead of the name
$column = "hostname";
if (ctype_digit($hostname)) {
$column = "device_id";
}
// find devices matching the name/id
$devices = array();
// FIXME: user-based permissions
foreach (dbFetchRows("SELECT * FROM `devices` WHERE `".$column."`=?", array($hostname)) as $device)
{
$devices[] = $device;
}
if (count($devices) == 0) {
// not found
$app->response->setStatus(404);
$output = array("status" => "error", "message" => "Device $hostname does not exist");
echo _json_encode($output);
$app->stop();
}
elseif (count($devices) > 1) {
// we got more than one device - something's weird
$app->response->setStatus(500);
$output = array("status" => "error", "message" => "Multiple devices matching $hostname", "devices" => $devices);
echo _json_encode($output);
$app->stop();
}
else {
// not found
$output = array("status" => "ok", "devices" => $devices);
echo _json_encode($output);
}
}
function list_devices()
{
// This will return a list of devices