diff --git a/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/mapper/HwDeviceModeFunctionMapper.java b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/mapper/HwDeviceModeFunctionMapper.java new file mode 100644 index 0000000..9985ee4 --- /dev/null +++ b/ruoyi-modules/hw-business/src/main/java/com/ruoyi/business/mapper/HwDeviceModeFunctionMapper.java @@ -0,0 +1,61 @@ +package com.ruoyi.business.mapper; + +import java.util.List; +import com.ruoyi.business.domain.HwDeviceModeFunction; + +/** + * 设备模型功能Mapper接口 + * + * @author xins + * @date 2023-09-05 + */ +public interface HwDeviceModeFunctionMapper +{ + /** + * 查询设备模型功能 + * + * @param modeFunctionId 设备模型功能主键 + * @return 设备模型功能 + */ + public HwDeviceModeFunction selectHwDeviceModeFunctionByModeFunctionId(Long modeFunctionId); + + /** + * 查询设备模型功能列表 + * + * @param hwDeviceModeFunction 设备模型功能 + * @return 设备模型功能集合 + */ + public List selectHwDeviceModeFunctionList(HwDeviceModeFunction hwDeviceModeFunction); + + /** + * 新增设备模型功能 + * + * @param hwDeviceModeFunction 设备模型功能 + * @return 结果 + */ + public int insertHwDeviceModeFunction(HwDeviceModeFunction hwDeviceModeFunction); + + /** + * 修改设备模型功能 + * + * @param hwDeviceModeFunction 设备模型功能 + * @return 结果 + */ + public int updateHwDeviceModeFunction(HwDeviceModeFunction hwDeviceModeFunction); + + /** + * 删除设备模型功能 + * + * @param modeFunctionId 设备模型功能主键 + * @return 结果 + */ + public int deleteHwDeviceModeFunctionByModeFunctionId(Long modeFunctionId); + + /** + * 批量删除设备模型功能 + * + * @param modeFunctionIds 需要删除的数据主键集合 + * @return 结果 + */ + public int deleteHwDeviceModeFunctionByModeFunctionIds(Long[] modeFunctionIds); +} diff --git a/ruoyi-modules/hw-websocket-server/pom.xml b/ruoyi-modules/hw-websocket-server/pom.xml new file mode 100644 index 0000000..610da24 --- /dev/null +++ b/ruoyi-modules/hw-websocket-server/pom.xml @@ -0,0 +1,65 @@ + + + + + com.ruoyi + ruoyi-modules + 3.6.3 + + 4.0.0 + + + ruoyi-modules-websocket + + + 海威物联网平台WebSocket模块 + + + + + 8 + + + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + + + + org.springframework.boot + spring-boot-starter-websocket + + + + org.projectlombok + lombok + provided + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/HwWebsocketServerApplication.java b/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/HwWebsocketServerApplication.java new file mode 100644 index 0000000..4661a58 --- /dev/null +++ b/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/HwWebsocketServerApplication.java @@ -0,0 +1,14 @@ +package com.hw.websocket; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) +public class HwWebsocketServerApplication { + + public static void main(String[] args) { + SpringApplication.run(HwWebsocketServerApplication.class, args); + } + +} diff --git a/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/config/WebSocketConfig.java b/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/config/WebSocketConfig.java new file mode 100644 index 0000000..21ba5d0 --- /dev/null +++ b/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/config/WebSocketConfig.java @@ -0,0 +1,23 @@ +package com.hw.websocket.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; + +/** + * @author Wen JY + * @description: TODO + * @date 2023-09-14 16:22:13 + * @version: 1.0 + */ +@Configuration +public class WebSocketConfig { + /** + * 注入ServerEndpointExporter, + * 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint + */ + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } +} diff --git a/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/controller/WebSocketApi.java b/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/controller/WebSocketApi.java new file mode 100644 index 0000000..780a025 --- /dev/null +++ b/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/controller/WebSocketApi.java @@ -0,0 +1,39 @@ +package com.hw.websocket.controller; + +import com.alibaba.fastjson2.JSONObject; +import com.hw.websocket.operate.WebSocket; +import com.ruoyi.common.core.utils.uuid.UUID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.annotation.Resource; + +/** + * @author Wen JY + * @description: TODO + * @date 2023-09-14 17:07:06 + * @version: 1.0 + */ +@Controller +@RequestMapping("/webSocketApi") +public class WebSocketApi { + + private Logger logger = LoggerFactory.getLogger(WebSocketApi.class); + @Resource + private WebSocket webSocket; + + @PostMapping() + @ResponseBody + public void push(String message){ + try{ + webSocket.sendAllMessage(message); + }catch (Exception ex){ + logger.error("WebSocket Api接口异常:"+ex.getMessage()); + } + } +} diff --git a/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/operate/WebSocket.java b/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/operate/WebSocket.java new file mode 100644 index 0000000..eb8e8ac --- /dev/null +++ b/ruoyi-modules/hw-websocket-server/src/main/java/com/hw/websocket/operate/WebSocket.java @@ -0,0 +1,134 @@ +package com.hw.websocket.operate; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.websocket.*; +import javax.websocket.server.PathParam; +import javax.websocket.server.ServerEndpoint; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArraySet; + +/** + * @author Wen JY + * @description: TODO WebSocket 操作类 + * @date 2023-09-14 16:32:43 + * @version: 1.0 + */ +@Component +@ServerEndpoint("/websocket/{userId}") // 接口路径 ws://localhost:8087/webSocket/userId; +public class WebSocket { + + private Logger logger = LoggerFactory.getLogger(WebSocket.class); + + //与某个客户端的连接会话,需要通过它来给客户端发送数据 + private Session session; + /** + * 用户ID + */ + private String userId; + + //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 + //虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。 + // 注:底下WebSocket是当前类名 + private static CopyOnWriteArraySet webSockets =new CopyOnWriteArraySet<>(); + // 用来存在线连接用户信息 + private static ConcurrentHashMap sessionPool = new ConcurrentHashMap(); + + /** + * 链接成功调用的方法 + */ + @OnOpen + public void onOpen(Session session, @PathParam(value="userId")String userId) { + try { + this.session = session; + this.userId = userId; + webSockets.add(this); + sessionPool.put(userId, session); + logger.info("【websocket消息】有新的连接,总数为:"+webSockets.size()); + } catch (Exception e) { + } + } + + /** + * 链接关闭调用的方法 + */ + @OnClose + public void onClose() { + try { + webSockets.remove(this); + sessionPool.remove(this.userId); + logger.info("【websocket消息】连接断开,总数为:"+webSockets.size()); + } catch (Exception e) { + } + } + + /** + * 收到客户端消息后调用的方法 + * + * @param message + * @param session + */ + @OnMessage + public void onMessage(String message) { + logger.info("【websocket消息】收到客户端消息:"+message); + } + + /** 发送错误时的处理 + * @param session + * @param error + */ + @OnError + public void onError(Session session, Throwable error) { + + logger.error("用户错误,原因:"+error.getMessage()); + error.printStackTrace(); + } + + + // 此为广播消息 + public void sendAllMessage(String message) { + logger.info("【websocket消息】广播消息:"+message); + for(WebSocket webSocket : webSockets) { + try { + if(webSocket.session.isOpen()) { + webSocket.session.getAsyncRemote().sendText(message); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + // 此为单点消息 + public void sendOneMessage(String userId, String message) { + Session session = sessionPool.get(userId); + if (session != null&&session.isOpen()) { + try { + logger.info("【websocket消息】 单点消息:"+message); + session.getAsyncRemote().sendText(message); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + // 此为单点消息(多人) + public void sendMoreMessage(String[] userIds, String message) { + for(String userId:userIds) { + Session session = sessionPool.get(userId); + if (session != null&&session.isOpen()) { + try { + logger.info("【websocket消息】 单点消息:"+message); + session.getAsyncRemote().sendText(message); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + } + + +} diff --git a/ruoyi-modules/hw-websocket-server/src/main/resources/application.yml b/ruoyi-modules/hw-websocket-server/src/main/resources/application.yml new file mode 100644 index 0000000..7e8b233 --- /dev/null +++ b/ruoyi-modules/hw-websocket-server/src/main/resources/application.yml @@ -0,0 +1,25 @@ +# Tomcat +server: + port: 9607 + +# Spring +spring: + application: + # ???? + name: hw-websocket + profiles: + # ???? + active: dev + cloud: + nacos: + discovery: + # ?????? + server-addr: localhost:8848 + config: + # ?????? + server-addr: localhost:8848 + # ?????? + file-extension: yml + # ???? + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/ruoyi-modules/pom.xml b/ruoyi-modules/pom.xml index 2207434..2fc130f 100644 --- a/ruoyi-modules/pom.xml +++ b/ruoyi-modules/pom.xml @@ -28,6 +28,7 @@ hw-basic hw-tdengine hw-data-process + hw-websocket-server ruoyi-modules