-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrt1900ac.js
123 lines (95 loc) · 2.98 KB
/
wrt1900ac.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
112
113
114
115
116
117
118
119
120
121
122
const request = require('request');
module.exports = class Wrt1900ac {
constructor(config) {
this._host = '192.168.1.1';
this._login = 'admin';
this._password = 'admin';
this._setConfig(config);
};
_setConfig(config) {
if (!config) {
return;
}
if (config.host) {
this.setHost(config.host);
}
if (config.login) {
this.setLogin(config.login);
}
if (config.password) {
this.setPass(config.password);
}
};
_getJNAPAuthorization() {
return 'Basic ' + Buffer.from(this._login + ':' + this._password).toString('base64');
}
_makeRequest(action, cb) {
const options = {
method: 'POST',
url: 'http://' + this._host + '/JNAP/',
headers: {
'X-JNAP-Authorization': this._getJNAPAuthorization(),
'X-JNAP-Action': 'http://linksys.com/jnap/core/Transaction',
'Content-Type': 'application/json; charset=UTF-8'
},
body: '[{"action":"http://linksys.com/jnap/' + action + '","request":{}}]'
};
function responseCallback(err, response, body) {
if (err) {
return cb(err, null);
}
if (response.statusCode !== 200) {
return cb(new Error('Wrong status code: ' + response.statusCode), body);
}
let data;
try {
data = JSON.parse(body);
} catch (e) {
return cb(e, null);
}
return cb(null, data);
}
request(options, responseCallback);
};
setHost(host) {
if (host) {
this._host = host;
}
return this;
};
setLogin(login) {
if (login) {
this._login = login;
}
return this;
};
setPass(password) {
if (password) {
this._password = password;
}
return this;
};
// -- api --
getNetworkConnections(cb) {
this._makeRequest('networkconnections/GetNetworkConnections', (err, data) => {
if (err) {
return cb(err, data);
}
if (!data || !data.responses || !data.responses[0] || !data.responses[0].output || !data.responses[0].output.connections) {
return cb(new Error('Wrong Response'), null);
}
return cb(err, data.responses[0].output.connections);
});
};
getDevices(cb) {
this._makeRequest('devicelist/GetDevices', (err, data) => {
if (err) {
return cb(err, data);
}
if (!data || !data.responses || !data.responses[0] || !data.responses[0].output || !data.responses[0].output.devices) {
return cb(new Error('Wrong Response'), null);
}
return cb(err, data.responses[0].output.devices);
});
};
};