diff --git a/app/src/main/java/com/casic/app/safetreecontroller/extensions/ByteArray.kt b/app/src/main/java/com/casic/app/safetreecontroller/extensions/ByteArray.kt index cd72aed..b1cf8ae 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/extensions/ByteArray.kt @@ -27,36 +27,20 @@ return builder.toString() } -fun ByteArray.handleGasConcentration(): Long { +fun ByteArray.handleGasConcentration(): Int { /** * [1, 0, 0, 0, 13, 14] * */ //负值需要计算补码 - val x = if (this[1] < 0) { - this[1].toInt() and 0xFF - } else { - this[1].toInt() - } - val y = if (this[2] < 0) { - this[2].toInt() and 0xFF - } else { - this[2].toInt() - } - val z = if (this[3] < 0) { - this[3].toInt() and 0xFF - } else { - this[3].toInt() - } - return (x * 65536 + y * 256 + z).toLong() + val x = this[1] and 0xFF.toByte() + val y = this[2] and 0xFF.toByte() + val z = this[3] and 0xFF.toByte() + return (x * 65536 + y * 256 + z) } fun ByteArray.handleCpuTemperature(): Float { //[1, -128, 24] val x = this[0] and 0xFF.toByte() - val y = if (this[1] < 0) { - this[1].toInt() and 0xFF - } else { - this[1].toInt() - } + val y = this[1] and 0xFF.toByte() return (x * 256 + y) / 10f } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/extensions/ByteArray.kt b/app/src/main/java/com/casic/app/safetreecontroller/extensions/ByteArray.kt index cd72aed..b1cf8ae 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/extensions/ByteArray.kt @@ -27,36 +27,20 @@ return builder.toString() } -fun ByteArray.handleGasConcentration(): Long { +fun ByteArray.handleGasConcentration(): Int { /** * [1, 0, 0, 0, 13, 14] * */ //负值需要计算补码 - val x = if (this[1] < 0) { - this[1].toInt() and 0xFF - } else { - this[1].toInt() - } - val y = if (this[2] < 0) { - this[2].toInt() and 0xFF - } else { - this[2].toInt() - } - val z = if (this[3] < 0) { - this[3].toInt() and 0xFF - } else { - this[3].toInt() - } - return (x * 65536 + y * 256 + z).toLong() + val x = this[1] and 0xFF.toByte() + val y = this[2] and 0xFF.toByte() + val z = this[3] and 0xFF.toByte() + return (x * 65536 + y * 256 + z) } fun ByteArray.handleCpuTemperature(): Float { //[1, -128, 24] val x = this[0] and 0xFF.toByte() - val y = if (this[1] < 0) { - this[1].toInt() and 0xFF - } else { - this[1].toInt() - } + val y = this[1] and 0xFF.toByte() return (x * 256 + y) / 10f } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index 3c0fb7f..f88cda7 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -155,7 +155,7 @@ LocaleConstant.QUERY_METHANE_THRESHOLD_RESPONSE_CODE -> { isGetThreshold = true - val ppm = msg.obj as Long + val ppm = msg.obj as Int val threshold = "%.2f".format(ppm / 10000f) SaveKeyValues.putValue( LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold.toFloat() @@ -189,7 +189,7 @@ } LocaleConstant.QUERY_METHANE_RESPONSE_CODE -> { - val ppm = msg.obj as Long + val ppm = msg.obj as Int //通常,对于气体在空气中的浓度,我们可以认为100000ppm大约等于200%LEL val formatValue = "%.2f".format(ppm / 500f) binding.currentGasValueView.text = "当前燃气浓度:${formatValue}%LEL"