WifiAction.kt 4.06 KB
Newer Older
patpat committed
1 2 3
package com.bgycc.smartcanteen.action

import android.util.Log
4
import com.bgycc.smartcanteen.event.SettingStateEvent
patpat committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import com.bgycc.smartcanteen.helper.TimerHelper
import com.bgycc.smartcanteen.helper.WifiHelpler
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

class WifiAction private constructor() : Action(ActionEnum.CONFIG_WIFI.name) {

    companion object {

        private val TAG = WifiAction::class.java.simpleName

        private var sDefault: WifiAction? = null
        @Synchronized fun getDefault(): WifiAction {
            if (sDefault == null) {
                sDefault = WifiAction()
            }
            return sDefault!!
        }
    }

    private fun fail() {
        fail("")
        TimerHelper.timeout({
            state = State.INITED
29
            EventBus.getDefault().post(SettingStateEvent(-1))
patpat committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43
        }, 3000)
    }

    fun exec(data: JSONObject) {
        if (state != State.INITED) return

        state = State.STARTED
        try {
            val ssid = data.getString("ssid")
            val pwd = data.getString("pwd")
            val type = data.optString("type", "wpa")
            val identity = data.optString("identity")
            WifiHelpler.connect(ssid, identity, pwd, type)

44
            EventBus.getDefault().post(SettingStateEvent(1, "正在启动Wifi"))
patpat committed
45
            if (!WifiHelpler.setEnable(true)) {
46
                EventBus.getDefault().post(SettingStateEvent(1, "无法启动Wifi"))
47
                TimerHelper.timeout({
patpat committed
48
                    state = State.INITED
49
                    EventBus.getDefault().post(SettingStateEvent(-1))
patpat committed
50 51 52 53
                }, 3000)
                return
            }

54
            EventBus.getDefault().post(SettingStateEvent(10, "正在配置Wifi"))
patpat committed
55 56
            val config = WifiHelpler.createWifiConfiguration(ssid, identity, pwd, type)
            if (config == null) {
57
                EventBus.getDefault().post(SettingStateEvent(10, "Wifi参数有误"))
58
                TimerHelper.timeout({
patpat committed
59
                    state = State.INITED
60
                    EventBus.getDefault().post(SettingStateEvent(-1))
patpat committed
61 62 63 64 65 66 67 68 69
                }, 3000)
                return
            }

            try {
                if (!WifiHelpler.removeWifiConfiguration(ssid)) throw Exception("无法修改Wifi配置")
                val netId = WifiHelpler.addNetwork(config)
                if (!WifiHelpler.enableNetwork(netId)) throw Exception("无法应用Wifi配置")
            } catch (e: Exception) {
70
                EventBus.getDefault().post(SettingStateEvent(20, e.message))
71
                TimerHelper.timeout({
patpat committed
72
                    state = State.INITED
73
                    EventBus.getDefault().post(SettingStateEvent(-1))
patpat committed
74 75 76 77
                }, 3000)
                return
            }

78
            EventBus.getDefault().post(SettingStateEvent(30, "正在连接Wifi"))
patpat committed
79
            TimerHelper.loop({ id, isLastTime ->
patpat committed
80 81 82
                if (isLastTime) {
                    if (state == State.STARTED) {
                        state = State.FAIL
83
                        EventBus.getDefault().post(SettingStateEvent(30, "无法连接Wifi"))
84
                        TimerHelper.timeout({
patpat committed
85
                            state = State.INITED
86
                            EventBus.getDefault().post(SettingStateEvent(-1))
patpat committed
87 88 89 90 91 92 93 94
                        }, 3000)
                    }
                } else {
                    val info = WifiHelpler.getWifiInfo()
                    if (info != null) {
                        Log.i(TAG, "ip: " + info.ipAddress)
                        if (info.ipAddress != 0) {
                            state = State.SUCCESS
95
                            EventBus.getDefault().post(SettingStateEvent(30, "已连接Wifi"))
96
                            TimerHelper.timeout({
patpat committed
97
                                state = State.INITED
98
                                EventBus.getDefault().post(SettingStateEvent(-1))
patpat committed
99
                            }, 3000)
patpat committed
100
                            TimerHelper.cancel(id)
patpat committed
101 102 103 104 105 106 107 108 109
                        }
                    }
                }
            }, 1000, 10)
        } catch (e: Exception) {

        }
    }
}