diff --git a/src/main/java/com/muyu/common/Config.java b/src/main/java/com/muyu/common/Config.java index 746970e..af9cfe6 100644 --- a/src/main/java/com/muyu/common/Config.java +++ b/src/main/java/com/muyu/common/Config.java @@ -1,11 +1,18 @@ package com.muyu.common; +import com.muyu.pojo.Vehicle; import io.netty.channel.ChannelHandlerContext; /** * netty 核心配置 */ public class Config { + + /** + * 车辆对象 + */ + public final static Vehicle VEHICLE = new Vehicle(); + /** * 分包符 */ diff --git a/src/main/java/com/muyu/controller/VehicleController.java b/src/main/java/com/muyu/controller/VehicleController.java new file mode 100644 index 0000000..5a5e3be --- /dev/null +++ b/src/main/java/com/muyu/controller/VehicleController.java @@ -0,0 +1,57 @@ +package com.muyu.controller; + +import com.muyu.common.Config; +import com.muyu.common.Response; +import com.muyu.netty.operate.NettyClientMsg; +import com.muyu.pojo.Vehicle; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author 牧鱼 + * @Classname VehicleController + * @Description 车辆状态controller方法 + * @Date 2021/12/8 16:45 + */ +@RestController +@RequestMapping("/vehicle") +public class VehicleController { + + /** + * 查看状态 + * @return + */ + @GetMapping + public Response getVehicle(){ + return Response.success(Config.VEHICLE); + } + + /** + * 开启 + * @return + */ + @GetMapping("/start") + public Response startVehicle(){ + if (Config.ctx == null){ + return Response.error("未连接netty服务器"); + } + Config.VEHICLE.setStatus(1); + NettyClientMsg.sendMsg(Config.VEHICLE_START_SUF+Config.VIN); + return Response.success(Config.VEHICLE); + } + + /** + * 停止 + * @return + */ + @GetMapping("/stop") + public Response stopVehicle(){ + if (Config.ctx == null){ + return Response.error("未连接netty服务器"); + } + Config.VEHICLE.setStatus(0); + NettyClientMsg.sendMsg(Config.VEHICLE_STOP_SUF+Config.VIN); + return Response.success(Config.VEHICLE); + } +} diff --git a/src/main/java/com/muyu/controller/VehicleOperateController.java b/src/main/java/com/muyu/controller/VehicleOperateController.java index da706a4..db2aeec 100644 --- a/src/main/java/com/muyu/controller/VehicleOperateController.java +++ b/src/main/java/com/muyu/controller/VehicleOperateController.java @@ -34,21 +34,6 @@ public class VehicleOperateController { */ private final LinkedBlockingQueue locusQueue = new LinkedBlockingQueue<>(); - /** - * 车辆启动报文 - * @param vin - * @return - */ - @GetMapping("/start/{vin}") - public Response sendVehicleStart(@PathVariable(value = "vin") String vin){ - if (Config.ctx == null){ - return Response.error("未与netty服务器建立连接"); - } - NettyClientMsg.sendMsg(Config.VEHICLE_START_SUF + vin); - NettyClientLogQueue.add("

VIN:"+vin+" , 发送启动报文

"); - return Response.success(); - } - /** * 通过VIN选择路线 * @param vin @@ -92,18 +77,5 @@ public class VehicleOperateController { } - /** - * 车辆关闭报文 - * @param vin - * @return - */ - @GetMapping("/stop/{vin}") - public Response sendVehicleStop(@PathVariable(value = "vin") String vin){ - if (Config.ctx == null){ - return Response.error("未与netty服务器建立连接"); - } - NettyClientMsg.sendMsg(Config.VEHICLE_STOP_SUF + vin); - NettyClientLogQueue.add("

VIN:"+vin+" , 发送关闭报文

"); - return Response.success(); - } + } diff --git a/src/main/java/com/muyu/netty/client/NettyClientHandler.java b/src/main/java/com/muyu/netty/client/NettyClientHandler.java index 8bb9e18..4494f08 100644 --- a/src/main/java/com/muyu/netty/client/NettyClientHandler.java +++ b/src/main/java/com/muyu/netty/client/NettyClientHandler.java @@ -50,10 +50,6 @@ public class NettyClientHandler extends ChannelInboundHandlerAdapter { break; } - if ("断开".equals(msg)){ - ctx.writeAndFlush("客户端退出").addListener(ChannelFutureListener.CLOSE); - } - } /** diff --git a/src/main/java/com/muyu/netty/client/NettyClientInit.java b/src/main/java/com/muyu/netty/client/NettyClientInit.java index f0c5cac..ad895d6 100644 --- a/src/main/java/com/muyu/netty/client/NettyClientInit.java +++ b/src/main/java/com/muyu/netty/client/NettyClientInit.java @@ -78,6 +78,9 @@ public class NettyClientInit { } catch (Exception e) { e.printStackTrace(); } finally { + //解决意外关闭的情况,重新创建工作组,新的默认工作组 + Common.workerGroup = new NioEventLoopGroup(); + //优雅的关闭工作组 Common.workerGroup.shutdownGracefully(); } } diff --git a/src/main/java/com/muyu/pojo/Vehicle.java b/src/main/java/com/muyu/pojo/Vehicle.java new file mode 100644 index 0000000..dd88533 --- /dev/null +++ b/src/main/java/com/muyu/pojo/Vehicle.java @@ -0,0 +1,26 @@ +package com.muyu.pojo; + +/** + * @author 牧鱼 + * @Classname Vehicle + * @Description 车辆基础类 + * @Date 2021/12/8 16:26 + */ +public class Vehicle { + + /** + * 车辆状态:0:未启动 1:启动 + */ + private int status = 0; + + public int isStatus() { + return status; + } + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } +} diff --git a/src/main/resources/templates/index.ftlh b/src/main/resources/templates/index.ftlh index a4e39e3..4d45c7d 100644 --- a/src/main/resources/templates/index.ftlh +++ b/src/main/resources/templates/index.ftlh @@ -72,6 +72,21 @@ 已断开 +
+
+ 车辆状态 + + + 关闭 + +
+
+ +
+
+ +
+
@@ -529,9 +544,11 @@