From 7827730ae597c0330d5e3b45779f90a4cf928016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=8A=9B?= Date: Fri, 29 Jul 2022 16:28:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=8F=98=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APIService/pom.xml | 6 +- .../com/storeroom/config/WebSocketConfig.java | 19 ---- .../controller/TestApiServiceController.java | 5 +- .../storeroom/controller/WebSoketService.java | 93 ------------------- .../service/impl/ApiServiceImpl.java | 11 +-- storeroom/pom.xml | 5 + .../config/websoket}/NettyConfig.java | 2 +- .../config/websoket}/NettyServer.java | 2 +- .../config/websoket}/WebSocketHandler.java | 3 +- .../controller/PushController.java | 4 +- .../storeroom3d}/service/PushService.java | 2 +- .../service/impl/PushServiceImpl.java | 7 +- system/src/main/resources/application.yml | 2 +- 13 files changed, 26 insertions(+), 135 deletions(-) delete mode 100644 APIService/src/main/java/com/storeroom/config/WebSocketConfig.java delete mode 100644 APIService/src/main/java/com/storeroom/controller/WebSoketService.java rename {APIService/src/main/java/com/storeroom/config => storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket}/NettyConfig.java (94%) rename {APIService/src/main/java/com/storeroom/common => storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket}/NettyServer.java (98%) rename {APIService/src/main/java/com/storeroom/common => storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket}/WebSocketHandler.java (98%) rename {APIService/src/main/java/com/storeroom => storeroom/src/main/java/com/storeroom/modules/storeroom3d}/controller/PushController.java (89%) rename {APIService/src/main/java/com/storeroom => storeroom/src/main/java/com/storeroom/modules/storeroom3d}/service/PushService.java (88%) rename {APIService/src/main/java/com/storeroom => storeroom/src/main/java/com/storeroom/modules/storeroom3d}/service/impl/PushServiceImpl.java (84%) diff --git a/APIService/pom.xml b/APIService/pom.xml index bdae929..d74e20d 100644 --- a/APIService/pom.xml +++ b/APIService/pom.xml @@ -29,11 +29,7 @@ 3.5.2 - - io.netty - netty-all - 4.1.33.Final - + \ No newline at end of file diff --git a/APIService/src/main/java/com/storeroom/config/WebSocketConfig.java b/APIService/src/main/java/com/storeroom/config/WebSocketConfig.java deleted file mode 100644 index 6c401ed..0000000 --- a/APIService/src/main/java/com/storeroom/config/WebSocketConfig.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.storeroom.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.stereotype.Service; -import org.springframework.web.socket.config.annotation.EnableWebSocket; -import org.springframework.web.socket.server.standard.ServerEndpointExporter; - -@Service -@Configuration -@EnableWebSocket -public class WebSocketConfig { - - @Bean - public ServerEndpointExporter serverEndpointExporter(){ - return new ServerEndpointExporter(); - } - -} diff --git a/APIService/src/main/java/com/storeroom/controller/TestApiServiceController.java b/APIService/src/main/java/com/storeroom/controller/TestApiServiceController.java index 6fa7009..a94cd2c 100644 --- a/APIService/src/main/java/com/storeroom/controller/TestApiServiceController.java +++ b/APIService/src/main/java/com/storeroom/controller/TestApiServiceController.java @@ -22,6 +22,7 @@ import java.util.Set; public class TestApiServiceController { private final ApiService apiService; + //private final PushService pushService; @ApiOperation("获取库房token") @AnonymousGetMapping("token") @@ -45,13 +46,15 @@ public class TestApiServiceController { @ApiOperation("获取报警信息") @AnonymousGetMapping("getcuralarm") public ApiResponse getInfo() { + Object objMsg = apiService.getAllDeviceAlarm(); + //pushService.pushMsgToAll(objMsg.toString()); return ApiResponse.success(apiService.getAllDeviceAlarm()); } @ApiOperation("获取设备监控参数实时值") @AnonymousPostMapping("getrealtimedata") public ApiResponse getrealtimedata(@RequestBody Set deviceIds) { - ; + ; return ApiResponse.success(apiService.getRealTimeData(deviceIds)); } } diff --git a/APIService/src/main/java/com/storeroom/controller/WebSoketService.java b/APIService/src/main/java/com/storeroom/controller/WebSoketService.java deleted file mode 100644 index ebe6aa4..0000000 --- a/APIService/src/main/java/com/storeroom/controller/WebSoketService.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.storeroom.controller; - - -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import javax.websocket.OnClose; -import javax.websocket.OnMessage; -import javax.websocket.OnOpen; -import javax.websocket.Session; -import javax.websocket.server.PathParam; -import javax.websocket.server.ServerEndpoint; -import java.util.concurrent.ConcurrentHashMap; - -@Slf4j -@Component -@ServerEndpoint(value = "/webSocket/{name}") -public class WebSoketService { - - /** - * 与某个客户端的连接对话,需要通过它来给客户端发送消息 - */ - private Session session; - - /** - * 标识当前连接客户端的用户名 - */ - private String name; - - - /** - * 用于存所有的连接服务的客户端,这个对象存储是安全的 - */ - private static ConcurrentHashMap webSocketSet = new ConcurrentHashMap<>(); - - @OnOpen - public void OnOpen(Session session, @PathParam(value = "name") String name) { - this.session = session; - this.name = name; - // name是用来表示唯一客户端,如果需要指定发送,需要指定发送通过name来区分 - webSocketSet.put(name, this); - log.info("[WebSocket] 连接成功,当前连接人数为:={}", webSocketSet.size()); - } - - - @OnClose - public void OnClose() { - webSocketSet.remove(this.name); - log.info("[WebSocket] 退出成功,当前连接人数为:={}", webSocketSet.size()); - } - - @OnMessage - public void OnMessage(String message) { - log.info("[WebSocket] 收到消息:{}", message); - //判断是否需要指定发送,具体规则自定义 - if (message.indexOf("TOUSER") == 0) { - String name = message.substring(message.indexOf("TOUSER") + 6, message.indexOf(";")); - AppointSending(name, message.substring(message.indexOf(";") + 1, message.length())); - } else { - GroupSending(message); - } - - } - - /** - * 群发 - * - * @param message - */ - public void GroupSending(String message) { - for (String name : webSocketSet.keySet()) { - try { - webSocketSet.get(name).session.getBasicRemote().sendText(message); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - /** - * 指定发送 - * - * @param name - * @param message - */ - public void AppointSending(String name, String message) { - try { - webSocketSet.get(name).session.getBasicRemote().sendText(message); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/APIService/src/main/java/com/storeroom/service/impl/ApiServiceImpl.java b/APIService/src/main/java/com/storeroom/service/impl/ApiServiceImpl.java index 92d4316..da05ad2 100644 --- a/APIService/src/main/java/com/storeroom/service/impl/ApiServiceImpl.java +++ b/APIService/src/main/java/com/storeroom/service/impl/ApiServiceImpl.java @@ -1,7 +1,6 @@ package com.storeroom.service.impl; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.storeroom.exception.BaseException; @@ -12,14 +11,12 @@ import com.storeroom.service.dto.RealTimeDataDto; import com.storeroom.utils.FastjsonUtils; import com.storeroom.utils.HttpUtils; import com.storeroom.utils.RedisUtils; -import com.storeroom.utils.StringUtils; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import okhttp3.*; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.cache.annotation.CacheConfig; -import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; @@ -116,8 +113,8 @@ public class ApiServiceImpl implements ApiService { map.put("Authorization", "Bearer" + token + ""); HttpResponse response = HttpUtils.doGet("http://jiton.8800.org:800", "/Api/Third/GetCurAlarm", "Get", map, null); if (response.getStatusLine().getStatusCode() == 200) { - String s = EntityUtils.toString(response.getEntity()); - Map dataMap = FastjsonUtils.toJavaMap(s); + String result = EntityUtils.toString(response.getEntity()); + Map dataMap = FastjsonUtils.toJavaMap(result); List list = new ArrayList<>(); dataMap.forEach((k, v) -> { if (k.equals("Data")) { @@ -141,7 +138,9 @@ public class ApiServiceImpl implements ApiService { } } }); - return list; + //PushServiceImpl pushService=new PushServiceImpl(); + // pushService.pushMsgToAll(result); + return result; } else { throw new BaseException("访问失败" + response.getStatusLine().getStatusCode() + ""); } diff --git a/storeroom/pom.xml b/storeroom/pom.xml index c5ae271..99cd16b 100644 --- a/storeroom/pom.xml +++ b/storeroom/pom.xml @@ -28,6 +28,11 @@ APIService 1.0 + + io.netty + netty-all + 4.1.33.Final + \ No newline at end of file diff --git a/APIService/src/main/java/com/storeroom/config/NettyConfig.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyConfig.java similarity index 94% rename from APIService/src/main/java/com/storeroom/config/NettyConfig.java rename to storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyConfig.java index d6bc58f..8ec75f0 100644 --- a/APIService/src/main/java/com/storeroom/config/NettyConfig.java +++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyConfig.java @@ -1,4 +1,4 @@ -package com.storeroom.config; +package com.storeroom.modules.storeroom3d.config.websoket; import io.netty.channel.Channel; import io.netty.channel.group.ChannelGroup; diff --git a/APIService/src/main/java/com/storeroom/common/NettyServer.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyServer.java similarity index 98% rename from APIService/src/main/java/com/storeroom/common/NettyServer.java rename to storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyServer.java index 1479677..bbbf216 100644 --- a/APIService/src/main/java/com/storeroom/common/NettyServer.java +++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/NettyServer.java @@ -1,4 +1,4 @@ -package com.storeroom.common; +package com.storeroom.modules.storeroom3d.config.websoket; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; diff --git a/APIService/src/main/java/com/storeroom/common/WebSocketHandler.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/WebSocketHandler.java similarity index 98% rename from APIService/src/main/java/com/storeroom/common/WebSocketHandler.java rename to storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/WebSocketHandler.java index 3d9ca25..6d25642 100644 --- a/APIService/src/main/java/com/storeroom/common/WebSocketHandler.java +++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/config/websoket/WebSocketHandler.java @@ -1,8 +1,7 @@ -package com.storeroom.common; +package com.storeroom.modules.storeroom3d.config.websoket; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; -import com.storeroom.config.NettyConfig; import io.netty.channel.*; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.timeout.IdleState; diff --git a/APIService/src/main/java/com/storeroom/controller/PushController.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/controller/PushController.java similarity index 89% rename from APIService/src/main/java/com/storeroom/controller/PushController.java rename to storeroom/src/main/java/com/storeroom/modules/storeroom3d/controller/PushController.java index 6fc3c0b..f4245bc 100644 --- a/APIService/src/main/java/com/storeroom/controller/PushController.java +++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/controller/PushController.java @@ -1,8 +1,8 @@ -package com.storeroom.controller; +package com.storeroom.modules.storeroom3d.controller; import com.storeroom.annotaion.rest.AnonymousGetMapping; import com.storeroom.annotaion.rest.AnonymousPostMapping; -import com.storeroom.service.PushService; +import com.storeroom.modules.storeroom3d.service.PushService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/APIService/src/main/java/com/storeroom/service/PushService.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/service/PushService.java similarity index 88% rename from APIService/src/main/java/com/storeroom/service/PushService.java rename to storeroom/src/main/java/com/storeroom/modules/storeroom3d/service/PushService.java index 3efd7d8..85c2393 100644 --- a/APIService/src/main/java/com/storeroom/service/PushService.java +++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/service/PushService.java @@ -1,4 +1,4 @@ -package com.storeroom.service; +package com.storeroom.modules.storeroom3d.service; public interface PushService { diff --git a/APIService/src/main/java/com/storeroom/service/impl/PushServiceImpl.java b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/service/impl/PushServiceImpl.java similarity index 84% rename from APIService/src/main/java/com/storeroom/service/impl/PushServiceImpl.java rename to storeroom/src/main/java/com/storeroom/modules/storeroom3d/service/impl/PushServiceImpl.java index 04185e8..12455e3 100644 --- a/APIService/src/main/java/com/storeroom/service/impl/PushServiceImpl.java +++ b/storeroom/src/main/java/com/storeroom/modules/storeroom3d/service/impl/PushServiceImpl.java @@ -1,7 +1,8 @@ -package com.storeroom.service.impl; +package com.storeroom.modules.storeroom3d.service.impl; -import com.storeroom.config.NettyConfig; -import com.storeroom.service.PushService; + +import com.storeroom.modules.storeroom3d.config.websoket.NettyConfig; +import com.storeroom.modules.storeroom3d.service.PushService; import io.netty.channel.Channel; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import org.springframework.stereotype.Service; diff --git a/system/src/main/resources/application.yml b/system/src/main/resources/application.yml index 084f1b0..ef972a7 100644 --- a/system/src/main/resources/application.yml +++ b/system/src/main/resources/application.yml @@ -83,7 +83,7 @@ hand-held: webSocket: netty: port: 7071 - path: /wsServer + path: /webSocket readerIdleTime: 30 #读空闲超时时间设置(Netty心跳检测配置) writerIdleTime: 30 #写空闲超时时间设置(Netty心跳检测配置) allIdleTime: 30 #读写空闲超时时间设置(Netty心跳检测配置)