-
Notifications
You must be signed in to change notification settings - Fork 4
/
ubusAPI.js
105 lines (86 loc) · 2.46 KB
/
ubusAPI.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
import Ubus from '../libs/ubus.js'
import config from '../config.js'
import env from '../env.js'
const ubus = new Ubus(config.ubusUrl)
import ubusRpcSession from './stubs/ubusRPCSessionID.stub.js'
import uciConfigs from './stubs/fetchUCIConfigs.stub.js'
function login(username, password) {
return ubus.call(null, 'session', 'login', { username, password })
}
function loginStub(username, password) {
return { ubus_rpc_session: ubusRpcSession }
}
function getConfig(config) {
return callUbusWithAuth('uci', 'get', { config })
}
function getConfigStub(config) {
return wrap({ values: JSON.parse(uciConfigs[config]) })
}
function logout(sessionID) {
return callUbusWithAuth('session', 'destroy', { sessionID })
}
function logoutStub(sessionID) {
// todo
return wrap()
}
function setWirelessConfig(section, toChange, value) {
return callUbusWithAuth('uci', 'set', {
config: 'wireless',
section: section,
values: {
[toChange]: value
}
})
}
function setWirelessConfigStub(ifname, toChange, value) {
// no response
return wrap()
}
function setTunneldiggerConfig(toChange, value) {
return callUbusWithAuth('uci', 'set', {
config: 'tunneldigger',
section: 'main',
values: {
[toChange]: value
}
})
}
function setTunneldiggerConfigStub(toChange, value) {
// no response
return wrap()
}
function commitConfig(config) {
return callUbusWithAuth('uci', 'commit', { config })
}
function commitConfigStub(config) {
// no response
return wrap()
}
// Hit Ubus API, authenticated with session ID. Logout if session is expired.
function callUbusWithAuth(object, method, args) {
return async (dispatch, getState) => {
const sessionID = localStorage.getItem('sessionID') || null
try {
return ubus.call(sessionID, object, method, args)
} catch (e) {
if (e.message.match(/session_expired/)) {
dispatch(logout())
} else {
throw e
}
}
}
}
// Wrap a result so that it can be `dispatch`ed. Ugh redux.
function wrap(result) {
return async(dispatch, getState) => result
}
const useStubs = env === 'development'
export default {
login: useStubs ? loginStub : login,
getConfig: useStubs ? getConfigStub : getConfig,
logout: useStubs ? logoutStub : logout,
setWirelessConfig: useStubs ? setWirelessConfigStub : setWirelessConfig,
setTunneldiggerConfig: useStubs ? setTunneldiggerConfigStub : setTunneldiggerConfig,
commitConfig: useStubs ? commitConfigStub : commitConfig
}