Skip to content

Commit

Permalink
Version 1.1.0
Browse files Browse the repository at this point in the history
Add getAccessControlGroups method (resolve #2)
  • Loading branch information
nioc committed Sep 27, 2022
1 parent 844f760 commit cc0e322
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![license: AGPLv3](https://img.shields.io/badge/license-AGPLv3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
[![GitHub release](https://img.shields.io/github/release/nioc/synology-srm-php-api.svg)](https://github.com/nioc/synology-srm-php-api/releases/latest)
[![Packagist Downloads](https://img.shields.io/packagist/dt/nioc/synology-srm-php-api?label=Composer%20installs)](https://packagist.org/packages/nioc/synology-srm-php-api)
[![GitHub all releases](https://img.shields.io/github/downloads/nioc/synology-srm-php-api/total?label=Release%20downloads)](https://github.com/nioc/synology-srm-php-api/releases)

API wrapper for Synology Router Manager (SRM).

Expand All @@ -13,6 +15,7 @@ API wrapper for Synology Router Manager (SRM).
- get devices traffic usage (live, day, week, month),
- get mesh nodes with status, connected devices, etc... ,
- get wake-on-lan devices,
- get access control groups with devices, online status, etc... ,
- add wake-on-lan on a device,
- wake-on-lan a device.

Expand Down
3 changes: 3 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
$devices = $client->getDevices();
$logger->info(json_encode($devices));

$accessControlGroups = $client->getAccessControlGroups(true);
$logger->info(json_encode($accessControlGroups));

$client->addWakeOnLanDevice('01:23:45:67:89:AB');

$wakeOnLanDevices = $client->getWakeOnLanDevices();
Expand Down
53 changes: 51 additions & 2 deletions src/SrmClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,20 +294,23 @@ public function getNetworkUtilization()
* Return knowned devices by router and information like IP, signal, etc...
*
* @param string $interval must be in 'all'
* @param string $info Info requested ('"basic"' for example)
* @throws Exception if an error occurs
* @return array An array of devices (object)
*/
public function getDevices($conntype = 'all')
public function getDevices($conntype = 'all', $info = null)
{
if (!in_array($conntype, ['all'])) {
throw new \Exception('Invalid connection type');
}
// [{"api":"SYNO.Core.Network.NSM.Beamforming","version":1,"method":"get"},{"api":"SYNO.Core.NGFW.QoS.Priority","version":1,"method":"list_high"},{"api":"SYNO.Core.NGFW.QoS.Priority","version":1,"method":"list_low"},{"api":"SYNO.Core.NGFW.QoS.Rules","version":1,"method":"get"},{"api":"SYNO.Core.Network.Router.BanDevice","version":1,"method":"get","recordtype":"all"},{"api":"SYNO.Core.Network.NSM.Device","version":4,"method":"get","conntype":"all"}]
$params = [
'method' => 'get',
'version' => 4,
'conntype' => $conntype
];
if (!is_null($info)) {
$params['info'] = $info;
}
try {
$response = $this->_request('entry.cgi', 'SYNO.Core.Network.NSM.Device', $params);
} catch (\Exception $e) {
Expand Down Expand Up @@ -368,6 +371,52 @@ public function getMeshNodes()
return $response->data->nodes;
}

/**
* Return access control groups (safe search)
*
* @param boolean $requestOnlineStatus Ask for group online status
* @throws Exception if an error occurs
* @return array An array of groups (object)
*/
public function getAccessControlGroups($requestOnlineStatus = false)
{
$additionnal = ['device', 'total_timespent'];
$params = [
'method' => 'get',
'version' => 1,
'additional' => \json_encode($additionnal),
];
try {
$response = $this->_request('entry.cgi', 'SYNO.SafeAccess.AccessControl.ConfigGroup', $params);
} catch (\Exception $e) {
throw new \Exception("Could not get access control groups ({$e->getMessage()})");
}
$groups = $response->data->config_groups;
$this->logger->info('Get access control groups successful');
if ($requestOnlineStatus === true) {
try {
// get devices online status
$devices = $this->getDevices('all', '"basic"');
// filter online devices
$onlineDevices = [];
foreach ($devices as $device) {
if ($device->is_online === true) {
array_push($onlineDevices, $device->mac);
}
}
// set access control group online status
foreach ($groups as $group) {
$groupOnlineDevices = array_intersect($group->devices, $onlineDevices);
$group->online_device_count = count($groupOnlineDevices);
$group->online = $group->online_device_count > 0;
}
} catch (\Exception $e) {
$this->logger->notice("Could not set access control groups online status ({$e->getMessage()})");
}
}
return $groups;
}

/**
* Return devices where wake on lan is configured.
*
Expand Down

0 comments on commit cc0e322

Please sign in to comment.