能跑起来了
parent
d02401bfd5
commit
537a07ae34
|
@ -68,14 +68,4 @@ public class ReqAddBreakDown {
|
|||
@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss",timezone = "GMT+8")
|
||||
private Date updateTime;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -46,7 +46,14 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</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 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -9,10 +9,9 @@ import com.god.common.log.annotation.Log;
|
|||
import com.god.common.log.enums.BusinessType;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 预警控制层
|
||||
|
@ -20,10 +19,10 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||
* @author LouZhiSHuo
|
||||
* @Date 2023/11/27 10:50
|
||||
**/
|
||||
@Controller
|
||||
@RestController
|
||||
public class AlarmController {
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private AlarmService alarmService;
|
||||
|
||||
|
||||
|
@ -51,7 +50,6 @@ public class AlarmController {
|
|||
public Result<String> insertBreakDownLogs(@RequestBody ReqAddBreakDown reqAddBreakDown) {
|
||||
//添加车辆预警记录
|
||||
alarmService.save(BreakDown.insertBreakDown(reqAddBreakDown));
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
|
|
@ -23,8 +23,8 @@ import java.util.*;
|
|||
* @author LouZhiSHuo
|
||||
* @Date 2023/11/27 20:40
|
||||
**/
|
||||
@Service
|
||||
@Log4j2
|
||||
@Service
|
||||
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, BreakDown> implements AlarmService {
|
||||
|
||||
@Resource
|
||||
|
@ -130,4 +130,4 @@ public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, BreakDown> implem
|
|||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ public class FenceAlgorithm {
|
|||
* @param pts
|
||||
* @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();
|
||||
|
||||
|
|
|
@ -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包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
|
||||
* 虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,所以可以用一个静态set保存起来。
|
||||
* 注:底下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);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 9804
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
|
@ -23,6 +22,23 @@ spring:
|
|||
# 共享配置
|
||||
shared-configs:
|
||||
- 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:
|
||||
level:
|
||||
com.god.system.mapper: DEBUG
|
||||
|
||||
|
|
Loading…
Reference in New Issue