Created
July 15, 2017 02:47
-
-
Save incorvia/3f04667fefb93685aa1e792f7b301ecc to your computer and use it in GitHub Desktop.
SoftAp-Setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @flow */ | |
const SCAN_WIFIS_START = 'SCAN_WIFIS_START' | |
const SCAN_WIFIS_SUCCEED = 'SCAN_WIFIS_SUCCEED' | |
const SCAN_WIFIS_FAILED = 'SCAN_WIFIS_FAILED' | |
const SET_ACTIVE_WIFI_ITEM = 'SET_ACTIVE_WIFI_ITEM' | |
const CONNECT_WIFI_START = 'CONNECT_WIFI_START' | |
const CONNECT_WIFI_SUCCEED = 'CONNECT_WIFI_SUCCEED' | |
const CONNECT_WIFI_FAILED = 'CONNECT_WIFI_FAILED' | |
import SoftAPSetup from 'softap-setup' | |
const sap = new SoftAPSetup({ timeout: 10000 }) | |
let wifiScanTimer = null | |
let publicKey = null | |
type Action = Object<{ type: string }> | |
const initialState = { | |
scanned: false, | |
scaning: false, | |
items: [], | |
error: null, | |
activeIndex: 0, | |
connectedItem: null, | |
connected: false, | |
connecting: false, | |
} | |
export default function wifi(state = initialState, action: Action) { | |
switch (action.type) { | |
case SCAN_WIFIS_START: | |
return { | |
...state, | |
scaning: true, | |
scanned: false, | |
error: null, | |
} | |
case SCAN_WIFIS_SUCCEED: | |
return { | |
...state, | |
scaning: false, | |
scanned: true, | |
items: action.items, | |
} | |
case SCAN_WIFIS_FAILED: | |
return { | |
...state, | |
scaning: false, | |
scanned: false, | |
error: action.error, | |
} | |
case SET_ACTIVE_WIFI_ITEM: | |
return { | |
...state, | |
activeIndex: action.index, | |
} | |
case CONNECT_WIFI_START: | |
return { | |
...state, | |
connecting: true, | |
connected: false, | |
connectedItem: null, | |
error: null, | |
} | |
case CONNECT_WIFI_SUCCEED: | |
return { | |
...state, | |
connecting: false, | |
connected: true, | |
connectedItem: action.item, | |
} | |
case CONNECT_WIFI_FAILED: | |
return { | |
...state, | |
connecting: false, | |
connected: false, | |
error: action.error, | |
} | |
default: | |
return state | |
} | |
} | |
// Actions | |
const scanWifiStart = (): Action => ({ | |
type: SCAN_WIFIS_START, | |
}) | |
const scanWifiSucceed = (items): Action => ({ | |
type: SCAN_WIFIS_SUCCEED, | |
items, | |
}) | |
const scanWifiFailed = (error): Action => ({ | |
type: SCAN_WIFIS_FAILED, | |
error, | |
}) | |
const scan = (dispatch) => { | |
sap.scan((err, data) => { | |
if (err) return | |
clearInterval(wifiScanTimer) | |
const items = data.map((item) => ({ | |
label: item.ssid, | |
password: item.sec, | |
channel: item.ch, | |
})) | |
sap.publicKey((error, dat) => { | |
if (error) return | |
publicKey = dat | |
dispatch(scanWifiSucceed(items)) | |
}) | |
}) | |
} | |
export const scanWifisRequest = (): Action => | |
(dispatch) => { | |
dispatch(scanWifiStart()) | |
if (wifiScanTimer) clearInterval(wifiScanTimer) | |
scan(dispatch) | |
wifiScanTimer = setInterval(() => { | |
scan(dispatch) | |
}, 10000) | |
return wifiScanTimer | |
} | |
export const setActiveWifiItem = (index): Action => ({ | |
type: SET_ACTIVE_WIFI_ITEM, | |
index, | |
}) | |
const connectWifiStart = (): Action => ({ | |
type: CONNECT_WIFI_START, | |
}) | |
const connectWifiSucceed = (item): Action => ({ | |
type: CONNECT_WIFI_SUCCEED, | |
item, | |
}) | |
const connectWifiFailed = (error): Action => ({ | |
type: CONNECT_WIFI_FAILED, | |
error, | |
}) | |
export const connectWifiRequest = (item, password): Action => | |
(dispatch) => { | |
dispatch(connectWifiStart()) | |
sap.configure({ | |
ssid: item.label, | |
security: item.password, | |
password, | |
channel: item.channel, | |
}, (err) => { | |
if (err) return dispatch(connectWifiFailed(' Password is invalid ')) | |
return sap.connect((error) => { | |
if (error) return dispatch(connectWifiFailed(' Password is invalid ')) | |
return dispatch(connectWifiSucceed(item)) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment