优化连接方案
parent
3f8c99bc70
commit
7416dd2f43
|
@ -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();
|
||||
|
||||
/**
|
||||
* 分包符
|
||||
*/
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -34,21 +34,6 @@ public class VehicleOperateController {
|
|||
*/
|
||||
private final LinkedBlockingQueue<String> 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("<p>VIN:"+vin+" , 发送启动报文</p>");
|
||||
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("<p>VIN:"+vin+" , 发送关闭报文</p>");
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -50,10 +50,6 @@ public class NettyClientHandler extends ChannelInboundHandlerAdapter {
|
|||
break;
|
||||
}
|
||||
|
||||
if ("断开".equals(msg)){
|
||||
ctx.writeAndFlush("客户端退出").addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -78,6 +78,9 @@ public class NettyClientInit {
|
|||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
//解决意外关闭的情况,重新创建工作组,新的默认工作组
|
||||
Common.workerGroup = new NioEventLoopGroup();
|
||||
//优雅的关闭工作组
|
||||
Common.workerGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -72,6 +72,21 @@
|
|||
<span id="disconnect" class="label label-danger">已断开</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-2 divCss divBorderHeight" style="padding: 2px">
|
||||
<div class="col-md-6" style="padding: 5px 0px;">
|
||||
<span>车辆状态</span>
|
||||
<span>
|
||||
<span id="vehicleStart" class="label label-success" style="display: none">运行</span>
|
||||
<span id="vehicleStop" class="label label-danger">关闭</span>
|
||||
</span>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<button type="button" class="btn btn-sm btn-default" onclick="vehicleStart()">启动</button>
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<button type="button" class="btn btn-sm btn-default" onclick="vehicleStop()">关闭</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-1 divCss divBorderHeight" style="padding: 2px">
|
||||
<div class="col-md-4">
|
||||
<button type="button" class="btn btn-sm btn-default" onclick="startNettySendMsg()">上报</button>
|
||||
|
@ -529,9 +544,11 @@
|
|||
</div>
|
||||
</body>
|
||||
<script>
|
||||
//日志定时器
|
||||
var nettyLogTime = null;
|
||||
//消息发送定时器
|
||||
var nettyMsgTime = null;
|
||||
|
||||
//数据模拟定时器
|
||||
var vehicleImitateData = null;
|
||||
/**
|
||||
* 连接服务器
|
||||
|
@ -569,7 +586,48 @@
|
|||
$("#disconnect").css("display","");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* 上线 */
|
||||
function vehicleStart() {
|
||||
$.muyuSend.get("/vehicle/start",undefined,function (response) {
|
||||
if (response.code == 100){
|
||||
if (response.data.status == 1){
|
||||
vehicleStartDis();
|
||||
}else {
|
||||
vehicleStopDis();
|
||||
}
|
||||
}else {
|
||||
$.modal.msgError(response.msg);
|
||||
}
|
||||
})
|
||||
}
|
||||
/* 下线 */
|
||||
function vehicleStop() {
|
||||
$.muyuSend.get("/vehicle/stop",undefined,function (response) {
|
||||
if (response.code == 100){
|
||||
if (response.data.status == 1){
|
||||
vehicleStartDis();
|
||||
}else {
|
||||
vehicleStopDis();
|
||||
}
|
||||
}else {
|
||||
$.modal.msgError(response.msg);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function vehicleStartDis() {
|
||||
$("#vehicleStart").css("display","");
|
||||
$("#vehicleStop").css("display","none");
|
||||
}
|
||||
function vehicleStopDis() {
|
||||
$("#vehicleStart").css("display","none");
|
||||
$("#vehicleStop").css("display","");
|
||||
}
|
||||
|
||||
$(function () {
|
||||
//查询netty状态
|
||||
$.muyuSend.get("/nettyClient/status",undefined,function (response) {
|
||||
if (response.code == 100){
|
||||
connectEnd();
|
||||
|
@ -577,37 +635,29 @@
|
|||
disconnect();
|
||||
}
|
||||
})
|
||||
//查询车辆状态
|
||||
$.muyuSend.get("/vehicle",undefined,function (response) {
|
||||
if (response.code == 100){
|
||||
if (response.data.status == 1){
|
||||
vehicleStartDis();
|
||||
}else {
|
||||
vehicleStopDis();
|
||||
}
|
||||
}
|
||||
})
|
||||
startNettyLog();
|
||||
})
|
||||
function addLog(msg){
|
||||
$("#vehicleMsgLog").append(msg);
|
||||
}
|
||||
function startNettySendMsg(){
|
||||
var vin = $("#vin").val();
|
||||
if (vin != null && vin != ''){
|
||||
$.muyuSend.get("/vehicle/operate/start/"+$("#vin").val(),undefined,function (response) {
|
||||
if (response.code == 100){
|
||||
nettyMsgTime = setInterval("sendNettySendMsg()", 1000);
|
||||
}else {
|
||||
alert(response.msg);
|
||||
}
|
||||
})
|
||||
}else {
|
||||
$.modal.msgWarning("请输入VIN");
|
||||
}
|
||||
|
||||
|
||||
nettyMsgTime = setInterval("sendNettySendMsg()", 1000);
|
||||
$.modal.msgSuccess("车辆开始上报")
|
||||
}
|
||||
function stopNettyStopMsg(){
|
||||
clearInterval(nettyMsgTime);
|
||||
nettyMsgTime = null;
|
||||
$.muyuSend.get("/vehicle/operate/stop/"+$("#vin").val(),undefined,function (response) {
|
||||
if (response.code == 100){
|
||||
$.modal.msgSuccess("车辆下线成功")
|
||||
}else {
|
||||
alert(response.msg);
|
||||
}
|
||||
})
|
||||
$.modal.msgSuccess("车辆结束上报")
|
||||
}
|
||||
function sendNettySendMsg(){
|
||||
$.muyuSend.post("/vehicle/operate/sendMsg",getFormToMap("vehicleForm"),function (response) {
|
||||
|
|
Loading…
Reference in New Issue