forked from nightscout/AndroidAPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Id.kt
43 lines (35 loc) · 1.25 KB
/
Id.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
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm
import info.nightscout.androidaps.utils.extensions.toHex
import java.nio.ByteBuffer
data class Id(val address: ByteArray) {
init {
require(address.size == 4)
}
/**
* Used to obtain podId from controllerId
* The original PDM seems to rotate over 3 Ids:
* controllerID+1, controllerID+2 and controllerID+3
*/
fun increment(): Id {
val nodeId = address.copyOf()
nodeId[3] = (nodeId[3].toInt() and -4).toByte()
nodeId[3] = (nodeId[3].toInt() or PERIPHERAL_NODE_INDEX).toByte()
return Id(nodeId)
}
override fun toString(): String {
val asInt = ByteBuffer.wrap(address).int
return "$asInt/${address.toHex()}"
}
fun toLong(): Long {
return ByteBuffer.wrap(address).int.toLong() and 0xffffffffL
}
companion object {
private val PERIPHERAL_NODE_INDEX = 1 // TODO: understand the meaning of this value. It comes from preferences
fun fromInt(v: Int): Id {
return Id(ByteBuffer.allocate(4).putInt(v).array())
}
fun fromLong(v: Long): Id {
return Id(ByteBuffer.allocate(8).putLong(v).array().copyOfRange(4, 8))
}
}
}