|
@@ -0,0 +1,207 @@
|
|
|
|
|
+package com.grkj.data.hardware.can
|
|
|
|
|
+
|
|
|
|
|
+import com.sik.sikcore.bit.BitTypeUtils
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 设备状态转换
|
|
|
|
|
+ */
|
|
|
|
|
+object DeviceParseStatus {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 钥匙仓位状态转换
|
|
|
|
|
+ */
|
|
|
|
|
+ fun parseKeyDockStatus(nodeId: Int, index: Int, statusData: ByteArray) {
|
|
|
|
|
+ val deviceModel = CanHelper.getDeviceByNodeId(nodeId)
|
|
|
|
|
+ val leftKeyModel: DeviceModel.DeviceKey =
|
|
|
|
|
+ (deviceModel.getOrNull(0) ?: DeviceModel.DeviceKey().apply {
|
|
|
|
|
+ this.nodeId = nodeId
|
|
|
|
|
+ this.deviceType = CanDeviceConst.DEVICE_KEY_DOCK
|
|
|
|
|
+ this.id = 0
|
|
|
|
|
+ this.deviceChange = CanDeviceConst.DEVICE_CHANGE_KEY_TAKE
|
|
|
|
|
+ }) as DeviceModel.DeviceKey
|
|
|
|
|
+ val rightKeyModel: DeviceModel.DeviceKey =
|
|
|
|
|
+ (deviceModel.getOrNull(1) ?: DeviceModel.DeviceKey().apply {
|
|
|
|
|
+ this.nodeId = nodeId
|
|
|
|
|
+ this.deviceType = CanDeviceConst.DEVICE_KEY_DOCK
|
|
|
|
|
+ this.id = 1
|
|
|
|
|
+ this.deviceChange = CanDeviceConst.DEVICE_CHANGE_KEY_TAKE
|
|
|
|
|
+ }) as DeviceModel.DeviceKey
|
|
|
|
|
+ when (index) {
|
|
|
|
|
+ CanCommands.Command.STATUS -> {
|
|
|
|
|
+ require(statusData.size == 2) { "Status payload size must is 2" }
|
|
|
|
|
+ val leftKeyData = statusData[0]
|
|
|
|
|
+ val rightKeyData = statusData[1]
|
|
|
|
|
+ val leftKeyExists = ((leftKeyData.toInt() shr 0) and 1) == 1
|
|
|
|
|
+ if (leftKeyModel.isExist != leftKeyExists) {
|
|
|
|
|
+ leftKeyModel.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ leftKeyModel.deviceChange,
|
|
|
|
|
+ if (leftKeyExists) CanDeviceConst.DEVICE_CHANGE_KEY_RETURN else CanDeviceConst.DEVICE_CHANGE_KEY_TAKE
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ leftKeyModel.isExist = leftKeyExists
|
|
|
|
|
+ leftKeyModel.isCharging = ((leftKeyData.toInt() shr 1) and 1) == 1
|
|
|
|
|
+ val rightKeyExists = ((rightKeyData.toInt() shr 0) and 1) == 1
|
|
|
|
|
+ if (rightKeyModel.isExist != rightKeyExists) {
|
|
|
|
|
+ rightKeyModel.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ rightKeyModel.deviceChange,
|
|
|
|
|
+ if (rightKeyExists) CanDeviceConst.DEVICE_CHANGE_KEY_RETURN else CanDeviceConst.DEVICE_CHANGE_KEY_TAKE
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ rightKeyModel.isExist = rightKeyExists
|
|
|
|
|
+ rightKeyModel.isCharging = ((rightKeyData.toInt() shr 1) and 1) == 1
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ CanCommands.Command.CONTROL_REG -> {
|
|
|
|
|
+ val keyData = statusData[0]
|
|
|
|
|
+ val leftKeyLocked = ((keyData.toInt() shr 0) and 1) == 1
|
|
|
|
|
+ if (leftKeyModel.locked != leftKeyLocked) {
|
|
|
|
|
+ leftKeyModel.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ leftKeyModel.deviceChange,
|
|
|
|
|
+ if (leftKeyLocked) CanDeviceConst.DEVICE_CHANGE_LOCKED else CanDeviceConst.DEVICE_CHANGE_UNLOCK
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ leftKeyModel.locked = leftKeyLocked
|
|
|
|
|
+ val rightKeyLocked = ((keyData.toInt() shr 4) and 1) == 1
|
|
|
|
|
+ if (rightKeyModel.locked != rightKeyLocked) {
|
|
|
|
|
+ rightKeyModel.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ rightKeyModel.deviceChange,
|
|
|
|
|
+ if (rightKeyLocked) CanDeviceConst.DEVICE_CHANGE_LOCKED else CanDeviceConst.DEVICE_CHANGE_UNLOCK
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ rightKeyModel.locked = rightKeyLocked
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ CanHelper.updateDeviceData(nodeId, listOf(leftKeyModel, rightKeyModel))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 锁仓状态转换
|
|
|
|
|
+ */
|
|
|
|
|
+ fun parseLockDockStatus(nodeId: Int, index: Int, statusData: ByteArray) {
|
|
|
|
|
+ var deviceModel = CanHelper.getDeviceByNodeId(nodeId)
|
|
|
|
|
+ if (deviceModel.isEmpty()) {
|
|
|
|
|
+ deviceModel = mutableListOf<DeviceModel.CommonDevice>()
|
|
|
|
|
+ for (i in 0 until 5) {
|
|
|
|
|
+ deviceModel.add(DeviceModel.CommonDevice().apply {
|
|
|
|
|
+ this.nodeId = nodeId
|
|
|
|
|
+ this.deviceType = CanDeviceConst.DEVICE_LOCK_DOCK
|
|
|
|
|
+ this.id = i + 1
|
|
|
|
|
+ this.deviceChange = CanDeviceConst.DEVICE_CHANGE_KEY_TAKE
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ when (index) {
|
|
|
|
|
+ CanCommands.Command.STATUS -> {
|
|
|
|
|
+ deviceModel.forEach {
|
|
|
|
|
+ val deviceExists = ((statusData[0].toInt() shr (it.id - 1)) and 1) == 1
|
|
|
|
|
+ if (it.isExist != deviceExists) {
|
|
|
|
|
+ it.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ it.deviceChange,
|
|
|
|
|
+ if (deviceExists) CanDeviceConst.DEVICE_CHANGE_LOCK_RETURN else CanDeviceConst.DEVICE_CHANGE_LOCK_TAKE
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ it.isExist = deviceExists
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ CanCommands.Command.CONTROL_REG -> {
|
|
|
|
|
+ deviceModel.forEach {
|
|
|
|
|
+ val deviceLocked = ((statusData[0].toInt() shr (it.id - 1)) and 1) == 1
|
|
|
|
|
+ if (it.locked != deviceLocked) {
|
|
|
|
|
+ it.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ it.deviceChange,
|
|
|
|
|
+ if (deviceLocked) CanDeviceConst.DEVICE_CHANGE_LOCKED else CanDeviceConst.DEVICE_CHANGE_UNLOCK
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ it.locked = deviceLocked
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ CanHelper.updateDeviceData(nodeId, deviceModel)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 钥匙柜控制板状态转换
|
|
|
|
|
+ */
|
|
|
|
|
+ fun parseKeyCabinetControlBoardStatus(nodeId: Int, index: Int, statusData: ByteArray) {
|
|
|
|
|
+ var deviceModel = CanHelper.getDeviceByNodeId(nodeId)
|
|
|
|
|
+ if (deviceModel.isEmpty()) {
|
|
|
|
|
+ deviceModel = mutableListOf<DeviceModel.DeviceKey>()
|
|
|
|
|
+ for (i in 0 until 5) {
|
|
|
|
|
+ deviceModel.add(DeviceModel.DeviceKey().apply {
|
|
|
|
|
+ this.nodeId = nodeId
|
|
|
|
|
+ this.deviceType = CanDeviceConst.DEVICE_KEY_CABINET_CONTROL_BOARD
|
|
|
|
|
+ this.id = i + 1
|
|
|
|
|
+ this.deviceChange = CanDeviceConst.DEVICE_CHANGE_KEY_TAKE
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ when (index) {
|
|
|
|
|
+ CanCommands.Command.STATUS -> {
|
|
|
|
|
+ deviceModel.forEach {
|
|
|
|
|
+ val keyExists = ((statusData[0].toInt() shr (it.id - 1)) and 1) == 1
|
|
|
|
|
+ if (it.isExist != keyExists) {
|
|
|
|
|
+ it.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ it.deviceChange,
|
|
|
|
|
+ if (keyExists) CanDeviceConst.DEVICE_CHANGE_KEY_RETURN else CanDeviceConst.DEVICE_CHANGE_KEY_TAKE
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ it.isExist = keyExists
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ CanCommands.Command.CONTROL_REG -> {
|
|
|
|
|
+ deviceModel.forEach {
|
|
|
|
|
+ val keyLocked = ((statusData[0].toInt() shr (it.id - 1)) and 1) == 1
|
|
|
|
|
+ if (it.locked != keyLocked) {
|
|
|
|
|
+ it.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ it.deviceChange,
|
|
|
|
|
+ if (keyLocked) CanDeviceConst.DEVICE_CHANGE_LOCKED else CanDeviceConst.DEVICE_CHANGE_UNLOCK
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ it.locked = ((statusData[0].toInt() shr (it.id - 1)) and 1) == 1
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ CanHelper.updateDeviceData(nodeId, deviceModel)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 物资柜控制板状态转换
|
|
|
|
|
+ */
|
|
|
|
|
+ fun parseMaterialCabinetControlBoardStatus(nodeId: Int, index: Int, statusData: ByteArray) {
|
|
|
|
|
+ var deviceModel = CanHelper.getDeviceByNodeId(nodeId)
|
|
|
|
|
+ if (deviceModel.isEmpty()) {
|
|
|
|
|
+ deviceModel = mutableListOf<DeviceModel.MaterialDevice>()
|
|
|
|
|
+ deviceModel.add(DeviceModel.MaterialDevice().apply {
|
|
|
|
|
+ this.nodeId = nodeId
|
|
|
|
|
+ this.deviceType = CanDeviceConst.DEVICE_MATERIAL_CABINET_CONTROL_BOARD
|
|
|
|
|
+ this.id = 0
|
|
|
|
|
+ this.deviceChange = CanDeviceConst.DEVICE_CHANGE_KEY_TAKE
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ when (index) {
|
|
|
|
|
+ CanCommands.Command.CONTROL_REG -> {
|
|
|
|
|
+ deviceModel.filterIsInstance<DeviceModel.MaterialDevice>().forEach {
|
|
|
|
|
+ val leftDoorLocked = ((statusData[0].toInt() shr 0) and 1) == 0
|
|
|
|
|
+ if (it.leftDoorLocked != leftDoorLocked) {
|
|
|
|
|
+ it.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ it.deviceChange,
|
|
|
|
|
+ if (leftDoorLocked) CanDeviceConst.DEVICE_CHANGE_MATERIAL_LEFT_DOOR_OPEN else CanDeviceConst.DEVICE_CHANGE_MATERIAL_LEFT_DOOR_CLOSED
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ it.leftDoorLocked = leftDoorLocked
|
|
|
|
|
+ val rightDoorLocked = ((statusData[0].toInt() shr 4) and 1) == 0
|
|
|
|
|
+ if (it.leftDoorLocked != rightDoorLocked) {
|
|
|
|
|
+ it.deviceChange = BitTypeUtils.addType(
|
|
|
|
|
+ it.deviceChange,
|
|
|
|
|
+ if (rightDoorLocked) CanDeviceConst.DEVICE_CHANGE_MATERIAL_RIGHT_DOOR_OPEN else CanDeviceConst.DEVICE_CHANGE_MATERIAL_RIGHT_DOOR_CLOSED
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ it.rightDoorLocked = rightDoorLocked
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ CanHelper.updateDeviceData(nodeId, deviceModel)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|