Skip to content

Commit

Permalink
Merge pull request #85 from fransjacobs/73-dcc-ex-serial-connection-t…
Browse files Browse the repository at this point in the history
…akes-a-long-time

73 dcc ex serial connection takes a long time
  • Loading branch information
fransjacobs authored Sep 8, 2024
2 parents e40579a + ffbb0c7 commit ad843a6
Show file tree
Hide file tree
Showing 38 changed files with 1,754 additions and 2,104 deletions.
40 changes: 22 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<version>2.3.232</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
<version>3.16.0</version>
</dependency>
<dependency>
<groupId>org.beryx</groupId>
Expand All @@ -52,7 +52,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.12</version>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>com.dieselpoint</groupId>
Expand All @@ -77,7 +77,7 @@
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-batik</artifactId>
<version>3.10.1</version>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
Expand All @@ -93,31 +93,31 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.10.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.3</version>
<version>3.26.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -135,12 +135,12 @@
<dependency>
<groupId>com.formdev</groupId>
<artifactId>flatlaf</artifactId>
<version>3.4</version>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.httprpc</groupId>
<artifactId>sierra</artifactId>
<version>2.0.5</version>
<version>2.3</version>
</dependency>
<dependency>
<groupId>eu.hansolo</groupId>
Expand All @@ -160,14 +160,14 @@
<dependency>
<groupId>org.netbeans.external</groupId>
<artifactId>AbsoluteLayout</artifactId>
<version>RELEASE210</version>
<version>RELEASE220</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<version>3.13.0</version>
<configuration>
<compilerArgs>--enable-preview</compilerArgs>
<release>21</release>
Expand All @@ -177,7 +177,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<version>3.7.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
Expand Down Expand Up @@ -224,7 +224,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.0</version>
<version>3.7.1</version>
<executions>
<execution>
<id>server</id>
Expand Down Expand Up @@ -252,15 +252,19 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<version>3.4.0</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<argLine>-Dfile.encoding=UTF-8</argLine>
<configurationParameters>
junit.jupiter.execution.parallel.enabled=false
</configurationParameters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.2</version>
<configuration>
<skipIfEmpty>true</skipIfEmpty>
<archive>
Expand Down Expand Up @@ -289,7 +293,7 @@
<plugin>
<groupId>io.github.fvarrui</groupId>
<artifactId>javapackager</artifactId>
<version>1.7.5</version>
<version>1.7.6</version>
<configuration>
<mainClass>${exec.mainClass}</mainClass>
<bundleJre>false</bundleJre>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/jcs/JCS.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ public void run() {
Thread.currentThread().setName("JCS finalize thread");
Logger.trace("Shutting Down...");
ProcessFactory.getInstance().shutdown();
Logger.info("JCS "+VersionInfo.getVersion()+" session finished");
Logger.info("JCS " + VersionInfo.getVersion() + " session finished");
}

