whyczh
2022-02-18 f99ef3e0d5c4fc6553b1e8e54e7b167e8eba4cdb
添加socket接口
4个文件已添加
1个文件已修改
350 ■■■■■ 已修改文件
src/main/java/com/whyc/controller/RealTimeController.java 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/BattDischargePlanSocket.java 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/PowerAlarmRealTimeSocket.java 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/PwrAlarmSocket.java 70 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/PwrAlarmTimeoutSocket.java 96 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/RealTimeController.java
@@ -1,9 +1,13 @@
package com.whyc.controller;
import com.whyc.dto.Response;
import com.whyc.dto.paramter.*;
import com.whyc.pojo.BattdischargePlan;
import com.whyc.pojo.PwrdevAlarm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -22,7 +26,39 @@
    @GetMapping("getProcessSurvey")
    public Response getProcessSurveyList(){return new Response();}
    @ApiOperation(value = "设备工作状态",notes = "请求链接:ws://localhost:8089/fg/fbsState 传参:{\"pageNum\":\"1\",\"pageSize\":\"10\",\"devErrcommcount\":\"1\",\"userId\":\"1002\"}")
    @ApiOperation(value = "设备工作状态",notes = "请求链接:ws://localhost:8089/fg/fbsState 传参:FbsStatePar实体类")
    @GetMapping("getFbsState")
    public Response getFbsState(){return new Response();}
    public Response getFbsState(@RequestBody FbsStatePar fbsStatePar){return new Response();}
    @ApiOperation(value = "报废电池信息管理",notes = "请求链接:ws://localhost:8089/fg/battScrap 传参:BattScrapPar实体类")
    @GetMapping("getBattScrap")
    public Response getBattScrap(@RequestBody BattScrapPar battScrapPar){return new Response();}
    @ApiOperation(value = "备件电池信息管理",notes = "请求链接:ws://localhost:8089/fg/battSpare 传参:BattSparePar实体类")
    @GetMapping("getBattSpare")
    public Response getBattSpare(@RequestBody BattSparePar battSparePar){return new Response();}
    @ApiOperation(value = "电池实时告警",notes = "请求链接:ws://localhost:8089/fg/battAlarmData 传参:AlarmPar实体类")
    @GetMapping("getBattAlarm")
    public Response getBattAlarm(@RequestBody AlarmPar alarmPar){return new Response();}
    @ApiOperation(value = "设备实时告警",notes = "请求链接:ws://localhost:8089/fg/devAlarmData 传参:DevAlarmPar实体类")
    @GetMapping("getDevAlarm")
    public Response getDevAlarm(@RequestBody DevAlarmPar devAlarmPar){return new Response();}
    @ApiOperation(value = "电源实时告警",notes = "请求链接:ws://localhost:8089/fg/powerAlarmRt 传参:PwrdevAlarm实体类")
    @GetMapping("getPowerAlarmRt")
    public Response getPowerAlarmRt(@RequestBody PwrdevAlarm pwrdevAlarm){return new Response();}
    @ApiOperation(value = "通信电源实时告警",notes = "请求链接:ws://localhost:8089/fg/pwrAlarm 传参:PwrdevAlarm实体类")
    @GetMapping("getTxPowerAlarm")
    public Response getTxPowerAlarm(@RequestBody PwrdevAlarm pwrdevAlarm){return new Response();}
    @ApiOperation(value = "通信电源超时告警",notes = "请求链接:ws://localhost:8089/fg/powerAlarmTimeout 传参:为空")
    @GetMapping("getPwrTimeoutAlarm")
    public Response getPwrTimeoutAlarm(){return new Response();}
    @ApiOperation(value = "放电计划管理",notes = "请求链接:ws://localhost:8089/fg/battDischargePlan 传参:BattdischargePlan实体类")
    @GetMapping("getBattDischargePlan")
    public Response getBattDischargePlan(@RequestBody BattdischargePlan battdischargePlan){return new Response();}
}
src/main/java/com/whyc/webSocket/BattDischargePlanSocket.java
New file
@@ -0,0 +1,72 @@
package com.whyc.webSocket;
import com.whyc.pojo.BattdischargePlan;
import com.whyc.pojo.PwrdevAlarm;
import com.whyc.service.BattdischargePlanService;
import com.whyc.service.PwrdevAlarmService;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
@ServerEndpoint(value = "/battDischargePlan",encoders = WebSocketEncoder.class)
public class BattDischargePlanSocket {
    private volatile Thread thread;
    private static final int executeTime = 5000;
    private static Map<String,Thread> threadMap = new HashMap<>();
    private static BattdischargePlanService service;
    @Autowired
    public void setService(BattdischargePlanService service){
        BattDischargePlanSocket.service = service;
    }
    @OnMessage
    public void onMessage(Session session, String message){
        //停止当前socket的线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            threadBefore.interrupt();
        }
        BattdischargePlan battdischargePlan = ActionUtil.getGson().fromJson(message, BattdischargePlan.class);
        thread = new Thread("Thread_battDischargePlan") {
            public void run() {
                while (!thread.isInterrupted()) {
                    try {
                        if (session.isOpen()) {
                            session.getBasicRemote().sendObject(service.serchByCondition(battdischargePlan));
                        }
                        sleep(executeTime);
                    } catch (IOException | InterruptedException | EncodeException e) {
                        interrupt();
                    }
                }
            }
        };
        thread.start();
    }
    @OnClose
    public void onClose(){
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
}
src/main/java/com/whyc/webSocket/PowerAlarmRealTimeSocket.java
New file
@@ -0,0 +1,72 @@
package com.whyc.webSocket;
import com.whyc.dto.paramter.DevAlarmPar;
import com.whyc.pojo.PwrdevAlarm;
import com.whyc.service.DevalarmDataService;
import com.whyc.service.PwrdevAlarmService;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
@ServerEndpoint(value = "/powerAlarmRt",encoders = WebSocketEncoder.class)
public class PowerAlarmRealTimeSocket {
    private volatile Thread thread;
    private static final int executeTime = 5000;
    private static Map<String,Thread> threadMap = new HashMap<>();
    private static PwrdevAlarmService service;
    @Autowired
    public void setService(PwrdevAlarmService service){
        PowerAlarmRealTimeSocket.service = service;
    }
    @OnMessage
    public void onMessage(Session session, String message){
        //停止当前socket的线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            threadBefore.interrupt();
        }
        PwrdevAlarm pwrdevAlarm = ActionUtil.getGson().fromJson(message, PwrdevAlarm.class);
        thread = new Thread("Thread_powerAlarmRt") {
            public void run() {
                while (!thread.isInterrupted()) {
                    try {
                        if (session.isOpen()) {
                            session.getBasicRemote().sendObject(service.getAllPage(pwrdevAlarm));
                        }
                        sleep(executeTime);
                    } catch (IOException | InterruptedException | EncodeException e) {
                        interrupt();
                    }
                }
            }
        };
        thread.start();
    }
    @OnClose
    public void onClose(){
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
}
src/main/java/com/whyc/webSocket/PwrAlarmSocket.java
New file
@@ -0,0 +1,70 @@
package com.whyc.webSocket;
import com.whyc.pojo.PwrdevAlarm;
import com.whyc.service.PwrdevAlarmService;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
@ServerEndpoint(value = "/pwrAlarm",encoders = WebSocketEncoder.class)
public class PwrAlarmSocket {
    private volatile Thread thread;
    private static final int executeTime = 5000;
    private static Map<String,Thread> threadMap = new HashMap<>();
    private static PwrdevAlarmService service;
    @Autowired
    public void setService(PwrdevAlarmService service){
        PwrAlarmSocket.service = service;
    }
    @OnMessage
    public void onMessage(Session session, String message){
        //停止当前socket的线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            threadBefore.interrupt();
        }
        PwrdevAlarm pwrdevAlarm = ActionUtil.getGson().fromJson(message, PwrdevAlarm.class);
        thread = new Thread("Thread_pwrAlarm") {
            public void run() {
                while (!thread.isInterrupted()) {
                    try {
                        if (session.isOpen()) {
                            session.getBasicRemote().sendObject(service.getAllPage2(pwrdevAlarm));
                        }
                        sleep(executeTime);
                    } catch (IOException | InterruptedException | EncodeException e) {
                        interrupt();
                    }
                }
            }
        };
        thread.start();
    }
    @OnClose
    public void onClose(){
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
}
src/main/java/com/whyc/webSocket/PwrAlarmTimeoutSocket.java
New file
@@ -0,0 +1,96 @@
package com.whyc.webSocket;
import com.whyc.dto.Response;
import com.whyc.dto.paramter.FbsStatePar;
import com.whyc.service.Fbs9100StateService;
import com.whyc.service.Fbs9600StateService;
import com.whyc.service.PwrdevTimeAdjAlarmService;
import com.whyc.service.PwrdevTimeOutAlarmService;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@ServerEndpoint(value = "/powerAlarmTimeout",encoders = WebSocketEncoder.class)
@Component
public class PwrAlarmTimeoutSocket {
    private volatile Thread thread;
    private static final int executeTime = 15000;
    private static boolean exit=false;
    private static Map<String,Thread> threadMap = new HashMap<>();
    private static PwrdevTimeAdjAlarmService adjAlarmService;
    private static PwrdevTimeOutAlarmService timeOutAlarmService;
    private Session session;
    @Autowired
    public void setService(PwrdevTimeAdjAlarmService adjAlarmService) {
        PwrAlarmTimeoutSocket.adjAlarmService = adjAlarmService;
    }
    @Autowired
    public void setService(PwrdevTimeOutAlarmService timeOutAlarmService) {
        PwrAlarmTimeoutSocket.timeOutAlarmService = timeOutAlarmService;
    }
    @OnMessage
    public void onMessage(Session session, String message){
        //停止当前socket的线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            threadBefore.interrupt();
            exit=true;
        }
        thread = new Thread("Thread_powerAlarmTimeout") {
            public void run() {
                exit=false;
                while ( !this.isInterrupted()&&!exit) {
                    try {
                        if (session.isOpen()) {
                            Map<String, Response> res=new HashMap<>();
                            //查询fbs9100信息
                            Response adjAlarmRes=adjAlarmService.getList();
                            res.put("adjAlarmService",adjAlarmRes);
                            Response timeOutAlarmRes=timeOutAlarmService.getList();
                            res.put("timeOutAlarmService",timeOutAlarmRes);
                            //推送信息
                            session.getBasicRemote().sendObject(new Response().set(1,res));
                        }
                        sleep(executeTime);
                    } catch ( IOException | InterruptedException | EncodeException e ) {
                        interrupt();
                    }
                }
            }
        };
        thread.start();
        //将线程存储,便于调用定位
        threadMap.put(session.getId(), this.thread);
    }
    @OnClose
    public void onClose(){
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }
}