Added parsing support for lat/lng coords from device location

This commit is contained in:
laf
2015-11-16 18:50:52 -08:00
parent a6f34fdf7e
commit 7d7479b977
5 changed files with 30 additions and 6 deletions

View File

@@ -122,17 +122,17 @@ var greenMarker = L.AwesomeMarkers.icon({
// Checking user permissions
if (is_admin() || is_read()) {
// Admin or global read-only - show all devices
$sql = "SELECT DISTINCT(`device_id`),`hostname`,`os`,`status`,`lat`,`lng` FROM `devices`
$sql = "SELECT DISTINCT(`device_id`),`devices`.`location`,`hostname`,`os`,`status`,`lat`,`lng` FROM `devices`
LEFT JOIN `locations` ON `devices`.`location`=`locations`.`location`
WHERE `disabled`=0 AND `ignore`=0 AND `lat` != '' AND `lng` != ''
WHERE `disabled`=0 AND `ignore`=0 AND ((`lat` != '' AND `lng` != '') OR (`devices`.`location` REGEXP '\[[0-9\.\, ]+\]'))
ORDER BY `status` ASC, `hostname`";
}
else {
// Normal user - grab devices that user has permissions to
$sql = "SELECT DISTINCT(`devices`.`device_id`) as `device_id`,`hostname`,`os`,`status`,`lat`,`lng`
$sql = "SELECT DISTINCT(`devices`.`device_id`) as `device_id`,`devices`.`location`,`hostname`,`os`,`status`,`lat`,`lng`
FROM `devices_perms`, `devices`
LEFT JOIN `locations` ON `devices`.`location`=`locations`.`location`
WHERE `disabled`=0 AND `ignore`=0 AND `lat` != '' AND `lng` != ''
WHERE `disabled`=0 AND `ignore`=0 AND ((`lat` != '' AND `lng` != '') OR (`devices`.`location` REGEXP '\[[0-9\.\, ]+\]'))
AND `devices`.`device_id` = `devices_perms`.`device_id`
AND `devices_perms`.`user_id` = ?
ORDER BY `status` ASC, `hostname`";
@@ -140,6 +140,11 @@ var greenMarker = L.AwesomeMarkers.icon({
foreach (dbFetchRows($sql, array($_SESSION['user_id'])) as $map_devices) {
$icon = 'greenMarker';
$z_offset = 0;
$tmp_loc = parse_location($map_devices['location']);
if (!empty($tmp_loc['lat']) && !empty($tmp_loc['lng'])) {
$map_devices['lat'] = $tmp_loc['lat'];
$map_devices['lng'] = $tmp_loc['lng'];
}
if ($map_devices['status'] == 0) {
$icon = 'redMarker';
$z_offset = 10000; // move marker to foreground

View File

@@ -78,7 +78,10 @@ if ($device['location']) {
}
}
$loc = dbFetchRow("SELECT `lat`,`lng` FROM `locations` WHERE `location`=? LIMIT 1", array($device['location']));
$loc = parse_location($device['location']);
if (!is_array($loc)) {
$loc = dbFetchRow("SELECT `lat`,`lng` FROM `locations` WHERE `location`=? LIMIT 1", array($device['location']));
}
if (is_array($loc)) {
echo '<tr>
<td>Lat / Lng</td>

View File

@@ -1249,3 +1249,4 @@ function generate_dynamic_config_panel($title,$end_panel=true,$config_groups,$it
}
return $output;
}//end generate_dynamic_config_panel()

View File

@@ -795,3 +795,15 @@ function ceph_rrd($gtype) {
$rrd = join('-', array('app', 'ceph', $vars['id'], $gtype, $var)).'.rrd';
return join('/', array($config['rrd_dir'], $device['hostname'], $rrd));
}
/**
* Parse location field for coordinates
* @param string location The location field to look for coords in.
* @return array Containing the lat and lng coords
**/
function parse_location($location) {
preg_match('/(\[)([0-9\. ]+), ([0-9\. ]+)(\])/', $location, $tmp_loc);
if (!empty($tmp_loc[2]) && !empty($tmp_loc[3])) {
return array('lat' => $tmp_loc[2], 'lng' => $tmp_loc[3]);
}
}//end parse_location()

View File

@@ -445,7 +445,10 @@ function location_to_latlng($device) {
if (!empty($device_location)) {
$new_device_location = preg_replace("/ /","+",$device_location);
// We have a location string for the device.
$loc = dbFetchRow("SELECT `lat`,`lng` FROM `locations` WHERE `location`=? LIMIT 1", array($device_location));
$loc = parse_location($device_location);
if (!is_array($loc)) {
$loc = dbFetchRow("SELECT `lat`,`lng` FROM `locations` WHERE `location`=? LIMIT 1", array($device_location));
}
if (is_array($loc) === false) {
// Grab data from which ever Geocode service we use.
switch ($config['geoloc']['engine']) {