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_ELEC_LOCK_BOARD 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 import java.util.stream.Collectors /** * 业务层管理 */ 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() } /** * @param key 可null,null时用applicationContext做全局监听 */ fun registerStatusListener(key: Any?) { ModBusController.registerStatusListener(key ?: MyApplication.instance!!.applicationContext) { res -> LogUtil.i("设备状态:${(res as List).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, if (keyBean.isLeft) 0 else 1) { 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") } } DEVICE_TYPE_FINGERPRINT -> { } } } } } } } } } fun readLockBuckleStatus() { // TODO slaveIdx暂时写死,调试用 ModBusController.readBuckleStatus(true, 0) { type, res -> LogUtil.i("单slave卡扣状态 : $type - ${res.toHexStrings()}") when (type) { 0 -> { val isLeftLock = (res[4].toInt() shr 0) and 0x1 == 1 val isRightLock = (res[4].toInt() shr 4) and 0x1 == 1 println("锁具底座卡扣状态 : $isLeftLock - $isRightLock") } 1 -> { val tempList = mutableListOf() for (i in 0..7) { tempList.add((res[4].toInt() shr i) and 0x1 == 1) } println("锁具底座卡扣1-8状态 : $tempList") } 2 -> { val lock9Status = (res[4].toInt() shr 0) and 0x1 == 1 val lock10Status = (res[4].toInt() shr 1) and 0x1 == 1 println("锁具底座卡扣9、10状态 : $lock9Status - $lock10Status") } } } } fun readKeyBuckleStatus() { // TODO slaveIdx暂时写死,调试用 ModBusController.readBuckleStatus(false, 1) { type, res -> LogUtil.i("单slave卡扣状态 : $type - ${res.toHexStrings()}") // TODO 待验证 when (type) { 0 -> { val isLeftLock = (res[4].toInt() shr 0) and 0x1 == 1 val isRightLock = (res[4].toInt() shr 4) and 0x1 == 1 println("钥匙底座卡扣状态 : $isLeftLock - $isRightLock") } 1 -> { val tempList = mutableListOf() for (i in 0..7) { tempList.add((res[4].toInt() shr i) and 0x1 == 1) } println("锁具底座卡扣1-8状态 : $tempList") } 2 -> { val lock9Status = (res[4].toInt() shr 0) and 0x1 == 1 val lock10Status = (res[4].toInt() shr 1) and 0x1 == 1 println("锁具底座卡扣9、10状态 : $lock9Status - $lock10Status") } } } } }