update
This commit is contained in:
@@ -7,6 +7,7 @@ public enum MesType {
|
||||
have_logged_in,//消息
|
||||
device_status_changed,//上下线状态变化
|
||||
device_error_push, //错误推送
|
||||
device_task_change, //任务状态变化推送
|
||||
heartbeat,
|
||||
remoteControl,
|
||||
detectionReport,//障碍物消息
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.maibu.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class WebDeviceTaskStatusMessageDTO {
|
||||
|
||||
private MesType event;
|
||||
|
||||
private String deviceId;
|
||||
|
||||
private Long taskId;
|
||||
|
||||
private String status;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.maibu.netty.handler;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.maibu.common.MiddleCommandConstant;
|
||||
import com.maibu.common.MiddleConstant;
|
||||
import com.maibu.core.enums.RespondCode;
|
||||
import com.maibu.dto.*;
|
||||
import com.maibu.memory.MiddleGlobalMemory;
|
||||
import com.maibu.netty.NettyClient;
|
||||
@@ -39,17 +40,18 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
try {
|
||||
byte[] data = (byte[]) msg;
|
||||
logger.debug("channelRead2: {}", Arrays.toString(data));
|
||||
//假如是心跳包
|
||||
// 假如是心跳包
|
||||
String key = ctx.channel().attr(MiddleConstant.ATT_MASTER_KEY).get();
|
||||
WebSocket webSocket = MiddleGlobalMemory.onlineSockets.get(key);
|
||||
if (isHeartbeat(data)) {
|
||||
//websocket 不在了停止发 心跳
|
||||
// websocket 不在了停止发 心跳
|
||||
if (webSocket != null && webSocket.isOpen()) {
|
||||
ctx.writeAndFlush(data);
|
||||
}
|
||||
NettyClient nettyClient = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||
if (nettyClient != null && StringUtils.isEmpty(nettyClient.currentDeviceId)) {
|
||||
if (nettyClient.lastSwitchTime == -1 || System.currentTimeMillis() - nettyClient.lastSwitchTime > nettyClient.Interval) {
|
||||
if (nettyClient.lastSwitchTime == -1
|
||||
|| System.currentTimeMillis() - nettyClient.lastSwitchTime > nettyClient.Interval) {
|
||||
if (!StringUtils.isEmpty(nettyClient.requestDeviceId) && !StringUtils.isEmpty(key)) {
|
||||
switchDevice(nettyClient, key);
|
||||
nettyClient.lastSwitchTime = System.currentTimeMillis();
|
||||
@@ -76,7 +78,7 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
if (StringUtils.isNotBlank(json)) {
|
||||
JSONObject obj = new JSONObject(json);
|
||||
if (obj.get("event") != null) {
|
||||
//状态变化 上下线
|
||||
// 状态变化 上下线
|
||||
String eventType = obj.get("event").toString();
|
||||
String deviceId = obj.get("deviceId").toString();
|
||||
if ("device_status_changed".equals(eventType)) {
|
||||
@@ -91,8 +93,8 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
}
|
||||
WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto));
|
||||
} else if ("device_error_push".equals(eventType)) {
|
||||
//todo 更新推送的错误
|
||||
String details = obj.get("details").toString();
|
||||
// todo 更新推送的错误
|
||||
// String details = obj.get("details").toString();
|
||||
WebOlineStatusMessageDTO dto = new WebOlineStatusMessageDTO();
|
||||
dto.setDeviceId(deviceId);
|
||||
dto.setType(MesType.device_error_push);
|
||||
@@ -100,6 +102,15 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
dto.setDescription("");
|
||||
dto.setErrorCode("");
|
||||
WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto));
|
||||
} else if ("device_task_change".equals(eventType)) {
|
||||
String taskId = obj.get("taskId").toString();
|
||||
String status = obj.get("status").toString();
|
||||
WebDeviceTaskStatusMessageDTO dto = new WebDeviceTaskStatusMessageDTO();
|
||||
dto.setDeviceId(deviceId);
|
||||
dto.setEvent(MesType.device_task_change);
|
||||
dto.setTaskId(Long.valueOf(taskId));
|
||||
dto.setStatus(status);
|
||||
WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto));
|
||||
}
|
||||
} else {
|
||||
/* 判断 respond 字段 */
|
||||
@@ -107,7 +118,7 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
String deviceId = obj.get("deviceId").toString();
|
||||
String userName = obj.get("userId").toString();
|
||||
String platform = obj.get("platform").toString();
|
||||
//直接转发 websocket 申请
|
||||
// 直接转发 websocket 申请
|
||||
WebAuthResponseDTO request = new WebAuthResponseDTO();
|
||||
request.setType(MesType.switchPermission);
|
||||
request.setUserName(userName);
|
||||
@@ -119,44 +130,47 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
if (obj.get("respond") != null) {
|
||||
if ("have_logged_in".equals(obj.get("respond").toString())) {
|
||||
/* 如果回复 have_logged_in 则调用logout */
|
||||
//System.out.println("检测到重复登录,执行 logout");
|
||||
//NettyClient nettyClient = GlobalMemory.nettyClientMap.get(key);
|
||||
//WebHasLoginDTO dto = new WebHasLoginDTO();
|
||||
//dto.setToken(nettyClient.token);
|
||||
//dto.setUserName(nettyClient.userName);
|
||||
//dto.setType(MesType.have_logged_in);
|
||||
//WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto));
|
||||
// System.out.println("检测到重复登录,执行 logout");
|
||||
// NettyClient nettyClient = GlobalMemory.nettyClientMap.get(key);
|
||||
// WebHasLoginDTO dto = new WebHasLoginDTO();
|
||||
// dto.setToken(nettyClient.token);
|
||||
// dto.setUserName(nettyClient.userName);
|
||||
// dto.setType(MesType.have_logged_in);
|
||||
// WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto));
|
||||
} else {
|
||||
//todo respond netty_server返回结果处理
|
||||
DeviceRespondDTO respond = JsonUtils.parseObject(obj.get("respond").toString(), DeviceRespondDTO.class);
|
||||
// todo respond netty_server返回结果处理
|
||||
DeviceRespondDTO respond = JsonUtils.parseObject(obj.get("respond").toString(),
|
||||
DeviceRespondDTO.class);
|
||||
if (respond != null) {
|
||||
//直接转发 websocket 申请
|
||||
// 直接转发 websocket 申请
|
||||
WebSwitchControlResponseDTO responseDTO = new WebSwitchControlResponseDTO();
|
||||
responseDTO.setType(MesType.switchResult);
|
||||
responseDTO.setRespond(respond);
|
||||
WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(responseDTO));
|
||||
WebsocketHandler.sendMessageToClient(webSocket,
|
||||
JsonUtils.toJsonString(responseDTO));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (obj.get("type").toString() != null && "detectionReport".equals(obj.get("type").toString())) {
|
||||
if (obj.get("type").toString() != null
|
||||
&& "detectionReport".equals(obj.get("type").toString())) {
|
||||
WebsocketHandler.sendMessageToClient(webSocket, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//error 推送
|
||||
// error 推送
|
||||
}
|
||||
|
||||
//TODO web 无切换控制请求
|
||||
// TODO web 无切换控制请求
|
||||
} catch (Exception e) {
|
||||
logger.error("事件推送解析失败:{}", e.getMessage());
|
||||
}
|
||||
} else if (isStatusDate(data)) {
|
||||
//状态消息推送给前端
|
||||
// 状态消息推送给前端
|
||||
WebStatusMessageDTO dto = createWebDeviceStatusMessage(data);
|
||||
WebsocketHandler.sendMessageToClient(webSocket, JsonUtils.toJsonString(dto));
|
||||
} else if (isPath(data)) {
|
||||
//已到达
|
||||
// 已到达
|
||||
if (data[5] == (byte) 0x01) {
|
||||
|
||||
}
|
||||
@@ -289,7 +303,7 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
detail17.setUnit("");
|
||||
transferData.add(detail17);
|
||||
|
||||
//18 时间暂未给值
|
||||
// 18 时间暂未给值
|
||||
|
||||
DeviceStatusDetail detail19 = new DeviceStatusDetail();
|
||||
detail19.setName("cuttingSpeed");
|
||||
@@ -324,13 +338,12 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
return transferData;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) {
|
||||
|
||||
logger.debug("连接开始channelActive:ChannelId:{} ", ctx.channel().id().asLongText());
|
||||
|
||||
//websocket连上后连接netty成功后发送登录认证包
|
||||
// websocket连上后连接netty成功后发送登录认证包
|
||||
// key username + : web + token
|
||||
String key = ctx.channel().attr(MiddleConstant.ATT_MASTER_KEY).get();
|
||||
System.out.println(key);
|
||||
@@ -350,11 +363,10 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
packet[tailStart + 3] = (byte) 0xAB;
|
||||
|
||||
ctx.writeAndFlush(packet);
|
||||
// startHeartbeat(nettyClient, ctx, key);
|
||||
// startHeartbeat(nettyClient, ctx, key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void switchDevice(NettyClient nettyClient, String key) {
|
||||
try {
|
||||
// 2. 调用 POST 接口:传入 Token B(与 Token A 不同)
|
||||
@@ -368,9 +380,9 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
if (resultDTO != null && resultDTO.getData() != null) {
|
||||
if ((boolean) resultDTO.getData()) {
|
||||
logger.debug("用户:{}切换设备:{}成功", key, nettyClient.requestDeviceId);
|
||||
//切换成功推送 websocket
|
||||
// 切换成功推送 websocket
|
||||
nettyClient.currentDeviceId = nettyClient.requestDeviceId;
|
||||
// nettyClient.requestDeviceId = null;
|
||||
// nettyClient.requestDeviceId = null;
|
||||
WebSocket webSocket = MiddleGlobalMemory.onlineSockets.get(key);
|
||||
if (webSocket != null && webSocket.isOpen()) {
|
||||
WebAuthResponseDTO responseDTO = new WebAuthResponseDTO();
|
||||
@@ -389,7 +401,6 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 启动心跳检测
|
||||
*/
|
||||
@@ -422,7 +433,7 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
public void channelInactive(ChannelHandlerContext ctx) {
|
||||
String key = ctx.channel().attr(MiddleConstant.ATT_MASTER_KEY).get();
|
||||
NettyClient nettyClient = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||
// stopHeartbeat(nettyClient);
|
||||
// stopHeartbeat(nettyClient);
|
||||
nettyClient.currentDeviceId = null;
|
||||
logger.debug("连接断开channelInactive,ChannelId:{} ", ctx.channel().id().asLongText());
|
||||
WebSocket webSocket = MiddleGlobalMemory.onlineSockets.get(key);
|
||||
@@ -435,7 +446,7 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
String key = ctx.channel().attr(MiddleConstant.ATT_MASTER_KEY).get();
|
||||
NettyClient nettyClient = MiddleGlobalMemory.nettyClientMap.get(key);
|
||||
// stopHeartbeat(nettyClient);
|
||||
// stopHeartbeat(nettyClient);
|
||||
cause.printStackTrace();
|
||||
logger.error("异常断开exceptionCaught:{}", cause.getMessage());
|
||||
ctx.close();
|
||||
@@ -471,9 +482,9 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
data[data.length - 2] == (byte) 0xAA &&
|
||||
data[data.length - 1] == (byte) 0xAB;
|
||||
|
||||
// // 只要是以 '{' 开头、以 '}' 结尾,就认为是事件 JSON
|
||||
// if (data == null || data.length < 2) return false;
|
||||
// return data[0] == '{' && data[data.length - 1] == '}';
|
||||
// // 只要是以 '{' 开头、以 '}' 结尾,就认为是事件 JSON
|
||||
// if (data == null || data.length < 2) return false;
|
||||
// return data[0] == '{' && data[data.length - 1] == '}';
|
||||
}
|
||||
|
||||
private boolean isStatusDate(byte[] data) {
|
||||
|
||||
Reference in New Issue
Block a user