|  |  |  | @ -3,10 +3,19 @@ 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.Gas | 
		
	
		
			
				|  |  |  |  | import com.common.commonlib.db.entity.GasType | 
		
	
		
			
				|  |  |  |  | import com.common.commonlib.utils.MMKVUtils | 
		
	
		
			
				|  |  |  |  | import com.common.serialport.utils.HexUtils | 
		
	
		
			
				|  |  |  |  | import com.yinuo.library.vlc.TxtOverlay | 
		
	
		
			
				|  |  |  |  | import com.yinuo.library.vlc.utils.LogUtils | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.constant.GAS_CLOUD_UPLOAD_SIZE_ONCE | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.net.DevicesApi | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.CH4 | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.CO | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.GasPortStatus | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.H2S | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.O2 | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.ResponseHelper | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.port.cmd.getGasTypeByCode | 
		
	
		
			
				|  |  |  |  | import kotlinx.coroutines.DelicateCoroutinesApi | 
		
	
	
		
			
				
					|  |  |  | @ -18,48 +27,45 @@ object ParseHelper { | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     private val mPortRunnable = hashMapOf<Int, FlagRunnable>() | 
		
	
		
			
				|  |  |  |  |     private val mHandler = Handler(Looper.getMainLooper()) | 
		
	
		
			
				|  |  |  |  |     private val tempGasData = mutableListOf<Gas>() | 
		
	
		
			
				|  |  |  |  |     private val devicesApi by lazy { | 
		
	
		
			
				|  |  |  |  |         DevicesApi() | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |     private val gasMap = hashMapOf<String, String>() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     fun parse(it: ByteArray) { | 
		
	
		
			
				|  |  |  |  |         try { | 
		
	
		
			
				|  |  |  |  |             if (it.isNotEmpty() && it.size >= GasUtils.FULL_MSG_SIZE) { | 
		
	
		
			
				|  |  |  |  |                 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) | 
		
	
		
			
				|  |  |  |  |                     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         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") | 
		
	
		
			
				|  |  |  |  |                     else -> { | 
		
	
		
			
				|  |  |  |  |                         LogUtils.v("unKnow status, do nothing") | 
		
	
		
			
				|  |  |  |  |                     } | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |         } catch (e: Exception) { | 
		
	
		
			
				|  |  |  |  |             e.printStackTrace() | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -71,7 +77,7 @@ object ParseHelper { | 
		
	
		
			
				|  |  |  |  |         LogUtils.v("receive msg $gasType, unitBinaryString 1 = $unitBinaryString") | 
		
	
		
			
				|  |  |  |  |         if (unitBinaryString.length < 16) { | 
		
	
		
			
				|  |  |  |  |             val offset = 16 - unitBinaryString.length | 
		
	
		
			
				|  |  |  |  |             repeat(offset){ | 
		
	
		
			
				|  |  |  |  |             repeat(offset) { | 
		
	
		
			
				|  |  |  |  |                 unitBinaryString = "0" + unitBinaryString; | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
	
		
			
				
					|  |  |  | @ -103,6 +109,59 @@ object ParseHelper { | 
		
	
		
			
				|  |  |  |  |         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" | 
		
	
		
			
				|  |  |  |  |         // 插入 | 
		
	
		
			
				|  |  |  |  |         insertGasData(gasType, value, unit, rangHexLong) | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     private fun setOverlayData() { | 
		
	
		
			
				|  |  |  |  |         val valueCo = gasMap[CO] | 
		
	
		
			
				|  |  |  |  |         val valueCH4 = gasMap[CH4] | 
		
	
		
			
				|  |  |  |  |         val valueO2 = gasMap[O2] | 
		
	
		
			
				|  |  |  |  |         val valueH2S = gasMap[H2S] | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         val builder: StringBuilder = java.lang.StringBuilder() | 
		
	
		
			
				|  |  |  |  |         valueCo?.let { | 
		
	
		
			
				|  |  |  |  |             builder.append(CO).append(valueCo).append("@") | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         valueCH4?.let { | 
		
	
		
			
				|  |  |  |  |             builder.append(CH4).append(valueCH4).append("@") | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         valueO2?.let { | 
		
	
		
			
				|  |  |  |  |             builder.append(O2).append(valueO2).append("@") | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         valueH2S?.let { | 
		
	
		
			
				|  |  |  |  |             builder.append(H2S).append(valueH2S) | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         TxtOverlay.setShowTip(builder.toString()) | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     /** | 
		
	
		
			
				|  |  |  |  |      * 插入气体数据 | 
		
	
		
			
				|  |  |  |  |      */ | 
		
	
		
			
				|  |  |  |  |     private fun insertGasData(gasType: String, value: Double, unit: String, rangHexLong: Long) { | 
		
	
		
			
				|  |  |  |  |         GlobalScope.launch { | 
		
	
		
			
				|  |  |  |  |             // 设置水印数据 | 
		
	
		
			
				|  |  |  |  |             setOverlayData() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |             // 构造气体数据 | 
		
	
		
			
				|  |  |  |  |             val timeMillis = System.currentTimeMillis() | 
		
	
		
			
				|  |  |  |  |             val gas = Gas(timeMillis, gasType, value, unit) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |             val gasDao = DBUtils.gasDao() | 
		
	
		
			
				|  |  |  |  |             gasDao.insert(gas) | 
		
	
		
			
				|  |  |  |  |             // 存储范围最大值 | 
		
	
		
			
				|  |  |  |  |             MMKVUtils.put("range_${gasType}", "0$unit~$rangHexLong$unit") | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |             // 上传实时气体数据 | 
		
	
		
			
				|  |  |  |  |             tempGasData.add(gas) | 
		
	
		
			
				|  |  |  |  |             if (tempGasData.size > GAS_CLOUD_UPLOAD_SIZE_ONCE) { | 
		
	
		
			
				|  |  |  |  |                 devicesApi.uploadGasData(tempGasData) | 
		
	
		
			
				|  |  |  |  |                 tempGasData.clear() | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     /** | 
		
	
	
		
			
				
					|  |  |  | @ -113,6 +172,7 @@ object ParseHelper { | 
		
	
		
			
				|  |  |  |  |         GlobalScope.launch { | 
		
	
		
			
				|  |  |  |  |             val typeDao = DBUtils.gasTypeDao() | 
		
	
		
			
				|  |  |  |  |             val gasType = typeDao.getByName(gasName) | 
		
	
		
			
				|  |  |  |  |             gasType?.status = status | 
		
	
		
			
				|  |  |  |  |             typeDao.insert(gasType ?: GasType(gasName, status)) | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
	
		
			
				
					|  |  |  | 
 |