[*] 日常优化
This commit is contained in:
@@ -68,7 +68,12 @@ public interface Client extends AutoCloseable {
|
||||
/**
|
||||
* @return 授权是否超时
|
||||
*/
|
||||
boolean timeout();
|
||||
boolean authorizeTimeout();
|
||||
|
||||
/**
|
||||
* @return 心跳是否超时
|
||||
*/
|
||||
boolean heartbeatTimeout();
|
||||
|
||||
/**
|
||||
* 设置授权
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.acgist.taoyao.signal.client;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -140,10 +142,16 @@ public abstract class ClientAdapter<T extends AutoCloseable> implements Client {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean timeout() {
|
||||
public boolean authorizeTimeout() {
|
||||
return System.currentTimeMillis() - this.time > this.timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean heartbeatTimeout() {
|
||||
final LocalDateTime last = this.status.getLastHeartbeat();
|
||||
return Duration.between(last, LocalDateTime.now()).getSeconds() >= this.timeout * 60;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorize(String clientId) {
|
||||
this.clientId = clientId;
|
||||
|
||||
@@ -220,8 +220,13 @@ public class ClientManager {
|
||||
private void closeTimeout() {
|
||||
final int oldSize = this.clients.size();
|
||||
this.clients.stream()
|
||||
.filter(v -> v.unauthorized())
|
||||
.filter(v -> v.timeout())
|
||||
.filter(v -> {
|
||||
if(v.unauthorized()) {
|
||||
return v.authorizeTimeout();
|
||||
} else {
|
||||
return v.heartbeatTimeout();
|
||||
}
|
||||
})
|
||||
.forEach(v -> {
|
||||
log.debug("关闭超时终端:{}", v);
|
||||
this.close(v);
|
||||
|
||||
@@ -54,7 +54,7 @@ public class ClientStatus {
|
||||
@Schema(title = "终端配置", description = "其他扩展终端配置")
|
||||
private Map<String, Object> config = new HashMap<>();
|
||||
@Schema(title = "最后心跳时间", description = "最后心跳时间")
|
||||
private LocalDateTime lastHeartbeat;
|
||||
private LocalDateTime lastHeartbeat = LocalDateTime.now();
|
||||
|
||||
/**
|
||||
* 拷贝属性
|
||||
@@ -62,18 +62,18 @@ public class ClientStatus {
|
||||
* @param body 消息主体
|
||||
*/
|
||||
public void copy(Map<String, Object> body) {
|
||||
this.setLatitude(MapUtils.get(body, Constant.LATITUDE, this.latitude));
|
||||
this.setLongitude(MapUtils.get(body, Constant.LONGITUDE, this.longitude));
|
||||
this.setHumidity(MapUtils.get(body, Constant.HUMIDITY, this.humidity));
|
||||
this.setTemperature(MapUtils.get(body, Constant.TEMPERATURE, this.temperature));
|
||||
this.setSignal(MapUtils.get(body, Constant.SIGNAL, this.signal));
|
||||
this.setBattery(MapUtils.get(body, Constant.BATTERY, this.battery));
|
||||
this.setAlarming(MapUtils.get(body, Constant.ALARMING, this.alarming));
|
||||
this.setCharging(MapUtils.get(body, Constant.CHARGING, this.charging));
|
||||
this.setLatitude(MapUtils.get(body, Constant.LATITUDE, this.latitude));
|
||||
this.setLongitude(MapUtils.get(body, Constant.LONGITUDE, this.longitude));
|
||||
this.setHumidity(MapUtils.get(body, Constant.HUMIDITY, this.humidity));
|
||||
this.setTemperature(MapUtils.get(body, Constant.TEMPERATURE, this.temperature));
|
||||
this.setSignal(MapUtils.get(body, Constant.SIGNAL, this.signal));
|
||||
this.setBattery(MapUtils.get(body, Constant.BATTERY, this.battery));
|
||||
this.setAlarming(MapUtils.get(body, Constant.ALARMING, this.alarming));
|
||||
this.setCharging(MapUtils.get(body, Constant.CHARGING, this.charging));
|
||||
this.setClientRecording(MapUtils.get(body, Constant.CLIENT_RECORDING, this.clientRecording));
|
||||
// this.setServerRecording(MapUtils.get(body, Constant.SERVER_RECORDING, this.serverRecording));
|
||||
this.status(MapUtils.get(body, Constant.STATUS, this.status));
|
||||
this.config(MapUtils.get(body, Constant.CONFIG, this.config));
|
||||
this.status(MapUtils.get(body, Constant.STATUS, this.status));
|
||||
this.config(MapUtils.get(body, Constant.CONFIG, this.config));
|
||||
this.setLastHeartbeat(LocalDateTime.now());
|
||||
}
|
||||
|
||||
|
||||
@@ -19,35 +19,36 @@ import com.acgist.taoyao.signal.protocol.ProtocolClientAdapter;
|
||||
@Description(
|
||||
body = """
|
||||
{
|
||||
"latitude": 纬度,
|
||||
"longitude": 经度,
|
||||
"humidity": 湿度,
|
||||
"temperature": 温度,
|
||||
"signal": 信号强度(0~100),
|
||||
"battery": 电池电量(0~100),
|
||||
"alarming": 是否发生告警(true|false),
|
||||
"charging": 是否正在充电(true|false),
|
||||
"recording": 是否正在录像(true|false),
|
||||
"lastHeartbeat": "最后心跳时间",
|
||||
"status": {更多状态},
|
||||
"config": {更多配置}
|
||||
"latitude" : 纬度,
|
||||
"longitude" : 经度,
|
||||
"humidity" : 湿度,
|
||||
"temperature" : 温度,
|
||||
"signal" : 信号强度(0~100),
|
||||
"battery" : 电池电量(0~100),
|
||||
"alarming" : 是否发生告警(true|false),
|
||||
"charging" : 是否正在充电(true|false),
|
||||
"clientRecording": 是否正在录像(true|false),
|
||||
"serverRecording": 是否正在录像(true|false),
|
||||
"lastHeartbeat" : "最后心跳时间",
|
||||
"status" : {更多状态},
|
||||
"config" : {更多配置}
|
||||
}
|
||||
""",
|
||||
flow = "终端->信令服务->终端"
|
||||
)
|
||||
public class ClientHeartbeatProtocol extends ProtocolClientAdapter {
|
||||
|
||||
public static final String SIGNAL = "client::heartbeat";
|
||||
|
||||
public ClientHeartbeatProtocol() {
|
||||
super("终端心跳信令", SIGNAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String clientId, ClientType clientType, Client client, Message message, Map<String, Object> body) {
|
||||
client.push(message.cloneWithoutBody());
|
||||
final ClientStatus status = client.getStatus();
|
||||
status.copy(body);
|
||||
}
|
||||
|
||||
public static final String SIGNAL = "client::heartbeat";
|
||||
|
||||
public ClientHeartbeatProtocol() {
|
||||
super("终端心跳信令", SIGNAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String clientId, ClientType clientType, Client client, Message message, Map<String, Object> body) {
|
||||
client.push(message.cloneWithoutBody());
|
||||
final ClientStatus status = client.getStatus();
|
||||
status.copy(body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,46 +30,47 @@ import com.acgist.taoyao.signal.protocol.ProtocolClientAdapter;
|
||||
"""
|
||||
[
|
||||
{
|
||||
"ip": "终端IP",
|
||||
"name": "终端名称",
|
||||
"clientId": "终端ID",
|
||||
"clientType": "终端类型",
|
||||
"latitude": 纬度,
|
||||
"longitude": 经度,
|
||||
"humidity": 湿度,
|
||||
"temperature": 温度,
|
||||
"signal": 信号强度(0~100),
|
||||
"battery": 电池电量(0~100),
|
||||
"alarming": 是否发生告警(true|false),
|
||||
"charging": 是否正在充电(true|false),
|
||||
"recording": 是否正在录像(true|false),
|
||||
"lastHeartbeat": "最后心跳时间",
|
||||
"status": {更多状态},
|
||||
"config": {更多配置}
|
||||
"ip" : "终端IP",
|
||||
"name" : "终端名称",
|
||||
"clientId" : "终端ID",
|
||||
"clientType" : "终端类型",
|
||||
"latitude" : 纬度,
|
||||
"longitude" : 经度,
|
||||
"humidity" : 湿度,
|
||||
"temperature" : 温度,
|
||||
"signal" : 信号强度(0~100),
|
||||
"battery" : 电池电量(0~100),
|
||||
"alarming" : 是否发生告警(true|false),
|
||||
"charging" : 是否正在充电(true|false),
|
||||
"clientRecording": 是否正在录像(true|false),
|
||||
"serverRecording": 是否正在录像(true|false),
|
||||
"lastHeartbeat" : "最后心跳时间",
|
||||
"status" : {更多状态},
|
||||
"config" : {更多配置}
|
||||
},
|
||||
...
|
||||
]
|
||||
"""
|
||||
},
|
||||
flow = "终端->信令服务->终端"
|
||||
flow = "终端=>信令服务->终端"
|
||||
)
|
||||
public class ClientListProtocol extends ProtocolClientAdapter {
|
||||
|
||||
public static final String SIGNAL = "client::list";
|
||||
|
||||
public ClientListProtocol() {
|
||||
super("终端列表信令", SIGNAL);
|
||||
}
|
||||
public static final String SIGNAL = "client::list";
|
||||
|
||||
public ClientListProtocol() {
|
||||
super("终端列表信令", SIGNAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String clientId, ClientType clientType, Client client, Message message, Map<String, Object> body) {
|
||||
final String queryClientType = MapUtils.get(body, Constant.CLIENT_TYPE);
|
||||
if(StringUtils.isEmpty(queryClientType)) {
|
||||
message.setBody(this.clientManager.getStatus());
|
||||
} else {
|
||||
message.setBody(this.clientManager.getStatus(ClientType.of(queryClientType)));
|
||||
}
|
||||
client.push(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String clientId, ClientType clientType, Client client, Message message, Map<String, Object> body) {
|
||||
final String queryClientType = MapUtils.get(body, Constant.CLIENT_TYPE);
|
||||
if(StringUtils.isEmpty(queryClientType)) {
|
||||
message.setBody(this.clientManager.getStatus());
|
||||
} else {
|
||||
message.setBody(this.clientManager.getStatus(ClientType.of(queryClientType)));
|
||||
}
|
||||
client.push(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,8 @@ import com.acgist.taoyao.signal.protocol.ProtocolClientAdapter;
|
||||
"battery": 电池电量(0~100),
|
||||
"alarming": 是否发生告警(true|false),
|
||||
"charging": 是否正在充电(true|false),
|
||||
"recording": 是否正在录像(true|false),
|
||||
"clientRecording": 是否正在录像(true|false),
|
||||
"serverRecording": 是否正在录像(true|false),
|
||||
"lastHeartbeat": "最后心跳时间",
|
||||
"status": {更多状态},
|
||||
"config": {更多配置}
|
||||
|
||||
@@ -45,7 +45,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
"battery": 电池电量(0~100),
|
||||
"alarming": 是否发生告警(true|false),
|
||||
"charging": 是否正在充电(true|false),
|
||||
"recording": 是否正在录像(true|false),
|
||||
"clientRecording": 是否正在录像(true|false),
|
||||
"serverRecording": 是否正在录像(true|false),
|
||||
"lastHeartbeat": "最后心跳时间",
|
||||
"status": {更多状态},
|
||||
"config": {更多配置}
|
||||
|
||||
@@ -39,7 +39,8 @@ import com.acgist.taoyao.signal.protocol.ProtocolClientAdapter;
|
||||
"battery": 电池电量(0~100),
|
||||
"alarming": 是否发生告警(true|false),
|
||||
"charging": 是否正在充电(true|false),
|
||||
"recording": 是否正在录像(true|false),
|
||||
"clientRecording": 是否正在录像(true|false),
|
||||
"serverRecording": 是否正在录像(true|false),
|
||||
"lastHeartbeat": "最后心跳时间",
|
||||
"status": {更多状态},
|
||||
"config": {更多配置}
|
||||
|
||||
@@ -43,7 +43,8 @@ import com.acgist.taoyao.signal.protocol.ProtocolRoomAdapter;
|
||||
"battery": 电池电量(0~100),
|
||||
"alarming": 是否发生告警(true|false),
|
||||
"charging": 是否正在充电(true|false),
|
||||
"recording": 是否正在录像(true|false),
|
||||
"clientRecording": 是否正在录像(true|false),
|
||||
"serverRecording": 是否正在录像(true|false),
|
||||
"lastHeartbeat": "最后心跳时间",
|
||||
"status": {更多状态},
|
||||
"config": {更多配置}
|
||||
|
||||
Reference in New Issue
Block a user