2020-02-17 13:34:52 -08:00
|
|
|
package com.geeksville.mesh.model
|
|
|
|
|
|
2022-04-22 17:22:06 -03:00
|
|
|
import androidx.lifecycle.LiveData
|
2020-04-08 09:53:04 -07:00
|
|
|
import androidx.lifecycle.MutableLiveData
|
2023-09-05 08:19:26 -03:00
|
|
|
import androidx.lifecycle.asLiveData
|
2021-03-14 11:42:04 +08:00
|
|
|
import com.geeksville.mesh.MeshProtos
|
2020-02-17 13:34:52 -08:00
|
|
|
import com.geeksville.mesh.MeshUser
|
|
|
|
|
import com.geeksville.mesh.NodeInfo
|
|
|
|
|
import com.geeksville.mesh.Position
|
2023-09-05 08:19:26 -03:00
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
2020-04-08 09:53:04 -07:00
|
|
|
|
|
|
|
|
/// NodeDB lives inside the UIViewModel, but it needs a backpointer to reach the service
|
|
|
|
|
class NodeDB(private val ui: UIViewModel) {
|
2020-02-17 13:34:52 -08:00
|
|
|
private val testPositions = arrayOf(
|
2022-03-26 17:44:59 -03:00
|
|
|
Position(32.776665, -96.796989, 35, 123), // dallas
|
|
|
|
|
Position(32.960758, -96.733521, 35, 456), // richardson
|
|
|
|
|
Position(32.912901, -96.781776, 35, 789) // north dallas
|
2020-02-17 13:34:52 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
val testNodeNoPosition = NodeInfo(
|
|
|
|
|
8,
|
|
|
|
|
MeshUser(
|
|
|
|
|
"+16508765308".format(8),
|
|
|
|
|
"Kevin MesterNoLoc",
|
2021-03-14 11:42:04 +08:00
|
|
|
"KLO",
|
2022-11-08 23:11:18 -03:00
|
|
|
MeshProtos.HardwareModel.ANDROID_SIM,
|
|
|
|
|
false
|
2020-02-17 13:34:52 -08:00
|
|
|
),
|
2020-02-19 10:53:36 -08:00
|
|
|
null
|
2020-02-17 13:34:52 -08:00
|
|
|
)
|
|
|
|
|
|
2022-04-22 17:22:06 -03:00
|
|
|
private val testNodes = testPositions.mapIndexed { index, it ->
|
2020-02-17 13:34:52 -08:00
|
|
|
NodeInfo(
|
|
|
|
|
9 + index,
|
|
|
|
|
MeshUser(
|
|
|
|
|
"+165087653%02d".format(9 + index),
|
|
|
|
|
"Kevin Mester$index",
|
2021-03-14 11:42:04 +08:00
|
|
|
"KM$index",
|
2022-11-08 23:11:18 -03:00
|
|
|
MeshProtos.HardwareModel.ANDROID_SIM,
|
|
|
|
|
false
|
2020-02-17 13:34:52 -08:00
|
|
|
),
|
2020-02-19 10:53:36 -08:00
|
|
|
it
|
2020-02-17 13:34:52 -08:00
|
|
|
)
|
2023-09-05 08:19:26 -03:00
|
|
|
}.associateBy { it.user?.id!! }
|
2020-02-17 13:34:52 -08:00
|
|
|
|
2021-02-01 10:56:47 +08:00
|
|
|
private val seedWithTestNodes = false
|
2020-02-25 10:48:54 -08:00
|
|
|
|
2023-09-05 08:19:26 -03:00
|
|
|
// The unique userId of our node
|
2022-04-22 17:22:06 -03:00
|
|
|
private val _myId = MutableLiveData<String?>(if (seedWithTestNodes) "+16508765309" else null)
|
|
|
|
|
val myId: LiveData<String?> get() = _myId
|
|
|
|
|
|
|
|
|
|
fun setMyId(myId: String?) {
|
|
|
|
|
_myId.value = myId
|
|
|
|
|
}
|
2020-02-17 13:34:52 -08:00
|
|
|
|
2023-09-05 08:19:26 -03:00
|
|
|
// A map from userId to NodeInfo
|
|
|
|
|
private val _nodes = MutableStateFlow(if (seedWithTestNodes) testNodes else mapOf())
|
|
|
|
|
val nodes: LiveData<Map<String, NodeInfo>> = _nodes.asLiveData()
|
2023-04-16 06:16:41 -03:00
|
|
|
val nodesByNum get() = nodes.value?.values?.associateBy { it.num }
|
2022-04-22 17:22:06 -03:00
|
|
|
|
|
|
|
|
fun setNodes(nodes: Map<String, NodeInfo>) {
|
|
|
|
|
_nodes.value = nodes
|
|
|
|
|
}
|
2023-09-05 08:19:26 -03:00
|
|
|
|
|
|
|
|
fun setNodes(list: List<NodeInfo>) {
|
|
|
|
|
setNodes(list.associateBy { it.user?.id!! })
|
|
|
|
|
}
|
|
|
|
|
}
|