From b8df0596a9aecad31d5f19cb490e0c1079d7e140 Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 13 Jul 2014 21:28:26 +0100 Subject: [PATCH] Added list devices api call --- html/api_v1.php | 3 +++ includes/api_functions.inc.php | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/html/api_v1.php b/html/api_v1.php index f47cc827f..18e8cf283 100644 --- a/html/api_v1.php +++ b/html/api_v1.php @@ -48,6 +48,9 @@ $app->group('/api', function() use ($app) { }); }); }); + $app->group('/list', function() use ($app) { + $app->get('/devices(/:order)(/:type)(/)', 'authToken', 'list_devices');//api/v1/list/devices (order can be any device column) (types = all, ignored, up, down, disabled) + }); }); }); diff --git a/includes/api_functions.inc.php b/includes/api_functions.inc.php index 56d5bcc34..7b9e0e9bf 100644 --- a/includes/api_functions.inc.php +++ b/includes/api_functions.inc.php @@ -188,3 +188,48 @@ function get_graph_generic_by_hostname() $app->response->headers->set('Content-Type', 'image/png'); require("includes/graphs/graph.inc.php"); } + +function list_devices() +{ + // This will return a list of devices + global $config; + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $order = $router['order']; + $type = $router['type']; + if(empty($order)) + { + $order = "hostname"; + } + if(stristr($order,' desc') === FALSE && stristr($order, ' asc') === FALSE) + { + $order .= ' ASC'; + } + if($type == 'all' || empty($type)) + { + $sql = "1"; + } + elseif($type == 'ignored') + { + $sql = "ignore='1' AND disabled='0'"; + } + elseif($type == 'up') + { + $sql = "status='1' AND ignore='0' AND disabled='0'"; + } + elseif($type == 'down') + { + $sql = "status='0' AND ignore='0' AND disabled='0'"; + } + elseif($type == 'disabled') + { + $sql = "disabled='1'"; + } + foreach (dbFetchRows("SELECT * FROM `devices` WHERE $sql ORDER by $order") as $device) + { + $devices[] = $device; + } + $output = array("status" => "ok", "devices" => $devices); + $app->response->headers->set('Content-Type', 'application/json'); + echo json_encode($output); +}