| | |
| | | package com.ruoyi.app.position.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.app.position.domain.ArdAppPosition; |
| | | import com.ruoyi.app.position.service.IArdAppPositionService; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | |
| | | * @Version 1.0 |
| | | * {"enabled":false,"frequency":1000,"userId":"69cf75f289424a48ba10cf33fa324206"} |
| | | */ |
| | | @Slf4j |
| | | public class AppPositionPushService { |
| | | public static Map<String, List<ArdAppPosition>> dataMap = new HashMap<>();//常规数据 每个用户关联 |
| | | public static Map<String, List<ArdAppPosition>> tempDataMap = new HashMap<>();//临时数据 每个用户关联 |
| | | public static Map<String, Timer> userTimerMap = new HashMap();//定时器 每个用户关联 |
| | | public static Map<String, Timer> tempUserTimerMap = new HashMap();//临时定时器 每个用户关联 |
| | | public static List<ArdAppPosition> initDataList; |
| | | |
| | | static { |
| | | initDataList = getAppPositionList(); |
| | | } |
| | | // public static List<ArdAppPosition> initDataList; |
| | | |
| | | //启动全局定时器 |
| | | public static Timer startTimerTask(String userId, Integer period) { |
| | |
| | | @Override |
| | | public void run() { |
| | | List<ArdAppPosition> data = dataMap.get(userId); |
| | | if (data.size() > 0) { |
| | | List<ArdAppPosition> onlineData = isOnlie(data); |
| | | if (onlineData.size() > 0) { |
| | | Map newMap = new HashMap<>(); |
| | | newMap.put("50000", data); |
| | | newMap.put("50000", onlineData); |
| | | WebSocketUtils.sendMessage(WebSocketUtils.ONLINE_USER_SESSIONS.get(userId), newMap); |
| | | } |
| | | } |
| | |
| | | public void run() { |
| | | //循环 |
| | | List<ArdAppPosition> data = tempDataMap.get(userId); |
| | | if (data.size() > 0) { |
| | | List<ArdAppPosition> onlineData = isOnlie(data); |
| | | if (onlineData.size() > 0) { |
| | | Map newMap = new HashMap<>(); |
| | | newMap.put("50000", data); |
| | | newMap.put("50000", onlineData); |
| | | WebSocketUtils.sendMessage(WebSocketUtils.ONLINE_USER_SESSIONS.get(userId), newMap); |
| | | } |
| | | } |
| | |
| | | public static void stopTimerTask(Timer timer) { |
| | | if (timer != null) { |
| | | timer.cancel(); |
| | | log.info("停止运行"); |
| | | } |
| | | } |
| | | |
| | |
| | | ISysUserService sysUserService = SpringUtils.getBean(ISysUserService.class); |
| | | //获取所有app用户 |
| | | List<SysUser> list = sysUserService.selectAllAppUserList(new SysUser()); |
| | | List<SysUser> onlineList = list.stream() |
| | | .filter(obj -> obj.getAppOnlineState().equals("1")) |
| | | .collect(Collectors.toList()); |
| | | for (SysUser sysUser : onlineList) { |
| | | // List<SysUser> onlineList = list.stream() |
| | | // .filter(obj -> obj.getAppOnlineState().equals("1")) |
| | | // .collect(Collectors.toList()); |
| | | for (SysUser sysUser : list) { |
| | | IArdAppPositionService ardAppPositionService = SpringUtils.getBean(IArdAppPositionService.class); |
| | | ArdAppPosition ardAppPosition = ardAppPositionService.selectLastArdAppPositionByUserId(sysUser.getUserId()); |
| | | ArdAppPositions.add(ardAppPosition); |
| | | if (StringUtils.isNotNull(ardAppPosition)) { |
| | | Map<String, Object> params=new HashMap<>(); |
| | | params.put("longitude",ardAppPosition.getLongitude()); |
| | | params.put("latitude",ardAppPosition.getLatitude()); |
| | | params.put("speed",ardAppPosition.getSpeed()); |
| | | params.put("bearing",ardAppPosition.getBearing()); |
| | | ardAppPosition.setParams(params); |
| | | ArdAppPositions.add(ardAppPosition); |
| | | } |
| | | } |
| | | return ArdAppPositions; |
| | | } |
| | | |
| | | //过滤在线app用户 |
| | | public static List<ArdAppPosition> isOnlie(List<ArdAppPosition> data) { |
| | | List<ArdAppPosition> onlineList = new ArrayList<>(); |
| | | if (data.size() > 0) { |
| | | for (ArdAppPosition ardAppPosition : data) { |
| | | ISysUserService sysUserService = SpringUtils.getBean(ISysUserService.class); |
| | | SysUser sysUser = sysUserService.selectUserById(ardAppPosition.getUserId()); |
| | | if(StringUtils.isNotNull(sysUser)) { |
| | | if(StringUtils.isNotNull(sysUser.getAppOnlineState())) { |
| | | if (sysUser.getAppOnlineState().equals("1")) { |
| | | onlineList.add(ardAppPosition); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return onlineList; |
| | | } |
| | | |
| | | //接收处理器 |
| | | public static void messageHandler(String userId, String message) { |
| | | Map<String, Object> map = JSONObject.parseObject(message, Map.class); |
| | | if (map.size() > 0) { |
| | | Boolean enabled = (Boolean) map.get("enabled"); |
| | | if (enabled) { |
| | | //启动全局推送 |
| | | List<ArdAppPosition> newList = new ArrayList<>(AppPositionPushService.getAppPositionList()); |
| | | AppPositionPushService.dataMap.put(userId, newList); |
| | | if (!AppPositionPushService.userTimerMap.containsKey(userId)) { |
| | | Timer timer = AppPositionPushService.startTimerTask(userId, 3000); |
| | | AppPositionPushService.userTimerMap.put(userId, timer);//将定时器关联用户 |
| | | } |
| | | } else { |
| | | //停止全局推送 |
| | | if (AppPositionPushService.userTimerMap.containsKey(userId)) { |
| | | Timer timer = AppPositionPushService.userTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(timer); |
| | | AppPositionPushService.userTimerMap.remove(userId); |
| | | } |
| | | if (AppPositionPushService.tempUserTimerMap.containsKey(userId)) { |
| | | Timer temptimer = AppPositionPushService.tempUserTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(temptimer); |
| | | AppPositionPushService.tempUserTimerMap.remove(userId); |
| | | } |
| | | AppPositionPushService.dataMap.remove(userId); |
| | | AppPositionPushService.tempDataMap.remove(userId); |
| | | } |
| | | |
| | | Integer frequency = (Integer) map.get("frequency"); |
| | | String uId = (String) map.get("userId"); |
| | | if (frequency > 0) { |
| | | List<ArdAppPosition> List = AppPositionPushService.getAppPositionList().stream() |
| | | .filter(obj -> !obj.getUserId().equals(uId)) |
| | | .collect(Collectors.toList());//去除引导后的list |
| | | AppPositionPushService.dataMap.put(userId, List); |
| | | |
| | | List<ArdAppPosition> tempList = AppPositionPushService.getAppPositionList().stream() |
| | | .filter(obj -> obj.getUserId().equals(uId)) |
| | | .collect(Collectors.toList());//获取引导的list |
| | | AppPositionPushService.tempDataMap.put(userId, tempList); |
| | | Timer timer = AppPositionPushService.startTempTimerTask(userId, frequency); |
| | | AppPositionPushService.tempUserTimerMap.put(userId, timer); |
| | | } else { |
| | | List<ArdAppPosition> newList = new ArrayList<>(AppPositionPushService.getAppPositionList()); |
| | | AppPositionPushService.dataMap.put(userId, newList); |
| | | Timer timer = AppPositionPushService.tempUserTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(timer); |
| | | AppPositionPushService.tempUserTimerMap.remove(userId); |
| | | AppPositionPushService.tempDataMap.remove(userId); |
| | | } |
| | | //region 旧方法 |
| | | // Map<String, List<ArdAppPosition>> dataMap = new HashMap<>(); |
| | | // List<ArdAppPosition> positionList = AppPositionPush.initDataMap.get(userId);//获取当前用户的默认发送的数据map |
| | | // if(frequency>0) |
| | | // { |
| | | // //接收到频率大于0,属于申请引导,获取除引导外的其他app用户的位置List |
| | | // List<ArdAppPosition> appPositionList = positionList.stream() |
| | | // .filter(obj -> !obj.getUserId().equals(uId)) |
| | | // .collect(Collectors.toList()); |
| | | // dataMap.put(userId,appPositionList); |
| | | // } |
| | | // else { |
| | | // //接收到频率等于0,属于释放引导,获取到全部app用户的位置List |
| | | // dataMap.put(userId,AppPositionPush.getAppPositionList()); |
| | | // } |
| | | // AppPositionPush.initDataMap.putAll(dataMap);//更新初始会话默认数据map |
| | | // AppPositionPush.startLocationPushTask(userId,session,uId, frequency);//开启新任务 |
| | | //endregion |
| | | } |
| | | } |
| | | |
| | | //停止处理器 |
| | | public static void stopHandler(String userId) { |
| | | //停止全局推送 |
| | | if (AppPositionPushService.userTimerMap.containsKey(userId)) { |
| | | Timer timer = AppPositionPushService.userTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(timer); |
| | | AppPositionPushService.userTimerMap.remove(userId); |
| | | } |
| | | if (AppPositionPushService.tempUserTimerMap.containsKey(userId)) { |
| | | Timer temptimer = AppPositionPushService.tempUserTimerMap.get(userId); |
| | | AppPositionPushService.stopTimerTask(temptimer); |
| | | AppPositionPushService.tempUserTimerMap.remove(userId); |
| | | } |
| | | AppPositionPushService.dataMap.remove(userId); |
| | | AppPositionPushService.tempDataMap.remove(userId); |
| | | // AppPositionPush.stopLocationPushTask(session);//旧方法 |
| | | } |
| | | } |