forked from guillaumewuip/freeboxApi_node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
111 lines (80 loc) · 2.64 KB
/
main.js
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
37
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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*jslint node:true, esversion:6 */
const Freebox=require('./lib/freebox');
const program = require('commander');
const fs=require('fs');
program.option("--app_id <appId>", "Application identifier");
program.option("--app_name <appName>", "Application name");
program.option("--app_version <appVersion>", "Application version");
program.option("--device_name <deviceName>", "Device name");
program.option("--authorization <path>", "Path of JSOn authorization");
program.option("--baseURL <baseURL>", "URL of freebox");
program.command('token').description("Return token").action( ()=> {
var config=fillConfig();
var freebox=new Freebox(config);
freebox.waitApplicationGranted(1000*60*2, (error, app) => {
console.error("error=",error,"app=",app);
});
});
program.command('wifiState').description("Return wifi state").action( ()=> {
var config=fillConfig();
var freebox = new Freebox(config);
freebox.getWifiState((error, state) => {
if (error) {
console.error(error);
return;
}
console.log("wifiState=",state);
});
});
program.command('setWifiState').description("Return wifi state").action( (state)=> {
var reg=/^(on|enable|enabled|1)$/i.exec(state);
state=!!reg;
console.log("State=",state,reg);
var config=fillConfig();
var freebox = new Freebox(config);
freebox.setWifiState(state, (error, newState) => {
if (error) {
console.error(error);
return;
}
console.log("change wifiState to ",newState);
});
});
program.command('calls').description("Return calls").action( ()=> {
var config=fillConfig();
var freebox = new Freebox(config);
freebox.calls((error, calls) => {
if (error) {
console.error(error);
return;
}
console.log("calls=",calls);
});
});
program.command('lanBrowser').description("Browse all lan hosts").action( ()=> {
var config=fillConfig();
var freebox=new Freebox(config);
freebox.lanBrowser().then((hosts) => {
console.log("hosts=",hosts);
}, (error) => {
console.error(error);
});
});
program.parse(process.argv);
function fillConfig() {
var app = {
app_id : program.app_id || "freeboxos",
app_name : program.app_name || "Test node app",
app_version : program.app_version || '0.0.1',
device_name : program.device_name || "NodeJs-API"
};
var config = {app};
if (program.authorization) {
config.jsonPath = program.authorization;
config.jsonAutoSave = true;
}
if (program.baseURL) {
config.baseURL=program.baseURL;
}
return config;
}