对前端访问服务器进行跨站处理,并增加其其它类进行处理,增加redis操作类

This commit is contained in:
wanglei 2025-09-16 16:13:09 +08:00
parent cddf5e66cc
commit 3629f00104
7 changed files with 650 additions and 78 deletions

View File

@ -5,11 +5,13 @@
</component>
<component name="ChangeListManager">
<list default="true" id="050b8051-b1ec-42aa-bac3-c1c189a4697d" name="更改" comment="修改线程继承自定义线程相关类错误问题">
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/config/CorsFilter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/redisUtil.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/controller/InferenceController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/controller/InferenceController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TimeSyncThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TimeSyncThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/CustomThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/CustomThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WebSocketServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WebSocketServer.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -126,7 +128,7 @@
<workItem from="1757664586037" duration="3070000" />
<workItem from="1757820561153" duration="249000" />
<workItem from="1757829080725" duration="24604000" />
<workItem from="1757922989495" duration="5398000" />
<workItem from="1757922989495" duration="18673000" />
</task>
<task id="LOCAL-00001" summary="修改配置文件和去掉打包时test模块">
<option name="closed" value="true" />

View File

@ -0,0 +1,15 @@
package com.simulationservice.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
@Configuration
public class CorsFilter {
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 设置允许跨域的路径
.allowedOrigins("*") //允许跨域的域名可以用*表示允许任何域名使用
.allowCredentials(true) // 是否允许证书
.allowedMethods("*") //允许任何方法postget等
.maxAge(3600); //缓存持续的最大时间
}
}

View File

@ -3,6 +3,7 @@ package com.simulationservice.controller;
import com.simulationservice.common.R;
import com.simulationservice.service.InferenceTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@ -15,6 +16,7 @@ import java.util.Map;
* 想定控制包括想定的加载开始暂停停止加速减速
*/
@RestController
@CrossOrigin
public class InferenceController {
@Autowired

View File

@ -23,6 +23,8 @@ public class InferenceTaskService {
timeSyncThread = new TimeSyncThread();
timeSyncThread.setRoomId(roomId);
timeSyncThread.setScenarioId(scenarioId);
timeSyncThread.setBeginTime("2026-01-01 07:00:00");
timeSyncThread.setContinueTime(3600);
timeSyncThread.start();
return true;
}
@ -47,15 +49,36 @@ public class InferenceTaskService {
timeSyncThread.closeThread();
}
private int speedToTime(String value) {
int time = 1000; //单位毫秒
if (value.equals("1"))
time = 1000;
else if (value.equals("2"))
time /= 2;
else if (value.equals("4"))
time /= 4;
else if (value.equals("8"))
time /= 8;
else if (value.equals("16"))
time /= 16;
else if (value.equals("32"))
time /= 32;
else if (value.equals("64"))
time /= 64;
return time;
}
@Async
public void speedPlay(String value) {
demandThread.setTime(Integer.parseInt(value));
timeSyncThread.setTime(Integer.parseInt(value));
int time = speedToTime(value);
demandThread.setTime(time);
timeSyncThread.setTime(time);
}
@Async
public void slowPlay(String value) {
demandThread.setTime(Integer.parseInt(value));
timeSyncThread.setTime(Integer.parseInt(value));
int time = speedToTime(value);
demandThread.setTime(time);
timeSyncThread.setTime(time);
}
}

View File

