|
|
@@ -16,11 +16,7 @@ object ModBusController {
|
|
|
*/
|
|
|
var deviceList: MutableList<DeviceBean> = mutableListOf()
|
|
|
|
|
|
- private const val LISTENER_TYPE_BOTTLE = 1
|
|
|
- private const val LISTENER_TYPE_WEIGHT = 2
|
|
|
private const val LISTENER_TYPE_STATUS = 3
|
|
|
- private const val LISTENER_TYPE_TEMPERA = 4
|
|
|
-
|
|
|
|
|
|
// 主控板管理器
|
|
|
private var modBusManager: ModBusManager? = null
|
|
|
@@ -30,8 +26,8 @@ object ModBusController {
|
|
|
|
|
|
private val listeners = ArrayList<StatusListener>()
|
|
|
|
|
|
- // 是否中断读取桶状态
|
|
|
- private var interruptReadTrashBinStatus: ArrayList<Boolean> = ArrayList()
|
|
|
+ // 是否中断读取状态
|
|
|
+ private var interruptReadStatus: ArrayList<Boolean> = ArrayList()
|
|
|
|
|
|
var shouldStopUpgrade = false
|
|
|
|
|
|
@@ -47,8 +43,8 @@ object ModBusController {
|
|
|
)
|
|
|
|
|
|
fun interruptReadTrashBinStatus(interrupt: Boolean) {
|
|
|
- interruptReadTrashBinStatus.clear()
|
|
|
- interruptReadTrashBinStatus.add(interrupt)
|
|
|
+ interruptReadStatus.clear()
|
|
|
+ interruptReadStatus.add(interrupt)
|
|
|
}
|
|
|
|
|
|
@ExperimentalUnsignedTypes
|
|
|
@@ -60,46 +56,14 @@ object ModBusController {
|
|
|
}
|
|
|
// 间隔 1 秒读一遍桶的状态
|
|
|
?.repeatSendToAll(MBFrame.READ_STATUS, {
|
|
|
- interruptReadTrashBinStatus
|
|
|
+ interruptReadStatus
|
|
|
}, { res ->
|
|
|
// // Logger.d("ModbusController", "res: ${res.map { it.toHexString() }}")
|
|
|
LogUtil.i("****************************************************************************")
|
|
|
-// // 第 0 个字节是 从机地址,第 1 个字节是 功能码,第 2 个字节是 字节数
|
|
|
-// // 瓶类计数
|
|
|
-// val bottles = res.map { bytes ->
|
|
|
-// if (bytes.size > 4) bytes[4].toUByte()
|
|
|
-// .toInt() else MODBUS_SLAVE_STATUS__NO_CHANGE
|
|
|
-// }
|
|
|
-// // 桶的重量
|
|
|
-// val weights = res.map { bytes ->
|
|
|
-// if (bytes.size > 6) (bytes[5].toUByte() * 256u + bytes[6].toUByte()).toInt() else MODBUS_SLAVE_STATUS__NO_CHANGE
|
|
|
-// }.map {
|
|
|
-// if (it > 32767) 0 else it // 大于 32767 的表示负数,作 0 处理
|
|
|
-// }
|
|
|
-// // 其它状态
|
|
|
-// val status = res.map { bytes ->
|
|
|
-// if (bytes.size > 8) (bytes[7].toUByte() * 256u + bytes[8].toUByte()).toInt() else MODBUS_SLAVE_STATUS__NO_CHANGE
|
|
|
-// }
|
|
|
- // 箱体温度
|
|
|
-// val temperatures = res.map { bytes ->
|
|
|
-// if (bytes.size > 10) {
|
|
|
-//// // Logger.d("ModBus", "桶状态: ${bytes.toHexString()}")
|
|
|
-// (bytes[9].toUByte() * 256u + bytes[10].toUByte()).toInt()
|
|
|
-// } else MODBUS_SLAVE_STATUS__NO_CHANGE
|
|
|
-// }
|
|
|
for (l in listeners) {
|
|
|
-// if (l.type == LISTENER_TYPE_BOTTLE) {
|
|
|
-// l.listener(bottles)
|
|
|
-// }
|
|
|
-// if (l.type == LISTENER_TYPE_WEIGHT) {
|
|
|
-// l.listener(weights)
|
|
|
-// }
|
|
|
if (l.type == LISTENER_TYPE_STATUS) {
|
|
|
l.listener(res)
|
|
|
}
|
|
|
-// if (l.type == LISTENER_TYPE_TEMPERA) {
|
|
|
-// l.listener(temperatures)
|
|
|
-// }
|
|
|
}
|
|
|
// TODO 临时改成5s
|
|
|
// }, 1000)
|
|
|
@@ -113,21 +77,10 @@ object ModBusController {
|
|
|
?.start()
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 回调的重量单位:0.01kg、10g
|
|
|
- */
|
|
|
-// fun registerWeightListener(key: Any, listener: (List<Int>) -> Unit) {
|
|
|
-// listeners.add(StatusListener(key, listener, LISTENER_TYPE_WEIGHT))
|
|
|
-// }
|
|
|
-
|
|
|
fun registerStatusListener(key: Any, listener: (Any) -> Unit) {
|
|
|
listeners.add(StatusListener(key, listener, LISTENER_TYPE_STATUS))
|
|
|
}
|
|
|
|
|
|
-// fun registerTemperaListener(key: Any, listener: (List<Int>) -> Unit) {
|
|
|
-// listeners.add(StatusListener(key, listener, LISTENER_TYPE_TEMPERA))
|
|
|
-// }
|
|
|
-
|
|
|
fun unregisterListener(key: Any) {
|
|
|
val it = listeners.iterator()
|
|
|
while (it.hasNext()) {
|
|
|
@@ -137,41 +90,6 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 开门
|
|
|
- */
|
|
|
- fun openDoor(index: Int) {
|
|
|
- // Logger.d("TRASH_BIN", "下发开门指令:${index}")
|
|
|
- modBusManager?.sendTo(index, MBFrame.WRITE_OPEN) {
|
|
|
- if (it.isEmpty()) {
|
|
|
- // Tip.toast("舱门[${index}]开启失败!")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 关门
|
|
|
- */
|
|
|
- fun closeDoor(index: Int) {
|
|
|
- // Logger.d("TRASH_BIN", "下发关门指令:${index}")
|
|
|
- modBusManager?.sendTo(index, MBFrame.WRITE_CLOSE) {
|
|
|
- if (it.isEmpty()) {
|
|
|
- // Tip.toast("舱门[${index}]关闭失败!")
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 开关灯
|
|
|
- */
|
|
|
- fun turnLight(on: Boolean) {
|
|
|
- if (on) {
|
|
|
- modBusManager?.sendToAll(MBFrame.WRITE_LIGHT_ON)
|
|
|
- } else {
|
|
|
- modBusManager?.sendToAll(MBFrame.WRITE_LIGHT_OFF)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
fun stop() {
|
|
|
modBusManager?.stop()
|
|
|
}
|
|
|
@@ -186,7 +104,6 @@ object ModBusController {
|
|
|
return deviceList.find { it.idx == byteArray[0] }?.parseStatus(byteArray)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 读取设备类型
|
|
|
*/
|
|
|
@@ -220,7 +137,7 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
fun closeLockBuckle(slaveIdx: Int?, lockIdx: Int, done: ((res: ByteArray) -> Unit)? = null) {
|
|
|
slaveIdx?.let {
|
|
|
modBusManager?.generateLockBuckleCmd(false, lockIdx)?.let { cmd ->
|