git ssb

0+

bencevans / huawei-mifi



Commit b0f2a12e61821d424e901ca7ac7f981aa5ea59d0

init

Ben Evans committed on 11/1/2018, 12:28:16 AM

Files changed

.gitignoreadded
index.jsadded
package.jsonadded
test.jsadded
.gitignoreView
@@ -1,0 +1,2 @@
1 +node_modules/
2 +package-lock.json
index.jsView
@@ -1,0 +1,77 @@
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 getStatus() {
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 getTrafficStatistics() {
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 getCheckNotifications() {
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 +}
package.jsonView
@@ -1,0 +1,10 @@
1 +{
2 + "dependencies": {
3 + "request": "^2.88.0",
4 + "request-promise-native": "^1.0.5",
5 + "xml2js": "^0.4.19"
6 + },
7 + "devDependencies": {
8 + "tape": "^4.9.1"
9 + }
10 +}
test.jsView
@@ -1,0 +1,28 @@
1 +const test = require('tape')
2 +const MiFi = require('.')
3 +
4 +let client
5 +
6 +test('create client and auth', async t => {
7 + client = new MiFi('192.168.8.1')
8 + await client.authenticate()
9 + t.end()
10 +})
11 +
12 +test('get status', async t => {
13 + const status = await client.getStatus()
14 + console.log(status)
15 + t.end()
16 +})
17 +
18 +test('get traffic stats', async t => {
19 + const status = await client.getTrafficStatistics()
20 + console.log(status)
21 + t.end()
22 +})
23 +
24 +test('check notifications', async t => {
25 + const status = await client.getCheckNotifications()
26 + console.log(status)
27 + t.end()
28 +})

Built with git-ssb-web