| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Scalar::Util "blessed"; | ||
| 5 | use Storable; | ||
| 6 | use Data::Dumper; | ||
| 7 | use Getopt::Long; | ||
| 8 | use JSON::MaybeXS; | ||
| 9 | use WWW::FBX; | ||
| 10 | |||
| 11 | my $fbx; | ||
| 12 | my $store = 'app_token'; | ||
| 13 | my $conn = { | ||
| 14 | app_id => "APP ID", | ||
| 15 | app_name => "APP NAME", | ||
| 16 | app_version => "1.0", | ||
| 17 | device_name => "debian", | ||
| 18 | }; | ||
| 19 | my $json; | ||
| 20 | my $res; | ||
| 21 | my ($quiet,$debug); | ||
| 22 | |||
| 23 | sub die_helper { | ||
| 24 | print "Usage : $0 [OPTIONS] [COMMAND] [PARAMETERS]\n"; | ||
| 25 | print "Control FreeboxOs v6 through its API v3\n\n"; | ||
| 26 | print "OPTIONS can be --debug, --quiet and --help\n\n"; | ||
| 27 | print "Without COMMAND, the script will list the permission granted and display your internet connection state as an example.\n\n"; | ||
| 28 | print "List of COMMAND and PARAMETERS are:\n"; | ||
| 29 | $conn->{ noauth }=1; | ||
| 30 | $fbx = WWW::FBX->new( $conn ); | ||
| 31 | for my $role ( @{ $fbx->meta->roles } ) { | ||
| 32 | if ($role->{package} eq "WWW::FBX::Role::API::APIv3") { | ||
| 33 | for my $meth ( sort $role->get_method_list ) { | ||
| 34 | print "\t$meth (", join(" , ", @{$role->get_method($meth)->params}), ")\n" unless $meth eq "meta"; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | } | ||
| 38 | exit; | ||
| 39 | } | ||
| 40 | |||
| 41 | for (qw/track_id app_token/) { | ||
| 42 | $conn->{$_} = $ENV{$_} if $ENV{$_}; | ||
| 43 | } | ||
| 44 | |||
| 45 | eval { | ||
| 46 | |||
| 47 | if (-f $store) { | ||
| 48 | my $token = retrieve $store; | ||
| 49 | %$conn = ( %$conn, %$token ); | ||
| 50 | warn "Retrieved track_id and app_token from $store\n"; | ||
| 51 | } else { | ||
| 52 | warn "No stored token found\n"; | ||
| 53 | } | ||
| 54 | GetOptions( | ||
| 55 | 'debug' => sub { $conn->{debug} = 1 }, | ||
| 56 | 'quiet' => sub { $quiet = 1 }, | ||
| 57 | 'help' => \&die_helper, | ||
| 58 | ) or die_helper; | ||
| 59 | |||
| 60 | $fbx = WWW::FBX->new( $conn ); | ||
| 61 | unless ( -f $store ) { | ||
| 62 | warn "Storing token in $store in current directory for further usage [ track_id = ", $fbx->track_id, " app_token = ", $fbx->app_token, " ]\n"; | ||
| 63 | warn "You can add the remaining permissions by connecting on the web interface\n"; | ||
| 64 | store { track_id => $fbx->track_id, app_token => $fbx->app_token }, $store; | ||
| 65 | } | ||
| 66 | |||
| 67 | if (! defined (my $cmd = shift @ARGV) ) { | ||
| 68 | #Just run a simple connection test | ||
| 69 | print "App permissions are:\n"; | ||
| 70 | while ( my( $key, $value ) = each %{ $fbx->uar->{result}{permissions} } ) { | ||
| 71 | print "\t $key\n" if $value; | ||
| 72 | } | ||
| 73 | $res = $fbx->connection; | ||
| 74 | printf "Your %s internet connection is %s\n", $res->{media}, $res->{state}; | ||
| 75 | } elsif ( $cmd eq "wifitab" ) { | ||
| 76 | #List wifi stations | ||
| 77 | my $wifi = $fbx->wifi_ap("0/stations"); | ||
| 78 | for my $host ( sort {$a->{hostname} cmp $b->{hostname} } @{ $wifi }) { | ||
| 79 | for my $l3c (@{$host->{host}{l3connectivities}}) { | ||
| 80 | next unless $l3c->{active}; | ||
| 81 | printf "%16.16s\t%s\t%15.15s\t%6.6sdB\t%6.6sMbps\t%6.6sMbps\n", $host->{hostname}, $host->{mac}, $l3c->{addr}, $host->{signal}, 10*$host->{last_rx}{bitrate}, 10*$host->{last_tx}{bitrate}; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } else { | ||
| 85 | #Execute given command (JSON or not) | ||
| 86 | local $Data::Dumper::Sortkeys = 1; | ||
| 87 | local $Data::Dumper::Indent = 1; | ||
| 88 | local $Data::Dumper::Deepcopy = 1; | ||
| 89 | if (eval { $json = from_json( $ARGV[0]) } ) { | ||
| 90 | $res = $fbx->$cmd($json); | ||
| 91 | } else { | ||
| 92 | $res = $fbx->$cmd(@ARGV); | ||
| 93 | } | ||
| 94 | print Dumper $res unless $quiet; | ||
| 95 | } | ||
| 96 | }; | ||
| 97 | |||
| 98 | if ( my $err = $@ ) { | ||
| 99 | die $@ unless blessed $err && $err->isa('WWW::FBX::Error'); | ||
| 100 | |||
| 101 | warn "HTTP Response Code: ", $err->code, "\n", | ||
| 102 | "HTTP Message......: ", $err->message, "\n", | ||
| 103 | "API Error.........: ", $err->error, "\n", | ||
| 104 | "Error Code........: ", $err->fbx_error_code, "\n", | ||
| 105 | } |