141 lines
5.7 KiB
Java
141 lines
5.7 KiB
Java
package com.goeing.printserver.main.config;
|
|
|
|
import lombok.Data;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.Properties;
|
|
|
|
/**
|
|
* 打印服务器配置类
|
|
* 负责管理打印服务器的所有配置信息,包括默认打印机、最大队列大小、通知设置等
|
|
*/
|
|
@Component
|
|
@Slf4j
|
|
@Data
|
|
public class PrintServerConfig {
|
|
|
|
private static final String CONFIG_FILE = "printserver.properties";
|
|
|
|
// 默认配置值
|
|
private static final String DEFAULT_PRINTER = "默认打印机";
|
|
private static final int DEFAULT_MAX_QUEUE_SIZE = 10;
|
|
private static final boolean DEFAULT_ENABLE_NOTIFICATIONS = true;
|
|
private static final boolean DEFAULT_START_MINIMIZED = false;
|
|
private static final boolean DEFAULT_AUTO_START = false;
|
|
private static final String DEFAULT_WEBSOCKET_URL = "ws://127.0.0.1:8080/print-websocket";
|
|
private static final String DEFAULT_PRINTER_ID = "123456";
|
|
private static final String DEFAULT_API_KEY = "519883ab-3677-ce4b-59ba-7263870d0a26";
|
|
|
|
// 配置属性
|
|
private String defaultPrinter = DEFAULT_PRINTER;
|
|
private int maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
|
|
private boolean enableNotifications = DEFAULT_ENABLE_NOTIFICATIONS;
|
|
private boolean startMinimized = DEFAULT_START_MINIMIZED;
|
|
private boolean autoStart = DEFAULT_AUTO_START;
|
|
private String websocketUrl = DEFAULT_WEBSOCKET_URL;
|
|
private String printerId = DEFAULT_PRINTER_ID;
|
|
private String apiKey = DEFAULT_API_KEY;
|
|
|
|
private Properties properties = new Properties();
|
|
private File configFile;
|
|
|
|
@PostConstruct
|
|
public void init() {
|
|
// 确定配置文件路径
|
|
String userHome = System.getProperty("user.home");
|
|
configFile = new File(userHome + File.separator + ".goeing" + File.separator + CONFIG_FILE);
|
|
|
|
// 确保目录存在
|
|
if (!configFile.getParentFile().exists()) {
|
|
configFile.getParentFile().mkdirs();
|
|
}
|
|
|
|
// 加载配置
|
|
loadConfig();
|
|
}
|
|
|
|
/**
|
|
* 加载配置
|
|
*/
|
|
public void loadConfig() {
|
|
if (configFile.exists()) {
|
|
try (FileInputStream fis = new FileInputStream(configFile)) {
|
|
properties.load(fis);
|
|
|
|
// 读取配置值
|
|
defaultPrinter = properties.getProperty("defaultPrinter", DEFAULT_PRINTER);
|
|
maxQueueSize = Integer.parseInt(properties.getProperty("maxQueueSize", String.valueOf(DEFAULT_MAX_QUEUE_SIZE)));
|
|
enableNotifications = Boolean.parseBoolean(properties.getProperty("enableNotifications", String.valueOf(DEFAULT_ENABLE_NOTIFICATIONS)));
|
|
startMinimized = Boolean.parseBoolean(properties.getProperty("startMinimized", String.valueOf(DEFAULT_START_MINIMIZED)));
|
|
autoStart = Boolean.parseBoolean(properties.getProperty("autoStart", String.valueOf(DEFAULT_AUTO_START)));
|
|
websocketUrl = properties.getProperty("websocketUrl", DEFAULT_WEBSOCKET_URL);
|
|
printerId = properties.getProperty("printerId", DEFAULT_PRINTER_ID);
|
|
apiKey = properties.getProperty("apiKey", DEFAULT_API_KEY);
|
|
|
|
log.info("配置已加载: {}, WebSocket URL: {}, PrinterId: {}", configFile.getAbsolutePath(), websocketUrl, printerId);
|
|
} catch (IOException e) {
|
|
log.error("加载配置文件失败", e);
|
|
// 使用默认值
|
|
resetToDefaults();
|
|
} catch (NumberFormatException e) {
|
|
log.error("解析配置值失败", e);
|
|
// 使用默认值
|
|
resetToDefaults();
|
|
}
|
|
} else {
|
|
log.info("配置文件不存在,使用默认配置");
|
|
// 使用默认值并保存
|
|
resetToDefaults();
|
|
saveConfig();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 保存配置
|
|
*/
|
|
public void saveConfig() {
|
|
try {
|
|
// 确保目录存在
|
|
configFile.getParentFile().mkdirs();
|
|
|
|
// 设置配置值
|
|
properties.setProperty("defaultPrinter", defaultPrinter);
|
|
properties.setProperty("maxQueueSize", String.valueOf(maxQueueSize));
|
|
properties.setProperty("enableNotifications", String.valueOf(enableNotifications));
|
|
properties.setProperty("startMinimized", String.valueOf(startMinimized));
|
|
properties.setProperty("autoStart", String.valueOf(autoStart));
|
|
properties.setProperty("websocketUrl", websocketUrl);
|
|
properties.setProperty("printerId", printerId);
|
|
properties.setProperty("apiKey", apiKey);
|
|
|
|
// 保存到文件
|
|
try (FileOutputStream fos = new FileOutputStream(configFile)) {
|
|
properties.store(fos, "Print Server Configuration");
|
|
}
|
|
|
|
log.info("配置已保存: {}, WebSocket URL: {}, PrinterId: {}", configFile.getAbsolutePath(), websocketUrl, printerId);
|
|
} catch (IOException e) {
|
|
log.error("保存配置文件失败", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 重置为默认配置
|
|
*/
|
|
public void resetToDefaults() {
|
|
defaultPrinter = DEFAULT_PRINTER;
|
|
maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
|
|
enableNotifications = DEFAULT_ENABLE_NOTIFICATIONS;
|
|
startMinimized = DEFAULT_START_MINIMIZED;
|
|
autoStart = DEFAULT_AUTO_START;
|
|
websocketUrl = DEFAULT_WEBSOCKET_URL;
|
|
printerId = DEFAULT_PRINTER_ID;
|
|
apiKey = DEFAULT_API_KEY;
|
|
}
|
|
} |