|
|
@@ -1,23 +1,11 @@
|
|
|
-package com.fenbide.utilities.peripheral
|
|
|
+package com.example.whatever.modbus
|
|
|
|
|
|
import android.content.Context
|
|
|
-import com.example.whatever.modbus.FRAME_TYPE_WRITE_FILE
|
|
|
-import com.example.whatever.modbus.FRAME_TYPE_WRITE_MULTI
|
|
|
-import com.example.whatever.modbus.MBFrame
|
|
|
-import com.example.whatever.modbus.MODBUS_SLAVE_STATUS__FULL
|
|
|
-import com.example.whatever.modbus.MODBUS_SLAVE_STATUS__NO_CHANGE
|
|
|
-import com.example.whatever.modbus.ModBusManager
|
|
|
-import com.example.whatever.modbus.PortManager
|
|
|
+import com.example.whatever.extentions.toHexString
|
|
|
import com.example.whatever.util.Executor
|
|
|
-import com.fenbide.utilities.*
|
|
|
-import java.io.File
|
|
|
-import java.math.BigInteger
|
|
|
+import com.example.whatever.util.log.LogUtil
|
|
|
import java.util.*
|
|
|
-import java.util.concurrent.ConcurrentHashMap
|
|
|
import java.util.concurrent.Executors
|
|
|
-import java.util.concurrent.TimeUnit
|
|
|
-import java.util.concurrent.atomic.AtomicInteger
|
|
|
-import kotlin.math.ceil
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -38,8 +26,6 @@ object ModBusController {
|
|
|
|
|
|
private val listeners = ArrayList<StatusListener>()
|
|
|
|
|
|
- private val fullPercentListeners = ArrayList<FullPercentListener>()
|
|
|
-
|
|
|
// 是否中断读取桶状态
|
|
|
private var interruptReadTrashBinStatus: ArrayList<Boolean> = ArrayList()
|
|
|
|
|
|
@@ -52,11 +38,6 @@ object ModBusController {
|
|
|
|
|
|
data class FullPercent(val percent: Int, val checkByLaser: Boolean)
|
|
|
|
|
|
- class FullPercentListener(
|
|
|
- val key: Any,
|
|
|
- val listener: (List<FullPercent>) -> Unit
|
|
|
- )
|
|
|
-
|
|
|
class StatusListener(
|
|
|
val key: Any,
|
|
|
val listener: (List<Int>) -> Unit,
|
|
|
@@ -80,6 +61,9 @@ object ModBusController {
|
|
|
interruptReadTrashBinStatus
|
|
|
}, { res ->
|
|
|
// // Logger.d("ModbusController", "res: ${res.map { it.toHexString() }}")
|
|
|
+ LogUtil.i("haha : ModbusController : $res")
|
|
|
+ res.forEach { data -> println("xixi : ${data.toHexString()}") }
|
|
|
+ println()
|
|
|
// 第 0 个字节是 从机地址,第 1 个字节是 功能码,第 2 个字节是 字节数
|
|
|
// 瓶类计数
|
|
|
val bottles = res.map { bytes ->
|
|
|
@@ -117,30 +101,6 @@ object ModBusController {
|
|
|
l.listener(temperatures)
|
|
|
}
|
|
|
}
|
|
|
- if (fullPercentListeners.isNotEmpty()) {
|
|
|
- // 满溢程度
|
|
|
- val fullPercents = res.mapIndexed { index, bytes ->
|
|
|
- var percent = MODBUS_SLAVE_STATUS__NO_CHANGE
|
|
|
- var checkByLaser = false
|
|
|
- if (bytes.size > 3) {
|
|
|
- if (percent < 0 || percent >= 255) {
|
|
|
- checkByLaser = false
|
|
|
- percent = if (status[index] and MODBUS_SLAVE_STATUS__FULL > 0) {
|
|
|
- 100
|
|
|
- } else {
|
|
|
- 0
|
|
|
- }
|
|
|
- } else {
|
|
|
- checkByLaser = true
|
|
|
- percent = (bytes[3].toUByte().toInt())
|
|
|
- }
|
|
|
- }
|
|
|
- FullPercent(percent, checkByLaser)
|
|
|
- }
|
|
|
- for (l in fullPercentListeners) {
|
|
|
- l.listener(fullPercents)
|
|
|
- }
|
|
|
- }
|
|
|
}, 1000)
|
|
|
?.also {
|
|
|
modBusManager = it
|
|
|
@@ -151,19 +111,6 @@ object ModBusController {
|
|
|
?.start()
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 满溢百分比(0-100, -1表示无效值)
|
|
|
- */
|
|
|
- fun registerFullPercentListener(key: Any, listener: (List<FullPercent>) -> Unit) {
|
|
|
- fullPercentListeners.add(FullPercentListener(key, listener))
|
|
|
- }
|
|
|
-
|
|
|
- fun registerBottleListener(key: Any, listener: (List<Int>) -> Unit) {
|
|
|
- listeners.add(StatusListener(key, listener, LISTENER_TYPE_BOTTLE))
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 回调的重量单位:0.01kg、10g
|
|
|
*/
|
|
|
@@ -188,162 +135,6 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 设置垃圾桶类别--智能厨余
|
|
|
- */
|
|
|
- fun setTrashBinTypeIntelliKitchen(index: Int) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.WRITE_TRASH_BIN_TYPE_INTELLI_KITCHEN) {
|
|
|
- if (it.isNotEmpty()) {
|
|
|
-// // Logger.d("AI_SETTINGS", "设置垃圾桶[${index}]类别--智能厨余--成功")
|
|
|
- } else {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置垃圾桶[${index}]类别--智能厨余--失败")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置氨气浓度报警值
|
|
|
- */
|
|
|
- fun writeNh3AlarmValue(index: Int, value: Int) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.writeNh3AlarmValue(value)) {
|
|
|
- if (it.isNotEmpty()) {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置氨气浓度报警值: ${value}--成功")
|
|
|
- } else {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置氨气浓度报警值: ${value}--失败")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置硫化氢浓度报警值
|
|
|
- */
|
|
|
- fun writeH2sAlarmValue(index: Int, value: Int) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.writeH2sAlarmValue(value)) {
|
|
|
- if (it.isNotEmpty()) {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置硫化氢浓度报警值: ${value}--成功")
|
|
|
- } else {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置硫化氢浓度报警值: ${value}--失败")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置新风系统强风时长
|
|
|
- */
|
|
|
- fun writeFierceWindSeconds(index: Int, value: Int) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.writeFierceWindSeconds(value)) {
|
|
|
- if (it.isNotEmpty()) {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置新风系统强风时长: ${value}--成功")
|
|
|
- } else {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置新风系统强风时长: ${value}--失败")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置新风系统弱风时长
|
|
|
- */
|
|
|
- fun writeWeakWindSeconds(index: Int, value: Int) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.writeWeakWindSeconds(value)) {
|
|
|
- if (it.isNotEmpty()) {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置新风系统弱风时长: ${value}--成功")
|
|
|
- } else {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置新风系统弱风时长: ${value}--失败")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置消毒喷雾间隔时长
|
|
|
- */
|
|
|
- fun writeSprayIntervalSeconds(index: Int, value: Int) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.writeSprayIntervalSeconds(value)) {
|
|
|
- if (it.isNotEmpty()) {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置消毒喷雾间隔时长: ${value}--成功")
|
|
|
- } else {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置消毒喷雾间隔时长: ${value}--失败")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置消毒喷雾单次时长
|
|
|
- */
|
|
|
- fun writeSprayRunningSeconds(index: Int, value: Int) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.writeSprayRunningSeconds(value)) {
|
|
|
- if (it.isNotEmpty()) {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置消毒喷雾单次时长: ${value}--成功")
|
|
|
- } else {
|
|
|
- // // Logger.d("AI_SETTINGS", "设置消毒喷雾单次时长: ${value}--失败")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 校准称重传感器
|
|
|
- */
|
|
|
- fun calibrateWeigher(index: Int, v0: Int, v10: Int, callback: (success: Boolean) -> Unit) {
|
|
|
- val v0HH = (v0 shr 24).toByte()
|
|
|
- val v0HL = (v0 shr 16).toByte()
|
|
|
- val v0LH = (v0 shr 8).toByte()
|
|
|
- val v0LL = (v0 % 256).toByte()
|
|
|
- val v10HH = (v10 shr 24).toByte()
|
|
|
- val v10HL = (v10 shr 16).toByte()
|
|
|
- val v10LH = (v10 shr 8).toByte()
|
|
|
- val v10LL = (v10 % 256).toByte()
|
|
|
- val frame = MBFrame(
|
|
|
- FRAME_TYPE_WRITE_MULTI,
|
|
|
- byteArrayOf(
|
|
|
- 0x01, 0x02, 0x00, 0x04, 0x08,
|
|
|
- v0HH, v0HL, v0LH, v0LL,
|
|
|
- v10HH, v10HL, v10LH, v10LL
|
|
|
- )
|
|
|
- )
|
|
|
- modBusManager?.sendTo(index, frame) { bytes ->
|
|
|
- callback(bytes.isNotEmpty())
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 打开清运门
|
|
|
- */
|
|
|
- fun openCleaningDoor(index: Int, onComplete: ((success: Boolean) -> Unit)? = null) {
|
|
|
- // // Logger.d("CLEANING_DOOR", "下发清运门开启指令:${index}")
|
|
|
- modBusManager?.sendTo(index, MBFrame.WRITE_CLEANING_DOOR_NON_OPENABLE) {
|
|
|
- if (it.isEmpty()) {
|
|
|
- // Tip.toast("电子锁[${index}]标记位重置失败!")
|
|
|
- onComplete?.invoke(false)
|
|
|
- return@sendTo
|
|
|
- }
|
|
|
- Executor.delayOnIO({
|
|
|
- modBusManager?.sendTo(index, MBFrame.WRITE_CLEANING_DOOR_OPENABLE) { it2 ->
|
|
|
- if (it2.isEmpty()) {
|
|
|
- // Tip.toast("清运门[${index}]开启指令发送失败!")
|
|
|
- }
|
|
|
- onComplete?.invoke(it2.isNotEmpty())
|
|
|
- }
|
|
|
- }, 1000)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 启用瓶类计数
|
|
|
- */
|
|
|
- fun enableBottleCounter(
|
|
|
- index: Int,
|
|
|
- enable: Boolean = true,
|
|
|
- callback: ((success: Boolean) -> Unit)? = null
|
|
|
- ) {
|
|
|
- val frame = if (enable) MBFrame.ENABLE_BOTTLE_COUNTER else MBFrame.DISABLE_BOTTLE_COUNTER
|
|
|
- modBusManager?.sendTo(index, frame) {
|
|
|
- if (it.isEmpty()) {
|
|
|
- // Tip.toast("舱门[${index}]设置瓶类计数指令失败!")
|
|
|
- }
|
|
|
- callback?.invoke(it.isNotEmpty())
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 开门
|
|
|
*/
|
|
|
@@ -392,89 +183,6 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 打开排风扇
|
|
|
- * 目前用户拍照照明灯
|
|
|
- */
|
|
|
- fun openFun(index: Int) {
|
|
|
- // Logger.d("TRASH_BIN", "下发打开排风扇指令:${index}")
|
|
|
- modBusManager?.sendTo(index, MBFrame.WRITE_FAN_ON) {
|
|
|
- if (it.isEmpty()) {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 打开排风扇
|
|
|
- * 目前用户拍照照明灯
|
|
|
- */
|
|
|
- fun closeFun(index: Int) {
|
|
|
- // Logger.d("TRASH_BIN", "下发关闭排风扇指令:${index}")
|
|
|
- modBusManager?.sendTo(index, MBFrame.WRITE_FAN_OFF) {
|
|
|
- if (it.isEmpty()) {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-// /**
|
|
|
-// * 关门
|
|
|
-// */
|
|
|
-// fun closeDoor(index: Int) {
|
|
|
-// // Logger.d("TRASH_BIN", "下发关门指令:${index}")
|
|
|
-// modBusManager?.sendTo(index, MBFrame.WRITE_CLOSE) {
|
|
|
-// if (it.isEmpty()) {
|
|
|
-// // Tip.toast("舱门[${index}]关闭失败!")
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- /**
|
|
|
- * 读取扫码头数据
|
|
|
- * @param index 从机编号
|
|
|
- */
|
|
|
- fun readScannerData(index: Int, success: (ByteArray) -> Unit) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.READ_CANNER) {
|
|
|
- if (it.isEmpty()) {
|
|
|
- // Tip.toast("扫码数据读取失败!")
|
|
|
- return@sendTo
|
|
|
- }
|
|
|
- val length = it[2].toInt() // 扫码数据长度
|
|
|
- if (length == 0) {
|
|
|
- // Tip.toast("扫码数据长度为 0 !: " + it.toHexString())
|
|
|
- return@sendTo
|
|
|
- }
|
|
|
- if (it.size != length + 5) {
|
|
|
- // Tip.toast("扫码数据长度不匹配: " + it.toHexString())
|
|
|
- return@sendTo
|
|
|
- }
|
|
|
- success(it.copyOfRange(3, 3 + length))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 读刷卡板
|
|
|
- * @param done 完成回调
|
|
|
- */
|
|
|
- fun readCardBoardData(index: Int, success: (res: ByteArray) -> Unit) {
|
|
|
- modBusManager?.sendTo(index, MBFrame.READ_CARD_BOARD) {
|
|
|
- // Logger.d("CARD_BOARD", "刷卡板数据(原始):${it.toHexString()}")
|
|
|
- if (it.isEmpty()) {
|
|
|
- // Tip.toast("刷卡板数据读取失败!")
|
|
|
- return@sendTo
|
|
|
- }
|
|
|
- val length = it[2].toInt() // 扫码数据长度
|
|
|
- if (length == 0) {
|
|
|
-// // Tip.toast("刷卡板数据长度为 0 !: " + it.toHexString())
|
|
|
- return@sendTo
|
|
|
- }
|
|
|
- if (it.size != length + 5) {
|
|
|
- // Tip.toast("刷卡板数据长度不匹配: " + it.toHexString())
|
|
|
- return@sendTo
|
|
|
- }
|
|
|
- success(it.copyOfRange(3, 3 + length))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 开关灯
|
|
|
*/
|