@ -5,6 +5,8 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import com.simulationservice.util.CustomThread;
import com.simulationservice.util.RedisUtil;
/**
* 时间同步线程用于处理想定的倒计时和想定时间结束
*/
@ -12,6 +14,7 @@ public class TimeSyncThread extends CustomThread {
private String _beginTime; //想定开始时间
private int _continueTime; //持续时间
private RedisUtil _redis;
private int[] _monArray = new int[]{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private String _resvTime = ""; //剩余时间
@ -29,93 +32,83 @@ public class TimeSyncThread extends CustomThread {
@Override
public void processBuss() {
//系统当前时间倒计时
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
if (_residualHour <= 0 && _residualMin <= 0 && _residualSec <= 0)
{
//倒计时时间结束处理时间结束业务逻辑
return;
}
//String dateString = "2023-04-01 12:00:00";
try {
Date dateT = sdf.parse(_beginTime);
System.out.println(dateT);
if (_residualHour <= 0 && _residualMin <= 0 && _residualSec <= 0)
if (_year % 4 == 0 && (_year % 100 != 0 || _year % 400 == 0))
_monArray[1]++;
_sec++;
if (_sec >= 60)
{
_sec -= 60;
_min++;
if (_min == 60)
{
//倒计时时间结束处理时间结束业务逻辑
return;
}
if (_year % 4 == 0 && (_year % 100 != 0 || _year % 400 == 0))
_monArray[1]++;
_sec++;
if (_sec >= 60)
{
_sec -= 60;
_min++;
if (_min == 60)
_min = 0;
_hour++;
if (_hour == 24)
{
_min = 0;
_hour++;
if (_hour == 24)
_hour = 0;
_day++;
if (_day == 32)
{
_hour = 0;
_day++;
if (_day == 32)
_day = 1;
_month++;
if (_month == 13)
{
_day = 1;
_month++;
if (_month == 13)
{
_month = 1;
_year++;
}
_month = 1;
_year++;
}
}
}
}
}
String month = String.format("%02d", _month);
String day = String.format("%02d", _day);
String hour = String.format("%02d", _hour);
String min = String.format("%02d", _min);
String sec = String.format("%02d", _sec);
String month = String.format("%02d", _month);
String day = String.format("%02d", _day);
String hour = String.format("%02d", _hour);
String min = String.format("%02d", _min);
String sec = String.format("%02d", _sec);
if (_residualSec > 0)
if (_residualSec > 0)
{
_residualSec--;
}
else
{
if (_residualMin > 0)
{
_residualSec--;
_residualSec = 59;
_residualMin--;
}
else
{
if (_residualMin > 0)
if (_residualHour > 0)
{
_residualMin = 59;
_residualSec = 59;
_residualMin--;
}
else
{
if (_residualHour > 0)
{
_residualMin = 59;
_residualSec = 59;
_residualHour--;
}
_residualHour--;
}
}
String residualHour = String.format("%02d", _residualHour);
String residualMin = String.format("%02d", _residualMin);
String residualSec = String.format("%02d", _residualSec);
String strTime = String.valueOf(_year) + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
_resvTime = residualHour + ":" + residualMin + ":" + residualSec;
System.out.println(getRoomId() + " " + strTime + " " + _resvTime);
} catch (ParseException e) {
e.printStackTrace();
}
String residualHour = String.format("%02d", _residualHour);
String residualMin = String.format("%02d", _residualMin);
String residualSec = String.format("%02d", _residualSec);
String strTime = String.valueOf(_year) + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
_resvTime = residualHour + ":" + residualMin + ":" + residualSec;
System.out.println(getRoomId() + " " + strTime + " " + _resvTime);
// 推演的时间键值roomId-runTime
String key = getRoomId() + "-runTime";
//_redis.set(key, strTime);
// 房间剩余时间键值roomId-resvTime
key = getRoomId() + "-resvTime";
//_redis.set(key, _resvTime);
}
public void setBeginTime(String beginTime) {

View File

@ -127,12 +127,13 @@ public class WebSocketServer {
* 发送自定义消息
*/
public static void sendInfo(String message, @PathParam("userId") String userId) throws IOException {
log.info("发送消息到:" + userId + ",报文:" + message);
if (StringUtils.isNotBlank(userId) && webSocketMap.containsKey(userId)) {
webSocketMap.get(userId).sendMessage(message);
} else {
log.error("用户" + userId + ",不在线!");
if (!StringUtils.isNotBlank(userId) || !webSocketMap.containsKey(userId)) {
//log.error("用户" + userId + ",不在线!");
return;
}
log.info("发送消息到:" + userId + ",报文:" + message);
webSocketMap.get(userId).sendMessage(message);
}
public static synchronized int getOnlineCount() {

View File

@ -0,0 +1,536 @@
package com.simulationservice.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 给一个指定的 key 值附加过期时间
*
* @param key
* @param time
* @return
*/
public boolean expire(String key, long time) {
return redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
/**
* 根据key 获取过期时间
*
* @param key
* @return
*/
public long getTime(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 根据key 获取过期时间
*
* @param key
* @return
*/
public boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 移除指定key 的过期时间
*
* @param key
* @return
*/
public boolean persist(String key) {
return redisTemplate.boundValueOps(key).persist();
}
//- - - - - - - - - - - - - - - - - - - - - String类型 - - - - - - - - - - - - - - - - - - - -
/**
* 根据key获取值
*
* @param key
* @return
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 将值放入缓存
*
* @param key
* @param value
* @return true成功 false 失败
*/
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 将值放入缓存并设置时间
*
* @param key
* @param value
* @param time 时间() -1为无期限
* @return true成功 false 失败
*/
public void set(String key, String value, long time) {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
redisTemplate.opsForValue().set(key, value);
}
}
/**
* 批量添加 key (重复的键会覆盖)
*
* @param keyAndValue
*/
public void batchSet(Map<String, String> keyAndValue) {
redisTemplate.opsForValue().multiSet(keyAndValue);
}
/**
* 批量添加 key-value 只有在键不存在时,才添加
* map 中只要有一个key存在,则全部不添加
*
* @param keyAndValue
*/
public void batchSetIfAbsent(Map<String, String> keyAndValue) {
redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);
}
/**
* 对一个 key-value 的值进行加减操作,
* 如果该 key 不存在 将创建一个key 并赋值该 number
* 如果 key 存在, value 不是长整型 ,将报错
*
* @param key
* @param number
*/
public Long increment(String key, long number) {
return redisTemplate.opsForValue().increment(key, number);
}
/**
* 对一个 key-value 的值进行加减操作,
* 如果该 key 不存在 将创建一个key 并赋值该 number
* 如果 key 存在, value 不是 纯数字 ,将报错
*
* @param key
* @param number
*/
public Double increment(String key, double number) {
return redisTemplate.opsForValue().increment(key, number);
}
/**
* 设置字符串值并设置过期时间单位
* 如果该 key 不存在 将创建一个key 并赋值该 number
* 如果 key 存在, value 不是 纯数字 ,将报错
*
* @param key
* @param value
* @Param timeout
*/
public void setWithExpire(String key, String value, long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
//- - - - - - - - - - - - - - - - - - - - - set类型 - - - - - - - - - - - - - - - - - - - -
/**
* 将数据放入set缓存
*
* @param key
* @return
*/
public void sSet(String key, String value) {
redisTemplate.opsForSet().add(key, value);
}
/**
* 获取变量中的值
*
* @param key
* @return
*/
public Set<Object> members(String key) {
return Collections.singleton(redisTemplate.opsForSet().members(key));
}
/**
* 随机获取变量中指定个数的元素
*
* @param key
* @param count
* @return
*/
public void randomMembers(String key, long count) {
redisTemplate.opsForSet().randomMembers(key, count);
}
/**
* 随机获取变量中的元素
*
* @param key
* @return
*/
public Object randomMember(String key) {
return redisTemplate.opsForSet().randomMember(key);
}
/**
* 弹出变量中的元素
*
* @param key
* @return
*/
public Object pop(String key) {
return redisTemplate.opsForSet().pop("setValue");
}
/**
* 获取变量中值的长度
*
* @param key
* @return
*/
public long size(String key) {
return redisTemplate.opsForSet().size(key);
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key
* @param value
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value);
}
/**
* 检查给定的元素是否在变量中
*
* @param key
* @param obj 元素对象
* @return
*/
public boolean isMember(String key, Object obj) {
return redisTemplate.opsForSet().isMember(key, obj);
}
/**
* 转移变量的元素值到目的变量
*
* @param key
* @param value 元素对象
* @param destKey 元素对象
* @return
*/
public boolean move(String key, String value, String destKey) {
return redisTemplate.opsForSet().move(key, value, destKey);
}
/**
* 批量移除set缓存中元素
*
* @param key
* @param values
* @return
*/
public void remove(String key, Object... values) {
redisTemplate.opsForSet().remove(key, values);
}
/**
* 通过给定的key求2个set变量的差值
*
* @param key
* @param destKey
* @return
*/
public Set<Set> difference(String key, String destKey) {
return Collections.singleton(redisTemplate.opsForSet().difference(key, destKey));
}
//- - - - - - - - - - - - - - - - - - - - - hash类型 - - - - - - - - - - - - - - - - - - - -
/**
* 加入缓存
*
* @param key
* @param map
* @return
*/
public void add(String key, Map<String, String> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 获取 key 下的 所有 hashkey value
*
* @param key
* @return
*/
public Map<Object, Object> getHashEntries(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 验证指定 key 有没有指定的 hashkey
*
* @param key
* @param hashKey
* @return
*/
public boolean hashKey(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* 获取指定key的值string
*
* @param key
* @param key2
* @return
*/
public String getMapString(String key, String key2) {
return redisTemplate.opsForHash().get("map1", "key1").toString();
}
/**
* 获取指定的值Int
*
* @param key
* @param key2
* @return
*/
public Integer getMapInt(String key, String key2) {
return (Integer) redisTemplate.opsForHash().get("map1", "key1");
}
/**
* 弹出元素并删除
*
* @param key
* @return
*/
public String popValue(String key) {
return redisTemplate.opsForSet().pop(key).toString();
}
/**
* 删除指定 键值
*
* @param key
* @return 删除成功的 数量
*/
public void delete(String key) {
redisTemplate.opsForHash().delete(key);
}
/**
* 给指定 hash hashkey 做增减操作
*
* @param key
* @param hashKey
* @param number
* @return
*/
public Long increment(String key, String hashKey, long number) {
return redisTemplate.opsForHash().increment(key, hashKey, number);
}
/**
* 给指定 hash hashkey 做增减操作
*
* @param key
* @param hashKey
* @param number
* @return
*/
public Double increment(String key, String hashKey, Double number) {
return redisTemplate.opsForHash().increment(key, hashKey, number);
}
/**
* 获取 key 下的 所有 hashkey 字段
*
* @param key
* @return
*/
public Set<Object> hashKeys(String key) {
return redisTemplate.opsForHash().keys(key);
}
/**
* 获取指定 hash 下面的 键值对 数量
*
* @param key
* @return
*/
public Long hashSize(String key) {
return redisTemplate.opsForHash().size(key);
}
//- - - - - - - - - - - - - - - - - - - - - list类型 - - - - - - - - - - - - - - - - - - - -
/**
* 在变量左边添加元素值
*
* @param key
* @param value
* @return
*/
public void leftPush(String key, Object value) {
redisTemplate.opsForList().leftPush(key, (String) value);
}
/**
* 获取集合指定位置的值
*
* @param key
* @param index
* @return
*/
public Object index(String key, long index) {
return redisTemplate.opsForList().index("list", 1);
}
/**
* 获取指定区间的值
*
* @param key
* @param start
* @param end
* @return
*/
public List<Object> range(String key, long start, long end) {
return Collections.singletonList(redisTemplate.opsForList().range(key, start, end));
}
/**
* 把最后一个参数值放到指定集合的第一个出现中间参数的前面
* 如果中间参数值存在的话
*
* @param key
* @param pivot
* @param value
* @return
*/
public void leftPush(String key, String pivot, String value) {
redisTemplate.opsForList().leftPush(key, pivot, value);
}
/**
* 向左边批量添加参数元素
*
* @param key
* @param values
* @return
*/
public void leftPushAll(String key, String... values) {
// redisTemplate.opsForList().leftPushAll(key,"w","x","y");
redisTemplate.opsForList().leftPushAll(key, values);
}
/**
* 向集合最右边添加元素
*
* @param key
* @param value
* @return
*/
public void leftPushAll(String key, String value) {
redisTemplate.opsForList().rightPush(key, value);
}
/**
* 向左边批量添加参数元素
*
* @param key
* @param values
* @return
*/
public void rightPushAll(String key, String... values) {
//redisTemplate.opsForList().leftPushAll(key,"w","x","y");
redisTemplate.opsForList().rightPushAll(key, values);
}
/**
* 向已存在的集合中添加元素
*
* @param key
* @param value
* @return
*/
public void rightPushIfPresent(String key, Object value) {
redisTemplate.opsForList().rightPushIfPresent(key, (String) value);
}
/**
* 向已存在的集合中添加元素
*
* @param key
* @return
*/
public long listLength(String key) {
return redisTemplate.opsForList().size(key);
}
/**
* 移除集合中的左边第一个元素
*
* @param key
* @return
*/
public void leftPop(String key) {
redisTemplate.opsForList().leftPop(key);
}
/**
* 移除集合中左边的元素在等待的时间里如果超过等待的时间仍没有元素则退出
*
* @param key
* @return
*/
public void leftPop(String key, long timeout, TimeUnit unit) {
redisTemplate.opsForList().leftPop(key, timeout, unit);
}
/**
* 移除集合中右边的元素
*
* @param key
* @return
*/
public void rightPop(String key) {
redisTemplate.opsForList().rightPop(key);
}
/**
* 移除集合中右边的元素在等待的时间里如果超过等待的时间仍没有元素则退出
*
* @param key
* @return
*/
public void rightPop(String key, long timeout, TimeUnit unit) {
redisTemplate.opsForList().rightPop(key, timeout, unit);
}
}