You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.3 KiB

3 years ago
package org.springblade.modules.job;
3 years ago
import com.alibaba.fastjson.JSON;
3 years ago
import lombok.extern.slf4j.Slf4j;
3 years ago
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
3 years ago
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;
3 years ago
import org.springblade.modules.websocket.vo.WebSocketMessage;
3 years ago
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;
3 years ago
import org.springframework.web.bind.annotation.RequestParam;
3 years ago
3 years ago
import java.util.HashMap;
import java.util.Map;
3 years ago
/**
* @Author: Chents
* @Create: 2023-05-09
*/
@Component
@Slf4j
public class CarInfoTask {
3 years ago
private static final String WEB = "web";
private static final String CURRENT_CAR_INFO = "current_car_info";
private static final String PUSH_STATUS = "push_status";
3 years ago
@Autowired
private ICarInfoService carInfoService;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private WebsocketService websocketService;
@Autowired
private IStationHintService stationHintService;
//@Scheduled(cron = "0/1 * * * * ?")
3 years ago
public void setCarInfo(Map<String, Object> resultMap) {
redisTemplate.opsForValue().set("current_car_info", resultMap);
3 years ago
System.out.println("======存入实时车辆信息到redis成功=========");
}
3 years ago
@Scheduled(cron = "0/1 * * * * ?")
3 years ago
public void setCarInfo() {
// redisTemplate.opsForValue().set("car-current-number", device);
}
@Scheduled(cron = "0/10 * * * * ?")
3 years ago
public void parseCarInfo() {
3 years ago
//从redis获取数据
3 years ago
Map<String, Object> carInfoMap = (Map<String, Object>) redisTemplate.opsForValue().get("current_car_info");
if (carInfoMap.isEmpty()) {
3 years ago
log.info("======未从redis获取当前车辆实时数据=======");
return;
}
// 解析车辆信息
3 years ago
HashMap<String, Object> map = new HashMap<>();
map.put("msgType", CURRENT_CAR_INFO);
map.put("data", carInfoMap);
String messageJson = JSON.toJSONString(map);
3 years ago
WebSocketMessage message = new WebSocketMessage();
3 years ago
message.setTitle(WEB);
3 years ago
message.setCode(200);
3 years ago
message.setContent(messageJson);
//log.info("解析车辆信息:{}", carInfoMap.toString());
3 years ago
// 推送消息给web页面
websocketService.broadcast(message);
log.info("推送车辆信息:{}", carInfoMap.toString());
3 years ago
}
3 years ago
3 years ago
@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) {
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());
}
3 years ago
}