README.md 5.94 KB
Newer Older
Laurent Kislaire's avatar
Laurent Kislaire committed
1
2
3
4
5
6
# Freebox client library

## Features

* Full API coverage
* Https and Mdns discovery
Laurent Kislaire's avatar
Laurent Kislaire committed
7
* Thin client wrapper library (<200 lines)
Laurent Kislaire's avatar
Laurent Kislaire committed
8
* Small script to perform most tasks
Laurent Kislaire's avatar
Laurent Kislaire committed
9

Laurent Kislaire's avatar
Laurent Kislaire committed
10
11
12
[![pipeline status](https://framagit.org/sun/pyfbx/badges/master/pipeline.svg)](https://framagit.org/sun/pyfbx/commits/master)
[![coverage report](https://framagit.org/sun/pyfbx/badges/master/coverage.svg)](https://framagit.org/sun/pyfbx/commits/master)

Laurent Kislaire's avatar
Laurent Kislaire committed
13
## Library usage
Laurent Kislaire's avatar
Laurent Kislaire committed
14

Laurent Kislaire's avatar
Laurent Kislaire committed
15
The library is available to create your own scripts.
Laurent Kislaire's avatar
Laurent Kislaire committed
16
17
18
19
The REST API is generated at runtime with the creation of class attributes for all Freebox subsystems.

```python
>>> from pyfbx import Fbx
Laurent Kislaire's avatar
Laurent Kislaire committed
20
21
>>> f=Fbx()             # By default, this will perform MDNS discovery
>>> f=Fbx(nomdns=True)  # Use faster http api discovery
Laurent Kislaire's avatar
Laurent Kislaire committed
22
23
24
25
26
27
28
29
30
31
32
33
34
>>> f.<TAB>
f.Airmedia         f.Download_Config  f.Lan              f.Rrd              f.Upnpav
f.Call             f.Download_Feeds   f.Lcd              f.Share            f.Vpn
f.Connection       f.Freeplug         f.Nat              f.Storage          f.Vpn_Client
f.Contacts         f.Fs               f.Network_Share    f.Switch           f.Wifi
f.Dhcp             f.Ftp              f.Parental         f.System           f.mksession(
f.Download         f.Igd              f.Pvr              f.Upload           f.register(
>>> f.Freeplug.<TAB>
f.Freeplug.Get_a_particular_Freeplug_information(
f.Freeplug.Reset_a_Freeplug(
f.Freeplug.Get_the_current_Freeplugs_networks(

# Register application to get app token. Physical access is required.
Laurent Kislaire's avatar
Laurent Kislaire committed
35
>>> token = f.register("AppId", "My App", "PC")
Laurent Kislaire's avatar
Laurent Kislaire committed
36
# Generate session token
Laurent Kislaire's avatar
Laurent Kislaire committed
37
>>> f.mksession(app_id="AppId", token=token)
Laurent Kislaire's avatar
Laurent Kislaire committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

>>> help(f.Lan.Update_the_current_Lan_configuration)
Help on method Update_the_current_Lan_configuration:

Update_the_current_Lan_configuration(post_data) method of pyfbx.client.Lan instance
    Update the current Lan configuration

    Url parameters:
    Post data:PostData

>>> help(f.Contacts.Access_a_given_contact_entry)
Help on method Access_a_given_contact_entry:

Access_a_given_contact_entry(id) method of pyfbx.client.Contacts instance
    Access a given contact entry

    Url parameters: id
    Post data:

>>> f.Lan.Get_the_current_Lan_configuration()
{'name_dns': 'freebox-server', 'name': 'Freebox Server', 'name_mdns': 'Freebox-Server', 
'mode': 'router', 'name_netbios': 'Freebox_Server', 'ip': '192.168.1.254'}

# Any subsequent call to mksession will refresh the session token
>>> f.mksession()
```

Laurent Kislaire's avatar
Laurent Kislaire committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
## Script

The included script can perform a lot of tasks.

### Basics

```shell
$ pyfbx -h
usage: pyfbx [-h] [-a APP_ID] [-t TOKEN] [-v] [-n] [-j] [-d DELAY] [-u URL]
             [-s SEND] [-c COMMAND]

optional arguments:
  -h, --help            show this help message and exit
  -a APP_ID, --app_id APP_ID
                        application identifier
  -t TOKEN, --token TOKEN
                        token (or f:<filename>)
  -v, --verbose         increase verbosity to INFO, use twice for DEBUG
  -n, --http            disable MDNS and use http known address
  -j, --json            json output
  -d DELAY, --delay DELAY
                        cylically send command (number of seconds)
  -u URL, --url URL     specific url to query
  -s SEND, --send SEND  url to send json to
  -c COMMAND, --command COMMAND
                        command, defaults to
                        System.Get_the_current_system_info()
```

First, register the application:

```shell
97
$ pyfbx -a SuperAppId
Laurent Kislaire's avatar
Laurent Kislaire committed
98
99
100
101
102
103
104
```
Once registration is done on the device, write down the application token.
You can also create a file with token as first line and application Id as second.

Using this app token, acquire a session token to execute a test command.

```shell
105
106
107
108
109
110
111
112
113
114
115
$ pyfbx -t '<TOKEN>' -a SuperAppId

$ cat token.txt
2OUDy4hhM/KLQ7ru+70gfi83h5A4DUoOpDztOfqM4c2wuUiWJ+gXTHB1SbxHAFP7
SuperAppId

$ pyfbx -t f:token.txt -c 'Connection.Get_the_current_Connection_status()'
{   'bandwidth_down': 1000000000,
    [..]
    'state': 'up',
    'type': 'ethernet'}
Laurent Kislaire's avatar
Laurent Kislaire committed
116
117
```

Laurent Kislaire's avatar
Laurent Kislaire committed
118
119
_Note_ : Don't forget to escape token and command with quotes.

Laurent Kislaire's avatar
Laurent Kislaire committed
120
121
122
123
124
125
126
### Advanced

You can run any commands on the freebox and retrieve complete result or single value
```shell
$ pyfbx -t f:/home/foo/token.txt SuperAppId -c 'Contacts.Create_a_contact(\
post_data={"display_name": "Sandy Kilo", "first_name": "Sandy", "last_name":"Kilo"})'
{   'birthday': '',
127
    [..]
Laurent Kislaire's avatar
Laurent Kislaire committed
128
129
    'photo_url': ''}

130
$ pyfbx -u https://0s2efr3i.fbxos.fr:15628 -c "Connection.Get_the_current_Connection_status()['rate_up']" -t f:token.txt
Laurent Kislaire's avatar
Laurent Kislaire committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
1700
```

With the delay option, commands can be sent cyclically:

```shell
$ pyfbx -d 1 -c "Connection.Get_the_current_Connection_status()['rate_up']" -t f:token.txt
42460
50710
53400
```

With the send option, result can be sent to a remote URL.
In the following, the result is sent cyclically to example.com in json format.

```shell
pyfbx -j -s http://example.com/values -d 10 -c "Connection.Get_the_current_Connection_status()" -t f:token.txt
```

It's possible to execute several commands.
Here, two commands are sent cyclically and results sent to an URL.

```shell
pyfbx -c 'System.Get_the_current_system_info()' -c 'Connection.Get_the_current_Connection_status()' -d 10 -j -s http://localhost:8182/telegraf -t f:token.txt
```

Telegraf http listener v2 input plugin with json format can be used to plot data in realtime.

Laurent Kislaire's avatar
Doc    
Laurent Kislaire committed
159
### Telegraf and Graphana plots
Laurent Kislaire's avatar
Laurent Kislaire committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

You can use a telegraph configuration in /etc/telegraf/telegraf.d/freebox:

```
[[inputs.http_listener_v2]]
    service_address = ":8182"
    path = "/telegraf"
    methods = ["POST"]
    read_timeout = "10s"
    write_timeout = "10s"
    max_body_size = "5KB"
    data_format = "json"
    name_override = "pyfbx"
```

Laurent's avatar
Laurent committed
175
176
![Grafana](https://framagit.org/uploads/-/system/temp/7b198d06e9c27996b60708d470af96ae/debit.png "Grafana Dashboard")

Laurent Kislaire's avatar
Laurent Kislaire committed
177
178
179
## Developpment

### Testing
Laurent Kislaire's avatar
Laurent Kislaire committed
180
181
182
183
184
185
186

You can run tests with

```shell
pip3 install -r requirements-dev.txt
pytest tests
```
Laurent Kislaire's avatar
Laurent Kislaire committed
187
188
The library mechanisms are tested. To get 100% coverage, a Freebox on its default IP (192.168.1.254) is required.

Laurent Kislaire's avatar
Laurent Kislaire committed
189
The API description is generated and thus not functionnaly tested.