diff --git a/src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStopCommand.java b/src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStopCommand.java index 2b6685b..7a7e2a6 100644 --- a/src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStopCommand.java +++ b/src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStopCommand.java @@ -32,9 +32,7 @@ public class SolutionAddStopCommand extends BaseCommandHandler { @Override public CompletableFuture handle(CommandDTO commandDTO) { - return runAsync(() -> { - channelCtrlService.solutionAddStop(); - }); + return runAsync(channelCtrlService::solutionAddStop); } } 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 a75b34d..e6c2632 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 @@ -14,6 +14,7 @@ import org.springframework.stereotype.Component; /** * 蓝牙手柄按钮事件监听 + * */ @Component diff --git a/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java b/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java new file mode 100644 index 0000000..ab31fd1 --- /dev/null +++ b/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java @@ -0,0 +1,39 @@ +package com.iflytop.handacid.app.scheduled; + +import com.iflytop.handacid.app.core.state.DeviceState; +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; +import org.springframework.stereotype.Service; + +/** + * 定时采集蓝牙的连接状态和电量 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class BleGamepadStateScheduledTask { + private final BleGamepadDriver bleGamepadDriver; + private final DeviceState deviceState; + + @Scheduled(fixedRate = 30 * 1000) + public void fetchTrayState() { + try { + + if (!deviceState.isVirtual()) { + boolean is_connect = bleGamepadDriver.is_connected(BleGamepadMid.BleGamePad); + deviceState.getRemoteControlState().setConnected(is_connect); + if (is_connect) { + Integer[] power_state = bleGamepadDriver.get_power_state(BleGamepadMid.BleGamePad); + deviceState.getRemoteControlState().setBatteryLevel(power_state[0]); + deviceState.getRemoteControlState().setCharging(power_state[1] != 0); + } + } + } catch (Exception e) { + log.error("定时采集蓝牙的连接状态和电量错误", e); + } + + } +} diff --git a/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java b/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java index 7f74e41..03346d3 100644 --- a/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java +++ b/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java @@ -1,6 +1,5 @@ package com.iflytop.handacid.app.service; -import cn.hutool.json.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.iflytop.handacid.app.common.enums.ChannelCode; import com.iflytop.handacid.app.common.enums.ChannelStateCode; diff --git a/src/main/java/com/iflytop/handacid/app/service/DeviceInitService.java b/src/main/java/com/iflytop/handacid/app/service/DeviceInitService.java index cb029e7..9f23c48 100644 --- a/src/main/java/com/iflytop/handacid/app/service/DeviceInitService.java +++ b/src/main/java/com/iflytop/handacid/app/service/DeviceInitService.java @@ -54,10 +54,9 @@ public class DeviceInitService { new Thread(() -> { try { log.info("初始化开始"); - initDeviceState(); initDeviceSetData();//初始化设备参数 initEnable();//使能 - // initDeviceState();//初始化设备状态 + initDeviceState(); log.info("初始化完毕"); } catch (Exception e) { throw new RuntimeException(e); diff --git a/src/main/java/com/iflytop/handacid/hardware/comm/can/A8kCanBusConnection.java b/src/main/java/com/iflytop/handacid/hardware/comm/can/A8kCanBusConnection.java index c061fcd..3a4208d 100644 --- a/src/main/java/com/iflytop/handacid/hardware/comm/can/A8kCanBusConnection.java +++ b/src/main/java/com/iflytop/handacid/hardware/comm/can/A8kCanBusConnection.java @@ -9,6 +9,7 @@ import com.iflytop.handacid.hardware.type.CmdId; import com.iflytop.handacid.hardware.type.MId; import com.iflytop.handacid.hardware.type.ModuleStatus; import com.iflytop.handacid.hardware.type.appevent.A8kCanBusOnConnectEvent; +import com.iflytop.handacid.hardware.type.appevent.A8kHardwareReport; import com.iflytop.handacid.hardware.type.error.A8kEcode; import com.iflytop.handacid.hardware.type.error.AEHardwareError; import com.iflytop.handacid.hardware.utils.OS; @@ -38,8 +39,8 @@ public class A8kCanBusConnection extends WebSocketClient { @Resource AppEventBusService eventBus; - String datachurl = null; - String cmdchurl = null; + String datachurl = null; + String cmdchurl = null; Boolean firstCall = true; @Value("${device.enableCanBus}") @@ -48,12 +49,12 @@ public class A8kCanBusConnection extends WebSocketClient { static class ProcessContext { BlockingQueue receiptQueue = new LinkedBlockingQueue<>(); // - A8kPacket cmdPacket; - Map txcmdcache = new HashMap<>(); + A8kPacket cmdPacket; + Map txcmdcache = new HashMap<>(); public A8kPacket getReceipt(int overtime) { - long end = System.currentTimeMillis() + overtime; + long end = System.currentTimeMillis() + overtime; A8kPacket packet = null; while (System.currentTimeMillis() < end) { try { @@ -96,7 +97,7 @@ public class A8kCanBusConnection extends WebSocketClient { synchronized void storageTxLastCmd(A8kPacket pack) { - MId mid = MId.valueOf(pack.getModuleId()); + MId mid = MId.valueOf(pack.getModuleId()); CmdId cmdId = CmdId.valueOf(pack.getCmdId()); if (cmdId != null && cmdId.isActionCmd()) txcmdcache.put(mid, cmdId); @@ -108,8 +109,8 @@ public class A8kCanBusConnection extends WebSocketClient { } - ProcessContext context = new ProcessContext(); - int packetIndex = 0;//发送包的packetIndex + ProcessContext context = new ProcessContext(); + int packetIndex = 0;//发送包的packetIndex public A8kCanBusConnection(String cmdchurl, String datachurl) { @@ -117,26 +118,30 @@ public class A8kCanBusConnection extends WebSocketClient { log.info("new A8kCanBusConnection: {} {}", cmdchurl, datachurl); this.datachurl = datachurl; - this.cmdchurl = cmdchurl; - packetIndex = 0; + this.cmdchurl = cmdchurl; + packetIndex = 0; } - @Override public void onOpen(ServerHandshake serverHandshake) { + @Override + public void onOpen(ServerHandshake serverHandshake) { log.info("a8k canbus connect sucess"); eventBus.pushEvent(new A8kCanBusOnConnectEvent()); } - @Override public void onMessage(String s) { + @Override + public void onMessage(String s) { log.trace("RX-RAW: {}", s); processCanRxMessage(s); } - @Override public void onClose(int i, String s, boolean b) { + @Override + public void onClose(int i, String s, boolean b) { log.warn("a8k canbus lost connection..."); } - @Override public void onError(Exception e) { + @Override + public void onError(Exception e) { log.info("a8k can-websocket-channel on error"); } @@ -293,7 +298,7 @@ public class A8kCanBusConnection extends WebSocketClient { } /** - * 处理接收到的消息 + * 处理接收到的消息 */ private void processCanRxMessage(String s) { byte[] rx = ByteArray.hexStringToBytes(s); @@ -321,11 +326,11 @@ public class A8kCanBusConnection extends WebSocketClient { if (packet.getPacketType() == A8kPacket.PACKET_TYPE_ACK || packet.getPacketType() == A8kPacket.PACKET_TYPE_ERROR_ACK) { - log.trace("RX-ACK |RAW:{}| {}", s, packet); + log.debug("RX-ACK |RAW:{}| {}", s, packet); context.pushReceipt(packet); } else if (packet.getPacketType() == A8kPacket.PACKET_TYPE_EVENT) { - log.trace("RX-REPORT |RAW:{}| {}", s, packet); -// eventBus.pushEvent(new A8kHardwareReport(packet)); + log.debug("RX-REPORT |RAW:{}| {}", s, packet); + eventBus.pushEvent(new A8kHardwareReport(packet)); } else { log.warn("RX-UNPROCESSABLE: |RAW:{}| {}", s, packet); } @@ -342,9 +347,9 @@ public class A8kCanBusConnection extends WebSocketClient { public HttpResponse callLocalCmd(String path) { // 创建一个HttpClient实例 - HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(String.format("%s/%s", cmdchurl, path))).build(); - HttpResponse ret = null; + HttpClient httpClient = HttpClient.newHttpClient(); + HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(String.format("%s/%s", cmdchurl, path))).build(); + HttpResponse ret = null; try { ret = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); } catch (Exception e) { @@ -355,9 +360,9 @@ public class A8kCanBusConnection extends WebSocketClient { } public static void main(String[] args) { - HttpClient httpClient = HttpClient.newHttpClient(); - HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create("http://192.168.8.10:19004/zexcan/restart")).build(); - HttpResponse ret = null; + HttpClient httpClient = HttpClient.newHttpClient(); + HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create("http://192.168.8.10:19004/zexcan/restart")).build(); + HttpResponse ret = null; try { ret = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); log.info("ret: \t{}", ret.body()); diff --git a/src/main/java/com/iflytop/handacid/hardware/drivers/BleGamepadDriver.java b/src/main/java/com/iflytop/handacid/hardware/drivers/BleGamepadDriver.java index 60ff803..48745ce 100644 --- a/src/main/java/com/iflytop/handacid/hardware/drivers/BleGamepadDriver.java +++ b/src/main/java/com/iflytop/handacid/hardware/drivers/BleGamepadDriver.java @@ -21,10 +21,11 @@ public class BleGamepadDriver { } - public void get_power_state(BleGamepadMid id, Integer power, Integer charge_state) throws HardwareException { + public Integer[] get_power_state(BleGamepadMid id) throws HardwareException { A8kPacket packet = canBus.callcmd(id.mid, CmdId.ble_gamepad_power_info); - power = packet.getContentI32(0); - charge_state = packet.getContentI32(1); + Integer power = packet.getContentI32(0); + Integer charge_state = packet.getContentI32(1); log.info("power: {}, charge_state: {}", power, charge_state); + return new Integer[]{power, charge_state}; } } \ No newline at end of file diff --git a/src/main/java/com/iflytop/handacid/hardware/service/AppEventBusService.java b/src/main/java/com/iflytop/handacid/hardware/service/AppEventBusService.java index 3e0508e..bf2c23b 100644 --- a/src/main/java/com/iflytop/handacid/hardware/service/AppEventBusService.java +++ b/src/main/java/com/iflytop/handacid/hardware/service/AppEventBusService.java @@ -15,7 +15,6 @@ import java.util.concurrent.LinkedBlockingQueue; /** * 事件总线 - * */ @Component public class AppEventBusService implements ApplicationListener { @@ -27,9 +26,9 @@ public class AppEventBusService implements ApplicationListener eventQueue = new LinkedBlockingQueue<>(); - List listeners = new ArrayList<>(); + List listeners = new ArrayList<>(); @PostConstruct public void init() {