| | |
| | | package com.whyc.webSocket; |
| | | |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.dto.paramter.DevAlarmPar; |
| | | import com.whyc.pojo.PwrdevAlarm; |
| | | import com.whyc.service.DevalarmDataService; |
| | | import com.whyc.service.PwrDevAlarmVerifyService; |
| | | import com.whyc.service.PwrdevAlarmService; |
| | | import com.whyc.util.ActionUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | |
| | | |
| | | private static final int executeTime = 5000; |
| | | |
| | | private static boolean exit=false; |
| | | private volatile boolean runFlag=true; |
| | | |
| | | private static Map<String,Thread> threadMap = new HashMap<>(); |
| | | private volatile Map<String,Thread> threadMap = new HashMap<>(); |
| | | |
| | | private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>(); |
| | | |
| | | private static PwrdevAlarmService service; |
| | | |
| | | private static PwrDevAlarmVerifyService verifyService; |
| | | |
| | | |
| | | private Session session; |
| | | |
| | | @Autowired |
| | | public void setService(PwrdevAlarmService service){ |
| | | PowerAlarmRealTimeSocket.service = service; |
| | | } |
| | | |
| | | @Autowired |
| | | public void setVerifyService(PwrDevAlarmVerifyService verifyService) { |
| | | PowerAlarmRealTimeSocket.verifyService = verifyService; |
| | | } |
| | | |
| | | @OnOpen |
| | |
| | | public void onMessage(Session session, String message){ |
| | | PwrdevAlarm pwrdevAlarm = ActionUtil.getGson().fromJson(message, PwrdevAlarm.class); |
| | | thread = new Thread("Thread_powerAlarmRt") { |
| | | @Override |
| | | public void run() { |
| | | while (true) { |
| | | while (runFlag && !isInterrupted()) { |
| | | Thread thread = currentThread(); |
| | | threadFlagMap.put(thread.getId(),true); |
| | | try { |
| | | if (session.isOpen()) { |
| | | Map<String, Response> res = new HashMap<>(); |
| | | Response alarmRes = service.getAllPageOfWebSocket(pwrdevAlarm); |
| | | res.put("alarmRes", alarmRes); |
| | | //查询分级告警的个数 |
| | | Response levelRes = service.serchByLevel(pwrdevAlarm.getUsrId(), pwrdevAlarm.getAlmTypes()); |
| | | res.put("levelRes", levelRes); |
| | | //告警手动确认 |
| | | Response verifyListRes = verifyService.getPageOfWebSocket(pwrdevAlarm); |
| | | res.put("verifyListRes",verifyListRes); |
| | | //推送信息 |
| | | synchronized (session) { |
| | | session.getBasicRemote().sendObject(service.getAllPageOfWebSocket(pwrdevAlarm)); |
| | | session.getBasicRemote().sendObject(new Response().set(1, res)); |
| | | } |
| | | threadFlagMap.put(thread.getId(),false); |
| | | threadFlagMap.put(thread.getId(), false); |
| | | } |
| | | sleep(executeTime); |
| | | //} catch (IOException | InterruptedException | EncodeException e) { |
| | |
| | | @OnClose |
| | | public void onClose(CloseReason closeReason){ |
| | | System.err.println("closeReason = " + closeReason); |
| | | runFlag = false; |
| | | if (thread != null && thread.isAlive()) { |
| | | thread.interrupt(); |
| | | } |