desc:调整代码

main
xiaowusky 2 years ago
parent 551239f1ab
commit 7c3d2f2e4a

@ -62,8 +62,8 @@ fun getGasHighThreshold(gasType: String): Float {
fun getGasUnit(gasType: String): String {
return when (gasType.uppercase()) {
"CO" -> "ppm"
"CH4" -> "%VOL"
"O2" -> "%LEL"
"O2" -> "%VOL"
"CH4" -> "%LEL"
"H2S" -> "ppm"
else -> ""
}

@ -35,6 +35,7 @@ object ParseHelper {
DevicesApi()
}
private val gasMap = hashMapOf<String, String>()
private val gasUnitMap = hashMapOf<String, String>()
fun parse(it: ByteArray) {
@ -82,25 +83,20 @@ object ParseHelper {
val unitToLong: Long = HexUtils.hexToLong(unitHex)
// 10000000000000
var unitBinaryString = java.lang.Long.toBinaryString(unitToLong)
LogUtils.v("receive msg $gasType, unitBinaryString 1 = $unitBinaryString")
if (unitBinaryString.length < 16) {
val offset = 16 - unitBinaryString.length
repeat(offset) {
unitBinaryString = "0" + unitBinaryString;
}
}
LogUtils.v("receive msg, unitBinaryString 2 = $unitBinaryString")
unitBinaryString = unitBinaryString.reversed()
LogUtils.v("receive msg, unitBinaryString 3 = $unitBinaryString")
//小数点
val subSequence = unitBinaryString.subSequence(8, 12).reversed()
val pointNum = ResponseHelper.getPointNum(subSequence)
LogUtils.v("receive msg, 小数点 = $subSequence$pointNum")
// 单位
val subSequence2 = unitBinaryString.subSequence(12, 16).reversed()
val unit = ResponseHelper.getGasUnit(subSequence2)
LogUtils.v("receive msg, 单位 = $subSequence2$unit")
val c17 = unitBinaryString[6].toString()
val c18 = unitBinaryString[7].toString()
@ -108,18 +104,24 @@ object ParseHelper {
val valueHex: String = HexUtils.byteArrToHex(it, 5, 5 + 2)
val valueHexLong: Long = HexUtils.hexToLong(valueHex)
// 浓度
val value =
var value =
c18.plus(c17).plus(java.lang.Long.toBinaryString(valueHexLong)).toInt(2) / 10.0.pow(
pointNum
)
LogUtils.v("receive msg $gasType, 浓度 = $c18$c17, | $valueHex, $valueHexLong, | $value")
if (gasType.uppercase() == "CH4") {
value = value / 20.0 * 10000
LogUtils.v("receive msg $gasType, 浓度 = $c18$c17, | $valueHex, $valueHexLong, | $value ppm")
} else {
LogUtils.v("receive msg $gasType, 浓度 = $c18$c17, | $valueHex, $valueHexLong, | $value $unit")
}
// 量程
val rangHex: String = HexUtils.byteArrToHex(it, 11, 11 + 2)
val rangHexLong: Long = HexUtils.hexToLong(rangHex)
LogUtils.v("receive msg, 量程 = $rangHex$rangHexLong")
// 存储临时数据
gasMap[gasType] = ": $value $unit"
gasMap[gasType] = "$value "
gasUnitMap[gasType] = "$unit"
// 插入
insertGasData(gasType, value, unit, rangHexLong)
}
@ -132,16 +134,16 @@ object ParseHelper {
val builder: StringBuilder = java.lang.StringBuilder()
valueCo?.let {
builder.append(CO).append(valueCo).append("@")
builder.append(CO).append(" : ").append(valueCo).append(gasUnitMap[CO]).append("@")
}
valueCH4?.let {
builder.append(CH4).append(valueCH4).append("@")
builder.append(CH4).append(" : ").append(valueCH4).append("ppm").append("@")
}
valueO2?.let {
builder.append(O2).append(valueO2).append("@")
builder.append(O2).append(" : ").append(valueO2).append(gasUnitMap[O2]).append("@")
}
valueH2S?.let {
builder.append(H2S).append(valueH2S)
builder.append(H2S).append(" : ").append(valueH2S).append(gasUnitMap[H2S])
}
TxtOverlay.setShowTip(builder.toString())
}
@ -179,6 +181,9 @@ object ParseHelper {
max
)
)
} else {
warning.gasValue = value
warningDao.update(warning)
}
} else {
warning?.let {

Loading…
Cancel
Save