diff --git a/src/main/java/com/iflytop/handacid/app/controller/DeviceController.java b/src/main/java/com/iflytop/handacid/app/controller/DeviceController.java index c605c62..9c3fda9 100644 --- a/src/main/java/com/iflytop/handacid/app/controller/DeviceController.java +++ b/src/main/java/com/iflytop/handacid/app/controller/DeviceController.java @@ -3,6 +3,7 @@ package com.iflytop.handacid.app.controller; import com.iflytop.handacid.app.core.state.DeviceState; import com.iflytop.handacid.app.model.bo.DeviceInfo; import com.iflytop.handacid.app.model.dto.SyncOperationsDTO; +import com.iflytop.handacid.app.scheduled.BleGamepadStateScheduledTask; import com.iflytop.handacid.app.service.DeviceService; import com.iflytop.handacid.common.result.Result; import io.swagger.v3.oas.annotations.Operation; @@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.*; public class DeviceController { private final DeviceState deviceState; private final DeviceService deviceService; + private final BleGamepadStateScheduledTask bleGamepadStateScheduledTask; @Operation(summary = "获取当前设备状态") @GetMapping("/device-status") @@ -29,6 +31,7 @@ public class DeviceController { @Operation(summary = "获取当前设备信息") @GetMapping("/device-info") public Result getDeviceInfo() { + bleGamepadStateScheduledTask.fetchState(); return Result.success(deviceState.getDeviceInfo()); } diff --git a/src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java b/src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java index fcf62e2..a64cf0a 100644 --- a/src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java +++ b/src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java @@ -9,6 +9,24 @@ import com.iflytop.handacid.common.enums.MotorDirection; * 生成给设备发送的指令 */ public class DeviceCommandGenerator { +//=========================================== 板卡 ================================================================= + + /** + * 获取板卡信息 + */ + public static DeviceCommand getHardWareBoard() { + return controlCmd(Device.HARDWARE_BOARD, Action.GET, null); + } + +//=========================================== 遥控手柄 ================================================================= + + /** + * 获取手柄信息 + */ + public static DeviceCommand getBleGamepad() { + return controlCmd(Device.BLE_GAMEPAD, Action.GET, null); + } + //=========================================== 阀 ================================================================= /** diff --git a/src/main/java/com/iflytop/handacid/app/core/listener/BleGamepadEventListener.java b/src/main/java/com/iflytop/handacid/app/core/listener/BleGamepadEventListener.java index b4789fb..9d4a41e 100644 --- a/src/main/java/com/iflytop/handacid/app/core/listener/BleGamepadEventListener.java +++ b/src/main/java/com/iflytop/handacid/app/core/listener/BleGamepadEventListener.java @@ -3,7 +3,6 @@ package com.iflytop.handacid.app.core.listener; import com.iflytop.handacid.app.core.state.DeviceState; import com.iflytop.handacid.app.scheduled.BleGamepadStateScheduledTask; import com.iflytop.handacid.app.service.ChannelCtrlService; -import com.iflytop.handacid.common.service.AuditRecordService; import com.iflytop.handacid.hardware.service.AppEventBusService; import com.iflytop.handacid.hardware.type.A8kPacket; import com.iflytop.handacid.hardware.type.CmdId; @@ -67,7 +66,7 @@ public class BleGamepadEventListener { } else if (CmdId.event_ble_gamepad_connected.equals(cmdId)) { log.info("蓝牙手柄 连接成功"); deviceState.getRemoteControlState().setConnected(true); - bleGamepadStateScheduledTask.fetchTrayState(); + bleGamepadStateScheduledTask.fetchState(); } else if (CmdId.event_ble_gamepad_disconnected.equals(cmdId)) { log.info("蓝牙手柄 连接断开"); deviceState.getRemoteControlState().setConnected(false); diff --git a/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java b/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java index c91a304..a9ace9e 100644 --- a/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java +++ b/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java @@ -1,8 +1,13 @@ package com.iflytop.handacid.app.scheduled; +import cn.hutool.json.JSONObject; +import com.iflytop.handacid.app.common.utils.CommandUtil; +import com.iflytop.handacid.app.core.command.CommandFuture; +import com.iflytop.handacid.app.core.command.DeviceCommand; +import com.iflytop.handacid.app.core.command.DeviceCommandGenerator; import com.iflytop.handacid.app.core.state.DeviceState; +import com.iflytop.handacid.app.service.DeviceCommandService; import com.iflytop.handacid.hardware.drivers.BleGamepadDriver; -import com.iflytop.handacid.hardware.type.BleGamepadMid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; @@ -15,23 +20,42 @@ import org.springframework.stereotype.Service; @Service @RequiredArgsConstructor public class BleGamepadStateScheduledTask { - private final BleGamepadDriver bleGamepadDriver; private final DeviceState deviceState; + private final DeviceCommandService deviceCommandService; + @Scheduled(fixedRate = 30 * 1000) - public void fetchTrayState() { + public void fetchState() { try { if (!deviceState.isVirtual()) { - boolean is_connect = bleGamepadDriver.is_connected(BleGamepadMid.BleGamePad); - deviceState.getRemoteControlState().setConnected(is_connect); - if (is_connect) { - Double power_state = bleGamepadDriver.get_power_state(BleGamepadMid.BleGamePad); - deviceState.getRemoteControlState().setBatteryLevel(power_state); - } - }else{ + DeviceCommand getHardWareBoardCommand = DeviceCommandGenerator.getHardWareBoard(); + CommandFuture getHardWareBoardCommandFuture = deviceCommandService.sendCommand(getHardWareBoardCommand); + CommandUtil.wait(getHardWareBoardCommandFuture); + DeviceCommand getBleGamepadCommand = DeviceCommandGenerator.getBleGamepad(); + CommandFuture getBleGamepadCommandFuture = deviceCommandService.sendCommand(getBleGamepadCommand); + CommandUtil.wait(getHardWareBoardCommandFuture, getBleGamepadCommandFuture); + + JSONObject bleGamepadJSONObject = getBleGamepadCommandFuture.getResponseResult(); + JSONObject hardWareBoardJSONObject = getHardWareBoardCommandFuture.getResponseResult(); + + boolean is_connected = bleGamepadJSONObject.getJSONObject("data").getBool("is_connected"); + int power = bleGamepadJSONObject.getJSONObject("data").getInt("power"); + String ioBoardVersion = hardWareBoardJSONObject.getJSONObject("data").getStr("io_board"); + String acidBoardVersion = hardWareBoardJSONObject.getJSONObject("data").getStr("acid_board"); + + deviceState.getRemoteControlState().setConnected(is_connected); + deviceState.getRemoteControlState().setBatteryLevel(power); + deviceState.getRemoteControlState().setCharging(false); + + deviceState.getDeviceInfo().setDeviceSoftwareVersion(acidBoardVersion); + deviceState.getDeviceInfo().setRemoteControlSoftwareVersion(acidBoardVersion); + + } else { deviceState.getRemoteControlState().setConnected(true); deviceState.getRemoteControlState().setBatteryLevel(88); deviceState.getRemoteControlState().setCharging(true); + deviceState.getDeviceInfo().setDeviceSoftwareVersion("123456"); + deviceState.getDeviceInfo().setRemoteControlSoftwareVersion("123456"); } } catch (Exception e) { log.error("定时采集蓝牙的连接状态和电量错误", e);