-
Notifications
You must be signed in to change notification settings - Fork 11
/
DeviceListVm.kt
194 lines (168 loc) · 6.83 KB
/
DeviceListVm.kt
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package io.omniedge.viewmodel
import android.annotation.SuppressLint
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import io.omniedge.App
import io.omniedge.OmniLog
import io.omniedge.R
import io.omniedge.RxBus
import io.omniedge.data.DataRepository
import io.omniedge.data.bean.*
import io.omniedge.n2n.event.StartEvent
import io.omniedge.n2n.event.StopEvent
import io.omniedge.n2n.event.SupernodeDisconnectEvent
import io.reactivex.Single
import io.reactivex.SingleObserver
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException
object DeviceListVm {
@SuppressLint("StaticFieldLeak")
private val repository = DataRepository.getInstance(App.instance)
private val compositeDisposable = CompositeDisposable()
private val loadingLv: MutableLiveData<Boolean> = MutableLiveData()
private val lvConnectStatus: MutableLiveData<Boolean> = MutableLiveData()
private val toastLv: MutableLiveData<String?> = MutableLiveData()
private val logoutLv: MutableLiveData<Boolean> = MutableLiveData()
private val networksLv: MutableLiveData<List<NetworkData>> = MutableLiveData()
private val joinedNetworkLv: MutableLiveData<JoinNetworkData?> = MutableLiveData()
private val currentNetworkLv: MutableLiveData<Pair<NetworkData, DeviceData>> = MutableLiveData()
init {
compositeDisposable.add(
RxBus.INSTANCE.toObservable(StartEvent::class.java)
.subscribe {
lvConnectStatus.postValue(true)
})
compositeDisposable.add(
RxBus.INSTANCE.toObservable(StopEvent::class.java)
.subscribe {
lvConnectStatus.postValue(false)
})
compositeDisposable.add(
RxBus.INSTANCE.toObservable(SupernodeDisconnectEvent::class.java)
.subscribe {
lvConnectStatus.postValue(false)
})
}
fun connectStatus() = lvConnectStatus
fun loading() = loadingLv
fun toast() = toastLv
fun logout() = logoutLv
fun networks() = networksLv
fun joinedNetwork() = joinedNetworkLv
fun currentNetwork() = currentNetworkLv
fun registerDevice() {
repository.registerDevice(
RegisterDevice(
repository.getDeviceName(),
repository.getHardwareUUID(),
repository.getOSInfo()
)
)
.subscribeOn(Schedulers.io())
.doOnSuccess { response ->
response.data?.id?.apply {
repository.updateDeviceUUID(this)
}
}
// list networks
.flatMap { repository.listNetworks() }
// create network if empty
.flatMap {
if (it.data.isEmpty()) {
repository.createNetwork(
// TODO: 2022/1/15 customize creation for network
CreateNetwork(
"network",
"100.100.0.0/24",
repository.generateUUID(),
)
).flatMap { repository.listNetworks() }
} else {
Single.just(it)
}
}
.subscribe(object : SingleObserver<ListNetworkResponse> {
override fun onSubscribe(d: Disposable) {
loadingLv.postValue(true)
}
override fun onSuccess(t: ListNetworkResponse) {
loadingLv.postValue(false)
OmniLog.d("response: $t")
networksLv.postValue(t.data)
}
override fun onError(e: Throwable) {
loadingLv.postValue(false)
OmniLog.e("error on list network", e)
if (e is HttpException) {
try {
val body = e.response()?.errorBody()?.string()
if (body != null) {
val response = Gson().fromJson(body, Response::class.java)
if (response?.message != null) {
toastLv.postValue(response.message)
return
}
}
} catch (e: Exception) {
}
}
toastLv.postValue(e.message)
}
})
}
fun listNetworks() {
repository.listNetworks()
.subscribeOn(Schedulers.io())
.subscribe(object : SingleObserver<ListNetworkResponse> {
override fun onSubscribe(d: Disposable) {
loadingLv.postValue(true)
}
override fun onSuccess(t: ListNetworkResponse) {
loadingLv.postValue(false)
OmniLog.d("response: $t")
networksLv.postValue(t.data)
}
override fun onError(e: Throwable) {
loadingLv.postValue(false)
OmniLog.e("error on list network", e)
toastLv.postValue(e.message)
}
})
}
fun joinNetwork(network: NetworkData?) {
val deviceID = repository.getDeviceUUID()
if (deviceID != null && network?.id != null) {
repository.joinNetwork(network.id, deviceID)
.subscribeOn(Schedulers.io())
// start service after joined network
.doOnSuccess { response ->
repository.setNetworkInfo(response.data)
joinedNetworkLv.postValue(response.data)
repository.setLatestJoinedNetworkUUID(network.id)
}
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(object : SingleObserver<JoinNetworkResponse> {
override fun onSubscribe(d: Disposable) {
loadingLv.postValue(true)
}
override fun onSuccess(t: JoinNetworkResponse) {
loadingLv.postValue(false)
}
override fun onError(e: Throwable) {
loadingLv.postValue(false)
OmniLog.e("error on join network", e)
toastLv.postValue(e.message)
}
})
} else {
toastLv.postValue(App.instance.getString(R.string.invalid_token))
logoutLv.postValue(true)
}
}
fun clearNetwork() {
joinedNetworkLv.postValue(null)
}
}