mirror of
https://gitee.com/republicline/rax-remote-v2.git
synced 2025-08-23 23:44:58 +08:00
add: 一期代码
This commit is contained in:
parent
1e39cc83a8
commit
17be499d6d
|
@ -1,46 +1,46 @@
|
|||
package com.rax.vital.common.datasource;
|
||||
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DynamicDataSource {
|
||||
|
||||
private HikariDataSource dataSource;
|
||||
|
||||
// 动态创建连接池
|
||||
public void createDataSource(String databaseName) {
|
||||
HikariConfig config = new HikariConfig();
|
||||
|
||||
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
||||
config.setJdbcUrl("jdbc:mysql://110.41.142.124:3306/" + databaseName + "?useUnicode=true&characterEncoding=utf-8&useSSL=false");
|
||||
config.setUsername("root");
|
||||
config.setPassword("Xg137839");
|
||||
|
||||
// 配置连接池参数
|
||||
config.setMinimumIdle(1);
|
||||
config.setMaximumPoolSize(5);
|
||||
config.setIdleTimeout(1000 * 60 * 3); // 空闲超时时间
|
||||
config.setConnectionTimeout(1000 * 60 * 3); // 连接超时时间
|
||||
config.setMaxLifetime(1000 * 60 * 3); // 最大连接寿命
|
||||
this.dataSource = new HikariDataSource(config);
|
||||
}
|
||||
|
||||
// 获取连接
|
||||
public Connection getConnection() throws SQLException {
|
||||
if (dataSource == null) {
|
||||
throw new IllegalStateException("DataSource is not initialized. Call createDataSource() first.");
|
||||
}
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
|
||||
// 关闭数据源
|
||||
public void closeDataSource() {
|
||||
if (dataSource != null) {
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
//package com.rax.vital.common.datasource;
|
||||
//
|
||||
//
|
||||
//import com.zaxxer.hikari.HikariConfig;
|
||||
//import com.zaxxer.hikari.HikariDataSource;
|
||||
//
|
||||
//import java.sql.Connection;
|
||||
//import java.sql.SQLException;
|
||||
//
|
||||
//public class DynamicDataSource {
|
||||
//
|
||||
// private HikariDataSource dataSource;
|
||||
//
|
||||
// // 动态创建连接池
|
||||
// public void createDataSource(String databaseName) {
|
||||
// HikariConfig config = new HikariConfig();
|
||||
//
|
||||
// config.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
||||
// config.setJdbcUrl("jdbc:mysql://110.41.142.124:3306/" + databaseName + "?useUnicode=true&characterEncoding=utf-8&useSSL=false");
|
||||
// config.setUsername("root");
|
||||
// config.setPassword("Xg137839");
|
||||
//
|
||||
// // 配置连接池参数
|
||||
// config.setMinimumIdle(1);
|
||||
// config.setMaximumPoolSize(5);
|
||||
// config.setIdleTimeout(1000 * 60 * 3); // 空闲超时时间
|
||||
// config.setConnectionTimeout(1000 * 60 * 3); // 连接超时时间
|
||||
// config.setMaxLifetime(1000 * 60 * 3); // 最大连接寿命
|
||||
// this.dataSource = new HikariDataSource(config);
|
||||
// }
|
||||
//
|
||||
// // 获取连接
|
||||
// public Connection getConnection() throws SQLException {
|
||||
// if (dataSource == null) {
|
||||
// throw new IllegalStateException("DataSource is not initialized. Call createDataSource() first.");
|
||||
// }
|
||||
// return dataSource.getConnection();
|
||||
// }
|
||||
//
|
||||
// // 关闭数据源
|
||||
// public void closeDataSource() {
|
||||
// if (dataSource != null) {
|
||||
// dataSource.close();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class MySQLSource extends CustomDataSource {
|
|||
boolean status = true;
|
||||
try {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mysql://" + this.host + "/" + this.database);
|
||||
dataSource.setUrl("jdbc:mysql://" + this.host + "/" + this.database +"?allowPublicKeyRetrieval=true");
|
||||
dataSource.setDriverClassName(driver);
|
||||
dataSource.setUsername(this.username);
|
||||
dataSource.setPassword(this.password);
|
||||
|
|
|
@ -34,8 +34,7 @@ public class SysLoggerBuilder {
|
|||
String uri = session.getUri() != null ? session.getUri().toString() : "unknown";
|
||||
String userAgent = session.getHandshakeHeaders().getFirst("User-Agent") != null ?
|
||||
session.getHandshakeHeaders().getFirst("User-Agent") : "unknown";
|
||||
String params = session.getUri() != null ?
|
||||
session.getUri().getQuery() : "unknown";
|
||||
String params = session.getUri() != null ? session.getUri().getQuery() : "unknown";
|
||||
|
||||
// 解析token
|
||||
Map<String, Object> stringObjectMap = tokenUtil.parseToken(token);
|
||||
|
|
|
@ -12,10 +12,10 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* project_name: remote-control-backend
|
||||
* time: 2024/9/4 14:56
|
||||
* author: republicline
|
||||
* description:
|
||||
* @project_name: remote-control-backend
|
||||
* @time: 2024/9/4 14:56
|
||||
* @author: republicline
|
||||
* @description:
|
||||
*/
|
||||
@Component
|
||||
public class TokenUtil {
|
||||
|
|
|
@ -1,130 +1,130 @@
|
|||
package com.rax.vital.controller;
|
||||
|
||||
import com.rax.common.core.util.R;
|
||||
import com.rax.common.security.annotation.Inner;
|
||||
import com.rax.vital.common.datasource.DynamicDataSource;
|
||||
import com.rax.vital.entity.VisualBody;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* @project_name: rax-remote-v2
|
||||
* @time: 2024/11/5 11:05
|
||||
* @author: republicline
|
||||
* @description: 仪器端和服务端通信接口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/machine")
|
||||
public class MachineController {
|
||||
|
||||
// 生命体征表名
|
||||
private static final String FEATURE_TABLE_NAME = "featuretable";
|
||||
|
||||
// 异步入库
|
||||
private ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||
|
||||
// 动态数据源
|
||||
private ConcurrentHashMap<String, DynamicDataSource> dynamicDataSources = new ConcurrentHashMap<>();
|
||||
|
||||
// 仪器端生命体征数据入库
|
||||
@PostMapping("/save")
|
||||
@Inner(value = false)
|
||||
public R<Object> saveVitalBodyData(VisualBody visualBody) {
|
||||
System.out.println("visualBody = " + visualBody);
|
||||
|
||||
for (String key : dynamicDataSources.keySet()) System.out.println("key = " + key);
|
||||
|
||||
// 校验
|
||||
if (visualBody == null) return R.failed("参数不能为空");
|
||||
|
||||
if (visualBody.getDatabaseName() == null ||
|
||||
visualBody.getPhase() == null || visualBody.getBIS() == null ||
|
||||
visualBody.getHR() == null || visualBody.getSBP() == null ||
|
||||
visualBody.getDBP() == null || visualBody.getST() == null ||
|
||||
visualBody.getTEMP() == null || visualBody.getSP02() == null ||
|
||||
visualBody.getEtCO2() == null || visualBody.getPPG() == null ||
|
||||
visualBody.getABG() == null || visualBody.getTOF() == null ||
|
||||
visualBody.getTIME() == null) {
|
||||
return R.failed("参数含有空值");
|
||||
}
|
||||
saveVitalBodyDataAsync(visualBody);
|
||||
return R.ok("success");
|
||||
}
|
||||
|
||||
// 关闭数据源
|
||||
@PostMapping("/close")
|
||||
@Inner(value = false)
|
||||
public R<Object> closeDataSource(String databaseName) {
|
||||
if (databaseName == null) {
|
||||
return R.failed("参数不能为空");
|
||||
}
|
||||
dynamicDataSources.remove(databaseName);
|
||||
DynamicDataSource dynamicDataSource = dynamicDataSources.get(databaseName);
|
||||
if (dynamicDataSource == null) {
|
||||
return R.failed("数据源不存在");
|
||||
}
|
||||
dynamicDataSource.closeDataSource();
|
||||
return R.ok("success");
|
||||
}
|
||||
|
||||
private void saveVitalBodyDataAsync(VisualBody visualBody) {
|
||||
executorService.execute(() -> {
|
||||
try {
|
||||
|
||||
String databaseName = visualBody.getDatabaseName();
|
||||
if (!dynamicDataSources.containsKey(databaseName)) {
|
||||
// 创建动态数据源
|
||||
System.out.println("连接池创建数据源");
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
dynamicDataSource.createDataSource(databaseName);
|
||||
dynamicDataSources.put(databaseName, dynamicDataSource);
|
||||
}
|
||||
|
||||
if (dynamicDataSources.get(databaseName) == null) {
|
||||
dynamicDataSources.remove(databaseName);
|
||||
throw new RuntimeException("数据源不存在");
|
||||
}
|
||||
|
||||
// 获取连接
|
||||
Connection connection = dynamicDataSources.get(databaseName).getConnection();
|
||||
|
||||
if (connection == null) {
|
||||
throw new RuntimeException("数据库连接失败");
|
||||
}
|
||||
|
||||
// 数据库操作
|
||||
String sql = String.format("""
|
||||
INSERT INTO %s
|
||||
(Phase, BIS, HR, SBP, DBP, ST, TEMP, SPO2, EtCO2, PPG, ABG, TOF, `TIME`)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);
|
||||
""", FEATURE_TABLE_NAME);
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
ps.setInt(1, visualBody.getPhase());
|
||||
ps.setDouble(2, visualBody.getBIS());
|
||||
ps.setDouble(3, visualBody.getHR());
|
||||
ps.setDouble(4, visualBody.getSBP());
|
||||
ps.setDouble(5, visualBody.getDBP());
|
||||
ps.setDouble(6, visualBody.getST());
|
||||
ps.setDouble(7, visualBody.getTEMP());
|
||||
ps.setDouble(8, visualBody.getSP02());
|
||||
ps.setDouble(9, visualBody.getEtCO2());
|
||||
ps.setDouble(10, visualBody.getPPG());
|
||||
ps.setDouble(11, visualBody.getABG());
|
||||
ps.setDouble(12, visualBody.getTOF());
|
||||
ps.setTimestamp(13, visualBody.getTIME());
|
||||
ps.execute();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
//package com.rax.vital.controller;
|
||||
//
|
||||
//import com.rax.common.core.util.R;
|
||||
//import com.rax.common.security.annotation.Inner;
|
||||
//import com.rax.vital.common.datasource.DynamicDataSource;
|
||||
//import com.rax.vital.entity.VisualBody;
|
||||
//import org.springframework.web.bind.annotation.PostMapping;
|
||||
//import org.springframework.web.bind.annotation.RequestMapping;
|
||||
//import org.springframework.web.bind.annotation.RestController;
|
||||
//
|
||||
//import java.sql.Connection;
|
||||
//import java.sql.PreparedStatement;
|
||||
//import java.sql.SQLException;
|
||||
//import java.util.concurrent.*;
|
||||
//
|
||||
///**
|
||||
// * @project_name: rax-remote-v2
|
||||
// * @time: 2024/11/5 11:05
|
||||
// * @author: republicline
|
||||
// * @description: 仪器端和服务端通信接口
|
||||
// */
|
||||
//@RestController
|
||||
//@RequestMapping("/machine")
|
||||
//public class MachineController {
|
||||
//
|
||||
// // 生命体征表名
|
||||
// private static final String FEATURE_TABLE_NAME = "featuretable";
|
||||
//
|
||||
// // 异步入库
|
||||
// private ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||
//
|
||||
// // 动态数据源
|
||||
// private ConcurrentHashMap<String, DynamicDataSource> dynamicDataSources = new ConcurrentHashMap<>();
|
||||
//
|
||||
// // 仪器端生命体征数据入库
|
||||
// @PostMapping("/save")
|
||||
// @Inner(value = false)
|
||||
// public R<Object> saveVitalBodyData(VisualBody visualBody) {
|
||||
// System.out.println("visualBody = " + visualBody);
|
||||
//
|
||||
// for (String key : dynamicDataSources.keySet()) System.out.println("key = " + key);
|
||||
//
|
||||
// // 校验
|
||||
// if (visualBody == null) return R.failed("参数不能为空");
|
||||
//
|
||||
// if (visualBody.getDatabaseName() == null ||
|
||||
// visualBody.getPhase() == null || visualBody.getBIS() == null ||
|
||||
// visualBody.getHR() == null || visualBody.getSBP() == null ||
|
||||
// visualBody.getDBP() == null || visualBody.getST() == null ||
|
||||
// visualBody.getTEMP() == null || visualBody.getSP02() == null ||
|
||||
// visualBody.getEtCO2() == null || visualBody.getPPG() == null ||
|
||||
// visualBody.getABG() == null || visualBody.getTOF() == null ||
|
||||
// visualBody.getTIME() == null) {
|
||||
// return R.failed("参数含有空值");
|
||||
// }
|
||||
// saveVitalBodyDataAsync(visualBody);
|
||||
// return R.ok("success");
|
||||
// }
|
||||
//
|
||||
// // 关闭数据源
|
||||
// @PostMapping("/close")
|
||||
// @Inner(value = false)
|
||||
// public R<Object> closeDataSource(String databaseName) {
|
||||
// if (databaseName == null) {
|
||||
// return R.failed("参数不能为空");
|
||||
// }
|
||||
// dynamicDataSources.remove(databaseName);
|
||||
// DynamicDataSource dynamicDataSource = dynamicDataSources.get(databaseName);
|
||||
// if (dynamicDataSource == null) {
|
||||
// return R.failed("数据源不存在");
|
||||
// }
|
||||
// dynamicDataSource.closeDataSource();
|
||||
// return R.ok("success");
|
||||
// }
|
||||
//
|
||||
// private void saveVitalBodyDataAsync(VisualBody visualBody) {
|
||||
// executorService.execute(() -> {
|
||||
// try {
|
||||
//
|
||||
// String databaseName = visualBody.getDatabaseName();
|
||||
// if (!dynamicDataSources.containsKey(databaseName)) {
|
||||
// // 创建动态数据源
|
||||
// System.out.println("连接池创建数据源");
|
||||
// DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
// dynamicDataSource.createDataSource(databaseName);
|
||||
// dynamicDataSources.put(databaseName, dynamicDataSource);
|
||||
// }
|
||||
//
|
||||
// if (dynamicDataSources.get(databaseName) == null) {
|
||||
// dynamicDataSources.remove(databaseName);
|
||||
// throw new RuntimeException("数据源不存在");
|
||||
// }
|
||||
//
|
||||
// // 获取连接
|
||||
// Connection connection = dynamicDataSources.get(databaseName).getConnection();
|
||||
//
|
||||
// if (connection == null) {
|
||||
// throw new RuntimeException("数据库连接失败");
|
||||
// }
|
||||
//
|
||||
// // 数据库操作
|
||||
// String sql = String.format("""
|
||||
// INSERT INTO %s
|
||||
// (Phase, BIS, HR, SBP, DBP, ST, TEMP, SPO2, EtCO2, PPG, ABG, TOF, `TIME`)
|
||||
// VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);
|
||||
// """, FEATURE_TABLE_NAME);
|
||||
// PreparedStatement ps = connection.prepareStatement(sql);
|
||||
// ps.setInt(1, visualBody.getPhase());
|
||||
// ps.setDouble(2, visualBody.getBIS());
|
||||
// ps.setDouble(3, visualBody.getHR());
|
||||
// ps.setDouble(4, visualBody.getSBP());
|
||||
// ps.setDouble(5, visualBody.getDBP());
|
||||
// ps.setDouble(6, visualBody.getST());
|
||||
// ps.setDouble(7, visualBody.getTEMP());
|
||||
// ps.setDouble(8, visualBody.getSP02());
|
||||
// ps.setDouble(9, visualBody.getEtCO2());
|
||||
// ps.setDouble(10, visualBody.getPPG());
|
||||
// ps.setDouble(11, visualBody.getABG());
|
||||
// ps.setDouble(12, visualBody.getTOF());
|
||||
// ps.setTimestamp(13, visualBody.getTIME());
|
||||
// ps.execute();
|
||||
// } catch (SQLException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// } catch (Exception e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
//
|
||||
// });
|
||||
// }
|
||||
//}
|
||||
|
|
|
@ -1,27 +1,29 @@
|
|||
package com.rax.vital.v1.handler;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.rax.common.core.util.RedisUtils;
|
||||
import com.rax.vital.common.datasource.CustomDataSource;
|
||||
import com.rax.vital.common.datasource.MySQLSource;
|
||||
import com.rax.vital.common.util.GetHttpParamUtil;
|
||||
import com.rax.vital.v1.medicine.service.ChatService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
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.util.StringUtils;
|
||||
import org.springframework.web.socket.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLDecoder;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class ChatHandler implements WebSocketHandler {
|
||||
@Resource
|
||||
|
@ -33,6 +35,20 @@ public class ChatHandler implements WebSocketHandler {
|
|||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
// mysql地址
|
||||
@Value("${vital-sign.mysql.host}")
|
||||
private String mysqlHost;
|
||||
|
||||
// mysql用户名
|
||||
@Value("${vital-sign.mysql.username}")
|
||||
private String mysqlUsername;
|
||||
|
||||
// mysql用户密码
|
||||
@Value("${vital-sign.mysql.password}")
|
||||
private String mysqlPassword;
|
||||
|
||||
private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(5);
|
||||
|
||||
private Map<String, ScheduledExecutorService> timerTaskMap = new ConcurrentHashMap();
|
||||
|
||||
// dbName -> sessionList
|
||||
|
@ -55,9 +71,9 @@ public class ChatHandler implements WebSocketHandler {
|
|||
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);
|
||||
|
||||
if ("heartbeat".equals(jsonObject.getString("msgType"))) {
|
||||
// session.sendMessage(new TextMessage(""));
|
||||
} else if ("init".equals(jsonObject.getString("msgType"))) {
|
||||
|
@ -79,6 +95,8 @@ public class ChatHandler implements WebSocketHandler {
|
|||
}
|
||||
List<WebSocketSession> webSocketSessions = dbNameSessionList.get(dbName);
|
||||
webSocketSessions.add(session);
|
||||
// 创建数据库表
|
||||
initTChatTable(dbName, session);
|
||||
// ArrayList<String> sessionIDList = new ArrayList<>();
|
||||
// for (WebSocketSession webSocketSession : webSocketSessions) {
|
||||
// if (webSocketSession.isOpen()) {
|
||||
|
@ -92,14 +110,31 @@ public class ChatHandler implements WebSocketHandler {
|
|||
// redisTemplate.expire("chat_" + dbName, 60 * 60, TimeUnit.SECONDS);
|
||||
// redisTemplate.opsForHash().putAll("chat_sessionId_" + dbName, sessionDbMap);
|
||||
// redisTemplate.expire("chat_" + dbName, 60 * 60, TimeUnit.SECONDS);
|
||||
} else {
|
||||
String patientName = jsonObject.getString("patientName");
|
||||
} else if ("msg".equals(jsonObject.getString("msgType")) ||
|
||||
"audio".equals(jsonObject.getString("msgType"))) {
|
||||
//String patientName = jsonObject.getString("patientName");
|
||||
String dbName = 'a' + jsonObject.getString("idNum");
|
||||
String date = jsonObject.getString("date");
|
||||
//String date = jsonObject.getString("date");
|
||||
String msgType = jsonObject.getString("msgType");
|
||||
// 消息内容
|
||||
String msg = jsonObject.getString("msg");
|
||||
String content = jsonObject.getString("msg");
|
||||
List<WebSocketSession> webSocketSessions = dbNameSessionList.get(dbName);
|
||||
chatService.sendMessageMysql(username, patientName, dbName, date, session, msg, webSocketSessions);
|
||||
// 转发消息
|
||||
JSONObject param = new JSONObject();
|
||||
param.put("msgType", msgType);
|
||||
param.put("createUser", username);
|
||||
param.put("createTime", DateUtil.now());
|
||||
param.put("msg", content);
|
||||
param.put("dbName", dbName);
|
||||
for (WebSocketSession webSocketSession : webSocketSessions) {
|
||||
webSocketSession.sendMessage(new TextMessage(param.toJSONString().getBytes()));
|
||||
}
|
||||
|
||||
// 异步入库
|
||||
EXECUTOR_SERVICE.execute(() -> saveChatMsg(param.toJSONString()));
|
||||
|
||||
// 之前的
|
||||
// chatService.sendMessageMysql(username, patientName, dbName, msgType, session, msg, webSocketSessions);
|
||||
}
|
||||
} else {
|
||||
String msgContent = "token无效,认证失败";
|
||||
|
@ -120,7 +155,7 @@ public class ChatHandler implements WebSocketHandler {
|
|||
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
|
||||
System.out.println("CloseStatus: " + closeStatus.getReason() + closeStatus.getCode());
|
||||
stopHeartbeat(session);
|
||||
chatService.stopTask(session.getId());
|
||||
//chatService.stopTask(session.getId());
|
||||
stopMap(session);
|
||||
}
|
||||
|
||||
|
@ -140,7 +175,7 @@ public class ChatHandler implements WebSocketHandler {
|
|||
session.sendMessage(new TextMessage(jsonObject.toJSONString().getBytes()));
|
||||
} else {
|
||||
stopHeartbeat(session);
|
||||
chatService.stopTask(session.getId());
|
||||
//chatService.stopTask(session.getId());
|
||||
session.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
@ -165,4 +200,110 @@ public class ChatHandler implements WebSocketHandler {
|
|||
sessionDbMap.remove(session.getId());
|
||||
}
|
||||
|
||||
|
||||
// 初始化聊天表格
|
||||
private void initTChatTable(String dbName, WebSocketSession session) {
|
||||
try {
|
||||
|
||||
CustomDataSource source = new MySQLSource(mysqlHost, mysqlPassword, mysqlUsername, dbName);
|
||||
source.open();
|
||||
Connection connection = source.getConnection();
|
||||
if (connection != null) {
|
||||
String tableName = "t_chat";
|
||||
Statement statement = connection.createStatement();
|
||||
// 检查表是否存在的 SQL 语句
|
||||
String checkTableSQL = "SELECT COUNT(*) FROM information_schema.tables " +
|
||||
"WHERE table_schema = DATABASE() AND table_name = '" + tableName + "';";
|
||||
ResultSet resultSet = statement.executeQuery(checkTableSQL);
|
||||
if (resultSet.next() && resultSet.getInt(1) == 0) { // 表不存在
|
||||
// 表不存在,执行创建操作
|
||||
String createTableSQL = """
|
||||
CREATE TABLE %s (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`content` blob,
|
||||
`create_time` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
|
||||
`create_user` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
|
||||
`msg_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
|
||||
`deleted` bit(1) NULL DEFAULT NULL,
|
||||
`revoked` bit(1) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
|
||||
""".formatted(tableName);
|
||||
statement.execute(createTableSQL);
|
||||
}
|
||||
source.close();
|
||||
} else {
|
||||
source.close();
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("status", 1);
|
||||
jsonObject.put("msg", "数据库未创建, 请重新连接");
|
||||
jsonObject.put("msgType", "msg");
|
||||
session.sendMessage(new TextMessage(jsonObject.toJSONString().getBytes()));
|
||||
}
|
||||
|
||||
} catch (SQLException | IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// 创建数据库,查询历史
|
||||
// DatabaseMetaData metaData = connection.getMetaData();
|
||||
// ResultSet tablesx = metaData.getTables(null, null, tableName, new String[]{"TABLE"});
|
||||
// String sql = "select content, create_time \"creatTime\", create_user \"createUser\", create_name \"createName\" from t_chat where deleted = 0 and revoked = 0 order by create_time asc ";
|
||||
// if (tablesx.next()) {
|
||||
//
|
||||
// Statement statement = connection.createStatement();
|
||||
// ResultSet resultSet = statement.executeQuery(sql);
|
||||
// while (resultSet.next()) {
|
||||
// Map map = new HashMap();
|
||||
// map.put("content", resultSet.getString("content"));
|
||||
// map.put("creatTime", resultSet.getString("creatTime"));
|
||||
// map.put("createUser", resultSet.getString("createUser"));
|
||||
// map.put("createName", resultSet.getString("createName"));
|
||||
// history.add(map);
|
||||
// }
|
||||
// JSONObject param = new JSONObject();
|
||||
// param.put("history", history);
|
||||
// param.put("msgType", "msg");
|
||||
//// session.sendMessage(new TextMessage(param.toJSONString().getBytes()));
|
||||
// for (WebSocketSession webSocketSession : webSocketSessionList) {
|
||||
// webSocketSession.sendMessage(new TextMessage(param.toJSONString().getBytes()));
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
// 保存聊天消息到数据库
|
||||
private void saveChatMsg(String jsonStr) {
|
||||
// 发消息, 入库
|
||||
if (StringUtils.hasText(jsonStr)) {
|
||||
JSONObject jsonObj = JSON.parseObject(jsonStr);
|
||||
|
||||
String msgType = jsonObj.getString("msgType");
|
||||
Object content = jsonObj.get("content");
|
||||
String username = jsonObj.getString("createUser");
|
||||
String dbName = jsonObj.getString("dbName");
|
||||
|
||||
String now = DateUtil.now();
|
||||
|
||||
MySQLSource source = new MySQLSource(mysqlHost, mysqlPassword, mysqlUsername, dbName);
|
||||
source.open();
|
||||
Connection connection = source.getConnection();
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement("INSERT INTO t_chat (content, create_time, create_user, msg_type, deleted, revoked) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
preparedStatement.setObject(1, content);
|
||||
preparedStatement.setString(2, now);
|
||||
preparedStatement.setString(3, username);
|
||||
preparedStatement.setString(4, msgType);
|
||||
preparedStatement.setInt(5, 0);
|
||||
preparedStatement.setInt(6, 0);
|
||||
preparedStatement.execute();
|
||||
} catch (SQLException e) {
|
||||
source.close();
|
||||
e.printStackTrace();
|
||||
}
|
||||
source.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -96,7 +96,7 @@ public class MedicineController {
|
|||
|
||||
@PostMapping("/getPatientInfoM")
|
||||
public R getPatientInfoM(String patientName, String idNum, String date) {
|
||||
String databaseName = idNum;
|
||||
String databaseName = 'a' + idNum;
|
||||
return vitalSignsService.getPatientInfoM(databaseName);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import java.util.List;
|
|||
|
||||
public interface ChatService {
|
||||
|
||||
void sendMessageMysql(String username, String patientName, String idNum, String date, WebSocketSession session, String msg, List<WebSocketSession> webSocketSessions) throws Exception;
|
||||
void sendMessageMysql(String username, String patientName, String idNum, String msgType, WebSocketSession session, String msg, List<WebSocketSession> webSocketSessions) throws Exception;
|
||||
|
||||
void stopTask(String simpSessionId);
|
||||
}
|
||||
|
|
|
@ -59,135 +59,60 @@ public class ChatServiceImpl implements ChatService {
|
|||
|
||||
@Override
|
||||
@Async
|
||||
public void sendMessageMysql(String username, String patientName, String idNum, String date, WebSocketSession session, String msg, List<WebSocketSession> webSocketSessionList) throws SQLException, IOException {
|
||||
public void sendMessageMysql(String username, String patientName, String idNum, String msgType, WebSocketSession session, String msg, List<WebSocketSession> webSocketSessionList) throws SQLException, IOException {
|
||||
CustomDataSource dataSource = datasourceMap.get(session.getId());
|
||||
String databaseName = idNum;
|
||||
ArrayList<Map> history = new ArrayList<>();
|
||||
|
||||
// 获取连接
|
||||
if (dataSource == null) {
|
||||
dataSource = new MySQLSource(mysqlHost, mysqlPassword, mysqlUsername, databaseName);
|
||||
dataSource = new MySQLSource(mysqlHost, mysqlPassword, mysqlUsername, idNum);
|
||||
boolean status = dataSource.open();
|
||||
// 查询历史消息
|
||||
if (status) {
|
||||
datasourceMap.put(session.getId(), dataSource);
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
Connection connection = dataSource.getConnection();
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
String tableName = "t_chat";
|
||||
ResultSet tablesx = metaData.getTables(null, null, tableName, new String[]{"TABLE"});
|
||||
String sql = "select content, create_time \"creatTime\", create_user \"createUser\", create_name \"createName\" from t_chat where deleted = 0 and revoked = 0 order by create_time asc ";
|
||||
if (tablesx.next()) {
|
||||
try {
|
||||
//
|
||||
// Statement statement = connection.createStatement();
|
||||
// ResultSet resultSet = statement.executeQuery(sql);
|
||||
// while (resultSet.next()) {
|
||||
// Map map = new HashMap();
|
||||
// map.put("content", resultSet.getString("content"));
|
||||
// map.put("creatTime", resultSet.getString("creatTime"));
|
||||
// map.put("createUser", resultSet.getString("createUser"));
|
||||
// map.put("createName", resultSet.getString("createName"));
|
||||
// history.add(map);
|
||||
// }
|
||||
// JSONObject param = new JSONObject();
|
||||
// param.put("history", history);
|
||||
// param.put("msgType", "msg");
|
||||
//// session.sendMessage(new TextMessage(param.toJSONString().getBytes()));
|
||||
// for (WebSocketSession webSocketSession : webSocketSessionList) {
|
||||
// webSocketSession.sendMessage(new TextMessage(param.toJSONString().getBytes()));
|
||||
// }
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Statement statement = connection.createStatement();
|
||||
String createSql = "CREATE TABLE `t_chat` (\n" +
|
||||
" `id` int NOT NULL AUTO_INCREMENT,\n" +
|
||||
" `content` varchar(5000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" +
|
||||
" `create_time` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" +
|
||||
" `create_user` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" +
|
||||
" `create_name` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" +
|
||||
" `deleted` bit(1) NULL DEFAULT NULL,\n" +
|
||||
" `revoked` bit(1) NULL DEFAULT NULL,\n" +
|
||||
" PRIMARY KEY (`id`) USING BTREE\n" +
|
||||
") ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;";
|
||||
statement.execute(createSql);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}else {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("status", 1);
|
||||
jsonObject.put("msg", "数据库未创建, 请重新连接。");
|
||||
jsonObject.put("msgType", "msg");
|
||||
try {
|
||||
session.sendMessage(new TextMessage(jsonObject.toJSONString().getBytes()));
|
||||
session.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
session.sendMessage(new TextMessage(jsonObject.toJSONString().getBytes()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 发消息, 入库
|
||||
if (StringUtils.hasText(msg)) {
|
||||
String now = DateUtil.now();
|
||||
|
||||
JSONObject param = new JSONObject();
|
||||
param.put("msgType", "msg");
|
||||
param.put("createName", username);
|
||||
param.put("msgType", msgType);
|
||||
param.put("createUser", username);
|
||||
param.put("createTime", now);
|
||||
param.put("content", msg);
|
||||
|
||||
|
||||
System.out.println("param = " + param);
|
||||
System.out.println("webSocketSessionList = " + webSocketSessionList);
|
||||
//System.out.println("param = " + param);
|
||||
//System.out.println("webSocketSessionList = " + webSocketSessionList);
|
||||
// 广播消息
|
||||
for (WebSocketSession webSocketSession : webSocketSessionList) {
|
||||
webSocketSession.sendMessage(new TextMessage(param.toJSONString().getBytes()));
|
||||
}
|
||||
//
|
||||
|
||||
|
||||
// 入库
|
||||
Connection connection = dataSource.getConnection();
|
||||
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement("INSERT INTO t_chat (content, create_time, create_user, create_name, deleted, revoked) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
preparedStatement = connection.prepareStatement("INSERT INTO t_chat (content, create_time, create_user, msg_type, deleted, revoked) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
preparedStatement.setString(1, msg);
|
||||
preparedStatement.setString(2, now);
|
||||
preparedStatement.setString(3, username);
|
||||
// String name = sysUser.getName();
|
||||
preparedStatement.setString(4, username);
|
||||
preparedStatement.setString(4, msgType);
|
||||
preparedStatement.setInt(5, 0);
|
||||
preparedStatement.setInt(6, 0);
|
||||
preparedStatement.execute();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//// Map<String, WebSocketSession> sessionMap1 = databaseSessionMap.get(databaseName);
|
||||
//// for (Map.Entry<String, WebSocketSession> entry : sessionMap1.entrySet()) {
|
||||
//// WebSocketSession value = entry.getValue();
|
||||
//// try {
|
||||
//// value.sendMessage(new TextMessage(param.toJSONString().getBytes()));
|
||||
//// } catch (IOException e) {
|
||||
//// e.printStackTrace();
|
||||
//// }
|
||||
//// }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -235,7 +235,7 @@ public class VitalSignServiceImpl implements VitalSignsService {
|
|||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
mySQLSource.close();
|
||||
return R.ok(map);
|
||||
}
|
||||
|
||||
|
|
|
@ -177,7 +177,6 @@ public class VitalSignTimerWS {
|
|||
TimerTask timerTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
Connection connection = finalDataSource.getConnection();
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// List<Map> vitalSignsList = vitalSignsService.getVitalSignsList(connection);
|
||||
|
|
Loading…
Reference in New Issue
Block a user