1
+ package com.github.eltonvs.obd.command.control
2
+
3
+ import com.github.eltonvs.obd.command.ObdCommand
4
+ import com.github.eltonvs.obd.command.ObdRawResponse
5
+
6
+
7
+ class AvailablePIDsCommand (val range : AvailablePIDsRanges ) : ObdCommand() {
8
+ override val tag = " AVAILABLE_COMMANDS_${range.name} "
9
+ override val name = " Available Commands - ${range.displayName} "
10
+ override val mode = " 01"
11
+ override val pid = range.pid
12
+
13
+ override val defaultUnit = " "
14
+ override val handler = { it: ObdRawResponse ->
15
+ parsePIDs(it.processedValue).joinToString(" ," ) { " %02X" .format(it) }
16
+ }
17
+
18
+ private fun parsePIDs (rawValue : String ): IntArray {
19
+ val value = rawValue.toLong(radix = 16 )
20
+ val initialPID = range.pid.toInt(radix = 16 )
21
+ return (1 .. 33 ).fold(listOf<Int >()) { acc, i ->
22
+ if (getBit(value, i) == 1 ) acc + listOf (i + initialPID)
23
+ else acc
24
+ }.toIntArray()
25
+ }
26
+
27
+ private fun getBit (number : Long , position : Int ) = (number shr (32 - position) and 1 ).toInt()
28
+
29
+ enum class AvailablePIDsRanges (val displayName : String , internal val pid : String ) {
30
+ PIDS_01_TO_20 (" PIDs from 01 to 20" , " 00" ),
31
+ PIDS_21_TO_40 (" PIDs from 21 to 40" , " 20" ),
32
+ PIDS_41_TO_60 (" PIDs from 41 to 60" , " 40" ),
33
+ PIDS_61_TO_80 (" PIDs from 61 to 80" , " 60" ),
34
+ PIDS_81_TO_100 (" PIDs from 81 to 100" , " 80" )
35
+ }
36
+ }
0 commit comments