聊天通讯
This commit is contained in:
parent
deae225785
commit
d18dd51d52
|
@ -16,6 +16,9 @@ import org.springframework.web.socket.WebSocketSession;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 网站发送给仪器给药信息
|
||||||
|
*/
|
||||||
public class AddMedicineHandler implements WebSocketHandler {
|
public class AddMedicineHandler implements WebSocketHandler {
|
||||||
@Resource
|
@Resource
|
||||||
private VitalSignTimer vitalSignTimer;
|
private VitalSignTimer vitalSignTimer;
|
||||||
|
|
|
@ -1,29 +1,60 @@
|
||||||
package com.rax.vital.handler;
|
package com.rax.vital.handler;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.rax.vital.medicine.service.ChatService;
|
||||||
|
import com.rax.vital.util.GetHttpParamUtil;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
||||||
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
||||||
|
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
|
||||||
import org.springframework.web.socket.CloseStatus;
|
import org.springframework.web.socket.CloseStatus;
|
||||||
import org.springframework.web.socket.WebSocketHandler;
|
import org.springframework.web.socket.WebSocketHandler;
|
||||||
import org.springframework.web.socket.WebSocketMessage;
|
import org.springframework.web.socket.WebSocketMessage;
|
||||||
import org.springframework.web.socket.WebSocketSession;
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class ChatHandler implements WebSocketHandler {
|
public class ChatHandler implements WebSocketHandler {
|
||||||
|
@Resource
|
||||||
|
private OAuth2AuthorizationService authorizationService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ChatService chatService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
||||||
|
String decode = URLDecoder.decode(session.getUri().getQuery());
|
||||||
|
Map params = GetHttpParamUtil.getParams(decode);
|
||||||
|
String token = (String) params.get("token");
|
||||||
|
OAuth2Authorization authorization = authorizationService.findByToken(token, OAuth2TokenType.ACCESS_TOKEN);
|
||||||
|
if (authorization != null) {
|
||||||
|
String username = authorization.getPrincipalName();
|
||||||
|
|
||||||
|
String payload = (String) message.getPayload();
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(payload);
|
||||||
|
String patientName = jsonObject.getString("patientName");
|
||||||
|
String idNum = jsonObject.getString("idNum");
|
||||||
|
String date = jsonObject.getString("date");
|
||||||
|
// 消息内容
|
||||||
|
String msg = jsonObject.getString("msg");
|
||||||
|
chatService.sendMessage(username, patientName, idNum, date, session, msg);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
|
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
|
||||||
|
chatService.stopTaskMongo(session.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.rax.vital.handler;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.rax.vital.timer.VitalSignTimer;
|
import com.rax.vital.timer.VitalSignTimer;
|
||||||
|
import com.rax.vital.util.DatabaseNameUtil;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
||||||
import org.springframework.web.socket.CloseStatus;
|
import org.springframework.web.socket.CloseStatus;
|
||||||
|
@ -9,6 +10,9 @@ import org.springframework.web.socket.WebSocketHandler;
|
||||||
import org.springframework.web.socket.WebSocketMessage;
|
import org.springframework.web.socket.WebSocketMessage;
|
||||||
import org.springframework.web.socket.WebSocketSession;
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 仪器获取网站给药信息
|
||||||
|
*/
|
||||||
public class MachineFeedbackHandler implements WebSocketHandler {
|
public class MachineFeedbackHandler implements WebSocketHandler {
|
||||||
@Resource
|
@Resource
|
||||||
private OAuth2AuthorizationService authorizationService;
|
private OAuth2AuthorizationService authorizationService;
|
||||||
|
@ -24,9 +28,16 @@ public class MachineFeedbackHandler implements WebSocketHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
||||||
JSONObject jsonObject = JSONObject.parseObject((String) message.getPayload());
|
JSONObject jsonObject = JSONObject.parseObject((String) message.getPayload());
|
||||||
String database = jsonObject.getString("database");
|
// 病人名
|
||||||
|
String patientName = jsonObject.getString("patientName");
|
||||||
|
// 病人身份证
|
||||||
|
String idNum = jsonObject.getString("idNum");
|
||||||
|
// yyyyMMdd
|
||||||
|
String date = jsonObject.getString("date");
|
||||||
|
String databaseName = DatabaseNameUtil.encrypt(patientName) + "_" + DatabaseNameUtil.encrypt(idNum) + "_" + date;
|
||||||
jsonObject.getBoolean("status");
|
jsonObject.getBoolean("status");
|
||||||
vitalSignTimer.sendMachineFlag(database, session);
|
|
||||||
|
vitalSignTimer.sendMachineFlag(databaseName, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
package com.rax.vital.medicine.service;
|
package com.rax.vital.medicine.service;
|
||||||
|
|
||||||
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public interface ChatService {
|
public interface ChatService {
|
||||||
void sendMessage(String username, String patientName, String idNum, String date, String simpSessionId, String msg);
|
void sendMessage(String username, String patientName, String idNum, String date, String simpSessionId, String msg);
|
||||||
|
|
||||||
|
void sendMessage(String username, String patientName, String idNum, String date, WebSocketSession session, String msg) throws IOException;
|
||||||
|
|
||||||
void stopTimerTaskMongo(String simpSessionId);
|
void stopTimerTaskMongo(String simpSessionId);
|
||||||
|
|
||||||
|
void stopTaskMongo(String simpSessionId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.rax.vital.medicine.service.impl;
|
package com.rax.vital.medicine.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.rax.admin.api.dto.UserDTO;
|
import com.rax.admin.api.dto.UserDTO;
|
||||||
import com.rax.admin.api.dto.UserInfo;
|
import com.rax.admin.api.dto.UserInfo;
|
||||||
import com.rax.admin.api.feign.RemoteUserService;
|
import com.rax.admin.api.feign.RemoteUserService;
|
||||||
|
@ -10,14 +11,16 @@ import com.rax.vital.datasource.MongoDBSource;
|
||||||
import com.rax.vital.medicine.service.ChatService;
|
import com.rax.vital.medicine.service.ChatService;
|
||||||
import com.rax.vital.util.DatabaseNameUtil;
|
import com.rax.vital.util.DatabaseNameUtil;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.checkerframework.checker.units.qual.A;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.socket.TextMessage;
|
||||||
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -31,7 +34,11 @@ public class ChatServiceImpl implements ChatService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private RemoteUserService remoteUserService;
|
private RemoteUserService remoteUserService;
|
||||||
|
|
||||||
private static final Map<String, MongoDBSource> datasourceMap = new HashMap<>(100);
|
private static final Map<String, MongoDBSource> datasourceMap = new HashMap<>();
|
||||||
|
|
||||||
|
private static final Map<String, Map<String, WebSocketSession>> databaseSessionMap = new HashMap<>();
|
||||||
|
|
||||||
|
private static final Map<String, String> sessionDatabaseMap = new HashMap<>();
|
||||||
|
|
||||||
// MongoDB的地址
|
// MongoDB的地址
|
||||||
@Value("${vital-sign.mongodb.host}")
|
@Value("${vital-sign.mongodb.host}")
|
||||||
|
@ -80,6 +87,59 @@ public class ChatServiceImpl implements ChatService {
|
||||||
simpMessagingTemplate.convertAndSendToUser(patientName + idNum + date, "/chatroomMessage", param);
|
simpMessagingTemplate.convertAndSendToUser(patientName + idNum + date, "/chatroomMessage", param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendMessage(String username, String patientName, String idNum, String date, WebSocketSession session, String msg) throws IOException {
|
||||||
|
MongoDBSource mongoDBSource = datasourceMap.get(session.getId());
|
||||||
|
String databaseName = DatabaseNameUtil.encrypt(patientName) + "_" + DatabaseNameUtil.encrypt(idNum) + "_" + date;
|
||||||
|
synchronized (username) {
|
||||||
|
if (mongoDBSource == null) {
|
||||||
|
mongoDBSource = new MongoDBSource(mongoDBHost, mongoPassword, mongoUsername, databaseName);
|
||||||
|
mongoDBSource.open();
|
||||||
|
datasourceMap.put(session.getId(), mongoDBSource);
|
||||||
|
sessionDatabaseMap.put(session.getId(), databaseName);
|
||||||
|
Map<String, WebSocketSession> sessionMap = databaseSessionMap.get(databaseName);
|
||||||
|
if (sessionMap == null) {
|
||||||
|
sessionMap = new HashMap();
|
||||||
|
sessionMap.put(session.getId(), session);
|
||||||
|
databaseSessionMap.put(databaseName, sessionMap);
|
||||||
|
} else {
|
||||||
|
if (!sessionMap.containsKey(session.getId())) {
|
||||||
|
sessionMap.put(session.getId(), session);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UserDTO userDTO = new UserDTO();
|
||||||
|
userDTO.setUsername(username);
|
||||||
|
R<UserInfo> info = remoteUserService.info(userDTO, SecurityConstants.FROM_IN);
|
||||||
|
UserInfo userInfo = info.getData();
|
||||||
|
|
||||||
|
JSONObject param = new JSONObject();
|
||||||
|
|
||||||
|
MongoTemplate template = mongoDBSource.getTemplate();
|
||||||
|
Document document = new Document();
|
||||||
|
document.put("content", msg);
|
||||||
|
param.put("content", msg);
|
||||||
|
String now = DateUtil.now();
|
||||||
|
document.put("create_time", now);
|
||||||
|
param.put("createTime", now);
|
||||||
|
document.put("create_user", username);
|
||||||
|
String name = userInfo.getSysUser().getName();
|
||||||
|
document.put("create_name", name);
|
||||||
|
param.put("createName", name);
|
||||||
|
document.put("deleted", 0);
|
||||||
|
document.put("revoked", 0);
|
||||||
|
template.insert(document, "t_chat");
|
||||||
|
|
||||||
|
Map<String, WebSocketSession> sessionMap = databaseSessionMap.get(databaseName);
|
||||||
|
for (Map.Entry<String, WebSocketSession> entry : sessionMap.entrySet()) {
|
||||||
|
WebSocketSession value = entry.getValue();
|
||||||
|
value.sendMessage(new TextMessage(param.toJSONString().getBytes()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void stopTimerTaskMongo(String simpSessionId) {
|
public synchronized void stopTimerTaskMongo(String simpSessionId) {
|
||||||
MongoDBSource mongoDBSource = datasourceMap.get(simpSessionId);
|
MongoDBSource mongoDBSource = datasourceMap.get(simpSessionId);
|
||||||
|
@ -88,4 +148,22 @@ public class ChatServiceImpl implements ChatService {
|
||||||
datasourceMap.remove(simpSessionId);
|
datasourceMap.remove(simpSessionId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void stopTaskMongo(String simpSessionId) {
|
||||||
|
MongoDBSource mongoDBSource = datasourceMap.get(simpSessionId);
|
||||||
|
if (mongoDBSource != null) {
|
||||||
|
mongoDBSource.close();
|
||||||
|
datasourceMap.remove(simpSessionId);
|
||||||
|
}
|
||||||
|
String databaseName = sessionDatabaseMap.get(simpSessionId);
|
||||||
|
if (databaseName != null) {
|
||||||
|
sessionDatabaseMap.remove(simpSessionId);
|
||||||
|
Map<String, WebSocketSession> stringWebSocketSessionMap = databaseSessionMap.get(databaseName);
|
||||||
|
stringWebSocketSessionMap.remove(simpSessionId);
|
||||||
|
if (stringWebSocketSessionMap.isEmpty()) {
|
||||||
|
databaseSessionMap.remove(databaseName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user