|
@@ -1,46 +1,76 @@
|
|
|
package com.module.backstage.fragment.product
|
|
|
|
|
|
+import android.util.Log
|
|
|
import android.view.View
|
|
|
import androidx.fragment.app.viewModels
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
import com.hboxs.serialport.plc.DialogClickListener
|
|
|
+import com.hboxs.serialport.plc.util.HexUtils
|
|
|
+import com.hboxs.serialport.sbc.VBoxMessage
|
|
|
+import com.hboxs.serialport.sbc.VboxSerialPortSendQueue
|
|
|
+import com.hboxs.serialport.sbc.frame.VboxCommand
|
|
|
+import com.hboxs.serialport.sbc.frame.VboxWriteCommand
|
|
|
import com.module.backstage.adapter.DebugProductAdapter
|
|
|
import com.module.backstage.databinding.BackstageFragmentDebugProductBinding
|
|
|
import com.module.backstage.dialog.OKCancelDialog
|
|
|
import com.module.backstage.vm.DebugProductFragmentVM
|
|
|
+import com.quyunshuo.androidbaseframemvvm.base.utils.RegisterEventBus
|
|
|
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
|
|
|
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
|
|
|
+import com.quyunshuo.androidbaseframemvvm.common.util.ByteUtils
|
|
|
+import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
|
|
|
import com.quyunshuo.androidbaseframemvvm.common.util.XLogUtil
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
+import kotlinx.coroutines.CoroutineScope
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.Job
|
|
|
+import kotlinx.coroutines.delay
|
|
|
+import kotlinx.coroutines.launch
|
|
|
+import org.greenrobot.eventbus.Subscribe
|
|
|
+import org.greenrobot.eventbus.ThreadMode
|
|
|
|
|
|
/**
|
|
|
- * 调试花型Fragment
|
|
|
+ * 调试商品页Fragment
|
|
|
*
|
|
|
* @author Qu Yunshuo
|
|
|
* @since 2021/8/1 11:46 下午
|
|
|
*/
|
|
|
+@RegisterEventBus
|
|
|
@AndroidEntryPoint
|
|
|
-class DebugProductFragment : BaseFragment<BackstageFragmentDebugProductBinding, DebugProductFragmentVM>() {
|
|
|
-
|
|
|
+class DebugProductFragment :
|
|
|
+ BaseFragment<BackstageFragmentDebugProductBinding, DebugProductFragmentVM>() {
|
|
|
|
|
|
override val mViewModel: DebugProductFragmentVM by viewModels()
|
|
|
override fun createVB() = BackstageFragmentDebugProductBinding.inflate(layoutInflater)
|
|
|
private var productAdapter: DebugProductAdapter? = null
|
|
|
var okCancelDialog: OKCancelDialog? = null
|
|
|
-
|
|
|
+ private val TAG = "DebugProductFragment"
|
|
|
|
|
|
override fun BackstageFragmentDebugProductBinding.initView() {
|
|
|
with(homeRvProduct) {
|
|
|
//设置布局排列方式,默认垂直排列
|
|
|
val gridLayoutManager: GridLayoutManager =
|
|
|
- GridLayoutManager(this@DebugProductFragment.context, 2, androidx.recyclerview.widget.GridLayoutManager.VERTICAL, false)
|
|
|
+ GridLayoutManager(
|
|
|
+ this@DebugProductFragment.context,
|
|
|
+ 2,
|
|
|
+ androidx.recyclerview.widget.GridLayoutManager.VERTICAL,
|
|
|
+ false
|
|
|
+ )
|
|
|
layoutManager = gridLayoutManager
|
|
|
|
|
|
productAdapter = DebugProductAdapter(mViewModel.getPriceData())
|
|
|
productAdapter!!.setItemListener(object : AdapterClickListener {
|
|
|
override fun onClickListener(view: View?, position: Int, data: String?) {
|
|
|
XLogUtil.d("DebugProductFragment position:$position")
|
|
|
- showOkCancelDialog(data!!)
|
|
|
+ Log.d(TAG, "onClickListener: " + data)
|
|
|
+ Log.d(TAG, "onClickListener: "+deviceState)
|
|
|
+ when(deviceState){
|
|
|
+ "02"->showOkCancelDialog(position)
|
|
|
+ else->{
|
|
|
+ ToastUtil.switchToastStyleToError("当前状态无法进行制作\n请重新开机后,再做尝试。")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
adapter = productAdapter
|
|
@@ -54,22 +84,20 @@ class DebugProductFragment : BaseFragment<BackstageFragmentDebugProductBinding,
|
|
|
mViewModel.getBanners()
|
|
|
mViewModel.getArticleData()
|
|
|
}
|
|
|
- private fun showOkCancelDialog(data: String) {
|
|
|
+
|
|
|
+ private fun showOkCancelDialog(data: Int) {
|
|
|
if (okCancelDialog == null) {
|
|
|
okCancelDialog = this@DebugProductFragment.context?.let { OKCancelDialog(it) }
|
|
|
}
|
|
|
- okCancelDialog?.setListener(object: DialogClickListener {
|
|
|
+ okCancelDialog?.setListener(object : DialogClickListener {
|
|
|
override fun onClickListener(type: Int, text: String?) {
|
|
|
when (type) {
|
|
|
- okCancelDialog?.type1-> {
|
|
|
+ okCancelDialog?.type1 -> {
|
|
|
okCancelDialog?.dismiss()
|
|
|
//是否确认操作
|
|
|
- if (data == "MAKE") {
|
|
|
- //开始制作的逻辑
|
|
|
- }else if(data =="PIC"){
|
|
|
- //切换图片
|
|
|
- }
|
|
|
+ startMake(data)
|
|
|
}
|
|
|
+
|
|
|
else -> {}
|
|
|
}
|
|
|
}
|
|
@@ -80,6 +108,76 @@ class DebugProductFragment : BaseFragment<BackstageFragmentDebugProductBinding,
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
mBinding.homeRvProduct.adapter = null
|
|
|
+ stopSendMake()
|
|
|
super.onDestroyView()
|
|
|
}
|
|
|
+
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ fun event(messageEvent: VBoxMessage) {
|
|
|
+ XLogUtil.d(messageEvent.cmd + ":messageEvent.data:" + messageEvent.data)
|
|
|
+ Log.d(TAG, messageEvent.cmd + "制作页面 :messageEvent.data:" + messageEvent.data)
|
|
|
+ val getSign = messageEvent.data.substring(2, 4)
|
|
|
+ when (getSign) {
|
|
|
+ "01" -> {
|
|
|
+ val overheatState = HexUtils.hexStr2BinStr(messageEvent.data.substring(4, 6))
|
|
|
+ val split = overheatState.trim().split("")
|
|
|
+ mBinding.tvLutou.text = ((split[1].toInt() * 256) + ByteUtils.hexStr2decimal(messageEvent.data.substring(6, 8))).toString()+"℃"
|
|
|
+ cmdHeartbeat(messageEvent)
|
|
|
+ }
|
|
|
+ else -> {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private var deviceState:String=""
|
|
|
+
|
|
|
+ private fun cmdHeartbeat(messageEvent: VBoxMessage) {
|
|
|
+ deviceState = messageEvent.data.substring(0, 2)//机器状态码
|
|
|
+ when (deviceState) {
|
|
|
+ "00"->deviceState="00"
|
|
|
+ "01"->deviceState="01"
|
|
|
+ "02"->deviceState="02"
|
|
|
+ "03"->deviceState="03"
|
|
|
+ "04" -> {//开始制作
|
|
|
+ deviceState="04"
|
|
|
+ stopSendMake()
|
|
|
+ }
|
|
|
+ "05"->deviceState="05"
|
|
|
+ "06"->deviceState="06"
|
|
|
+ "10"->deviceState="10"
|
|
|
+ else -> {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var jobM1: Job? = null
|
|
|
+ get() = field
|
|
|
+ var isActiveM1: Boolean = true
|
|
|
+ get() = field
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+ }
|
|
|
+
|
|
|
+ fun startMake(value: Int) {
|
|
|
+ val productData: String =
|
|
|
+ if (value == 0) {
|
|
|
+ "00FC0200"//甜味爆米花
|
|
|
+ } else {
|
|
|
+ "00FC0100"//咸味爆米花
|
|
|
+ }
|
|
|
+
|
|
|
+ stopSendMake()
|
|
|
+ isActiveM1 = true
|
|
|
+ jobM1 = CoroutineScope(Dispatchers.Main).launch {
|
|
|
+ while (isActiveM1) {
|
|
|
+ Log.d(TAG, "startMake: ")
|
|
|
+ VboxSerialPortSendQueue.getInstance()
|
|
|
+ .sendCommand(VboxWriteCommand(VboxCommand.MAKE_ORDER, productData))
|
|
|
+ delay(1000) // 每隔2秒重复执行
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun stopSendMake() {
|
|
|
+ isActiveM1 = false
|
|
|
+ jobM1?.cancel()
|
|
|
+ }
|
|
|
}
|