| | |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.utils.websocket.util.WebSocketUtils; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.Resource; |
| | | import javax.websocket.Session; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.ScheduledFuture; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Description: app位置推送 |
| | |
| | | //app位置上传 |
| | | public static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(20); |
| | | public static final Map<Session, ScheduledFuture<?>> taskMap = new HashMap<>(); |
| | | public static final Map<Session, Map> defaultDataMap = new HashMap<>();//默认会话发送数据map |
| | | public static final Map<String, List<ArdAppPosition>> defaultDataMap = new HashMap<>();//默认会话发送数据map |
| | | |
| | | // 初始定时任务,根据用户设置的频率定期推送实时位置信息 |
| | | public static void initPushTask(Session session, int pushFrequency) { |
| | | public static void initPushTask(String userId,Session session, int pushFrequency) { |
| | | defaultDataMap.put(userId,getAppPositionList()); |
| | | // 启动新的定时任务 |
| | | if (pushFrequency > 0) { |
| | | scheduler.scheduleAtFixedRate(() -> { |
| | | Map map = defaultDataMap.get(session); |
| | | WebSocketUtils.sendMessage(session, map); |
| | | List<ArdAppPosition> appPositionList = defaultDataMap.get(userId); |
| | | Map newMap = new HashMap<>(); |
| | | newMap.put("50000", appPositionList); |
| | | WebSocketUtils.sendMessage(session, newMap); |
| | | }, 0, pushFrequency, TimeUnit.MILLISECONDS); |
| | | } |
| | | } |
| | | |
| | | // 新的定时任务,根据用户设置的频率定期推送实时位置信息 |
| | | public static void startLocationPushTask(String userId, Session session, int pushFrequency) { |
| | | public static void startLocationPushTask(String userId, Session session,String guideUserId, int pushFrequency) { |
| | | // 取消之前设置的定时任务 |
| | | stopLocationPushTask(session); |
| | | |
| | | // 启动新的定时任务 |
| | | if (pushFrequency > 0) { |
| | | ScheduledFuture<?> task = ScheduledFutureTask(session, userId, pushFrequency); |
| | | ScheduledFuture<?> task = ScheduledFutureTask(session, userId,guideUserId, pushFrequency); |
| | | taskMap.put(session, task); // 存储新的定时任务 |
| | | } |
| | | } |
| | | public static ScheduledFuture<?> ScheduledFutureTask(Session session, String userId, Integer pushFrequency) { |
| | | |
| | | public static ScheduledFuture<?> ScheduledFutureTask(Session session, String userId,String guideUserId, Integer pushFrequency) { |
| | | return scheduler.scheduleAtFixedRate(() -> { |
| | | ArdAppPosition ardAppPosition = getAppPositionList().get(userId); |
| | | Map<String, ArdAppPosition> DataMap = new HashMap<>(); |
| | | DataMap.put(userId,ardAppPosition); |
| | | WebSocketUtils.sendMessage(session, DataMap); |
| | | List<ArdAppPosition> appPositionList = getAppPositionList().stream() |
| | | .filter(obj -> obj.getUserId().equals(guideUserId)) |
| | | .collect(Collectors.toList()); |
| | | Map newMap = new HashMap<>(); |
| | | newMap.put("50000", appPositionList); |
| | | WebSocketUtils.sendMessage(session, newMap); |
| | | }, 0, pushFrequency, TimeUnit.MILLISECONDS); |
| | | } |
| | | |
| | | //取消定时任务 |
| | | public static void stopLocationPushTask(Session session) { |
| | | ScheduledFuture<?> scheduledTask = taskMap.get(session); |
| | |
| | | scheduledTask.cancel(false); |
| | | } |
| | | } |
| | | |
| | | //查询所有app用户的位置信息 |
| | | public static Map<String,ArdAppPosition> getAppPositionList() |
| | | { |
| | | Map<String,ArdAppPosition> userMap=new HashMap<>(); |
| | | public static List<ArdAppPosition> getAppPositionList() { |
| | | List<ArdAppPosition>ArdAppPositions = new ArrayList<>(); |
| | | ISysUserService sysUserService = SpringUtils.getBean(ISysUserService.class); |
| | | //获取所有app用户 |
| | | List<SysUser> list = sysUserService.selectAppUserListNoDataScope(new SysUser()); |
| | | for(SysUser sysUser:list) |
| | | { |
| | | List<SysUser> list = sysUserService.selectAllAppUserList(new SysUser()); |
| | | for (SysUser sysUser : list) { |
| | | IArdAppPositionService ardAppPositionService = SpringUtils.getBean(IArdAppPositionService.class); |
| | | ArdAppPosition ardAppPosition = ardAppPositionService.selectLastArdAppPositionByUserId(sysUser.getUserId()); |
| | | userMap.put(sysUser.getUserId(),ardAppPosition); |
| | | ArdAppPositions.add(ardAppPosition); |
| | | } |
| | | |
| | | return userMap; |
| | | return ArdAppPositions; |
| | | } |
| | | } |
| | | |