2020-02-17 13:34:52 -08:00
|
|
|
package com.geeksville.mesh.model
|
|
|
|
|
|
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
|
2023-10-20 18:31:13 -03:00
|
|
|
import com.geeksville.mesh.MyNodeInfo
|
2020-02-17 13:34:52 -08:00
|
|
|
import com.geeksville.mesh.NodeInfo
|
|
|
|
|
import com.geeksville.mesh.Position
|
2023-10-20 18:31:13 -03:00
|
|
|
import com.geeksville.mesh.database.dao.MyNodeInfoDao
|
|
|
|
|
import com.geeksville.mesh.database.dao.NodeInfoDao
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
2023-09-05 08:19:26 -03:00
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
2023-10-20 18:31:13 -03:00
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
import javax.inject.Singleton
|
|
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
|
class NodeDB @Inject constructor(
|
|
|
|
|
private val myNodeInfoDao: MyNodeInfoDao,
|
|
|
|
|
private val nodeInfoDao: NodeInfoDao,
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
fun myNodeInfoFlow(): Flow<MyNodeInfo?> = myNodeInfoDao.getMyNodeInfo()
|
|
|
|
|
private suspend fun setMyNodeInfo(myInfo: MyNodeInfo) = withContext(Dispatchers.IO) {
|
|
|
|
|
myNodeInfoDao.setMyNodeInfo(myInfo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun nodeInfoFlow(): Flow<List<NodeInfo>> = nodeInfoDao.getNodes()
|
|
|
|
|
suspend fun upsert(node: NodeInfo) = withContext(Dispatchers.IO) {
|
|
|
|
|
nodeInfoDao.upsert(node)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suspend fun installNodeDB(mi: MyNodeInfo, nodes: List<NodeInfo>) {
|
|
|
|
|
myNodeInfoDao.clearMyNodeInfo()
|
|
|
|
|
nodeInfoDao.clearNodeInfo()
|
|
|
|
|
nodeInfoDao.putAll(nodes)
|
|
|
|
|
setMyNodeInfo(mi) // set MyNodeInfo last
|
|
|
|
|
}
|
2020-04-08 09:53:04 -07:00
|
|
|
|
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
|
2023-10-20 18:31:13 -03:00
|
|
|
Position(32.912901, -96.781776, 35, 789), // north dallas
|
2020-02-17 13:34:52 -08:00
|
|
|
)
|
|
|
|
|
|
2023-10-20 18:31:13 -03:00
|
|
|
private val testNodeNoPosition = NodeInfo(
|
2020-02-17 13:34:52 -08:00
|
|
|
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
|
|
|
)
|
|
|
|
|
|
2023-10-20 18:31:13 -03:00
|
|
|
private val testNodes = (listOf(testNodeNoPosition) + 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-10-20 18:31:13 -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
|
2023-10-20 18:31:13 -03:00
|
|
|
private val _myId = MutableStateFlow(if (seedWithTestNodes) "+16508765309" else null)
|
|
|
|
|
val myId: StateFlow<String?> get() = _myId
|
2022-04-22 17:22:06 -03:00
|
|
|
|
|
|
|
|
fun setMyId(myId: String?) {
|
|
|
|
|
_myId.value = myId
|
|
|
|
|
}
|
2020-02-17 13:34:52 -08:00
|
|
|
|
2023-10-20 18:31:13 -03:00
|
|
|
// A map from nodeNum to NodeInfo
|
|
|
|
|
private val _nodeDBbyNum = MutableStateFlow<Map<Int, NodeInfo>>(mapOf())
|
|
|
|
|
val nodeDBbyNum: StateFlow<Map<Int, NodeInfo>> get() = _nodeDBbyNum
|
|
|
|
|
val nodesByNum get() = nodeDBbyNum.value
|
|
|
|
|
|
2023-09-05 08:19:26 -03:00
|
|
|
// A map from userId to NodeInfo
|
|
|
|
|
private val _nodes = MutableStateFlow(if (seedWithTestNodes) testNodes else mapOf())
|
2023-10-20 18:31:13 -03:00
|
|
|
val nodes: StateFlow<Map<String, NodeInfo>> get() = _nodes
|
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!! })
|
2023-10-20 18:31:13 -03:00
|
|
|
_nodeDBbyNum.value = list.associateBy { it.num }
|
2023-09-05 08:19:26 -03:00
|
|
|
}
|
|
|
|
|
}
|