Initial upload opcua service
This commit is contained in:
64
main/java/de/opcua/app/config/Settings.java
Normal file
64
main/java/de/opcua/app/config/Settings.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package de.opcua.app.config;
|
||||
|
||||
import de.opcua.app.logging.ScriptLogger;
|
||||
|
||||
/**
|
||||
* Enhanced Settings with HTTP/HTTPS configuration
|
||||
*/
|
||||
public record Settings(
|
||||
String endpoint,
|
||||
String host,
|
||||
|
||||
// HTTP Configuration
|
||||
boolean httpEnabled,
|
||||
int httpPort,
|
||||
|
||||
// HTTPS Configuration
|
||||
boolean httpsEnabled,
|
||||
int httpsPort,
|
||||
String keystorePath,
|
||||
String keystorePassword,
|
||||
String keystoreType,
|
||||
|
||||
// Script Logging
|
||||
boolean scriptLoggingEnabled,
|
||||
String scriptLogLevel,
|
||||
String scriptLogDirectory
|
||||
) {
|
||||
|
||||
// Default constructor with sensible defaults
|
||||
public Settings() {
|
||||
this(
|
||||
"opc.tcp://opcuaserver.com:48010",
|
||||
"http://localhost:8080",
|
||||
true, // HTTP enabled by default
|
||||
8081, // HTTP port
|
||||
false, // HTTPS disabled by default
|
||||
8443, // HTTPS port
|
||||
"",
|
||||
"",
|
||||
"JKS",
|
||||
true,
|
||||
"INFO",
|
||||
System.getProperty("user.home") + "/.opcua-gui/logs"
|
||||
);
|
||||
}
|
||||
|
||||
// Backward compatibility - old constructor
|
||||
public Settings(String listener, String endpoint, String host) {
|
||||
this(endpoint, host, true, 8081, false, 8443, "", "", "JKS",
|
||||
true, "INFO", System.getProperty("user.home") + "/.opcua-gui/logs");
|
||||
}
|
||||
|
||||
public static Settings defaults() {
|
||||
return new Settings();
|
||||
}
|
||||
|
||||
public ScriptLogger.Level getLogLevel() {
|
||||
try {
|
||||
return ScriptLogger.Level.valueOf(scriptLogLevel.toUpperCase());
|
||||
} catch (Exception e) {
|
||||
return ScriptLogger.Level.INFO;
|
||||
}
|
||||
}
|
||||
}
|
||||
102
main/java/de/opcua/app/config/SettingsService.java
Normal file
102
main/java/de/opcua/app/config/SettingsService.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package de.opcua.app.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
public final class SettingsService {
|
||||
private static final Path FILE = Path.of("settings.json");
|
||||
private final ObjectMapper om = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
|
||||
|
||||
public Settings load() {
|
||||
if (!Files.exists(FILE)) return Settings.defaults();
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> m = om.readValue(Files.readString(FILE), Map.class);
|
||||
|
||||
Settings defaults = Settings.defaults();
|
||||
|
||||
String endpoint = (String) m.getOrDefault("Endpoint", defaults.endpoint());
|
||||
String host = (String) m.getOrDefault("Host", defaults.host());
|
||||
|
||||
// HTTP Configuration
|
||||
boolean httpEnabled = m.containsKey("httpEnabled")
|
||||
? (Boolean) m.get("httpEnabled")
|
||||
: true;
|
||||
|
||||
int httpPort = 8081;
|
||||
if (m.containsKey("httpPort")) {
|
||||
httpPort = ((Number) m.get("httpPort")).intValue();
|
||||
} else if (m.containsKey("httpUrl")) {
|
||||
// Backward compatibility - extract port from httpUrl
|
||||
try {
|
||||
String url = (String) m.get("httpUrl");
|
||||
if (url.contains(":")) {
|
||||
String portStr = url.substring(url.lastIndexOf(":") + 1).replace("/", "");
|
||||
httpPort = Integer.parseInt(portStr);
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
|
||||
// HTTPS Configuration
|
||||
boolean httpsEnabled = m.containsKey("httpsEnabled")
|
||||
? (Boolean) m.get("httpsEnabled")
|
||||
: defaults.httpsEnabled();
|
||||
|
||||
int httpsPort = m.containsKey("httpsPort")
|
||||
? ((Number) m.get("httpsPort")).intValue()
|
||||
: defaults.httpsPort();
|
||||
|
||||
String keystorePath = (String) m.getOrDefault("keystorePath", defaults.keystorePath());
|
||||
String keystorePassword = (String) m.getOrDefault("keystorePassword", defaults.keystorePassword());
|
||||
String keystoreType = (String) m.getOrDefault("keystoreType", defaults.keystoreType());
|
||||
|
||||
// Logging fields
|
||||
boolean loggingEnabled = m.containsKey("scriptLoggingEnabled")
|
||||
? (Boolean) m.get("scriptLoggingEnabled")
|
||||
: defaults.scriptLoggingEnabled();
|
||||
|
||||
String logLevel = (String) m.getOrDefault("scriptLogLevel", defaults.scriptLogLevel());
|
||||
String logDir = (String) m.getOrDefault("scriptLogDirectory", defaults.scriptLogDirectory());
|
||||
|
||||
return new Settings(
|
||||
endpoint, host,
|
||||
httpEnabled, httpPort,
|
||||
httpsEnabled, httpsPort, keystorePath, keystorePassword, keystoreType,
|
||||
loggingEnabled, logLevel, logDir
|
||||
);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error loading settings: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return Settings.defaults();
|
||||
}
|
||||
}
|
||||
|
||||
public void save(Settings s) throws IOException {
|
||||
Map<String, Object> m = new java.util.HashMap<>();
|
||||
m.put("Endpoint", s.endpoint());
|
||||
m.put("Host", s.host());
|
||||
|
||||
// HTTP Configuration
|
||||
m.put("httpEnabled", s.httpEnabled());
|
||||
m.put("httpPort", s.httpPort());
|
||||
|
||||
// HTTPS Configuration
|
||||
m.put("httpsEnabled", s.httpsEnabled());
|
||||
m.put("httpsPort", s.httpsPort());
|
||||
m.put("keystorePath", s.keystorePath());
|
||||
m.put("keystorePassword", s.keystorePassword());
|
||||
m.put("keystoreType", s.keystoreType());
|
||||
|
||||
// Logging
|
||||
m.put("scriptLoggingEnabled", s.scriptLoggingEnabled());
|
||||
m.put("scriptLogLevel", s.scriptLogLevel());
|
||||
m.put("scriptLogDirectory", s.scriptLogDirectory());
|
||||
|
||||
Files.writeString(FILE, om.writeValueAsString(m));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user