package com.sw.platecabinet.activity import android.content.Context import android.content.Intent import android.view.inputmethod.InputMethodManager import com.sw.plate.utils.ToastUtils import com.sw.platecabinet.databinding.ActivityPlcSettingBinding import com.sw.platecabinet.databinding.ItemTitleTimeBinding import com.sw.platecabinet.ext.clickWithDebounce import com.sw.platecabinet.utils.mego.PlcCallback import com.sw.platecabinet.utils.mego.PlcHelper import com.sw.platecabinet.utils.mego.PlcStatus /** * PLC 控制设置界面 * 通过右侧时间双击(onRightDoubleClick)打开,提供全套 PLC 控制操作入口 */ class PlcSettingActivity : BaseActivity() { companion object { /** * 启动 PlcSettingActivity */ fun start(context: Context) { context.startActivity(Intent(context, PlcSettingActivity::class.java)) } } override fun inflateViewBinding(): ActivityPlcSettingBinding { return ActivityPlcSettingBinding.inflate(layoutInflater) } override fun inflateTitleBinding(): ItemTitleTimeBinding { return binding.includeHeader } override fun initialize() { initConnectButtons() initOutPlateButton() initPlatformButtons() initAlarmButton() initParamButtons() initStatusButton() } // ==================== 连接控制 ==================== /** * 初始化连接控制区按钮:打开串口、打开自动模式 */ private fun initConnectButtons() { binding.btnOpenSerialPort.clickWithDebounce { showWaitingDialog("正在打开串口……") PlcHelper.getInstance(this).openSerialPort(object : PlcCallback { override fun onSuccess(result: String) { hideWaitingDialog() ToastUtils.showToast("打开串口成功:$result") } override fun onError(message: String) { hideWaitingDialog() ToastUtils.showToast("打开串口失败:$message") } }) } binding.btnOpenAutoMode.clickWithDebounce { showWaitingDialog("正在打开自动模式……") PlcHelper.getInstance(this).openAutoMode(object : PlcCallback { override fun onSuccess(result: String) { hideWaitingDialog() ToastUtils.showToast("打开自动模式成功:$result") } override fun onError(message: String) { hideWaitingDialog() ToastUtils.showToast("打开自动模式失败:$message") } }) } } // ==================== 出盘操作 ==================== /** * 初始化出盘操作按钮 */ private fun initOutPlateButton() { binding.btnOutPlate.clickWithDebounce { showWaitingDialog("正在出盘……") PlcHelper.getInstance(this).outPlate(object : PlcCallback { override fun onSuccess(result: String) { hideWaitingDialog() ToastUtils.showToast("出盘成功:$result") } override fun onError(message: String) { hideWaitingDialog() ToastUtils.showToast("出盘失败:$message") } }) } } // ==================== 平台控制 ==================== /** * 初始化平台控制区按钮:平台下降、平台上升、返回营业 */ private fun initPlatformButtons() { binding.btnPlatformDown.clickWithDebounce { showWaitingDialog("正在执行平台下降……") PlcHelper.getInstance(this).platformDown(object : PlcCallback { override fun onSuccess(result: String) { hideWaitingDialog() ToastUtils.showToast("平台下降成功:$result") } override fun onError(message: String) { hideWaitingDialog() ToastUtils.showToast("平台下降失败:$message") } }) } binding.btnPlatformUp.clickWithDebounce { showWaitingDialog("正在执行平台上升……") PlcHelper.getInstance(this).platformUp(object : PlcCallback { override fun onSuccess(result: String) { hideWaitingDialog() ToastUtils.showToast("平台上升成功:$result") } override fun onError(message: String) { hideWaitingDialog() ToastUtils.showToast("平台上升失败:$message") } }) } binding.btnBackToBusiness.clickWithDebounce { showWaitingDialog("正在返回营业状态……") PlcHelper.getInstance(this).backToBusiness(object : PlcCallback { override fun onSuccess(result: String) { hideWaitingDialog() ToastUtils.showToast("返回营业成功:$result") } override fun onError(message: String) { hideWaitingDialog() ToastUtils.showToast("返回营业失败:$message") } }) } } // ==================== 故障处理 ==================== /** * 初始化故障复位按钮 */ private fun initAlarmButton() { binding.btnAlarmReset.clickWithDebounce { showWaitingDialog("正在故障复位……") PlcHelper.getInstance(this).alarmReset(object : PlcCallback { override fun onSuccess(result: String) { hideWaitingDialog() ToastUtils.showToast("故障复位成功:$result") } override fun onError(message: String) { hideWaitingDialog() ToastUtils.showToast("故障复位失败:$message") } }) } } // ==================== 参数设置 ==================== /** * 初始化参数设置按钮 * 读取参数类型和参数值输入框内容,调用 setParams */ private fun initParamButtons() { binding.btnSetParams.clickWithDebounce { val paramType = binding.etParamType.text.toString().trim() val paramValueStr = binding.etParamValue.text.toString().trim() if (paramType.isEmpty()) { ToastUtils.showToast("请输入参数类型") return@clickWithDebounce } if (paramValueStr.isEmpty()) { ToastUtils.showToast("请输入参数值") return@clickWithDebounce } val paramValue = paramValueStr.toIntOrNull() if (paramValue == null) { ToastUtils.showToast("参数值须为整数") return@clickWithDebounce } // 隐藏键盘 hideKeyboard() showWaitingDialog("正在设置参数……") PlcHelper.getInstance(this).setParams( paramType, paramValue, object : PlcCallback { override fun onSuccess(result: String) { hideWaitingDialog() ToastUtils.showToast("参数设置成功:$result") } override fun onError(message: String) { hideWaitingDialog() ToastUtils.showToast("参数设置失败:$message") } } ) } } // ==================== 状态显示 ==================== /** * 初始化状态刷新按钮,获取 PLC 状态并格式化展示 */ private fun initStatusButton() { binding.btnRefreshStatus.clickWithDebounce { showWaitingDialog("正在获取状态……") PlcHelper.getInstance(this).getStatus(object : PlcCallback { override fun onSuccess(result: PlcStatus) { hideWaitingDialog() binding.tvStatusInfo.text = buildStatusText(result) } override fun onError(message: String) { hideWaitingDialog() binding.tvStatusInfo.text = "获取状态失败:$message" ToastUtils.showToast("获取状态失败:$message") } }) } } /** * 将 PlcStatus 格式化为多行可读文本 */ private fun buildStatusText(status: PlcStatus): String { return """ 空盘状态:${if (status.isEmptyPlate) "是(无餐盘)" else "否(有餐盘)"} 平台下位:${if (status.isPlateformDown) "是" else "否"} 平台上位:${if (status.isPlateformUp) "是" else "否"} 取盘红外:${if (status.isTakePlateInfrared) "已触发" else "未触发"} 报警代码:${if (status.alarmCode == 0) "正常(0)" else "异常(${status.alarmCode})"} """.trimIndent() } // ==================== 生命周期 ==================== /** * 左侧日期双击 → 关闭设置界面 */ override fun onLeftDoubleClick() { finish() } /** * 右侧时间双击 → 不做操作,防止循环打开 */ override fun onRightDoubleClick() { // 空实现,避免在设置页再次触发跳转 } // ==================== 工具方法 ==================== /** * 隐藏软键盘 */ private fun hideKeyboard() { val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(currentFocus?.windowToken, 0) } }