能跑起来了

master
one 2023-11-30 14:41:53 +08:00
parent d02401bfd5
commit 537a07ae34
9 changed files with 271 additions and 22 deletions

View File

@ -68,14 +68,4 @@ public class ReqAddBreakDown {
@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss",timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss",timezone = "GMT+8")
private Date updateTime; private Date updateTime;
} }

View File

@ -46,7 +46,14 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- Swagger UI --> <!-- Swagger UI -->
<dependency> <dependency>
<groupId>io.springfox</groupId> <groupId>io.springfox</groupId>

View File

@ -0,0 +1,20 @@
package com.god.base.server.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* Description:
*
* @Author: sun-cool-boy
* @Date: 2023/11/29
* @info: webSocket
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}

View File

@ -0,0 +1,35 @@
package com.god.base.server.consumer;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Description:
*
* @Author: sun-cool-boy
* @Date: 2023/11/29
* @info:
*/
@Component
@Log4j2
public class RabbitConsumer {
@RabbitListener(queuesToDeclare = {@Queue("OUT_FENCE")})
public void one(String msg){
log.info("监听到消息:{} , 队列名:{}",msg,"OUT_FENCE");
System.out.println(msg);
}
@RabbitListener(queuesToDeclare = {@Queue("IN_FENCE")})
public void two(String msg){
log.info("监听到消息:{} , 队列名:{}",msg,"IN_FENCE");
System.out.println(msg);
}
}

View File

@ -9,10 +9,9 @@ import com.god.common.log.annotation.Log;
import com.god.common.log.enums.BusinessType; import com.god.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestParam;
/** /**
* *
@ -20,10 +19,10 @@ import org.springframework.web.bind.annotation.RequestParam;
* @author LouZhiSHuo * @author LouZhiSHuo
* @Date 2023/11/27 10:50 * @Date 2023/11/27 10:50
**/ **/
@Controller @RestController
public class AlarmController { public class AlarmController {
@Autowired @Resource
private AlarmService alarmService; private AlarmService alarmService;
@ -51,7 +50,6 @@ public class AlarmController {
public Result<String> insertBreakDownLogs(@RequestBody ReqAddBreakDown reqAddBreakDown) { public Result<String> insertBreakDownLogs(@RequestBody ReqAddBreakDown reqAddBreakDown) {
//添加车辆预警记录 //添加车辆预警记录
alarmService.save(BreakDown.insertBreakDown(reqAddBreakDown)); alarmService.save(BreakDown.insertBreakDown(reqAddBreakDown));
return Result.success(); return Result.success();
} }

View File

@ -23,8 +23,8 @@ import java.util.*;
* @author LouZhiSHuo * @author LouZhiSHuo
* @Date 2023/11/27 20:40 * @Date 2023/11/27 20:40
**/ **/
@Service
@Log4j2 @Log4j2
@Service
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, BreakDown> implements AlarmService { public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, BreakDown> implements AlarmService {
@Resource @Resource

View File

@ -21,7 +21,7 @@ public class FenceAlgorithm {
* @param pts * @param pts
* @return * @return
*/ */
public boolean computeFence(Point2D.Double point, List<Point2D.Double> pts){ public static boolean computeFence(Point2D.Double point, List<Point2D.Double> pts){
//围栏顶点坐标数 //围栏顶点坐标数
int fenceSize = pts.size(); int fenceSize = pts.size();

View File

@ -0,0 +1,183 @@
package com.god.base.server.websocket;
import com.alibaba.fastjson.JSON;
import com.mysql.cj.xdevapi.JsonString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* Description:
*
* @Author: sun-cool-boy
* @Date: 2023/11/29
* @info:
*/
@Slf4j
@Component
@ServerEndpoint("/websocket/{vin}")
public class WebSocketServer{
/**
* session
*/
private Session session;
/**
* vin
*/
private String vin;
/**
* concurrent线SetMyWebSocket
* @Componentspringbootwebsocketbeanset
* WebSocketServer
*/
private static final CopyOnWriteArraySet<WebSocketServer> webSocketServers = new CopyOnWriteArraySet<>();
/**
* ConcurrentHashMap vin session
*/
private static final ConcurrentHashMap<String,Session> map = new ConcurrentHashMap<>();
/**
*
* @param session
* @param vin
*/
@OnOpen
public void onOpen(Session session, @PathParam(value = "vin") String vin){
try {
this.session = session;
this.vin = vin;
webSocketServers.add(this);
map.put(vin,session);
log.info("【webSocket消息】 有新的连接,总数为:"+webSocketServers.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(vin , string);
}
}catch (Exception e){
log.info("【webSocket消息】 客户端 {} 连接失败",vin);
e.printStackTrace();
}
}
/**
*
*/
@OnClose
public void onClose(){
webSocketServers.remove(this);
map.remove(this.vin);
log.info("【websocket消息】连接断开 客户端id {} , 剩余客户端连接 {}",this.vin,webSocketServers.size());
}
/**
*
* @param msg
*/
@OnMessage
public void onMessage(String msg){
log.info("【webSocket消息】收到客户端消息:" + msg);
}
/**
*
* @param throwable
*/
@OnError
public void onError(Throwable throwable){
log.error("【webSocket消息】客户端错误 " + throwable.getMessage());
throwable.printStackTrace();
}
/**
* 广
* @param msg
*/
public void sendAllMessage(String msg){
log.info("【webSocket消息】服务端发起了广播消息{}",msg);
webSocketServers.forEach(obj -> {
if (obj.session.isOpen()){
obj.session.getAsyncRemote().sendText(msg);
}
});
}
/**
*
* @param vin
* @param msg
*/
public void sendOneMessage(String vin , String msg){
log.info("【webSocket消息】向客户端 {} 发送消息 , 消息内容 {}",vin,msg);
Session session = map.get(vin);
if (null != session && session.isOpen()){
session.getAsyncRemote().sendText(JSON.toJSONString(msg));
}
}
/**
*
* @param vinArr
* @param msg
*/
public void sendManyMessage(String [] vinArr,String msg){
log.info("【webSocket消息】向客户端 {} 发送消息 , 消息内容 {}", Arrays.toString(vinArr),msg);
for (String vin : vinArr) {
Session session = map.get(vin);
session.getAsyncRemote().sendText(JSON.toJSONString(msg));
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
WebSocketServer that = (WebSocketServer) o;
return Objects.equals(session, that.session) && Objects.equals(vin, that.vin);
}
@Override
public int hashCode() {
return Objects.hash(session, vin);
}
}

View File

@ -1,7 +1,6 @@
# Tomcat # Tomcat
server: server:
port: 9804 port: 9804
# Spring # Spring
spring: spring:
application: application:
@ -23,6 +22,23 @@ spring:
# 共享配置 # 共享配置
shared-configs: shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
rabbitmq:
port: 5672
host: 10.100.1.5
username: guest
password: guest
#这个配置是保证提供者确保消息推送到交换机中,不管成不成功,都会回调
publisher-confirm-type: correlated
#保证交换机能把消息推送到队列中
publisher-returns: true
virtual-host: /
#这个配置是保证消费者会消费消息,手动确认
listener:
simple:
acknowledge-mode: manual
template:
mandatory: true
logging: logging:
level: level:
com.god.system.mapper: DEBUG com.god.system.mapper: DEBUG