|  |  |  | @ -0,0 +1,144 @@ | 
		
	
		
			
				|  |  |  |  | package com.yinuo.safetywatcher.watcher.port | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | import android.os.Handler | 
		
	
		
			
				|  |  |  |  | import android.os.Looper | 
		
	
		
			
				|  |  |  |  | import com.common.commonlib.db.DBUtils | 
		
	
		
			
				|  |  |  |  | import com.common.commonlib.db.entity.GasType | 
		
	
		
			
				|  |  |  |  | import com.common.serialport.utils.HexUtils | 
		
	
		
			
				|  |  |  |  | import com.yinuo.library.vlc.utils.LogUtils | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.GasPortStatus | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.ResponseHelper | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.getGasTypeByCode | 
		
	
		
			
				|  |  |  |  | import kotlinx.coroutines.DelicateCoroutinesApi | 
		
	
		
			
				|  |  |  |  | import kotlinx.coroutines.GlobalScope | 
		
	
		
			
				|  |  |  |  | import kotlinx.coroutines.launch | 
		
	
		
			
				|  |  |  |  | import kotlin.math.pow | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | object ParseHelper { | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     private val mPortRunnable = hashMapOf<Int, FlagRunnable>() | 
		
	
		
			
				|  |  |  |  |     private val mHandler = Handler(Looper.getMainLooper()) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     fun parse(it: ByteArray) { | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         if (it.isNotEmpty() && it.size >= GasUtils.FULL_MSG_SIZE) { | 
		
	
		
			
				|  |  |  |  | //                    val checkSum = getCheckSum(it) | 
		
	
		
			
				|  |  |  |  | //                    // 校验通过 | 
		
	
		
			
				|  |  |  |  | //                    if (checkSum == it[8]) { | 
		
	
		
			
				|  |  |  |  | //                        val gasName = it[1] | 
		
	
		
			
				|  |  |  |  | //                        val gasUnit = it[2] | 
		
	
		
			
				|  |  |  |  | //                        val gasValue: Double = (it[4] * 256 + it[5]) / 100.00 | 
		
	
		
			
				|  |  |  |  | //                        setFlag(0, gasName, "/dev/tyyS0") | 
		
	
		
			
				|  |  |  |  | // | 
		
	
		
			
				|  |  |  |  | //                        // TODO | 
		
	
		
			
				|  |  |  |  | //                        TxtOverlay.setShowTip("") | 
		
	
		
			
				|  |  |  |  | //                    } | 
		
	
		
			
				|  |  |  |  |             LogUtils.v("receive msg, ${HexUtils.byteArrToHex(it)}") | 
		
	
		
			
				|  |  |  |  |             val gasIndex = it[0].toInt() | 
		
	
		
			
				|  |  |  |  |             val status = it[14].toInt() | 
		
	
		
			
				|  |  |  |  |             val gasType = getGasTypeByCode(it[19].toInt()) | 
		
	
		
			
				|  |  |  |  |             when (status) { | 
		
	
		
			
				|  |  |  |  |                 // 预热 | 
		
	
		
			
				|  |  |  |  |                 0 -> { | 
		
	
		
			
				|  |  |  |  |                     updateGasTypeDb(gasType, GasPortStatus.PRE_HOT) | 
		
	
		
			
				|  |  |  |  |                     setFlag(gasIndex, gasType) | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |                 // 正常 | 
		
	
		
			
				|  |  |  |  |                 1 -> { | 
		
	
		
			
				|  |  |  |  |                     updateGasTypeDb(gasType, GasPortStatus.OK) | 
		
	
		
			
				|  |  |  |  |                     setFlag(gasIndex, gasType) | 
		
	
		
			
				|  |  |  |  |                     parseGasData(gasType, it) | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |                 // 故障 | 
		
	
		
			
				|  |  |  |  |                 2, 3, 7 -> { | 
		
	
		
			
				|  |  |  |  |                     updateGasTypeDb(gasType, GasPortStatus.ERROR) | 
		
	
		
			
				|  |  |  |  |                     setFlag(gasIndex, gasType) | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |                 else -> { | 
		
	
		
			
				|  |  |  |  |                     LogUtils.v("unKnow status, do nothing") | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     private fun parseGasData(gasType: String, it: ByteArray) { | 
		
	
		
			
				|  |  |  |  |         val unitHex: String = HexUtils.byteArrToHex(it, 3, 3 + 2) | 
		
	
		
			
				|  |  |  |  |         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) | 
		
	
		
			
				|  |  |  |  |         val pointNum = ResponseHelper.getPointNum(subSequence) | 
		
	
		
			
				|  |  |  |  |         LogUtils.v("receive msg, 小数点 = $subSequence,$pointNum") | 
		
	
		
			
				|  |  |  |  |         // 单位 | 
		
	
		
			
				|  |  |  |  |         val subSequence2 = unitBinaryString.subSequence(12, 16) | 
		
	
		
			
				|  |  |  |  |         val unit = ResponseHelper.getGasUnit(subSequence2) | 
		
	
		
			
				|  |  |  |  |         LogUtils.v("receive msg, 单位 = $subSequence2,$unit") | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         val c17 = unitBinaryString[6].toString() | 
		
	
		
			
				|  |  |  |  |         val c18 = unitBinaryString[7].toString() | 
		
	
		
			
				|  |  |  |  |         // 气体浓度 | 
		
	
		
			
				|  |  |  |  |         val valueHex: String = HexUtils.byteArrToHex(it, 5, 5 + 2) | 
		
	
		
			
				|  |  |  |  |         val valueHexLong: Long = HexUtils.hexToLong(valueHex) | 
		
	
		
			
				|  |  |  |  |         // 浓度 | 
		
	
		
			
				|  |  |  |  |         val value = | 
		
	
		
			
				|  |  |  |  |             c18.plus(c17).plus(java.lang.Long.toBinaryString(valueHexLong)).toInt(2) / 10.0.pow( | 
		
	
		
			
				|  |  |  |  |                 pointNum | 
		
	
		
			
				|  |  |  |  |             ) | 
		
	
		
			
				|  |  |  |  |         LogUtils.v("receive msg, 浓度 = $c18,$c17, | $valueHex, $valueHexLong, | $value") | 
		
	
		
			
				|  |  |  |  |         // 量程 | 
		
	
		
			
				|  |  |  |  |         val rangHex: String = HexUtils.byteArrToHex(it, 11, 11 + 2) | 
		
	
		
			
				|  |  |  |  |         val rangHexLong: Long = HexUtils.hexToLong(rangHex) | 
		
	
		
			
				|  |  |  |  |         LogUtils.v("receive msg, 量程 = $rangHex,$rangHexLong") | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     /** | 
		
	
		
			
				|  |  |  |  |      * 更新数据库 | 
		
	
		
			
				|  |  |  |  |      */ | 
		
	
		
			
				|  |  |  |  |     @OptIn(DelicateCoroutinesApi::class) | 
		
	
		
			
				|  |  |  |  |     private fun updateGasTypeDb(gasName: String, status: Int) { | 
		
	
		
			
				|  |  |  |  |         GlobalScope.launch { | 
		
	
		
			
				|  |  |  |  |             val typeDao = DBUtils.gasTypeDao() | 
		
	
		
			
				|  |  |  |  |             val gasType = typeDao.getByName(gasName) | 
		
	
		
			
				|  |  |  |  |             typeDao.insert(gasType ?: GasType(gasName, status)) | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     /** | 
		
	
		
			
				|  |  |  |  |      * 启动离线检查 | 
		
	
		
			
				|  |  |  |  |      */ | 
		
	
		
			
				|  |  |  |  |     private fun setFlag(index: Int, gasName: String) { | 
		
	
		
			
				|  |  |  |  |         var flagRunnable = mPortRunnable[index] | 
		
	
		
			
				|  |  |  |  |         if (flagRunnable != null) { | 
		
	
		
			
				|  |  |  |  |             mHandler.removeCallbacks(flagRunnable) | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         flagRunnable = FlagRunnable(gasName) | 
		
	
		
			
				|  |  |  |  |         mPortRunnable[index] = flagRunnable | 
		
	
		
			
				|  |  |  |  |         // 如果一段时间内没有收到消息,认为连接断开 | 
		
	
		
			
				|  |  |  |  |         mHandler.postDelayed(flagRunnable, GasUtils.CHECK_TIME) | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     /** | 
		
	
		
			
				|  |  |  |  |      * 离线检查 | 
		
	
		
			
				|  |  |  |  |      */ | 
		
	
		
			
				|  |  |  |  |     class FlagRunnable( | 
		
	
		
			
				|  |  |  |  |         private val gasName: String | 
		
	
		
			
				|  |  |  |  |     ) : Runnable { | 
		
	
		
			
				|  |  |  |  |         override fun run() { | 
		
	
		
			
				|  |  |  |  |             updateGasTypeDb(gasName, GasPortStatus.OUTLINE) | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | } |