diff --git a/src/main/java/org/springblade/modules/business/controller/CommonApiController.java b/src/main/java/org/springblade/modules/business/controller/CommonApiController.java index e3c956d..1481c74 100644 --- a/src/main/java/org/springblade/modules/business/controller/CommonApiController.java +++ b/src/main/java/org/springblade/modules/business/controller/CommonApiController.java @@ -1,14 +1,19 @@ package org.springblade.modules.business.controller; +import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.swagger.annotations.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.beetl.core.util.ArrayMap; +import org.jetbrains.annotations.NotNull; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STOleUpdate; import org.springblade.common.cache.business.BusLineCache; import org.springblade.common.cache.business.SiteListCache; import org.springblade.common.constant.BusinessConstant; import org.springblade.common.constant.CommonConstant; +import org.springblade.common.utils.CommonDateUtil; import org.springblade.common.utils.CommonUtil; import org.springblade.core.boot.ctrl.BladeController; import org.springblade.core.tenant.annotation.TenantDS; @@ -20,9 +25,13 @@ import org.springblade.modules.business.service.IStationHintService; import org.springblade.modules.business.service.impl.PublishService; import org.springblade.modules.business.vo.*; import org.springblade.modules.job.CarInfoTask; +import org.springblade.upload.entity.PocBusLineImg; +import org.springblade.upload.mapper.ProBusLineImgMapper; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.TimeUnit; @@ -50,6 +59,9 @@ public class CommonApiController extends BladeController { private final IStationHintService stationHintService; + @Resource + ProBusLineImgMapper proBusLineImgMapper; + /** * 设置车牌号 * @@ -252,7 +264,7 @@ public class CommonApiController extends BladeController { resultMap.put("articleData", busLine.getActicle()); resultMap.put("activityData", busLine.getActivity()); resultMap.put("scenicData", busLine.getScenic()); - + resultMap.put("scenicData", busLine.getScenic()); // redis缓存控制显示景区图片还是地图图片, 每10s切换一次, 若无景区图片, 显示地图图片 if (Func.isBlank(busLine.getImgScenic())) { resultMap.put("imgData", busLine.getImgMap()); @@ -306,179 +318,155 @@ public class CommonApiController extends BladeController { return R.data(carInfoService.getAllCarInfo()); } + /** - * 获取当前实时的车辆信息 - * - * @return + * 获取实时车辆站点信息 */ - @ApiOperation(value = "获取当前实时的车辆信息", notes = "carNo") - @PostMapping("/get-current-car-info") - public R getCurrentCarInfo(@RequestParam String carNo) { - CarInfoVO carInfo = carInfoService.getCarInfo(carNo); - //获取当前站点 - SiteInfoVO currentStation = getCurrentStation(carInfo.getLinename(),carInfo.getUpordown(), carInfo.getInorder()); - //当前站点 - String currentSnameKey = currentStation.getSname(); - //获取上一站 - SiteInfoVO upStation = getUpStation(carInfo.getLinename(), carInfo.getUpordown(), currentSnameKey); - //获取下一站 - SiteInfoVO downStation = getDownStation(carInfo.getLinename(), carInfo.getUpordown(), currentSnameKey); - //计算当前车辆距离上一站点百分比 + @GetMapping("/get-current-info") + public R getCurrentInfo(@RequestParam String carNo) { + CarInfoVO info = carInfoService.getCarInfo(carNo); + if (Func.isEmpty(info)) { + return R.fail("未获取到车辆信息, 请检查车是否已掉线"); + } + List siteList = SiteListCache.getSiteList(info.getLinename(), info.getUpordown()); + int nextSiteIdx = Integer.parseInt(info.getInorder()); + if (BusinessConstant.CAR_STAT_IN.toString().equals(info.getNowstate())) { + nextSiteIdx -= 1; + } + + // 下一站 + List> snameList = new ArrayList<>(); + SiteInfoVO nextSite = nextSiteIdx < siteList.size() ? siteList.get(nextSiteIdx) : null; + SiteInfoVO nextSite2 = (nextSiteIdx + 1) < siteList.size() ? siteList.get((nextSiteIdx) + 1) : null; + SiteInfoVO nextSite3 = (nextSiteIdx + 2) < siteList.size() ? siteList.get(nextSiteIdx + 2) : null; + // 上一站 + SiteInfoVO preSite = nextSiteIdx - 1 > 0 ? siteList.get(nextSiteIdx - 1) : null; + // 上两站 + SiteInfoVO preTwoSite = nextSiteIdx - 2 > 0 ? siteList.get(nextSiteIdx - 2) : null; + // 上三站 + SiteInfoVO preThreeSite = nextSiteIdx - 3 > 0 ? siteList.get(nextSiteIdx - 3) : null; + + Map siteMsgMap; + if (Func.isNotEmpty(preThreeSite)) { + siteMsgMap = new HashMap<>(); + siteMsgMap.put("siteName", preThreeSite.getSname()); + siteMsgMap.put("type", "before"); + snameList.add(siteMsgMap); + } + if (Func.isNotEmpty(preTwoSite)) { + siteMsgMap = new HashMap<>(); + siteMsgMap.put("siteName", preTwoSite.getSname()); + siteMsgMap.put("type", "before"); + snameList.add(siteMsgMap); + } + if (Func.isNotEmpty(preSite)) { + siteMsgMap = new HashMap<>(); + siteMsgMap.put("siteName", preSite.getSname()); + siteMsgMap.put("type", "before"); + snameList.add(siteMsgMap); + } + if (Func.isNotEmpty(nextSite)) { + siteMsgMap = new HashMap<>(); + siteMsgMap.put("siteName", nextSite.getSname()); + siteMsgMap.put("type", "after"); + snameList.add(siteMsgMap); + } + if (Func.isNotEmpty(nextSite2)) { + siteMsgMap = new HashMap<>(); + siteMsgMap.put("siteName", nextSite2.getSname()); + siteMsgMap.put("type", "after"); + snameList.add(siteMsgMap); + } +// if (Func.isNotEmpty(nextSite3)) { +// snameList.add(nextSite3.getSname()); +// } + + // 计算当前车辆与前一站和后一站的距离 double pass = 0; - if (Func.isNotEmpty(upStation)) { - pass = CommonUtil.calculateDistance(Double.parseDouble(upStation.getJingdu()), Double.parseDouble(upStation.getWeidu()), - Double.parseDouble(carInfo.getJingdu()), Double.parseDouble(carInfo.getWeidu())); + if (Func.isNotEmpty(preSite)) { + pass = CommonUtil.calculateDistance(Double.parseDouble(preSite.getJingdu()), Double.parseDouble(preSite.getWeidu()), + Double.parseDouble(info.getJingdu()), Double.parseDouble(info.getWeidu())); } double unpass = 0; - if (Func.isNotEmpty(downStation)) { - unpass = CommonUtil.calculateDistance(Double.parseDouble(downStation.getJingdu()), Double.parseDouble(downStation.getWeidu()), - Double.parseDouble(carInfo.getJingdu()), Double.parseDouble(carInfo.getWeidu())); + if (Func.isNotEmpty(nextSite)) { + unpass = CommonUtil.calculateDistance(Double.parseDouble(nextSite.getJingdu()), Double.parseDouble(nextSite.getWeidu()), + Double.parseDouble(info.getJingdu()), Double.parseDouble(info.getWeidu())); } double runPercent = (pass + unpass) > 0 ? (pass / (pass + unpass)) * 100 : 0; + DecimalFormat df1 = new DecimalFormat("#"); - BusLine busLine = BusLineCache.getBusLine(carInfo.getUpordown(), downStation.getSname(), downStation.getWeizhi()); + // 先从缓存中获取上一次的运行百分比, 然后将本次的运行百分比更新到缓存中 + String runPercentStr = df1.format(runPercent > 100 ? 100 : runPercent); + String prePercentStr = (String) redisTemplate.opsForValue().get("car-run-percent-info"); + + log.error("____________________________________________"); + log.error("runPercentStr : " + runPercentStr); + log.error("prePercentStr : " + prePercentStr); + log.error("CarInfoVO: " + info.toString()); + + prePercentStr = Func.isBlank(prePercentStr) ? "0" : prePercentStr; + redisTemplate.opsForValue().set("car-run-percent-info", runPercentStr, 30, TimeUnit.SECONDS); + + Map siteMap = new HashMap<>(); + siteMap.put("startSite", siteList.get(0).getSname()); + siteMap.put("endSite", siteList.get(siteList.size() - 1).getSname()); + siteMap.put("siteList", snameList); + siteMap.put("prePercent", prePercentStr); + siteMap.put("runPercent", runPercentStr); + + if (Func.isEmpty(nextSite)) { + nextSite = preSite; + } + BusLine busLine = BusLineCache.getBusLine(info.getUpordown(), nextSite.getSname(), nextSite.getWeizhi()); + // 组织返回数据 Map resultMap = new HashMap<>(); - resultMap.put("catInfo", redisTemplate.opsForValue().get("car-current-Info" + carNo)); + resultMap.put("siteData", siteMap); + resultMap.put("catStat", redisTemplate.opsForValue().get("car-current-stat-info" + carNo)); if (Func.isEmpty(busLine)) { resultMap.put("siteTips", ""); resultMap.put("articleData", ""); resultMap.put("activityData", ""); resultMap.put("scenicData", ""); resultMap.put("imgData", ""); + resultMap.put("speed", ""); } else { resultMap.put("siteTips", busLine.getTips()); resultMap.put("articleData", busLine.getActicle()); resultMap.put("activityData", busLine.getActivity()); resultMap.put("scenicData", busLine.getScenic()); - - // redis缓存控制显示景区图片还是地图图片, 每10s切换一次, 若无景区图片, 显示地图图片 - if (Func.isBlank(busLine.getImgScenic())) { - resultMap.put("imgData", busLine.getImgMap()); - } else { - // 图片类型 1: 地图, 2:景点 - Integer imgType = (Integer) redisTemplate.opsForValue().get("site-img-type"); - if (Func.isEmpty(imgType)) { - imgType = 2; - redisTemplate.opsForValue().set("site-img-type", imgType, 30, TimeUnit.SECONDS); - } - - // 上一次放入地图的时间 - Long prePutImgTime = (Long) redisTemplate.opsForValue().get("site-img-time"); - if (Func.isEmpty(prePutImgTime)) { - prePutImgTime = System.currentTimeMillis(); - redisTemplate.opsForValue().set("site-img-time", prePutImgTime, 30, TimeUnit.DAYS); - } - // 若距离上一次更新图片<10s, 不修改放入的地图类型 - if (System.currentTimeMillis() - prePutImgTime < 10000) { - if (1 == imgType) { - resultMap.put("imgData", busLine.getImgMap()); - } else { - resultMap.put("imgData", busLine.getImgScenic()); - } - } else { - // 否则, 修改返回的图片类型, 并更新缓存 - if (1 == imgType) { - imgType = 2; - resultMap.put("imgData", busLine.getImgScenic()); - } else { - imgType = 1; - resultMap.put("imgData", busLine.getImgMap()); - } - redisTemplate.opsForValue().set("site-img-type", imgType, 30, TimeUnit.SECONDS); - } + resultMap.put("speed",info.getSpeed()); + List imgListR = (List) redisTemplate.opsForValue().get("imgList-info"); + if (CollectionUtils.isEmpty(imgListR)){ + List busLineList = stationHintService.getBusLineByName(nextSite.getSname()); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.select("img_url","img_type"); + queryWrapper.eq("bus_line_id",busLineList.get(0).getId()); + ArrayList imgList = getImgVos(queryWrapper); + redisTemplate.opsForValue().set("imgList-info", imgList); + resultMap.put("imgData", imgList); + //定时任务缓存 + carInfoTask.setCarInfo(resultMap); + return R.data(resultMap); } + resultMap.put("imgData", imgListR); } - //组装返回对象 - CurrentCarInfoVo currentCarInfoVo = new CurrentCarInfoVo(); - currentCarInfoVo.setBusno(carInfo.getBusno()); - currentCarInfoVo.setLinename(carInfo.getLinename()); - currentCarInfoVo.setSpeed(carInfo.getSpeed()); - currentCarInfoVo.setCurrentStation(currentSnameKey); - currentCarInfoVo.setUpStation(upStation.getSname()); - currentCarInfoVo.setDownStation(downStation.getSname()); - currentCarInfoVo.setPercentage(runPercent); - currentCarInfoVo.setInfoMap(resultMap); //定时任务缓存 - carInfoTask.setCarInfo(currentCarInfoVo); - return R.data(currentCarInfoVo); - } - - - /** - * 根据上下行和已经经过了第几个站点计算当前站点 - * - * @param upOrDown 上下行 - * @param inOrder 已经经过了第几个站点 - * @return 当前站点 - */ - private SiteInfoVO getCurrentStation(String linename, String upOrDown, String inOrder) { - // 根据上下行获取对应的站点集合 - List stationList = getStationListByUpOrDown(linename,upOrDown); - // 计算当前站点 - int currentStationIndex = Integer.parseInt(inOrder) - 1; - if (currentStationIndex < 0) { - currentStationIndex = 0; - } else if (currentStationIndex >= stationList.size()) { - currentStationIndex = stationList.size() - 1; - } - return stationList.get(currentStationIndex); - } - - /** - * 当前车辆上一站 - * @Date 2023/5/10 17:02 - * @param linename - * @param upordown - * @param currentSnameKey - * @return - **/ - private SiteInfoVO getUpStation(String linename, String upordown, String currentSnameKey) { - List busLineVoList = getStationListByUpOrDown(linename, upordown); - for (int i = 0; i < busLineVoList.size(); i++) { - if (busLineVoList.get(i).getSname().equals(currentSnameKey)) { - if (i == 0) { - return null; - } else { - return busLineVoList.get(i - 1); - } - } - } - return null; + carInfoTask.setCarInfo(resultMap); + return R.data(resultMap); } - /** - * 当前车辆下一站 - * @Date 2023/5/10 17:02 - * @param linename - * @param upordown - * @param currentSnameKey - * @return - **/ - private SiteInfoVO getDownStation(String linename, String upordown, String currentSnameKey) { - List busLineVoList = getStationListByUpOrDown(linename, upordown); - for (int i = 0; i < busLineVoList.size(); i++) { - if (busLineVoList.get(i).getSname().equals(currentSnameKey)) { - if (i == busLineVoList.size() - 1) { - return null; - } else { - return busLineVoList.get(i + 1); - } - } + @NotNull + private ArrayList getImgVos(QueryWrapper queryWrapper) { + List pocBusLineImgs = proBusLineImgMapper.selectList(queryWrapper); + ArrayList imgList = new ArrayList<>(); + for(PocBusLineImg pocBusLineImg : pocBusLineImgs){ + imgVo img = new imgVo(); + img.setImgUrl(pocBusLineImg.getImgUrl()); + img.setImgType(pocBusLineImg.getImgType()); + img.setBusLineId(pocBusLineImg.getBusLineId()); + imgList.add(img); } - return null; - } - - - /** - * 根据上下行获取对应的站点集合 - * - * @param upOrDown 上下行 - * @return 站点集合 - */ - private List getStationListByUpOrDown(String linename,String upOrDown) { - //根据在那条线路,上下行获取对应的站点集合 - List siteList = SiteListCache.getSiteList(linename,upOrDown); - return siteList; + return imgList; } diff --git a/src/main/java/org/springblade/modules/business/service/IStationHintService.java b/src/main/java/org/springblade/modules/business/service/IStationHintService.java index 3a6c5b2..a23e310 100644 --- a/src/main/java/org/springblade/modules/business/service/IStationHintService.java +++ b/src/main/java/org/springblade/modules/business/service/IStationHintService.java @@ -31,4 +31,7 @@ public interface IStationHintService extends BaseService { boolean delete(String ids); R> QueryBusLineMessage(String linename, String updown); + + //根据name获取返回id + List getBusLineByName(String name); } diff --git a/src/main/java/org/springblade/modules/business/service/impl/StationHintServiceImpl.java b/src/main/java/org/springblade/modules/business/service/impl/StationHintServiceImpl.java index 93cc11e..50cf9da 100644 --- a/src/main/java/org/springblade/modules/business/service/impl/StationHintServiceImpl.java +++ b/src/main/java/org/springblade/modules/business/service/impl/StationHintServiceImpl.java @@ -139,6 +139,15 @@ public class StationHintServiceImpl extends BaseServiceImpl getBusLineByName(String name) { + QueryWrapper busLineQueryWrapper = new QueryWrapper<>(); + busLineQueryWrapper.select("id"); + busLineQueryWrapper.eq("sname_key", name); + List busLines = busLineMapper.selectList(busLineQueryWrapper); + return busLines; + } + @Override @Transactional(rollbackFor = Exception.class) diff --git a/src/main/java/org/springblade/modules/job/CarInfoTask.java b/src/main/java/org/springblade/modules/job/CarInfoTask.java index 85f8d62..0aa9363 100644 --- a/src/main/java/org/springblade/modules/job/CarInfoTask.java +++ b/src/main/java/org/springblade/modules/job/CarInfoTask.java @@ -13,6 +13,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.List; +import java.util.Map; /** * @Author: Chents @@ -35,16 +36,16 @@ public class CarInfoTask { private IStationHintService stationHintService; //@Scheduled(cron = "0/1 * * * * ?") - public void setCarInfo(CurrentCarInfoVo currentCarInfoVo) { - redisTemplate.opsForValue().set("current_car_info", currentCarInfoVo); + public void setCarInfo(Map resultMap) { + redisTemplate.opsForValue().set("current_car_info", resultMap); System.out.println("======存入实时车辆信息到redis成功========="); } //@Scheduled(cron = "0/1 * * * * ?") public void parseCarInfo() { //从redis获取数据 - CurrentCarInfoVo carInfo = (CurrentCarInfoVo) redisTemplate.opsForValue().get("current_car_info"); - if (carInfo == null) { + Map carInfoMap = (Map ) redisTemplate.opsForValue().get("current_car_info"); + if (carInfoMap.isEmpty()) { log.info("======未从redis获取当前车辆实时数据======="); return; } @@ -52,11 +53,11 @@ public class CarInfoTask { WebSocketMessage message = new WebSocketMessage(); message.setTitle("current_car_info"); message.setCode(200); - message.setContent("车辆信息:" + carInfo.toString()); - log.info("解析车辆信息:{}", carInfo.toString()); + message.setContent("车辆信息:" + carInfoMap.toString()); + log.info("解析车辆信息:{}", carInfoMap.toString()); // 推送消息给web页面 websocketService.broadcast(message); - log.info("推送车辆信息:{}", carInfo.toString()); + log.info("推送车辆信息:{}", carInfoMap.toString()); } } diff --git a/src/main/resources/log/logback-dev.xml b/src/main/resources/log/logback-dev.xml index b99139f..4b1623a 100644 --- a/src/main/resources/log/logback-dev.xml +++ b/src/main/resources/log/logback-dev.xml @@ -17,7 +17,7 @@ - INFO + DEBUG ACCEPT DENY @@ -45,11 +45,11 @@ - + - +