public static JCS getInstance() {
if (instance == null) {
instance = new JCS();

// Prepare for shutdown...
Runtime.getRuntime().addShutdownHook(instance);
}
Expand All @@ -161,7 +162,7 @@ public static JCS getInstance() {
public static void main(String[] args) {
System.setProperty("fazecast.jSerialComm.appid", "JCS");
version = VersionInfo.getVersion();
Logger.info("Starting JCS Version "+version+"...");
Logger.info("Starting JCS Version " + version + "...");

if (GraphicsEnvironment.isHeadless()) {
Logger.error("This JDK environment is headless, can't start a GUI!");
Expand Down Expand Up @@ -212,7 +213,7 @@ public static void main(String[] args) {

if (persistentStore != null) {
if ("true".equalsIgnoreCase(System.getProperty("commandStation.autoconnect", "true"))) {
if(jcsCommandStation != null) {
if (jcsCommandStation != null) {
boolean connected = jcsCommandStation.connect();
if (connected) {
logProgress("Connected with Command Station...");
Expand All @@ -225,8 +226,8 @@ public static void main(String[] args) {
logProgress("Could NOT connect with Command Station...");
}
} else {
logProgress("NO Default Command Station found...");
}
logProgress("NO Default Command Station found...");
}
}

logProgress("Starting UI...");
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/jcs/commandStation/AbstractController.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void removeDisconnectionEventListener(DisconnectionEventListener listener
this.disconnectionEventListeners.remove(listener);
}

public synchronized boolean isPower() {
public boolean isPower() {
return this.power;
}

Expand Down Expand Up @@ -141,7 +141,6 @@ public void removeLocomotiveSpeedEventListener(LocomotiveSpeedEventListener list
this.locomotiveSpeedEventListeners.remove(listener);
}


protected void pause(long millis) {
try {
Thread.sleep(millis);
Expand Down
59 changes: 35 additions & 24 deletions src/main/java/jcs/commandStation/ControllerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,74 +32,81 @@
* Factory to create the different controllers
*/
public class ControllerFactory {

private static DecoderController decoderController;
private static final Map<String, AccessoryController> accessoryControllers = new HashMap<>();
private static final Map<String, FeedbackController> feedbackControllers = new HashMap<>();

private static ControllerFactory instance;

private ControllerFactory() {
loadPersistentJCSProperties();
}

public static ControllerFactory getInstance() {
if (instance == null) {
instance = new ControllerFactory();
}
return instance;
}


static void reset() {
decoderController = null;
accessoryControllers.clear();
feedbackControllers.clear();
}

public static DecoderController getDecoderController() {
return ControllerFactory.getDecoderController(null, false);
}

public static AccessoryController getAccessoryController(CommandStationBean commandStationBean) {
if (ControllerFactory.accessoryControllers.isEmpty()) {
instantiateAccessoryControllers();
}
return ControllerFactory.accessoryControllers.get(commandStationBean.getId());
}

public static AccessoryController getAccessoryController(String id) {
return ControllerFactory.accessoryControllers.get(id);
}

public static FeedbackController getFeedbackController(String id) {
return ControllerFactory.feedbackControllers.get(id);
}

public static synchronized DecoderController getDecoderController(CommandStationBean commandStationBean, boolean autoConnect) {

if ((decoderController == null && commandStationBean != null)
|| (decoderController != null && !decoderController.getCommandStationBean().equals(commandStationBean))) {
decoderController = instantiateDecoderController(commandStationBean, autoConnect);
}
return decoderController;
}

public static List<AccessoryController> getAccessoryControllers() {
return ControllerFactory.getInstance().getAccessoryControllerImpls();
}

private List<AccessoryController> getAccessoryControllerImpls() {
if (accessoryControllers.isEmpty()) {
instantiateAccessoryControllers();
}
return new ArrayList<>(accessoryControllers.values());
}

public static List<FeedbackController> getFeedbackControllers() {
return ControllerFactory.getInstance().getFeedbackControllerImpls();
}

private List<FeedbackController> getFeedbackControllerImpls() {
if (feedbackControllers.isEmpty()) {
instance.instantiateFeedbackControllers();
}
return new ArrayList<>(feedbackControllers.values());
}

private static DecoderController instantiateDecoderController(CommandStationBean commandStationBean, boolean autoConnect) {
CommandStationBean bean;
if (commandStationBean != null) {
Expand All @@ -108,39 +115,43 @@ private static DecoderController instantiateDecoderController(CommandStationBean
bean = PersistenceFactory.getService().getDefaultCommandStation();
Logger.trace("Default Command station: " + bean.getDescription() + " Decoder Support: " + bean.isDecoderControlSupport());
}

if (bean.isDecoderControlSupport() && bean.isEnabled()) {
String className = bean.getClassName();
JCS.logProgress("Invoking CommandStation: " + className);
Logger.trace("Invoking decoderController: " + className);

try {
Constructor c = Class.forName(className).getConstructor(CommandStationBean.class, Boolean.TYPE);
decoderController = (DecoderController) c.newInstance(commandStationBean, autoConnect);
} catch (ClassNotFoundException | InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException ex) {
Logger.error("Can't instantiate a '" + className + "' " + ex.getMessage());
}
}

if (decoderController != null && bean.isDecoderControlSupport() && bean.isEnabled()) {
accessoryControllers.put(bean.getId(), (AccessoryController) decoderController);
Logger.trace("decoderController is also accessoryController.");
}

if (decoderController != null && bean.isFeedbackSupport() && bean.isEnabled()) {
feedbackControllers.put(bean.getId(), (FeedbackController) decoderController);
Logger.trace("decoderController is also feedbackController.");
}

if (decoderController != null) {
Logger.trace("CommandStationId: " + (decoderController.getCommandStationBean() != null ? decoderController.getCommandStationBean().getId() : "?"));
} else {
Logger.warn("No Default Command Station found for " + commandStationBean.getShortName());
if (commandStationBean != null) {
Logger.warn("No Default Command Station found for " + commandStationBean.getShortName());
} else {
Logger.warn("Command Station NOT set!");
}
}

return decoderController;
}

private static boolean instantiateAccessoryControllers() {
List<CommandStationBean> beans = PersistenceFactory.getService().getCommandStations();
//In case the AccessoryController is the same instance as the DecoderController, which is by example the case for a Marklin CS 3
Expand All @@ -162,7 +173,7 @@ private static boolean instantiateAccessoryControllers() {
}
return !accessoryControllers.isEmpty();
}

private boolean instantiateFeedbackControllers() {
List<CommandStationBean> beans = PersistenceFactory.getService().getCommandStations();
//In case the FeedbackController is the same instance as the DecoderController, which is by example the case for a Marklin CS 3
Expand All @@ -184,7 +195,7 @@ private boolean instantiateFeedbackControllers() {
}
return !feedbackControllers.isEmpty();
}

private void loadPersistentJCSProperties() {
JCS.logProgress("Obtain properties from Persistent store");
if (PersistenceFactory.getService() != null) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/jcs/commandStation/JCSCommandStationImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ public void disconnect() {
this.decoderController = null;
this.accessoryControllers.clear();
this.feedbackControllers.clear();

this.commandStation = null;
ControllerFactory.reset();
}

@Override
Expand Down Expand Up @@ -405,7 +408,7 @@ public String getCommandStationArticleNumber() {

@Override
public void switchPower(boolean on) {
Logger.trace("Switch Power " + (on ? "On" : "Off"));
//Logger.trace("Switch Power " + (on ? "On" : "Off"));
if (decoderController != null) {
decoderController.power(on);
}
Expand Down
Loading

0 comments on commit ad843a6

Please sign in to comment.