pom.xml
@@ -61,7 +61,11 @@ <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!--websocket--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <!--mybatis 及mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> src/main/java/com/whyc/config/WebSocketConfig.java
New file @@ -0,0 +1,30 @@ package com.whyc.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; import javax.servlet.http.HttpSession; import javax.websocket.HandshakeResponse; import javax.websocket.server.HandshakeRequest; import javax.websocket.server.ServerEndpointConfig; @Configuration public class WebSocketConfig extends ServerEndpointConfig.Configurator { @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { //HttpSession httpSession = (ShiroHttpSession) request.getHttpSession(); HttpSession httpSession = (HttpSession)request.getHttpSession(); if(httpSession!=null) { sec.getUserProperties().put("httpSession", httpSession); } super.modifyHandshake(sec, request, response); } @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } } src/main/java/com/whyc/controller/BattInfController.java
New file @@ -0,0 +1,17 @@ package com.whyc.controller; import com.whyc.service.BattInfService; import io.swagger.annotations.Api; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @Api(tags = "在用蓄电池模块") @RequestMapping("binf") public class BattInfController { @Autowired private BattInfService service; } src/main/java/com/whyc/controller/SinfBinfController.java
New file @@ -0,0 +1,25 @@ package com.whyc.controller; import com.whyc.dto.Response; import com.whyc.service.SinfBinfService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @Api(tags = "在用蓄电池模块") @RequestMapping("binf") public class SinfBinfController { @Autowired private SinfBinfService service; @ApiOperation("查询左侧机房信息列表") @GetMapping("getAllSinfBinf") public Response getAllSinfBinf(){ Response res=service.getAllSinfBinf(); return res; } } src/main/java/com/whyc/mapper/BattInfMapper.java
New file @@ -0,0 +1,6 @@ package com.whyc.mapper; import com.whyc.pojo.BattInf; public interface BattInfMapper extends CustomMapper<BattInf>{ } src/main/java/com/whyc/mapper/RtDataMapper.java
New file @@ -0,0 +1,15 @@ package com.whyc.mapper; import com.whyc.pojo.RtData; /** * packageName com.whyc.mapper * * @author lxw * @version JDK 8 * @className RtDataMapper (此处以class为例) * @date 2024/6/15 * @description TODO */ public interface RtDataMapper extends CustomMapper<RtData>{ } src/main/java/com/whyc/mapper/RtStateMapper.java
New file @@ -0,0 +1,15 @@ package com.whyc.mapper; import com.whyc.pojo.RtState; /** * packageName com.whyc.mapper * * @author lxw * @version JDK 8 * @className RtStateMapper (此处以class为例) * @date 2024/6/15 * @description TODO */ public interface RtStateMapper extends CustomMapper<RtState>{ } src/main/java/com/whyc/mapper/SinfBinfMapper.java
New file @@ -0,0 +1,11 @@ package com.whyc.mapper; import com.whyc.pojo.SinfBinf; import com.whyc.pojo.StationInf; import java.util.List; public interface SinfBinfMapper extends CustomMapper<SinfBinf>{ //查询左侧机房信息列表 List<StationInf> getAllSinfBinf(); } src/main/java/com/whyc/pojo/StationInf.java
@@ -9,6 +9,8 @@ import lombok.NoArgsConstructor; import lombok.ToString; import java.util.List; @Data @AllArgsConstructor @NoArgsConstructor @@ -27,4 +29,7 @@ @TableField("sinf_ip") @ApiModelProperty("机房ip") private String sinfIp; @TableField(exist = false) private List<BattInf> binfList; } src/main/java/com/whyc/service/BattInfService.java
New file @@ -0,0 +1,12 @@ package com.whyc.service; import com.whyc.mapper.BattInfMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class BattInfService { @Autowired(required = false) private BattInfMapper mapper; } src/main/java/com/whyc/service/RtDataService.java
New file @@ -0,0 +1,36 @@ package com.whyc.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.whyc.dto.Response; import com.whyc.mapper.RtDataMapper; import com.whyc.pojo.RtData; import com.whyc.pojo.RtState; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * packageName com.whyc.service * * @author lxw * @version JDK 8 * @className RtDataService (此处以class为例) * @date 2024/6/15 * @description TODO */ @Service public class RtDataService { @Autowired(required = false) private RtDataMapper mapper; /* 在用电池组实时推送单体信息 **/ public Response getResRtData(int binfId) { QueryWrapper wrapper=new QueryWrapper(); wrapper.eq("binf_id",binfId); List<RtData> list=mapper.selectList(wrapper); return new Response().setII(1,list!=null,list,"在用电池组实时推送单体信息"); } } src/main/java/com/whyc/service/RtStateService.java
New file @@ -0,0 +1,35 @@ package com.whyc.service; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.whyc.dto.Response; import com.whyc.mapper.RtStateMapper; import com.whyc.pojo.RtState; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * packageName com.whyc.service * * @author lxw * @version JDK 8 * @className RtStateService (此处以class为例) * @date 2024/6/15 * @description TODO */ @Service public class RtStateService { @Autowired(required = false) private RtStateMapper mapper; //在用电池模块实时推送组端信息 public Response getResRtState(int binfId) { QueryWrapper wrapper=new QueryWrapper(); wrapper.eq("binf_id",binfId); wrapper.last("limit 1"); RtState rtState=mapper.selectOne(wrapper); return new Response().setII(1,rtState!=null,rtState,"在用电池模块实时推送组端信息"); } } src/main/java/com/whyc/service/SinfBinfService.java
New file @@ -0,0 +1,27 @@ package com.whyc.service; import com.whyc.dto.Response; import com.whyc.mapper.SinfBinfMapper; import com.whyc.pojo.StationInf; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class SinfBinfService { @Autowired(required = false) private SinfBinfMapper mapper; /*查询左侧机房信息列表 * @param null 入参 * @return null * @author lxw * @date 2024/6/15 9:41 **/ public Response getAllSinfBinf() { List<StationInf> list=mapper.getAllSinfBinf(); return new Response().setII(1,list!=null,list,"左侧机房信息"); } } src/main/java/com/whyc/webSocket/BattSocket.java
New file @@ -0,0 +1,120 @@ package com.whyc.webSocket; import com.whyc.config.WebSocketConfig; import com.whyc.dto.Response; import com.whyc.factory.ThreadPoolExecutorFactory; import com.whyc.pojo.RtState; import com.whyc.pojo.StationInf; import com.whyc.service.BattInfService; import com.whyc.service.RtDataService; import com.whyc.service.RtStateService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.servlet.http.HttpSession; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * packageName com.whyc.websocket * * @author lxw * @version JDK 8 * @className BattSocket (此处以class为例) * @date 2024/6/15 * @description TODO */ @Component @ServerEndpoint(value = "/batt", encoders = WebSocketEncoder.class) public class BattSocket { private Session session; private Thread thread; private volatile boolean runFlag = true; private volatile Map<String, Thread> threadMap = new HashMap<>(); private volatile Map<Long, Boolean> threadFlagMap = new HashMap<>(); private static final int executeTime = 5000; private static RtStateService rtStateService; private static RtDataService rtDataService; @Autowired public void setRtStateService(RtStateService rtStateService) { BattSocket.rtStateService = rtStateService; } @Autowired public void setRtDataService(RtDataService rtDataService) { BattSocket.rtDataService = rtDataService; } @OnOpen public void onOpen(Session session) { this.session = session; } @OnMessage public void onMessage(Session session, String message) { int binfId=Integer.valueOf(message); Thread thread = new Thread() { @Override public void run() { try { Map<String, Object> res = new HashMap<>(); while (!currentThread().isInterrupted()) { ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor(); CountDownLatch latch = new CountDownLatch(2); poolExecutor.execute(() -> { Response resRtState = rtStateService.getResRtState(binfId); res.put("resRtState", resRtState); latch.countDown(); }); poolExecutor.execute(() -> { Response resRtData = rtDataService.getResRtData(binfId); res.put("resRtData", resRtData); latch.countDown(); }); latch.await(10, TimeUnit.MINUTES); session.getBasicRemote().sendObject(new Response().set(1, res)); sleep(executeTime); } } catch (Exception e) { this.interrupt(); } } }; thread.start(); this.thread = thread; } @OnClose public void onClose(CloseReason closeReason) { System.err.println("closeReason = " + closeReason); runFlag = false; if (thread != null && thread.isAlive()) { thread.interrupt(); } threadMap.remove(session.getId()); } @OnError public void onError(Throwable error) { error.printStackTrace(); if (thread != null && thread.isAlive()) { thread.interrupt(); } threadMap.remove(session.getId()); } } src/main/java/com/whyc/webSocket/WebSocketEncoder.java
New file @@ -0,0 +1,30 @@ package com.whyc.webSocket; import com.google.gson.GsonBuilder; import com.whyc.dto.Response; import javax.websocket.EncodeException; import javax.websocket.Encoder; import javax.websocket.EndpointConfig; /** * 编译器 */ public class WebSocketEncoder implements Encoder.Text<Response> { @Override public String encode(Response o) throws EncodeException { return new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(o); } @Override public void init(EndpointConfig endpointConfig) { } @Override public void destroy() { } } src/main/resources/mapper/SinfBinfMapper.xml
New file @@ -0,0 +1,22 @@ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.whyc.mapper.SinfBinfMapper"> <resultMap id="sinfList" type="stationInf"> <id property="sinfId" column="sinf_id"></id> <result property="sinfName" column="sinf_name"></result> <result property="sinfIp" column="sinf_ip"></result> <collection property="binfList" javaType="java.util.ArrayList" ofType="com.whyc.pojo.BattInf" column="{sinfId=sinf_id}" select="selectBinfList"> </collection> </resultMap> <select id="getAllSinfBinf" resultMap="sinfList"> select * from db_batt.station_inf order by sinf_id asc </select> <select id="selectBinfList" resultType="battInf"> select batt_inf.* from db_batt.batt_inf,db_batt.sinf_binf <where> batt_inf.binf_id=sinf_binf.binf_id and sinf_binf.sinf_id=#{sinfId} </where> </select> </mapper>