Browse Source

新增刷卡登录逻辑

Frankensteinly 10 tháng trước cách đây
mục cha
commit
7d5a4b35d5

+ 10 - 0
app/src/main/java/com/grkj/iscs/extentions/Long.kt

@@ -7,4 +7,14 @@ fun Long.toByteArray(): ByteArray {
     return ByteBuffer.allocate(java.lang.Long.BYTES)
         .order(ByteOrder.LITTLE_ENDIAN)
         .putLong(this).array()
+}
+
+fun Long.toByteArrays(capability: Int = 4): ByteArray {
+//    require(capability in 1..4) { "Length must be between 1 and 4" }
+    val bytes = ByteArray(capability)
+    for (i in 0 until capability) {
+//        bytes[capability - i - 1] = ((this ushr (i * 8)) and 0xFF).toByte() // 大端模式
+        bytes[i] = ((this ushr (i * 8)) and 0xFF).toByte()  // 小端模式
+    }
+    return bytes
 }

+ 8 - 0
app/src/main/java/com/grkj/iscs/presentation/PresentationManager.kt

@@ -228,4 +228,12 @@ object PresentationManager {
             }
         }
     }
+
+
+
+
+
+
+    /*******************************新的Demo*******************************/
+    var mStep = 0;
 }

+ 28 - 77
app/src/main/java/com/grkj/iscs/presentation/simple/SimpleLoginActivity.kt

@@ -2,31 +2,28 @@ package com.grkj.iscs.presentation.simple
 
 import android.content.Intent
 import android.os.Environment
-import android.view.View
+import android.view.InputDevice
+import android.view.KeyEvent
 import com.google.gson.Gson
 import com.google.gson.reflect.TypeToken
 import com.grkj.iscs.BusinessManager
 import com.grkj.iscs.R
 import com.grkj.iscs.base.BaseActivity
 import com.grkj.iscs.databinding.ActivitySimpleLoginBinding
-import com.grkj.iscs.extentions.removeLeadingZeros
+import com.grkj.iscs.extentions.toByteArrays
 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_KEY
-import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
-import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
-import com.grkj.iscs.model.WorkTicketStatusBean
-import com.grkj.iscs.presentation.LockerActivity
-import com.grkj.iscs.presentation.LockerTogetherActivity
 import com.grkj.iscs.presentation.PresentationManager
-import com.grkj.iscs.util.ActivityUtils
+import com.grkj.iscs.presentation.PresentationManager.PCardInfoBean
 import com.grkj.iscs.util.Executor
 import com.grkj.iscs.util.FileUtil
 import com.grkj.iscs.util.ToastUtils
 
