ElectronicLedgerActivity.kt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.example.chemical.ui.warehousing
  2. import android.os.Bundle
  3. import android.os.Handler
  4. import android.os.Looper
  5. import android.view.LayoutInflater
  6. import com.bumptech.glide.Glide
  7. import com.bumptech.glide.load.engine.DiskCacheStrategy
  8. import com.bumptech.glide.request.RequestOptions
  9. import com.caysn.autoreplyprint.AutoReplyPrint
  10. import com.example.chemical.ChemicalApp
  11. import com.example.chemical.R
  12. import com.example.chemical.databinding.ActivityElectronicLedgerBinding
  13. import com.example.chemical.ui.SplashActivity
  14. import com.example.chemical.ui.common.BaseCountDownActivity
  15. import com.example.chemical.utils.BitmapUtils
  16. import com.example.chemical.utils.MediaPlayerHelper
  17. import com.example.chemical.utils.TimeUpdater
  18. import com.example.chemical.utils.UiManager
  19. import com.example.chemical.weidith.AirBottleDialog
  20. import com.example.chemical.weidith.CustomDialog
  21. import com.rc.core.log.RcLog
  22. import com.rc.httpcore.HttpClient
  23. import com.rc.httpcore.HttpConfig
  24. import com.sun.jna.Pointer
  25. //标签管理
  26. class ElectronicLedgerActivity : BaseCountDownActivity<ActivityElectronicLedgerBinding>() {
  27. private lateinit var timeUpdater: TimeUpdater
  28. override fun createViewBinding() =
  29. ActivityElectronicLedgerBinding.inflate(LayoutInflater.from(this))
  30. override fun initViews(savedInstanceState: Bundle?) {
  31. super.initViews(savedInstanceState)
  32. viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
  33. viewBinding.tvOutLogin.setOnClickListener {
  34. callLogoutApi {
  35. ChemicalApp.userData = null
  36. HttpClient.token = null
  37. ChemicalApp.subjectId = null
  38. UiManager.switcherCashier(this, SplashActivity::class.java)
  39. }
  40. }
  41. viewBinding.tvReturn.setOnClickListener {
  42. finish()
  43. }
  44. viewBinding.chemicalLabeling.setOnClickListener {
  45. //化学品标签
  46. UiManager.switcher(this, ChemicalLabelingActivity::class.java)
  47. }
  48. viewBinding.cylinderLabel.setOnClickListener {
  49. //气瓶标签
  50. var airBottleDialog = AirBottleDialog(this, object : AirBottleDialog.ILintDate {
  51. override fun onLintDate(cont: String) {
  52. if (cont.isNotEmpty()){
  53. labelPrint(cont)
  54. }
  55. }
  56. })
  57. airBottleDialog.show()
  58. }
  59. }
  60. override fun initData() {
  61. super.initData()
  62. val handler = Handler(Looper.getMainLooper())
  63. timeUpdater = TimeUpdater(handler) { currentTime ->
  64. viewBinding.nowTime.text = "$currentTime"
  65. }
  66. // 启动定时更新
  67. timeUpdater.startUpdating()
  68. viewBinding.tvName.text = ChemicalApp.userData!!.userName
  69. val imageView = viewBinding.imageName
  70. // 使用 Glide 加载网络图片
  71. Glide.with(this)
  72. .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
  73. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  74. .into(imageView)
  75. // 使用 Glide 加载网络图片
  76. viewBinding.deptName.text="${ChemicalApp.confs!!.deptName}-${ChemicalApp.confs!!.roomNum}"
  77. Glide.with(this)
  78. .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.circularLogo}")
  79. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  80. .into(viewBinding.image)
  81. }
  82. override fun onBackPressed() {
  83. super.onBackPressed()
  84. finish()
  85. }
  86. override fun cdTime(cd: Int) {
  87. viewBinding.tvReturn.text = "返回${cd}s"
  88. }
  89. override fun onDestroy() {
  90. super.onDestroy()
  91. try {
  92. timeUpdater.stopUpdating()
  93. } catch (e: Exception) {
  94. }
  95. }
  96. var h = Pointer.NULL
  97. private fun labelPrint(rfid:String) {
  98. val thread = Thread {
  99. try {
  100. try {//关闭打印
  101. h = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 1)
  102. } catch (e: Exception) {
  103. RcLog.info("初始化异常")
  104. }
  105. val bitmap = BitmapUtils.airBottlePrint(rfid) //图片信息
  106. AutoReplyPrint.INSTANCE.CP_Label_BackPaperToPrintPosition(h)
  107. AutoReplyPrint.CP_Pos_PrintRasterImageFromData_Helper.PrintRasterImageFromBitmap(
  108. h,
  109. bitmap!!.width,
  110. bitmap.height,
  111. bitmap,
  112. AutoReplyPrint.CP_ImageBinarizationMethod_Thresholding,
  113. AutoReplyPrint.CP_ImageCompressionMethod_None
  114. )
  115. AutoReplyPrint.INSTANCE.CP_Label_PagePrint(h, 1)
  116. val result = AutoReplyPrint.INSTANCE.CP_Pos_HalfCutPaper(h) //全切
  117. if (result) {
  118. //打印成功
  119. MediaPlayerHelper.playRawMp3(this, R.raw.zhantiefrid)
  120. customDialogView(0, "请将二维码粘贴在RFID标签上")
  121. closePort()
  122. } else {
  123. //失败
  124. closePort()
  125. RcLog.info("=====异常2222")
  126. }
  127. } catch (e: Exception) {
  128. // 监听子线程中抛出的异常
  129. closePort()
  130. RcLog.info("=======异常${e.message}")
  131. }
  132. }
  133. thread.start() // 启动子线程
  134. }
  135. private fun closePort() {
  136. if (h !== Pointer.NULL) {
  137. AutoReplyPrint.INSTANCE.CP_Port_Close(h)
  138. h = Pointer.NULL
  139. }
  140. }
  141. /**
  142. * 0 没有图标 1 绿色(成功) 2红色(失败)
  143. * 失败或者成功的弹框
  144. */
  145. private fun customDialogView(types: Int, msg: String) {
  146. if (!this.isFinishing && !this.isDestroyed) {
  147. val customDialog = CustomDialog(this, types, msg)
  148. customDialog.show()
  149. }
  150. }
  151. }