PROJET AUTOBLOG


Planet-Libre

source: Planet-Libre

⇐ retour index

Thomas Tourlourat : Désactiver le WiFi de la Freebox à certaines heures

mardi 9 octobre 2012 à 08:53

Je viens de sortir un SDK – short edition – sous NodeJS pour la Freebox: node-freebox-sdk.

Actuellement, il n’y a que la gestion du wifi de disponible.
Mon objectif étant de pouvoir activer / désactiver le WiFi de notre domicile à certaines heures.

Sur linuxfr.org, quelqu’un a rendu disponible une solution pour faire cela en utilisant le shell & curl.
Cette solution fonctionne très bien, mais je souhaitais développer un peu avec NodeJS.

Voyons comment mettre en place la solution NodeJS avec une tâche planifiée.

Nous allons commencer par installer le SDK Freebox via npm.

1
npm install freebox-sdk

Ensuite, nous allons créer l’application /home/john.doe/freebox-wifi-manager

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var Freebox = require("freebox-sdk");
var freebox = new Freebox({
    password: "",
    wifiChannel: 11,
    wifiHtMode: "disabled"  //disabled, 20, 40_lower, 40_upper
});

var command = process.argv[2];

freebox.on("connect", function() {
    switch (data) {
        case "wifi:on" :
            freebox.wifiOn(function() {
                console.log("wifi is on");
            });
            break;
        case "wifi:off" :
            freebox.wifiOff(function() {
                console.log("wifi is off");
            });
            break;
        case "wifi:status" :
            freebox.wifiStatus(function(status) {
                console.log("wifi is " + (status.active ? "on" : "off"));
            });
            break;
        default:
            console.log("command unknown");
    }
});

freebox.on("error", function(message) {
    console.log("error: " + message);
});

freebox.connect();

Vous pouvez tester si cela fonctionne

1
2
3
4
5
chmod +x /home/john.doe/freebox-wifi-manager
node /home/john.doe/freebox-wifi-manager wifi:on
node /home/john.doe/freebox-wifi-manager wifi:status
node /home/john.doe/freebox-wifi-manager wifi:off
node /home/john.doe/freebox-wifi-manager wifi:status

Pour finir,
Mise en place du crontab qui permet d’allumer le WiFi en semaine de 7h à 9h, de 17h à 00h et le weekend. Le reste du temps, le WiFi sera désactivé.

1
2
3
4
5
# m h  dom mon dow   command
0 7 * * 1-6 node /home/john.doe/freebox-wifi-manager on
0 9 * * 1-5 node /home/john.doe/freebox-wifi-manager off
0 17 * * 1-5 node /home/john.doe/freebox-wifi-manager on
0 0 * * 0-5 node /home/john.doe/freebox-wifi-manager off

Gravatar de Thomas Tourlourat
Original post of Thomas Tourlourat.Votez pour ce billet sur Planet Libre.