Add LAN API

This commit is contained in:
alphayax 2016-05-19 20:53:57 +02:00
parent a44e4494a4
commit 5176cf084b
13 changed files with 704 additions and 0 deletions

31
exemple/config/LAN.php Normal file
View File

@ -0,0 +1,31 @@
<?php
/// Require Composer AutoLoader
require_once '../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.config', 'PHP API Example (Config)', '1.0.0');
$App->authorize();
$App->openSession();
// Lan Config
$LanConfig = new \alphayax\freebox\api\v3\services\config\LAN\LAN( $App);
$a = $LanConfig->getConfiguration();
print_r( $a);
// Lan Browser
$LanBrowser = new \alphayax\freebox\api\v3\services\config\LAN\Browser($App);
$LanInterfaces = $LanBrowser->getBrowsableInterfaces();
print_r( $LanInterfaces);
/** @var \alphayax\freebox\api\v3\models\LAN\LanInterface $LanInterface1 */
$LanInterface1 = $LanInterfaces[0];
$LanHosts = $LanBrowser->getHostsFromInterface( $LanInterfaces[0]);
print_r( $LanHosts);
$LanHost1 = $LanBrowser->getHostFromId( $LanInterface1->getName(), $LanHosts[0]->getId());
print_r( $LanHost1);
$wol_status = $LanBrowser->wakeOnLan( $LanInterface1, $LanHost1);
print_r( $wol_status);

View File

