Files: 24bc4bc0950ac6be38c29058e291fe380aa12189 / index.js
2326 bytesRaw
1 | const {promisify} = require('util') |
2 | const parseXML = promisify(require('xml2js').parseString) |
3 | const request = (require('request-promise-native')).defaults({ |
4 | jar: true |
5 | }) |
6 | |
7 | function objectMap(object, mapFn) { |
8 | return Object.keys(object).reduce(function(result, key) { |
9 | result[key] = mapFn(object[key]) |
10 | return result |
11 | }, {}) |
12 | } |
13 | |
14 | module.exports = |
15 | class MifiClient { |
16 | constructor(host = '192.168.8.1') { |
17 | this.host = host |
18 | |
19 | this._request = request.defaults({ |
20 | jar: true |
21 | }) |
22 | } |
23 | |
24 | async request(path) { |
25 | const xml = await this._request(`http://${this.host}${path}`) |
26 | const {response} = await parseXML(xml) |
27 | return objectMap(response, val => val[0]) |
28 | } |
29 | |
30 | async authenticate() { |
31 | await this._request(`http://${this.host}`) |
32 | } |
33 | |
34 | async status() { |
35 | const res = await this.request(`/api/monitoring/status`) |
36 | return { |
37 | /** |
38 | * mobile signal strength |
39 | * number: 0 - 5 |
40 | */ |
41 | signal: parseInt(res.SignalIcon), |
42 | /** |
43 | * battery percentage |
44 | * number 0 - 100 |
45 | */ |
46 | battery: parseInt(res.BatteryPercent), |
47 | /** |
48 | * current devices connected |
49 | * number 0+ |
50 | */ |
51 | currentDevices: parseInt(res.CurrentWifiUser) |
52 | } |
53 | } |
54 | |
55 | async traffic() { |
56 | const res = await this.request(`/api/monitoring/traffic-statistics`) |
57 | return { |
58 | currentConnectTime: parseInt(res.CurrentConnectTime), |
59 | currentUpload: parseInt(res.CurrentUpload), |
60 | currentDownload: parseInt(res.CurrentDownload), |
61 | currentDownloadRate: parseInt(res.CurrentDownloadRate), |
62 | currentUploadRate: parseInt(res.CurrentUploadRate), |
63 | totalUpload: parseInt(res.TotalUpload), |
64 | totalDownload: parseInt(res.TotalDownload), |
65 | totalConnectTime: parseInt(res.TotalConnectTime), |
66 | } |
67 | } |
68 | |
69 | async notifications() { |
70 | const res = await this.request(`/api/monitoring/check-notifications`) |
71 | return { |
72 | unreadMessage: parseInt(res.UnreadMessage), |
73 | smsStorageFull: parseInt(res.SmsStorageFull), |
74 | onlineUpdateStatus: parseInt(res.OnlineUpdateStatus), |
75 | } |
76 | } |
77 | } |
78 |
Built with git-ssb-web