src/main/java/com/whyc/config/CaffeineConfig.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/dto/RealDto.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/mapper/LockInfMapper.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/service/LockInfService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/service/UserBridgeService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/webSocket/RealLockSocket.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/resources/mapper/LockInfMapper.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/whyc/config/CaffeineConfig.java
@@ -1,3 +1,4 @@ /* package com.whyc.config; import com.github.benmanes.caffeine.cache.Caffeine; @@ -10,9 +11,11 @@ import java.util.ArrayList; */ /** * Caffeine配置 */ *//* @Configuration public class CaffeineConfig { @@ -71,6 +74,7 @@ } */ /*@Bean(value = "defaultCache") public Cache<String,Object> defaultCache(){ return Caffeine.newBuilder().recordStats() @@ -84,6 +88,8 @@ .maximumSize(Caches.defaultCache2Exp.getMaxSize()) .expireAfterWrite(Caches.defaultCache2Exp.ttl, TimeUnit.HOURS) .build(); }*/ }*//* } */ src/main/java/com/whyc/dto/RealDto.java
New file @@ -0,0 +1,10 @@ package com.whyc.dto; import lombok.Data; @Data public class RealDto { private Integer areaId; private Integer pageNum; private Integer pageSize; } src/main/java/com/whyc/mapper/LockInfMapper.java
@@ -18,4 +18,6 @@ //根据锁的lockIds查询锁的记录 List<LockInf> selectlinfByLockIds(@Param("lockIds") List<Integer> lockIds); //实时获取获取区域下所有锁的状态 List<LockInf> getRealLock(@Param("areaList") List<Integer> areaList); } src/main/java/com/whyc/service/LockInfService.java
@@ -13,12 +13,11 @@ import com.whyc.pojo.db_area.LockInf; import com.whyc.pojo.db_user.UserInf; import com.whyc.util.ActionUtil; import com.whyc.util.PageInfoUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.*; import java.util.stream.Collectors; @Service @@ -185,4 +184,45 @@ .collect(Collectors.toList()); // 转换为列表*/ return new Response().setII(1,list!=null,typeList,"查询屏柜全部品牌(下拉)"); } //实时获取获取区域下所有锁的状态 public Response getRealLock(Integer areaId, Integer pageNum, Integer pageSize) { Map<String,Object> map=new HashMap<>(); map.put("sumLinf",0); map.put("onlineNum",0); map.put("offLineNum",0); map.put("openNum",0); map.put("closeNum",0); map.put("unLoadNum",0); List<Integer> areaList=new ArrayList(); areaList.add(areaId); areaInfService.getAllAreaId(areaId,areaList); List<LockInf> linfs=mapper.getRealLock(areaList); if(linfs!=null&&linfs.size()>0){ map.put("sumLinf",linfs.size()); Map<Integer, List<LockInf>> onlinemap = linfs.stream().collect(Collectors.groupingBy(LockInf::getLockOnline)); for (Integer state : onlinemap.keySet()) { if(state==0){ map.put("offLineNum", onlinemap.get(0).size());//离线 } if(state==1){ map.put("onlineNum", onlinemap.get(1).size());//在线 } } Map<Integer, List<LockInf>> openmap = linfs.stream().collect(Collectors.groupingBy(LockInf::getLockState)); for (Integer open : openmap.keySet()) { if(open==0){ map.put("closeNum", openmap.get(0).size());//闭锁 } if(open==1){ map.put("openNum", openmap.get(1).size());//开锁 } if(open==-1){ map.put("unLoadNum", openmap.get(-1).size());//未安装 } } } PageInfo pageInfo= PageInfoUtils.list2PageInfo(linfs,pageNum,pageSize); map.put("pageInfo",pageInfo); return new Response().setII(1,linfs!=null,map,"实时获取获取区域下所有锁的状态"); } } src/main/java/com/whyc/service/UserBridgeService.java
@@ -19,14 +19,14 @@ @Service //Unified Naming //@CacheConfig(cacheNames ={"userBridge"}) @DependsOn("caffeineCacheManager") //@DependsOn("caffeineCacheManager") public class UserBridgeService { @Resource private DocUserMapper userMapper; @Resource private CacheManager caffeineCacheManager; /* @Resource private CacheManager caffeineCacheManager;*/ public UserInf findPasswordByUserName(String userName) { UserInf userInf = null; src/main/java/com/whyc/webSocket/RealLockSocket.java
New file @@ -0,0 +1,108 @@ package com.whyc.webSocket; import com.whyc.config.WebSocketConfig; import com.whyc.dto.LockRDto; import com.whyc.dto.RealDto; import com.whyc.dto.Response; import com.whyc.service.LockInfService; import com.whyc.util.JsonUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.util.HashMap; import java.util.Map; /** * 实时状态 */ @Component @ServerEndpoint(value = "/real",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class) public class RealLockSocket { private Session session; private Thread thread; private static LockInfService lockInfService; private volatile boolean runFlag = true; private volatile Map<String, Thread> threadMap = new HashMap<>(); private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>(); @Autowired public void setLockInfService(LockInfService lockInfService) { RealLockSocket.lockInfService = lockInfService; } @OnOpen public void onOpen(Session session) { this.session = session; } @OnMessage public void onMessage(Session session, String message) { RealDto dto= JsonUtil.getGson().fromJson(message,RealDto.class); thread = new Thread("Thread_realLockSocket") { @Override public void run() { while (runFlag && !isInterrupted()) { Thread thread = currentThread(); threadFlagMap.put(thread.getId(), true); try { Response res=getRealLock(dto); if (session.isOpen()) { //推送信息 synchronized (session) { session.getBasicRemote().sendObject(res); } threadFlagMap.put(thread.getId(), false); } sleep(4000); } catch (Exception e) { interrupt(); } } } }; thread.start(); 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); } //获取区域下所有锁的状态 private Response getRealLock(RealDto dto) { Response res=lockInfService.getRealLock(dto.getAreaId(),dto.getPageNum(),dto.getPageSize()); return res; } @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/resources/mapper/LockInfMapper.xml
@@ -52,5 +52,18 @@ </if> </where> </select> <select id="getRealLock" resultType="com.whyc.pojo.db_area.LockInf"> select tb_lock_inf.*,tb_area_inf.area_name,tb_area_inf.area_path from db_area.tb_lock_inf,db_area.tb_area_inf <where> tb_lock_inf.area_id=tb_area_inf.id <if test="areaList!=null"> and area_id in <foreach collection="areaList" item="id" open="(" separator="," close=")"> #{id} </foreach> </if> </where> order by last_update_time desc </select> </mapper>