@ -0,0 +1,177 @@
<?php
namespace alphayax\freebox\api\v3\models\LAN;
use alphayax\freebox\api\v3\Model;
/**
* Class LanHost
* @package alphayax\freebox\api\v3\models\LAN
*/
class LanHost extends Model {
/** @var string (Read-only) : Host id (unique on this interface) */
protected $id ;
/** @var string : Host primary name (chosen from the list of available names, or manually set by user) */
protected $primary_name ;
/**
* @var string
* @see alphayax\freebox\api\v3\symbols\Lan\LanHostType
* When possible, the Freebox will try to guess the host_type, but you can manually override this to the correct value
*/
protected $host_type ;
/** @var bool (Read-only) : If true the primary name has been set manually */
protected $primary_name_manual ;
/** @var LanHostL2Ident[] array of LanHostL2Ident (Read-only) : Layer 2 network id and its type */
protected $l2ident;
/** @var string (Read-only) : Host vendor name (from the mac address) */
protected $vendor_name ;
/** @var bool : If true the host is always shown even if it has not been active since the Freebox startup */
protected $persistent ;
/** @var bool (Read-only) : If true the host can receive traffic from the Freebox */
protected $reachable ;
/** @var int timestamp (Read-only) : Last time the host was reached */
protected $last_time_reachable ;
/** @var bool (Read-only) : If true the host sends traffic to the Freebox */
protected $active ;
/** @var int timestamp (Read-only) : Last time the host sent traffic */
protected $last_activity ;
/** @var LanHostName[] array of LanHostName (Read-only) : List of available names, and their source */
protected $names;
/** @var LanHostL3Connectivity[] array of LanHostL3Connectivity (Read-only) : List of available layer 3 network connections */
protected $l3connectivities;
/**
* LanHost constructor.
* @param array $properties_x
*/
public function __construct( array $properties_x){
parent::__construct( $properties_x);
$this->initPropertyArray( 'l2ident' , LanHostL2Ident::class);
$this->initPropertyArray( 'names' , LanHostName::class);
$this->initPropertyArray( 'l3connectivities', LanHostL3Connectivity::class);
}
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @return string
*/
public function getPrimaryName() {
return $this->primary_name;
}
/**
* @param string $primary_name
*/
public function setPrimaryName( $primary_name){
$this->primary_name = $primary_name;
}
/**
* @return string
*/
public function getHostType() {
return $this->host_type;
}
/**
* @param string $host_type
*/
public function setHostType( $host_type) {
$this->host_type = $host_type;
}
/**
* @return boolean
*/
public function isPrimaryNameManual() {
return $this->primary_name_manual;
}
/**
* @return LanHostL2Ident[]
*/
public function getL2ident() {
return $this->l2ident;
}
/**
* @return string
*/
public function getVendorName() {
return $this->vendor_name;
}
/**
* @return boolean
*/
public function isPersistent() {
return $this->persistent;
}
/**
* @param boolean $persistent
*/
public function setPersistent($persistent) {
$this->persistent = $persistent;
}
/**
* @return boolean
*/
public function isReachable() {
return $this->reachable;
}
/**
* @return int
*/
public function getLastTimeReachable() {
return $this->last_time_reachable;
}
/**
* @return boolean
*/
public function isActive() {
return $this->active;
}
/**
* @return int
*/
public function getLastActivity() {
return $this->last_activity;
}
/**
* @return LanHostName[]
*/
public function getNames() {
return $this->names;
}
/**
* @return LanHostL3Connectivity[]
*/
public function getL3connectivities() {
return $this->l3connectivities;
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace alphayax\freebox\api\v3\models\LAN;
use alphayax\freebox\api\v3\Model;
/**
* Class LanHostL2Ident
* @package alphayax\freebox\api\v3\models\LAN
*/
class LanHostL2Ident extends Model {
/** @var string (Read-only) : Layer 2 id */
protected $id;
/**
* @var string (Read-only) : Type of layer 2 address
* @see alphayax\freebox\api\v3\symbols\Lan\LanHostL2IdentType
**/
protected $type;
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @return string
*/
public function getType() {
return $this->type;
}
}

View File

@ -0,0 +1,74 @@
<?php
namespace alphayax\freebox\api\v3\models\LAN;
use alphayax\freebox\api\v3\Model;
/**
* Class LanHostL3Connectivity
* @package alphayax\freebox\api\v3\models\LAN
*/
class LanHostL3Connectivity extends Model {
/** @var string (Read-only) : Layer 3 address */
protected $addr;
/**
* @var string (Read-only)
* @see alphayax\freebox\api\v3\symbols\Lan\LanHostL3ConnectivityAF
**/
protected $af;
/** @var bool (Read-only) : is the connection active */
protected $active;
/** @var bool (Read-only) : is the connection reachable */
protected $reachable;
/** @var int timestamp (Read-only) : last activity timestamp */
protected $last_activity;
/** @var int timestamp (Read-only) : last reachable timestamp */
protected $last_time_reachable;
/**
* @return string
*/
public function getAddr() {
return $this->addr;
}
/**
* @return string
*/
public function getAf() {
return $this->af;
}
/**
* @return boolean
*/
public function isActive() {
return $this->active;
}
/**
* @return boolean
*/
public function isReachable() {
return $this->reachable;
}
/**
* @return int
*/
public function getLastActivity() {
return $this->last_activity;
}
/**
* @return int
*/
public function getLastTimeReachable() {
return $this->last_time_reachable;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace alphayax\freebox\api\v3\models\LAN;
use alphayax\freebox\api\v3\Model;
/**
* Class LanHostName
* @package alphayax\freebox\api\v3\models\LAN
*/
class LanHostName extends Model {
/** @var string (Read-only) : Host name */
protected $name;
/** @var string (Read-only) : source of the name */
protected $source;
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @return string
*/
public function getSource() {
return $this->source;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace alphayax\freebox\api\v3\models\LAN;
use alphayax\freebox\api\v3\Model;
/**
* Class LanConfig
* @package alphayax\freebox\api\v3\models
*/
class LanInterface extends Model {
/** @var string : Lan Interface name */
protected $name;
/** @var int : Number of hosts in interface */
protected $host_count;
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @return int
*/
public function getHostCount() {
return $this->host_count;
}
}

View File

@ -0,0 +1,117 @@
<?php
namespace alphayax\freebox\api\v3\models;
use alphayax\freebox\api\v3\Model;
/**
* Class LanConfig
* @package alphayax\freebox\api\v3\models
*/
class LanConfig extends Model {
/** @var string : Freebox Server IPv4 address */
protected $ip;
/** @var string : Freebox Server name */
protected $name;
/** @var string : Freebox Server DNS name */
protected $name_dns;
/** @var string : Freebox Server mDNS name */
protected $name_mdns;
/** @var string : Freebox Server netbios name */
protected $name_netbios;
/**
* @var string : LAN modes
* @see alphayax\freebox\api\v3\symbols\Lan\LanType
* NOTE: in bridge mode, most of Freebox services are disabled. It is recommended to use the router mode, and third party apps should not change this setting
*/
protected $mode;
/**
* @return string
*/
public function getIp(){
return $this->ip;
}
/**
* @param string $ip
*/
public function setIp( $ip){
$this->ip = $ip;
}
/**
* @return string
*/
public function getName(){
return $this->name;
}
/**
* @param string $name
*/
public function setName( $name){
$this->name = $name;
}
/**
* @return string
*/
public function getNameDns(){
return $this->name_dns;
}
/**
* @param string $name_dns
*/
public function setNameDns( $name_dns){
$this->name_dns = $name_dns;
}
/**
* @return string
*/
public function getNameMdns(){
return $this->name_mdns;
}
/**
* @param string $name_mdns
*/
public function setNameMdns( $name_mdns){
$this->name_mdns = $name_mdns;
}
/**
* @return string
*/
public function getNameNetbios(){
return $this->name_netbios;
}
/**
* @param string $name_netbios
*/
public function setNameNetbios( $name_netbios){
$this->name_netbios = $name_netbios;
}
/**
* @return string
*/
public function getMode(){
return $this->mode;
}
/**
* @param string $mode
*/
public function setMode( $mode){
$this->mode = $mode;
}
}

View File

@ -0,0 +1,107 @@
<?php
namespace alphayax\freebox\api\v3\services\config\LAN;
use alphayax\freebox\api\v3\models\LAN\LanHost;
use alphayax\freebox\api\v3\models\LAN\LanInterface;
use alphayax\freebox\api\v3\Service;
/**
* Class Browser
* @package alphayax\freebox\api\v3\services\config\LAN
*/
class Browser extends Service {
const API_LAN_BROWSER_INTERFACES = '/api/v3/lan/browser/interfaces/';
const API_LAN_BROWSER_INTERFACE = '/api/v3/lan/browser/%s/';
const API_LAN_BROWSER_HOST = '/api/v3/lan/browser/%s/%s/';
const API_WAKE_ON_LAN = '/api/v3/lan/wol/%s/';
/**
* @throws \Exception
* @return LanInterface
*/
public function getBrowsableInterfaces(){
$rest = $this->getAuthService( self::API_LAN_BROWSER_INTERFACES);
$rest->GET();
$LanInterface_xs = $rest->getCurlResponse()['result'];
$LanInterfaces = [];
foreach( $LanInterface_xs as $LanInterface_x) {
$LanInterfaces[] = new LanInterface( $LanInterface_x);
}
return $LanInterfaces;
}
/**
* @param LanInterface $lanInterface
* @return LanHost[]
* @throws \Exception
*/
public function getHostsFromInterface( LanInterface $lanInterface){
return $this->getHostsFromInterfaceName( $lanInterface->getName());
}
/**
* @param string $lanInterfaceId
* @return LanHost[]
* @throws \Exception
*/
public function getHostsFromInterfaceName( $lanInterfaceId){
$service = sprintf( self::API_LAN_BROWSER_INTERFACE, $lanInterfaceId);
$rest = $this->getAuthService( $service);
$rest->GET();
$LanHost_xs = $rest->getCurlResponse()['result'];
$LanHosts = [];
foreach( $LanHost_xs as $LanHost_x) {
$LanHosts[] = new LanHost( $LanHost_x);
}
return $LanHosts;
}
/**
* @param string $lanInterfaceId
* @param string $hostId
* @return LanHost
*/
public function getHostFromId( $lanInterfaceId, $hostId){
$service = sprintf( self::API_LAN_BROWSER_HOST, $lanInterfaceId, $hostId);
echo $service;
$rest = $this->getAuthService( $service);
$rest->GET();
return new LanHost( $rest->getCurlResponse()['result']);
}
/**
* @param LanHost $LanHost
* @param string $lanInterfaceId
* @return LanHost
*/
public function updateHostFromInterfaceId( LanHost $LanHost, $lanInterfaceId){
$service = sprintf( self::API_LAN_BROWSER_HOST, $lanInterfaceId, $LanHost->getId());
$rest = $this->getAuthService( $service);
$rest->PUT( $LanHost->toArray());
return new LanHost( $rest->getCurlResponse()['result']);
}
/**
* Send Wake ok Lan packet to an host
* @param LanInterface $lanInterface
* @param LanHost $lanHost
* @param string $password
* @return bool
*/
public function wakeOnLan( LanInterface $lanInterface, LanHost $lanHost, $password = ''){
$service = sprintf( self::API_WAKE_ON_LAN, $lanInterface->getName());
$rest = $this->getAuthService( $service);
$rest->PUT([
'mac' => $lanHost->getId(),
'password' => $password,
]);
return (bool) $rest->getCurlResponse()['result'];
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace alphayax\freebox\api\v3\services\config\LAN;
use alphayax\freebox\api\v3\models\LanConfig;
use alphayax\freebox\api\v3\Service;
/**
* Class LAN
* @package alphayax\freebox\api\v3\services\config
*/
class LAN extends Service {
const API_LAN_CONFIG = '/api/v3/lan/config/';
/**
* @throws \Exception
* @return LanConfig
*/
public function getConfiguration(){
$rest = $this->getAuthService( self::API_LAN_CONFIG);
$rest->GET();
return new LanConfig( $rest->getCurlResponse()['result']);
}
/**
* @param LanConfig $new_LanConfig
* @return LanConfig
* @throws \Exception
*/
public function setConfiguration( LanConfig $new_LanConfig){
$rest = $this->getAuthService( self::API_LAN_CONFIG);
$rest->PUT( $new_LanConfig->toArray());
return new LanConfig( $rest->getCurlResponse()['result']);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace alphayax\freebox\api\v3\symbols\Lan;
/**
* Interface LanHostL2IdentType
* @package alphayax\freebox\api\v3\symbols\Lan
*/
interface LanHostL2IdentType {
const DHCP = 'dhcp'; // DHCP
const NETBIOS = 'netbios'; // Netbios
const MDNS = 'mdns'; // mDNS
const UPNP = 'upnp'; // UPnP
}

View File

@ -0,0 +1,11 @@
<?php
namespace alphayax\freebox\api\v3\symbols\Lan;
/**
* Interface LanHostL3ConnectivityAF
* @package alphayax\freebox\api\v3\symbols\Lan
*/
interface LanHostL3ConnectivityAF {
const IPV4 = 'ipv4'; // IPv4
const IPV6 = 'ipv6'; // IPv6
}

View File

@ -0,0 +1,24 @@
<?php
namespace alphayax\freebox\api\v3\symbols\Lan;
/**
* Interface LanHostType
* @package alphayax\freebox\api\v3\symbols\Lan
*/
interface LanHostType {
const WORKSTATION = 'workstation';
const LAPTOP = 'laptop';
const SMARTPHONE = 'smartphone';
const TABLET = 'tablet';
const PRINTER = 'printer';
const VIDEO_GAME_CONSOLE = 'vg_console';
const TELEVISION = 'television';
const NAS = 'nas';
const IP_CAMERA = 'ip_camera';
const IP_PHONE = 'ip_phone';
const FREEBOX_PLAYER = 'freebox_player';
const FREEBOX_SERVER = 'freebox_hd';
const NETWORKING_DEVICE = 'networking_device';
const MULTIMEDIA_DEVICE = 'multimedia_device';
const OTHER = 'other';
}

View File

@ -0,0 +1,16 @@
<?php
namespace alphayax\freebox\api\v3\symbols\Lan;
/**
* Symbol LanType
* @package alphayax\freebox\api\v3\symbols\Lan
*/
interface LanType {
/** The Freebox acts as a network router */
const ROUTER = 'router';
/** The Freebox acts as a network bridge */
const BRIDGE = 'bridge';
}