whyclxw
2024-08-28 86480602129429a2dbf5c3743f93049feaa7bb72
设备实时状态推送
4个文件已修改
83 ■■■■ 已修改文件
src/main/java/com/whyc/controller/DevInfController.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/mapper/DevInfMapper.java 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/DevInfSocket.java 66 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/DevInfMapper.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/DevInfController.java
@@ -1,5 +1,6 @@
package com.whyc.controller;
import com.whyc.dto.Page;
import com.whyc.dto.Response;
import com.whyc.pojo.db_lithium_ram_db.DevInf;
import com.whyc.service.DevInfService;
@@ -21,4 +22,10 @@
    public Response addDev(@RequestBody DevInf devInf){
        return service.addDev(devInf);
    }
/*    @ApiOperation(value = "获取设备信息")
    @PostMapping("getAllInf")
    public Response getAllInf(@RequestBody Page page){
        return service.getAllInf(101,page);
    }*/
}
src/main/java/com/whyc/mapper/DevInfMapper.java
@@ -1,12 +1,15 @@
package com.whyc.mapper;
import com.whyc.pojo.db_lithium_ram_db.DevInf;
import lombok.Value;
import org.apache.ibatis.annotations.Param;
import javax.websocket.server.PathParam;
import java.util.List;
public interface DevInfMapper extends CustomMapper<DevInf>{
    //查询包机人管理的设备id
    List<DevInf> getAllInf(Integer uid);
    List<DevInf> getAllInf(@Param("uid") Integer uid);
    //查询该类型最大设备编号
    Integer getMaxDevId(Integer devType);
}
src/main/java/com/whyc/webSocket/DevInfSocket.java
@@ -13,6 +13,11 @@
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
@@ -29,6 +34,12 @@
    private HttpSession httpSession;
    private volatile boolean runFlag = true;
    private volatile Map<String, Thread> threadMap = new HashMap<>();
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
    @Autowired
    public void setDevInfService(DevInfService dinfService) {
        DevInfSocket.dinfService = dinfService;
@@ -42,43 +53,62 @@
    @OnMessage
    public void onMessage(Session session, String message) {
        Page page= ActionUtil.getGson().fromJson(message,Page.class);
        UserInf user = (UserInf) this.httpSession.getAttribute("user");
        final int userId = user.getUid();
        //final int userId = 1087;
        Thread thread = new Thread() {
        //UserInf user = (UserInf) this.httpSession.getAttribute("user");
        //final int userId = user.getUid();
        final int userId = 101;
        thread = new Thread("Thread_DinfSocket") {
            @Override
            public void run() {
                try {
                    while (!currentThread().isInterrupted()) {
                Map<String, Object> res = new HashMap<>();
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        Response response=dinfService.getAllInf(userId,page);
                        session.getBasicRemote().sendObject(response);
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(new Response().set(1, response));
                            }
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(4000);
                    } catch (Exception e) {
                        interrupt();
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
        threadFlagMap.put(thread.getId(),true);
        //停止老的socket线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            while (threadFlagMap.get(threadBefore.getId())){
            }
            threadBefore.interrupt();
        }
        //将线程存储,便于调用定位
        threadMap.put(session.getId(), this.thread);
    }
    @OnClose
    public void onClose(CloseReason closeReason) throws IOException {
    public void onClose(CloseReason closeReason){
        System.err.println("closeReason = " + closeReason);
        if(session.isOpen()){
            session.close();
        runFlag = false;
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
    @OnError
    public void onError(Throwable error) throws IOException {
    public void onError(Throwable error) {
        error.printStackTrace();
        thread.isInterrupted();
        if(session.isOpen()){
            session.close();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
}
src/main/resources/mapper/DevInfMapper.xml
@@ -6,10 +6,11 @@
        select * from db_lithium_ram_db.tb_dev_inf
        <where>
            dev_id in (
            select distinct  dev_id from db_user.tb_battgroup_baojigroup
            SELECT distinct dev_id from db_user.tb_battgroup_baojigroup,db_user.tb_battgroup_usr
            <where>
                tb_battgroup_baojigroup.baoji_group_id=tb_battgroup_usr.baoji_group_id
                <if test="uid!=null">
                    user_id=#{uid}
                    and uid=#{uid}
                </if>
            </where>