+
 class SimpleLoginActivity : BaseActivity<ActivitySimpleLoginBinding>() {
 
+    private var cardNo = ""
+    private lateinit var mLockerList: List<PCardInfoBean>
+
     override val viewBinding: ActivitySimpleLoginBinding
         get() = ActivitySimpleLoginBinding.inflate(layoutInflater)
 
@@ -35,7 +32,6 @@ class SimpleLoginActivity : BaseActivity<ActivitySimpleLoginBinding>() {
             startActivity(Intent(this, SimpleSettingActivity::class.java))
         }
 
-
         val path =
             Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath + "/presentation/presentation.txt"
         val str = FileUtil.readTxt(path)
@@ -48,9 +44,9 @@ class SimpleLoginActivity : BaseActivity<ActivitySimpleLoginBinding>() {
         PresentationManager.cardList =
             Gson().fromJson(str, object : TypeToken<List<PresentationManager.PCardInfoBean>>() {}.type)
 
-        val lockerList = PresentationManager.cardList.filter { it.isLocker }
-        for (i in lockerList.indices) {
-            PresentationManager.mLockerList.add(PresentationManager.PLockerBean("上锁人${i + 1}", lockerList[i].rfid, 0))
+        mLockerList = PresentationManager.cardList.filter { it.isLocker }
+        for (i in mLockerList.indices) {
+            PresentationManager.mLockerList.add(PresentationManager.PLockerBean("上锁人${i + 1}", mLockerList[i].rfid, 0))
         }
         val togetherList = PresentationManager.cardList.filter { !it.isLocker }
         for (i in togetherList.indices) {
@@ -70,71 +66,26 @@ class SimpleLoginActivity : BaseActivity<ActivitySimpleLoginBinding>() {
             handleLoading(false)
         }
 
-        BusinessManager.registerStatusListener(this) { dockBean ->
-            when (dockBean.type) {
-                DOCK_TYPE_PORTABLE -> {
-                    dockBean.deviceList.forEach { deviceBean ->
-                        if (deviceBean.isExist) {
-                            when (deviceBean.type) {
-                                DEVICE_TYPE_CARD -> {
-                                    ModBusController.readPortalCaseCardRfid(dockBean.addr.toInt() - 1) { res ->
-                                        if (res.size >= 11) {
-                                            val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
-                                            println("卡片RFID : $rfid")
-                                            if(window.decorView.visibility != View.VISIBLE) {
-//                                            ToastUtils.tip("当前页面不可见")
-                                                val current = ActivityUtils.currentActivity()
-                                                if (current is LockerActivity) {
-                                                    current.showDlg(rfid)
-                                                } else if (current is LockerTogetherActivity) {
-                                                    current.showDlg(rfid)
-                                                }
-                                            } else {
-                                                if (PresentationManager.cardList.any { it.rfid == rfid && it.isLocker}) {
-                                                    ToastUtils.tip(getString(R.string.simple_login_success_welcome, rfid))
-                                                    val intent = Intent(this, LockerActivity::class.java)
-                                                    intent.putExtra("card", rfid)
-                                                    startActivity(intent)
-                                                } else if (PresentationManager.cardList.any { it.rfid == rfid && !it.isLocker}) {
-                                                    ToastUtils.tip(getString(R.string.simple_login_success_welcome, rfid))
-                                                    val intent = Intent(this, LockerTogetherActivity::class.java)
-                                                    intent.putExtra("card", rfid)
-                                                    startActivity(intent)
-                                                } else {
-                                                    ToastUtils.tip("登录失败,$rfid 没有权限")
-                                                }
-                                            }
-                                        }
-                                    }
-                                }
-
+    }
 
-                                DEVICE_TYPE_KEY -> {
-                                    Executor.runOnMain {
-                                        BusinessManager.getTicketStatusBusiness(PresentationManager.keyMac, this) { b, s ->
-                                            handleLoading(b, s)
-                                            if (!s.isNullOrEmpty() && s.startsWith("工作票完成接收")) {
-                                                val ticket = s.substring(7)
-                                                val ticketStatusBean = Gson().fromJson(ticket, WorkTicketStatusBean::class.java)
-                                                PresentationManager.updateWorkTicket(ticketStatusBean)
-                                            }
-                                        }
-                                    }
-                                }
-                                DEVICE_TYPE_LOCK -> {
-                                    Executor.runOnMain {
-                                        ModBusController.readLockRfid(dockBean.addr.toInt() - 1, deviceBean.idx) { res ->
-                                            val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
-                                            PresentationManager.updateLockReturnStatus(rfid)
-                                        }
-                                    }
-                                }
-                            }
-                        }
-                    }
+    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
+        if (event.action == KeyEvent.ACTION_UP && event.source == InputDevice.SOURCE_KEYBOARD) {
+            // 检测到回车开始处理
+            if (event.keyCode == 66) {
+                val formattedCard = cardNo.toLong().toByteArrays().toHexStrings(false)
+                println("haha : $formattedCard")
+                // 重置cardNo
+                cardNo = ""
+                if (mLockerList.any { it.rfid == formattedCard }) {
+                    startActivity(Intent(this, SimpleProcessActivity::class.java).apply {
+                        PresentationManager.mStep = 0
+                        putExtra("step", PresentationManager.mStep)
+                    })
                 }
+                return super.dispatchKeyEvent(event)
             }
+            cardNo += event.keyCharacterMap.getDisplayLabel(event.keyCode)
         }
+        return super.dispatchKeyEvent(event)
     }
-
 }