Merge branch 'master' of https://gitea.qinmian.online/ToBeNumberOne/god-car-base
commit
b658eb7362
|
@ -40,4 +40,10 @@ public class FenceQueryRequest extends PageQuery {
|
|||
*/
|
||||
private Integer driveStatus;
|
||||
|
||||
|
||||
/**
|
||||
* 车辆VIN
|
||||
*/
|
||||
private String carVinId;
|
||||
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@ import org.springframework.web.bind.annotation.*;
|
|||
public interface RemoteCarService {
|
||||
|
||||
|
||||
@GetMapping("/car/list/{vinId}")
|
||||
public Result<Car> list(@PathVariable String vinId);
|
||||
@GetMapping("/car/list")
|
||||
public Result<Car> list(@RequestParam("carVinId") String carVinId);
|
||||
|
||||
@PostMapping("/baseFence/fenceById")
|
||||
public Result<Fence> fenceById(@RequestParam("fenceId") Integer fenceId);
|
||||
|
|
|
@ -26,7 +26,7 @@ public class RemoteCarFallbackFactory implements FallbackFactory<RemoteCarServic
|
|||
|
||||
return new RemoteCarService() {
|
||||
@Override
|
||||
public Result<Car> list(String vinId) {
|
||||
public Result<Car> list(String carVinId) {
|
||||
return Result.error("保存操作日志失败:" + cause.getMessage());
|
||||
}
|
||||
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
com.god.base.remote.factory.RemoteCarFallbackFactory
|
||||
com.god.base.remote.factory.RemoteFenceFallbackFactory
|
||||
|
|
|
@ -29,11 +29,7 @@
|
|||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- webSocket -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
|
@ -78,11 +74,6 @@
|
|||
<version>3.4.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.4.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
|
|
@ -46,11 +46,10 @@ public class CarController extends BaseController {
|
|||
/**
|
||||
* 根据vin查询车辆信息管理列表
|
||||
*/
|
||||
@RequiresPermissions
|
||||
@GetMapping("/list/{vinId}")
|
||||
public Result<Car> list(@PathVariable String vinId){
|
||||
@GetMapping("/list")
|
||||
public Result<Car> list(@RequestParam("carVinId") String carVinId){
|
||||
|
||||
Car list = carService.selectCarInfoList(vinId);
|
||||
Car list = carService.selectCarInfoList(carVinId);
|
||||
|
||||
return Result.success(list);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.god.base.server.mapper;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.god.base.domain.Car;
|
||||
import com.god.base.domain.request.CarRequest;
|
||||
|
|
|
@ -84,7 +84,7 @@ public class FenceServiceImpl extends ServiceImpl<FenceMapper , Fence> implement
|
|||
// 将围栏坐标存储到redis中
|
||||
// key 围栏编号
|
||||
// value 围栏坐标信息
|
||||
redisService.setCacheObject(entity.getFenceId()+"",fenceLocation);
|
||||
// redisService.setCacheObject(entity.getFenceId()+"",fenceLocation);
|
||||
boolean update = super.updateById(entity);
|
||||
if (!update){
|
||||
log.warn("编辑围栏[/-{}-/]未成功! 请求参数:{}",
|
||||
|
@ -135,6 +135,10 @@ public class FenceServiceImpl extends ServiceImpl<FenceMapper , Fence> implement
|
|||
//精确查询
|
||||
lambdaQueryWrapper.eq(Fence::getDriveStatus,fenceQueryRequest.getDriveStatus());
|
||||
}
|
||||
//判断车辆VIN是否为空
|
||||
if (StringUtils.isNotEmpty(fenceQueryRequest.getCarVinId())) {
|
||||
lambdaQueryWrapper.eq(Fence::getCarVinId , fenceQueryRequest.getCarVinId());
|
||||
}
|
||||
//分页
|
||||
Page<Fence> fencePage = fenceQueryRequest.buildPage();
|
||||
log.info("分页查询:[ {} ]", JSONObject.toJSONString(fencePage));
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
package com.god.base.server.utils;
|
||||
|
||||
import com.god.base.domain.Car;
|
||||
import com.god.base.domain.Fence;
|
||||
import com.god.base.domain.request.FenceQueryRequest;
|
||||
import com.god.base.server.service.CarService;
|
||||
import com.god.base.server.service.FenceService;
|
||||
import com.god.common.core.domain.Result;
|
||||
import com.god.common.core.web.page.TableDataInfo;
|
||||
import com.god.common.redis.service.RedisService;
|
||||
import com.god.common.security.utils.SecurityUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 监控车辆围栏实时数据
|
||||
*
|
||||
* @author LouZhiSHuo
|
||||
* @Date 2023/11/29 21:31
|
||||
**/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class CarFenceMonitor {
|
||||
|
||||
|
||||
@Autowired
|
||||
private CarService carService;
|
||||
@Autowired
|
||||
private FenceService fenceService;
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
@Bean
|
||||
public void getCarFencesList(){
|
||||
log.info("开始获取车辆围栏信息");
|
||||
|
||||
//获取车辆信息
|
||||
Result<List<Car>> listResult = carService.carList(String.valueOf(SecurityUtils.getUserId()));
|
||||
List<Car> dataCar = listResult.getData();
|
||||
|
||||
if (dataCar.size() <= 0) {
|
||||
throw new RuntimeException("查无管理车辆");
|
||||
}
|
||||
|
||||
for (Car car : dataCar) {
|
||||
//单线程获取车辆围栏信息
|
||||
new Thread(() -> {
|
||||
FenceQueryRequest fenceQueryRequest = new FenceQueryRequest();
|
||||
|
||||
fenceQueryRequest.setPage(1);
|
||||
fenceQueryRequest.setPageSize(100);
|
||||
fenceQueryRequest.setCarVinId(car.getCarVinId());
|
||||
//获取车辆围栏信息
|
||||
TableDataInfo<Fence> fenceTableDataInfo = fenceService.fenceListAndPage(fenceQueryRequest);
|
||||
List<Fence> rows = fenceTableDataInfo.getRows();
|
||||
|
||||
if (rows.size() <= 0) {
|
||||
throw new RuntimeException("该车辆未绑定围栏");
|
||||
}
|
||||
|
||||
//围栏对象集合
|
||||
List<Fence> locationList = new ArrayList<>();
|
||||
|
||||
for (Fence row : rows) {
|
||||
locationList.add(row);
|
||||
}
|
||||
|
||||
//存储redis
|
||||
redisService.setCacheList(car.getCarVinId()+"fence",locationList);
|
||||
|
||||
}).start();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package com.god.base.server.webSocket.config;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* WebSocketConfig
|
||||
* 开始webSocket
|
||||
* WenHao.Sao
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter(){
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
//package com.god.base.server.webSocket.controller;
|
||||
//
|
||||
//import com.god.base.server.webSocket.handler.SocketBatchHandler;
|
||||
//import lombok.extern.log4j.Log4j2;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import javax.websocket.OnOpen;
|
||||
//import javax.websocket.Session;
|
||||
//import javax.websocket.server.ServerEndpoint;
|
||||
//
|
||||
///**
|
||||
// * @ClassName WebSocketBatchSever
|
||||
// * @Author WenHao.Sao
|
||||
// */
|
||||
//@ServerEndpoint("/car/batch")
|
||||
//@Component
|
||||
//@Log4j2
|
||||
//public class WebSocketBatchSever {
|
||||
//
|
||||
// /**
|
||||
// * 连接建立成功方法
|
||||
// */
|
||||
// @OnOpen
|
||||
// public void onOpen(Session session){
|
||||
// log.info("车辆大屏新连接" + session.getId());
|
||||
// SocketBatchHandler.sessions.add(session);
|
||||
// }
|
||||
//
|
||||
//}
|
|
@ -1,156 +0,0 @@
|
|||
package com.god.base.server.webSocket.controller;
|
||||
|
||||
import java.net.http.WebSocket;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@ServerEndpoint("/websocket/{vin}") // 接口路径 ws://localhost:8087/webSocket/userId;
|
||||
public class WebSocketServer {
|
||||
|
||||
//与某个客户端的连接会话,需要通过它来给客户端发送数据
|
||||
private Session session;
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private String userId;
|
||||
|
||||
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
|
||||
//虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。
|
||||
// 注:底下WebSocket是当前类名
|
||||
private static CopyOnWriteArraySet<WebSocketServer> webSockets =new CopyOnWriteArraySet<>();
|
||||
// 用来存在线连接用户信息
|
||||
private static ConcurrentHashMap<String,Session> sessionPool = new ConcurrentHashMap<String,Session>();
|
||||
|
||||
/**
|
||||
* 链接成功调用的方法
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session, @PathParam(value="vin")String userId) {
|
||||
try {
|
||||
this.session = session;
|
||||
this.userId = userId;
|
||||
webSockets.add(this);
|
||||
sessionPool.put(userId, session);
|
||||
log.info("【websocket消息】有新的连接,总数为:"+webSockets.size());
|
||||
//发送实时轨迹数据
|
||||
double[][] arr = new double[][]{
|
||||
{116.478935,39.997761},
|
||||
{116.478939,39.997825},
|
||||
{116.478912,39.998549},
|
||||
{116.478998,39.998555},
|
||||
{116.479282,39.99856},
|
||||
{116.479658,39.998528},
|
||||
{116.480151,39.998453},
|
||||
{116.480784,39.998302},
|
||||
{116.481149,39.998184},
|
||||
{116.481573,39.997997},
|
||||
{116.481863,39.997846},
|
||||
{116.482072,39.997718},
|
||||
{116.482362,39.997718},
|
||||
{116.483633,39.998935},
|
||||
{116.48367,39.998968},
|
||||
{116.484648,39.999861}
|
||||
};
|
||||
|
||||
for (double[] doubles : arr) {
|
||||
Thread.sleep(500);
|
||||
String string = Arrays.toString(doubles);
|
||||
System.out.println(string);
|
||||
Thread.sleep(1000);
|
||||
sendOneMessage(userId , string);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 链接关闭调用的方法
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose() {
|
||||
try {
|
||||
webSockets.remove(this);
|
||||
sessionPool.remove(this.userId);
|
||||
log.info("【websocket消息】连接断开,总数为:"+webSockets.size());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 收到客户端消息后调用的方法
|
||||
*
|
||||
* @param message
|
||||
* @param session
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(String message,Session session) {
|
||||
log.info("【websocket消息】收到客户端消息:"+message);
|
||||
}
|
||||
|
||||
/** 发送错误时的处理
|
||||
* @param session
|
||||
* @param error
|
||||
*/
|
||||
@OnError
|
||||
public void onError(Session session, Throwable error) {
|
||||
|
||||
log.error("用户错误,原因:"+error.getMessage());
|
||||
error.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
// 此为广播消息
|
||||
public void sendAllMessage(String message) {
|
||||
log.info("【websocket消息】广播消息:"+message);
|
||||
for(WebSocketServer webSocket : webSockets) {
|
||||
try {
|
||||
if(webSocket.session.isOpen()) {
|
||||
webSocket.session.getAsyncRemote().sendText(JSON.toJSONString(message));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 此为单点消息
|
||||
public void sendOneMessage(String userId, String message) {
|
||||
Session session = sessionPool.get(userId);
|
||||
if (session != null&&session.isOpen()) {
|
||||
try {
|
||||
log.info("【websocket消息】 单点消息:"+message);
|
||||
session.getAsyncRemote().sendText(message);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 此为单点消息(多人)
|
||||
public void sendMoreMessage(String[] userIds, String message) {
|
||||
for(String userId:userIds) {
|
||||
Session session = sessionPool.get(userId);
|
||||
if (session != null&&session.isOpen()) {
|
||||
try {
|
||||
log.info("【websocket消息】 单点消息:"+message);
|
||||
session.getAsyncRemote().sendText(message);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.god.base.server.webSocket.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.websocket.Session;
|
||||
|
||||
/**
|
||||
* @ClassName SocketData
|
||||
* @Author WenHao.Sao
|
||||
*/
|
||||
@Data
|
||||
public class SocketData {
|
||||
/**
|
||||
* vin
|
||||
*/
|
||||
private String vin;
|
||||
/**
|
||||
*连接对象
|
||||
*/
|
||||
private Session session;
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
package com.god.base.server.webSocket.entity;
|
||||
|
||||
import com.god.base.domain.VehicleMessage;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ClassName VehicleAllData
|
||||
* @Author WenHao.Sao
|
||||
*/
|
||||
@Data
|
||||
public class VehicleAllData {
|
||||
private List<VehicleMessage> messages;
|
||||
|
||||
private String info;
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
//package com.god.base.server.webSocket.handler;
|
||||
//
|
||||
//import com.alibaba.fastjson.JSON;
|
||||
//import com.god.base.domain.VehicleMessage;
|
||||
//import com.god.base.common.constant.RedisConstant;
|
||||
//import com.god.base.server.mapper.CarMapper;
|
||||
//import com.god.base.server.webSocket.entity.VehicleAllData;
|
||||
//import lombok.extern.log4j.Log4j2;
|
||||
//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 javax.websocket.Session;
|
||||
//import java.util.List;
|
||||
//import java.util.concurrent.CopyOnWriteArrayList;
|
||||
//import java.util.stream.Collectors;
|
||||
//
|
||||
///**
|
||||
// * @author swh的mac
|
||||
// * @Date 2023/11/20 14:37
|
||||
// */
|
||||
//@Log4j2
|
||||
//@Component
|
||||
//public class SocketBatchHandler {
|
||||
// public static CopyOnWriteArrayList<Session> sessions = new CopyOnWriteArrayList<>();
|
||||
//
|
||||
// @Autowired
|
||||
// private CarMapper carMapper;
|
||||
//
|
||||
// @Autowired
|
||||
// private RedisTemplate redisTemplate;
|
||||
//
|
||||
// @Scheduled(cron = "0/1 * * * * ?")
|
||||
// public void sendMessageFlush(){
|
||||
// for (Session session : sessions) {
|
||||
// List<String> ids = carMapper.getOnlineCarIds();
|
||||
// List<VehicleMessage> messages = ids.stream().map(id -> {
|
||||
// VehicleMessage message = (VehicleMessage) redisTemplate.opsForValue().get(RedisConstant.CURRENT_INFO + ":" + id);
|
||||
// return message;
|
||||
// }).collect(Collectors.toList());
|
||||
// Integer day = (Integer) redisTemplate.opsForValue().get(RedisConstant.VEHICLE_INFO_TOKEN+RedisConstant.CURRENT_DAY_FAULT);
|
||||
// Integer month = (Integer) redisTemplate.opsForValue().get(RedisConstant.VEHICLE_INFO_TOKEN+RedisConstant.CURRENT_MONTH_FAULT);
|
||||
// Integer onlineCount = carMapper.getOnlineCarCount();
|
||||
// Integer unOnlineCount = carMapper.getUnOnlineCount();
|
||||
// VehicleAllData vehicleAllData = new VehicleAllData();
|
||||
// vehicleAllData.setMessages(messages);
|
||||
// vehicleAllData.setInfo(day+":"+month+":"+onlineCount+":"+unOnlineCount);
|
||||
// try {
|
||||
// session.getBasicRemote().sendText(JSON.toJSONString(vehicleAllData));
|
||||
// } catch (Exception e) {
|
||||
// log.info("{}:连接中断",session.getId());
|
||||
// sessions.remove(session);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
|
@ -1,42 +0,0 @@
|
|||
//package com.god.base.server.webSocket.handler;
|
||||
//
|
||||
//import com.alibaba.fastjson.JSON;
|
||||
//import com.god.base.domain.VehicleMessage;
|
||||
//import com.god.base.common.constant.RedisConstant;
|
||||
//import com.god.base.server.webSocket.entity.SocketData;
|
||||
//import lombok.extern.log4j.Log4j2;
|
||||
//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 javax.websocket.Session;
|
||||
//import java.util.concurrent.CopyOnWriteArrayList;
|
||||
//
|
||||
///**
|
||||
// * @author swh的mac
|
||||
// * @Date 2023/11/20 15:51
|
||||
// */
|
||||
//@Log4j2
|
||||
//@Component
|
||||
//public class SocketHandler {
|
||||
// //存放每个客户端对应的MyWebSocket
|
||||
// public static CopyOnWriteArrayList<SocketData> socketList = new CopyOnWriteArrayList<>();
|
||||
//
|
||||
// @Autowired
|
||||
// private RedisTemplate redisTemplate;
|
||||
//
|
||||
// @Scheduled(cron = "0/1 * * * * ?")
|
||||
// public void sendMessageFlush(){
|
||||
// for (SocketData socketData : socketList) {
|
||||
// Session session = socketData.getSession();
|
||||
// VehicleMessage msg = (VehicleMessage) redisTemplate.opsForValue().get(RedisConstant.CURRENT_INFO + ":" + socketData.getVin());
|
||||
// try {
|
||||
// session.getBasicRemote().sendText(JSON.toJSONString(msg));
|
||||
// } catch (Exception e) {
|
||||
// log.info("{}:连接中断",socketData.getSession().getId());
|
||||
// socketList.remove(socketData);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
Loading…
Reference in New Issue