Trigger Device Rediscovery for a device group (#10832)

* trigger rediscover for whole device group
* travis fix
* Update rediscover-device.inc.php
This commit is contained in:
SourceDoctor
2019-11-21 11:55:15 +01:00
committed by PipoCanaja
parent f19bbcab41
commit 4ead80d06c
2 changed files with 58 additions and 12 deletions

View File

@@ -22,18 +22,42 @@ if (!Auth::user()->hasGlobalAdmin()) {
}
// FIXME: Make this part of the API instead of a standalone function
if (!is_numeric($_POST['device_id'])) {
$status = 'error';
$message = 'Invalid device id';
} else {
$update = dbUpdate(array('last_discovered' => array('NULL')), 'devices', '`device_id` = ?', array($_POST['device_id']));
if (!empty($update) || $update == '0') {
$status = 'ok';
$message = 'Device will be rediscovered';
if (isset($_POST['device_id'])) {
if (!is_numeric($_POST['device_id'])) {
$status = 'error';
$message = 'Invalid device id ' . $_POST['device_id'];
} else {
$status = 'error';
$message = 'Error rediscovering device';
$update = dbUpdate(array('last_discovered' => array('NULL')), 'devices', '`device_id` = ?', array($_POST['device_id']));
if (!empty($update) || $update == '0') {
$status = 'ok';
$message = 'Device ' . $_POST['device_id'] . ' will be rediscovered';
} else {
$status = 'error';
$message = 'Error rediscovering device ' . $_POST['device_id'];
}
}
} elseif (isset($_POST['device_group_id'])) {
if (!is_numeric($_POST['device_group_id'])) {
$status = 'error';
$message = 'Invalid device group id ' . $_POST['device_group_id'];
} else {
$device_ids = dbFetchColumn("SELECT `device_id` FROM `device_group_device` WHERE `device_group_id`=" . $_POST['device_group_id']);
$update = 0;
foreach ($device_ids as $device_id) {
$update += dbUpdate(array('last_discovered' => array('NULL')), 'devices', '`device_id` = ?', array($device_id));
}
if (!empty($update) || $update == '0') {
$status = 'ok';
$message = 'Devices of group ' . $_POST['device_group_id'] . ' will be rediscovered';
} else {
$status = 'error';
$message = 'Error rediscovering devices of group ' . $_POST['device_group_id'];
}
}
} else {
$status = 'Error';
$message = 'Undefined POST keys received';
}
$output = array(

View File

@@ -41,10 +41,14 @@
</td>
<td>{{ $device_group->type == 'dynamic' ? $device_group->getParser()->toSql(false) : '' }}</td>
<td>
<a type="button" class="btn btn-primary btn-sm" aria-label="@lang('Edit')"
<button type="button" title="@lang('Rediscover all Devices of Device Group')" class="btn btn-warning btn-sm" aria-label="@lang('Rediscover Group')"
onclick="discover_dg(this, '{{ $device_group->id }}')">
<i
class="fa fa-retweet" aria-hidden="true"></i></button>
<a type="button" title="@lang('edit Device Group')" class="btn btn-primary btn-sm" aria-label="@lang('Edit')"
href="{{ route('device-groups.edit', $device_group->id) }}">
<i class="fa fa-pencil" aria-hidden="true"></i></a>
<button type="button" class="btn btn-danger btn-sm" aria-label="@lang('Delete')"
<button type="button" class="btn btn-danger btn-sm" title="@lang('delete Device Group')" aria-label="@lang('Delete')"
onclick="delete_dg(this, '{{ $device_group->name }}', '{{ route('device-groups.destroy', $device_group->id) }}')">
<i
class="fa fa-trash" aria-hidden="true"></i></button>
@@ -111,6 +115,24 @@
return false;
}
function discover_dg(button, id) {
$.ajax({
type: 'POST',
url: 'ajax_form.php',
data: { type: "rediscover-device", device_group_id: id },
dataType: "json",
success: function(data){
if(data['status'] == 'ok') {
toastr.success(data['message']);
} else {
toastr.error(data['message']);
}
},
error:function(){
toastr.error('An error occured setting this device Group to be rediscovered');
}
});
}
</script>
@endsection