parent
199a6bb99b
commit
c9994b391e
18 changed files with 370 additions and 60 deletions
@ -0,0 +1,75 @@ |
|||||||
|
package org.springblade.modules.business.service.impl; |
||||||
|
|
||||||
|
import org.springblade.core.tool.utils.Func; |
||||||
|
import org.springblade.modules.websocket.service.WebsocketService; |
||||||
|
import org.springblade.modules.websocket.vo.WebSocketMessage; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.data.redis.core.RedisTemplate; |
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author: Chents |
||||||
|
* @Create: 2023-05-10 |
||||||
|
*/ |
||||||
|
@Service |
||||||
|
public class PublishService { |
||||||
|
|
||||||
|
private static final String CURRENT_FACILITY_STAT = "set-current-Facility-stat"; |
||||||
|
|
||||||
|
private static final String CURRENT_FACILITY_RETURN = "return-current-Facility-stat"; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private WebsocketService websocketService; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private RedisTemplate redisTemplate; |
||||||
|
|
||||||
|
/** |
||||||
|
* 推送车辆状态数据到web |
||||||
|
* |
||||||
|
* @Author chents |
||||||
|
* @Date 2023/5/10 11:25 |
||||||
|
* @param carNo |
||||||
|
* @param status |
||||||
|
* @return |
||||||
|
**/ |
||||||
|
public void publishToWeb(String carNo, String status) { |
||||||
|
// 1. 启动线程
|
||||||
|
Thread thread = new Thread(new Runnable() { |
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
int times = 0; |
||||||
|
// 重试策略: 先发三次, 全部失败后, 等待1s再发三次
|
||||||
|
while (times < 1) { |
||||||
|
for (int i=0; i<2; i ++) { |
||||||
|
// 2. 调用websocket方法发送数据
|
||||||
|
String currentFacility = (String) redisTemplate.opsForValue().get(CURRENT_FACILITY_STAT); |
||||||
|
WebSocketMessage message = new WebSocketMessage(); |
||||||
|
message.setTitle(CURRENT_FACILITY_STAT); |
||||||
|
message.setCode(200); |
||||||
|
message.setContent("设备信息:"+currentFacility); |
||||||
|
websocketService.broadcast(message); |
||||||
|
// 3. 查看websocket的返回结果
|
||||||
|
String result = (String) redisTemplate.opsForValue().get(CURRENT_FACILITY_RETURN); |
||||||
|
if (Func.isNotBlank(result)) { |
||||||
|
//缓存数据到redis,设置5s过期时间
|
||||||
|
redisTemplate.opsForValue().set(CURRENT_FACILITY_RETURN, carNo + ":" + status, 5, TimeUnit.SECONDS); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
try { |
||||||
|
Thread.sleep(1000); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
thread.start(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
@ -0,0 +1,29 @@ |
|||||||
|
package org.springblade.modules.business.vo; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
import java.io.Serializable; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author: Chents |
||||||
|
* @Create: 2023-05-10 |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
public class CurrentCarInfoVo implements Serializable { |
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L; |
||||||
|
// 车牌号
|
||||||
|
private String busno; |
||||||
|
// 在哪条线路
|
||||||
|
private String linename; |
||||||
|
// 速度
|
||||||
|
private String speed; |
||||||
|
//当前站点
|
||||||
|
private String currentStation; |
||||||
|
//上一站
|
||||||
|
private String upStation; |
||||||
|
//下一站
|
||||||
|
private String downStation; |
||||||
|
//当前车辆距离上一站点百分比
|
||||||
|
private double percentage; |
||||||
|
} |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
package org.springblade.modules.websocket.vo; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Author: Chents |
||||||
|
* @Create: 2023-05-10 |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
public class WebSocketMessage { |
||||||
|
|
||||||
|
//自定义为唯一表示
|
||||||
|
private String title; |
||||||
|
|
||||||
|
//http状态码
|
||||||
|
private int code; |
||||||
|
|
||||||
|
//返回对象
|
||||||
|
private Object content; |
||||||
|
} |
||||||
Loading…
Reference in new issue