|
|
@ -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); |
|
|
|