|
|
|
|
package org.springblade.modules.job;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
|
|
import org.springblade.core.tool.utils.Func;
|
|
|
|
|
import org.springblade.modules.business.service.ICarInfoService;
|
|
|
|
|
import org.springblade.modules.business.service.IStationHintService;
|
|
|
|
|
import org.springblade.modules.business.vo.CarInfoVO;
|
|
|
|
|
import org.springblade.modules.websocket.service.WebsocketService;
|
|
|
|
|
import org.springblade.modules.websocket.vo.WebSocketMessage;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Author: Chents
|
|
|
|
|
* @Create: 2023-05-09
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class CarInfoTask {
|
|
|
|
|
|
|
|
|
|
private static final String WEB = "web";
|
|
|
|
|
private static final String CURRENT_CAR_INFO = "current_car_info";
|
|
|
|
|
private static final String PUSH_STATUS = "push_status";
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ICarInfoService carInfoService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private WebsocketService websocketService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IStationHintService stationHintService;
|
|
|
|
|
|
|
|
|
|
//@Scheduled(cron = "0/1 * * * * ?")
|
|
|
|
|
public void setCarInfo(Map<String, Object> resultMap) {
|
|
|
|
|
redisTemplate.opsForValue().set("current_car_info", resultMap);
|
|
|
|
|
System.out.println("======存入实时车辆信息到redis成功=========");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Scheduled(cron = "0/10 * * * * ?")
|
|
|
|
|
public void parseCarInfo() {
|
|
|
|
|
//从redis获取数据
|
|
|
|
|
Map<String, Object> carInfoMap = (Map<String, Object>) redisTemplate.opsForValue().get("current_car_info");
|
|
|
|
|
if (carInfoMap.isEmpty()) {
|
|
|
|
|
log.info("======未从redis获取当前车辆实时数据=======");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 解析车辆信息
|
|
|
|
|
HashMap<String, Object> map = new HashMap<>();
|
|
|
|
|
map.put("msgType", CURRENT_CAR_INFO);
|
|
|
|
|
map.put("data", carInfoMap);
|
|
|
|
|
String messageJson = JSON.toJSONString(map);
|
|
|
|
|
|
|
|
|
|
WebSocketMessage message = new WebSocketMessage();
|
|
|
|
|
message.setTitle(WEB);
|
|
|
|
|
message.setCode(200);
|
|
|
|
|
message.setContent(messageJson);
|
|
|
|
|
//log.info("解析车辆信息:{}", carInfoMap.toString());
|
|
|
|
|
// 推送消息给web页面
|
|
|
|
|
websocketService.broadcast(message);
|
|
|
|
|
log.info("推送车辆信息:{}", carInfoMap.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Scheduled(cron = "0/1 * * * * ?")
|
|
|
|
|
public void parsePushStatus() {
|
|
|
|
|
//从redis获取数据
|
|
|
|
|
String carNo = (String) redisTemplate.opsForValue().get("car-current-number");
|
|
|
|
|
String pushStatus = (String) redisTemplate.opsForValue().get("set-current-Facility-stat" + carNo);
|
|
|
|
|
Map<String, String> resultData = new HashMap<>();
|
|
|
|
|
resultData.put("carNo", carNo);
|
|
|
|
|
resultData.put("pushStatus", pushStatus);
|
|
|
|
|
|
|
|
|
|
// 解析车辆信息
|
|
|
|
|
HashMap<String, Object> map = new HashMap<>();
|
|
|
|
|
map.put("msgType", PUSH_STATUS);
|
|
|
|
|
map.put("data", resultData);
|
|
|
|
|
String messageJson = JSON.toJSONString(map);
|
|
|
|
|
|
|
|
|
|
WebSocketMessage message = new WebSocketMessage();
|
|
|
|
|
message.setTitle(WEB);
|
|
|
|
|
message.setCode(200);
|
|
|
|
|
message.setContent(messageJson);
|
|
|
|
|
//log.info("解析车辆信息:{}", carInfoMap.toString());
|
|
|
|
|
// 推送消息给web页面
|
|
|
|
|
websocketService.broadcast(message);
|
|
|
|
|
log.info("推送车辆信息:{}", map.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Scheduled(cron = "0/1 * * * * ?")
|
|
|
|
|
public void getCarSpeed() {
|
|
|
|
|
String carNo = (String) redisTemplate.opsForValue().get("car-current-number");
|
|
|
|
|
CarInfoVO info = carInfoService.getCarInfo(carNo);
|
|
|
|
|
if (Func.isEmpty(info)) {
|
|
|
|
|
throw new RuntimeException("未获取到车辆信息, 请检查车是否已掉线");
|
|
|
|
|
}
|
|
|
|
|
String speedJson = JSON.toJSONString(info.getSpeed());
|
|
|
|
|
WebSocketMessage message = new WebSocketMessage();
|
|
|
|
|
message.setTitle(WEB);
|
|
|
|
|
message.setCode(200);
|
|
|
|
|
message.setContent(speedJson);
|
|
|
|
|
websocketService.broadcast(message);
|
|
|
|
|
log.info("车辆实时车速:{}", message.getContent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|