|
|
@@ -0,0 +1,98 @@
|
|
|
+package com.grkj.iscs
|
|
|
+
|
|
|
+import com.grkj.iscs.extentions.removeLeadingZeros
|
|
|
+import com.grkj.iscs.extentions.toHexStrings
|
|
|
+import com.grkj.iscs.modbus.ModBusController
|
|
|
+import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_CARD
|
|
|
+import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_FINGERPRINT
|
|
|
+import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
|
|
|
+import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
|
|
|
+import com.grkj.iscs.util.ToastUtils
|
|
|
+import com.grkj.iscs.util.log.LogUtil
|
|
|
+
|
|
|
+/**
|
|
|
+ * 业务层管理
|
|
|
+ */
|
|
|
+object BusinessManager {
|
|
|
+
|
|
|
+ fun connectDock(count: Int, isNeedInit: Boolean = false) {
|
|
|
+ ModBusController.setSlaveCount(count)
|
|
|
+ ModBusController.interruptReadTrashBinStatus(false)
|
|
|
+ ModBusController.start(MyApplication.instance!!.applicationContext)
|
|
|
+ ModBusController.unregisterListener(MyApplication.instance!!.applicationContext)
|
|
|
+
|
|
|
+ if (isNeedInit) {
|
|
|
+ ModBusController.initDevicesStatus()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun disconnectDock() {
|
|
|
+ ModBusController.stop()
|
|
|
+ }
|
|
|
+
|
|
|
+ fun registerStatusListener() {
|
|
|
+ ModBusController.registerStatusListener(this) { res ->
|
|
|
+ LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
|
|
|
+ res.forEach { bytes ->
|
|
|
+ val dockBean = ModBusController.updateStatus(bytes) ?: return@forEach
|
|
|
+ when (dockBean.type) {
|
|
|
+ DOCK_TYPE_KEY -> {
|
|
|
+ dockBean.getKeyList().forEach { keyBean ->
|
|
|
+ if (keyBean.isExist) {
|
|
|
+ // 放回钥匙,读取rfid
|
|
|
+ ModBusController.readKeyRfid(dockBean.addr.toInt() - 1, keyBean.isLeft) { isLeft, res ->
|
|
|
+ val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
+ ModBusController.updateKeyRfid(dockBean.addr.toInt(), keyBean.isLeft, rfid)
|
|
|
+ // TODO 从HTTP读取Mac
|
|
|
+ // TODO 蓝牙通信
|
|
|
+ // TODO 关卡扣
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DOCK_TYPE_LOCK -> {
|
|
|
+ dockBean.getLockList().forEach { lockBean ->
|
|
|
+ if (lockBean.isExist) {
|
|
|
+ ModBusController.readLockRfid(dockBean.addr.toInt() - 1, lockBean.idx) { res ->
|
|
|
+ val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
+ ModBusController.updateLockRfid(dockBean.addr.toInt(), lockBean.idx, rfid)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DOCK_TYPE_PORTABLE -> {
|
|
|
+ // TODO 便携式待完善
|
|
|
+ dockBean.deviceList.forEach { deviceBean ->
|
|
|
+ if (deviceBean.isExist) {
|
|
|
+ when (deviceBean.type) {
|
|
|
+ DEVICE_TYPE_KEY -> {
|
|
|
+
|
|
|
+ }
|
|
|
+ DEVICE_TYPE_LOCK -> {
|
|
|
+ ModBusController.readLockRfid(dockBean.addr.toInt() - 1, deviceBean.idx) { res ->
|
|
|
+ val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
+ ModBusController.updateLockRfid(dockBean.addr.toInt(), deviceBean.idx, rfid)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DEVICE_TYPE_CARD -> {
|
|
|
+ ModBusController.readPortalCaseCardRfid(dockBean.addr.toInt() - 1) { res ->
|
|
|
+ val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
+ println("卡片RFID : $rfid")
|
|
|
+ ToastUtils.tip("卡片RFID : $rfid")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DEVICE_TYPE_FINGERPRINT -> {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|