重构模型类包结构,将模型类按功能模块划分到不同的子包中。优化序列化处理,为VO类添加serialVersionUID并实现Serializable接口。移除部分冗余的serialVersionUID字段,简化模型类代码。同时修复UITools中空值处理的问题,并更新pom版本至0.0.100-SNAPSHOT。 - 将模型类按功能模块划分到ds子包中 - 为VO类添加序列化支持 - 移除冗余的serialVersionUID字段 - 修复UITools空值处理问题 - 更新项目版本号
132 lines
4.6 KiB
Java
132 lines
4.6 KiB
Java
package com.ecep.contract;
|
||
|
||
import java.beans.PropertyDescriptor;
|
||
import java.util.Locale;
|
||
import java.util.Map;
|
||
import java.util.UUID;
|
||
import java.util.logging.Level;
|
||
|
||
import javafx.application.Platform;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.BeanUtils;
|
||
|
||
import com.ecep.contract.constant.WebSocketConstant;
|
||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||
import com.fasterxml.jackson.databind.JsonNode;
|
||
|
||
import lombok.Getter;
|
||
|
||
/**
|
||
*
|
||
*/
|
||
public class WebSocketClientSession {
|
||
private static final Logger logger = LoggerFactory.getLogger(WebSocketClientSession.class);
|
||
/**
|
||
* 会话ID由客户端创建,服务器不保存会话只回传会话ID
|
||
*/
|
||
@Getter
|
||
private final String sessionId = UUID.randomUUID().toString();
|
||
|
||
private WebSocketClientTasker tasker;
|
||
|
||
private final WebSocketClientService webSocketService;
|
||
|
||
public WebSocketClientSession(WebSocketClientService webSocketService) {
|
||
this.webSocketService = webSocketService;
|
||
}
|
||
|
||
public void close() {
|
||
webSocketService.closeSession(this);
|
||
}
|
||
|
||
public void submitTask(WebSocketClientTasker tasker, Locale locale, Object... args) throws JsonProcessingException {
|
||
this.tasker = tasker;
|
||
send("createTask", tasker.getTaskName(), locale.toLanguageTag(), args);
|
||
}
|
||
|
||
public void send(String type, Object... args) throws JsonProcessingException {
|
||
Map<String, Object> arguments = Map.of(
|
||
WebSocketConstant.SESSION_ID_FIELD_NAME, getSessionId(),
|
||
"type", type,
|
||
WebSocketConstant.ARGUMENTS_FIELD_NAME, args);
|
||
webSocketService.send(arguments);
|
||
}
|
||
|
||
public void onMessage(JsonNode node) {
|
||
if (node.has("type")) {
|
||
String type = node.get("type").asText();
|
||
JsonNode args = node.get(WebSocketConstant.ARGUMENTS_FIELD_NAME);
|
||
if (type.equals("message")) {
|
||
handleAsMessage(args);
|
||
} else if (type.equals("title")) {
|
||
handleAsTitle(args);
|
||
} else if (type.equals("property")) {
|
||
handleAsProperty(args);
|
||
} else if (type.equals("progress")) {
|
||
handleAsProgress(args);
|
||
} else if (type.equals("start")) {
|
||
handleAsStart(args);
|
||
} else if (type.equals("done")) {
|
||
handleAsDone(args);
|
||
} else {
|
||
tasker.updateMessage(java.util.logging.Level.INFO, "未知的消息类型: " + node.toString());
|
||
}
|
||
} else {
|
||
tasker.updateMessage(java.util.logging.Level.INFO, "未知的消息: " + node.toString());
|
||
}
|
||
}
|
||
|
||
private void handleAsStart(JsonNode args) {
|
||
tasker.updateMessage(java.util.logging.Level.INFO, "任务开始");
|
||
}
|
||
|
||
private void handleAsDone(JsonNode args) {
|
||
tasker.updateMessage(java.util.logging.Level.INFO, "任务完成");
|
||
close();
|
||
}
|
||
|
||
private void handleAsProgress(JsonNode args) {
|
||
long current = args.get(0).asLong();
|
||
long total = args.get(1).asLong();
|
||
tasker.updateProgress(current, total);
|
||
}
|
||
|
||
private void handleAsProperty(JsonNode args) {
|
||
String name = args.get(0).asText();
|
||
Object value = args.get(1);
|
||
try {
|
||
PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(tasker.getClass(), name);
|
||
if (descriptor == null) {
|
||
tasker.updateMessage(java.util.logging.Level.SEVERE, "属性 " + name + " 不存在");
|
||
return;
|
||
}
|
||
Object object = webSocketService.getObjectMapper().convertValue(value, descriptor.getPropertyType());
|
||
if (descriptor.getWriteMethod() == null) {
|
||
tasker.updateMessage(java.util.logging.Level.SEVERE, "属性 " + name + " 不可写");
|
||
} else {
|
||
descriptor.getWriteMethod().invoke(tasker, object);
|
||
}
|
||
} catch (Exception e) {
|
||
tasker.updateMessage(java.util.logging.Level.SEVERE, "属性设置失败: " + name + " = " + value);
|
||
logger.error("set {} = {}", name, value, e);
|
||
}
|
||
}
|
||
|
||
private void handleAsTitle(JsonNode args) {
|
||
String message = args.get(0).asText();
|
||
tasker.updateTitle(message);
|
||
}
|
||
|
||
private void handleAsMessage(JsonNode args) {
|
||
String level = args.get(0).asText();
|
||
String message = args.get(1).asText();
|
||
updateMessage(java.util.logging.Level.parse(level), "[R] " + message);
|
||
}
|
||
|
||
public void updateMessage(Level level, String message) {
|
||
tasker.updateMessage(level, message);
|
||
}
|
||
|
||
}
|