考勤设备 websocket命令解析
This commit is contained in:
parent
021a486541
commit
0c7e9dc026
@ -13,18 +13,23 @@ import cn.iocoder.yudao.framework.websocket.core.session.WebSocketSessionManager
|
||||
import cn.iocoder.yudao.framework.websocket.core.util.WebSocketFrameworkUtils;
|
||||
import com.github.yulichang.toolkit.SpringContentUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
||||
/**
|
||||
* JSON 格式 {@link WebSocketHandler} 实现类
|
||||
*
|
||||
@ -73,11 +78,11 @@ public class JsonWebSocketMessageHandler extends TextWebSocketHandler {
|
||||
if ( cmd != null && cmd.equals("ping") ) {
|
||||
log.info("[设备心跳]"+ "||"+ session.getId()+ "||"+ message.getPayload(), session.getId(), message.getPayload());
|
||||
session.sendMessage(new TextMessage("{\"cmd\": \"pong\"}"));
|
||||
//TODO 更新设备心跳请求时间
|
||||
// 传入设备sn编码, 调用相关方法,更新对应sn的本次请求时间。
|
||||
// 并且将设备的状态,更新成在线状态
|
||||
|
||||
|
||||
// 更新设备心跳请求时间
|
||||
// 传入设备sn编码,更新对应sn的本次请求时间至redis。
|
||||
StringRedisTemplate redisTemplate = SpringContentUtils.getBean(StringRedisTemplate.class);
|
||||
redisTemplate.opsForValue().set(sn, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
redisTemplate.expire(sn, 30, TimeUnit.SECONDS);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
@ -90,34 +95,23 @@ public class JsonWebSocketMessageHandler extends TextWebSocketHandler {
|
||||
}
|
||||
|
||||
try {
|
||||
// 2.1 解析消息
|
||||
JsonWebSocketMessage jsonMessage = JsonUtils.parseObject(message.getPayload(), JsonWebSocketMessage.class);
|
||||
if (message.getPayload() == null) {
|
||||
log.error("[handleTextMessage][session({}) message({}) 解析为空]", session.getId(), message.getPayload());
|
||||
return;
|
||||
}
|
||||
JsonWebSocketMessage jsonMessage = new JsonWebSocketMessage();
|
||||
|
||||
//此处 因为web端和设备返回的信息格式不一致
|
||||
//所以先判断 信息来源
|
||||
JSONObject object = new JSONObject(message.getPayload());
|
||||
String cmd = object.getStr("cmd") ;
|
||||
String cmd = object.getStr("cmd");
|
||||
if (StrUtil.isEmpty( cmd )) {
|
||||
log.error("[handleTextMessage][session({}) message({}) 数据格错误]", session.getId(), message.getPayload());
|
||||
return;
|
||||
|
||||
cmd = new JSONObject(object.get("content")).getStr("cmd");
|
||||
}
|
||||
|
||||
if( cmd.equals(AttendanceConstants.CMD_TO_DEVICE) ) {
|
||||
jsonMessage.setType("attendance-message-send") ;
|
||||
//服务器下发数据到设备
|
||||
//TODO 记录服务器下数据
|
||||
if( cmd.equals(AttendanceConstants.CMD_TO_CLIENT) ) {
|
||||
|
||||
|
||||
}else if( cmd.equals(AttendanceConstants.CMD_TO_CLIENT) ) {
|
||||
jsonMessage.setType("attendance-message-send") ;
|
||||
//设备的响应数据返回服务器
|
||||
//{"cmd":"to_client","form":"QT74824","from":"QT74824","to":"system","data":{"cmd":"addUserRet","code":0,"msg":"下发成功","user_id":"999999"},"extra":"null"}
|
||||
String data = object.getStr("data") ;
|
||||
JSONObject dataObject = new JSONObject(data);
|
||||
String msg= dataObject.getStr("msg") ;
|
||||
// TODO 记录响应
|
||||
jsonMessage.setContent(object.toString());
|
||||
}else if (cmd.equals(AttendanceConstants.CMD_TO_DEVICE)){
|
||||
jsonMessage = JsonUtils.parseObject(message.getPayload(), JsonWebSocketMessage.class);
|
||||
}
|
||||
|
||||
// 2.2 获得对应的 WebSocketMessageListener 根据Type找到对应的处理监听器
|
||||
@ -128,14 +122,26 @@ public class JsonWebSocketMessageHandler extends TextWebSocketHandler {
|
||||
}
|
||||
// 2.3 处理消息
|
||||
Type type = TypeUtil.getTypeArgument(messageListener.getClass(), 0);
|
||||
JSONObject object1 = new JSONObject(message.getPayload());
|
||||
Object data = object1.get("data");
|
||||
object1.set("data",data.toString()) ;
|
||||
String string = object1.toString();
|
||||
Object messageObj = JsonUtils.parseObject(string, type);
|
||||
Object messageObj;
|
||||
if( cmd.equals(AttendanceConstants.CMD_TO_CLIENT) ) {
|
||||
|
||||
object.set("data", object.toString());
|
||||
messageObj = JsonUtils.parseObject(object.toString(), type);
|
||||
}else if (cmd.equals(AttendanceConstants.CMD_TO_DEVICE)){
|
||||
|
||||
Object to = object.get("to");
|
||||
Object data = object.get("content");
|
||||
object.set("data",data.toString());
|
||||
object.set("to", to.toString());
|
||||
String string = object.toString();
|
||||
messageObj = JsonUtils.parseObject(string, type);
|
||||
} else {
|
||||
messageObj = null;
|
||||
}
|
||||
|
||||
Long tenantId = WebSocketFrameworkUtils.getTenantId(session);
|
||||
TenantUtils.execute(tenantId, () -> messageListener.onMessage(session, messageObj));
|
||||
} catch (Throwable ex) {
|
||||
} catch (Exception ex) {
|
||||
log.error("[handleTextMessage][session({}) message({}) 处理异常]", session.getId(), message.getPayload());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user