Skip to content

Commit 437ef74

Browse files
committed
Added discovery
1 parent 06c7cfd commit 437ef74

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

src/main/java/jcs/entities/CommandStationBean.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ public void setConnectVia(String connectVia) {
116116

117117
@Transient
118118
public ConnectionType getConnectionType() {
119-
return ConnectionType.get(connectVia);
119+
if (connectVia != null) {
120+
return ConnectionType.get(connectVia);
121+
} else {
122+
return null;
123+
}
120124
}
121125

122126
@Transient

src/main/java/jcs/ui/settings/CommandStationDialog1.java

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@
1616
package jcs.ui.settings;
1717

1818
import com.fazecast.jSerialComm.SerialPort;
19-
import java.net.InetAddress;
2019
import java.util.ArrayList;
2120
import java.util.List;
2221
import java.util.concurrent.ExecutorService;
2322
import java.util.concurrent.Executors;
2423
import javax.swing.ComboBoxModel;
2524
import javax.swing.DefaultComboBoxModel;
25+
import javax.swing.JOptionPane;
2626
import javax.swing.UIManager;
2727
import javax.swing.UnsupportedLookAndFeelException;
2828
import jcs.JCS;
29+
import jcs.commandStation.esu.ecos.net.EcosConnectionFactory;
2930
import jcs.commandStation.marklin.cs.net.CSConnectionFactory;
3031
import jcs.entities.CommandStationBean;
3132
import jcs.persistence.PersistenceFactory;
3233
import org.tinylog.Logger;
34+
import java.beans.PropertyChangeEvent;
35+
import java.beans.PropertyChangeListener;
36+
import java.net.InetAddress;
37+
import javax.swing.JDialog;
3338

3439
/**
3540
*
@@ -48,10 +53,9 @@ public class CommandStationDialog1 extends javax.swing.JDialog {
4853

4954
private CommandStationBean emptyCS;
5055
private CommandStationBean emptyFB;
51-
56+
5257
private static final String MARKLIN_CS = "marklin.cs";
5358
private static final String ESU_ECOS = "esu-ecos";
54-
5559

5660
/**
5761
* Creates new form CommandStationDialog1
@@ -138,7 +142,7 @@ private void initModels(CommandStationBean selected) {
138142
networkRB.setSelected(true);
139143
}
140144

141-
if (CommandStationBean.ConnectionType.SERIAL == selectedFeedbackProvider.getConnectionType()) {
145+
if (selectedFeedbackProvider.getConnectionType() != null && CommandStationBean.ConnectionType.SERIAL == selectedFeedbackProvider.getConnectionType()) {
142146
String port = selectedFeedbackProvider.getSerialPort();
143147
fbpSerialCB.setSelectedItem(port);
144148
}
@@ -434,6 +438,7 @@ private void persistCommandStation(final CommandStationBean commandStation) {
434438

435439
private void discoverBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_discoverBtnActionPerformed
436440
Logger.trace("Try to discover " + selectedCommandStation.getDescription());
441+
executor.execute(() -> discover(selectedCommandStation));
437442
}//GEN-LAST:event_discoverBtnActionPerformed
438443

439444
private void feedbackCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_feedbackCBActionPerformed
@@ -472,7 +477,7 @@ private void networkRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
472477
selectedCommandStation.setConnectionType(CommandStationBean.ConnectionType.NETWORK);
473478
selectedCommandStation.setSerialPort(null);
474479
} else {
475-
selectedCommandStation.setConnectionType(CommandStationBean.ConnectionType.SERIAL);
480+
selectedCommandStation.setConnectionType(CommandStationBean.ConnectionType.SERIAL);
476481
}
477482
persistCommandStation(selectedCommandStation);
478483
}//GEN-LAST:event_networkRBActionPerformed
@@ -487,36 +492,66 @@ private void serialRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
487492
}//GEN-LAST:event_serialRBActionPerformed
488493

489494
private void ipTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ipTFActionPerformed
490-
Logger.trace("ip Address: "+this.ipTF.getText());
495+
Logger.trace("ip Address: " + this.ipTF.getText());
491496
selectedCommandStation.setIpAddress(ipTF.getText());
492497
}//GEN-LAST:event_ipTFActionPerformed
493498

494499
private void ipTFFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ipTFFocusLost
495-
Logger.trace("ip Address: "+this.ipTF.getText());
500+
Logger.trace("ip Address: " + this.ipTF.getText());
496501
selectedCommandStation.setIpAddress(ipTF.getText());
497502
}//GEN-LAST:event_ipTFFocusLost
498503

499504
private void ipTFMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ipTFMouseExited
500-
Logger.trace("ip Address: "+this.ipTF.getText());
505+
Logger.trace("ip Address: " + this.ipTF.getText());
501506
selectedCommandStation.setIpAddress(ipTF.getText());
502507
}//GEN-LAST:event_ipTFMouseExited
503508

504509
private void connectBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectBtnActionPerformed
505510
// TODO add your handling code here:
506511
}//GEN-LAST:event_connectBtnActionPerformed
507512

508-
509-
private void discover() {
510-
//Start een wacht dialog box...
511-
if(MARKLIN_CS.equals(selectedCommandStation.getId())) {
512-
InetAddress csAddress = CSConnectionFactory.discoverCs();
513-
} else if(ESU_ECOS.equals(selectedCommandStation.getId())) {
514-
513+
private InetAddress discover(final CommandStationBean commandStation) {
514+
final JOptionPane optionPane = new JOptionPane("Try to discovering a " + commandStation.getDescription(),
515+
JOptionPane.INFORMATION_MESSAGE,
516+
JOptionPane.OK_OPTION);
517+
518+
final JDialog discoverDialog = new JDialog(this, "Discovering...");
519+
discoverDialog.setContentPane(optionPane);
520+
discoverDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
521+
discoverDialog.pack();
522+
discoverDialog.setLocationRelativeTo(null);
523+
discoverDialog.setVisible(true);
524+
525+
InetAddress inetAddress = null;
526+
if (MARKLIN_CS.equals(commandStation.getId())) {
527+
inetAddress = CSConnectionFactory.discoverCs();
528+
} else if (ESU_ECOS.equals(commandStation.getId())) {
529+
inetAddress = EcosConnectionFactory.discoverEcos();
530+
}
531+
532+
// optionPane.addPropertyChangeListener((PropertyChangeEvent e) -> {
533+
// String prop = e.getPropertyName();
534+
//
535+
// if (discoverDialog.isVisible()
536+
// && (e.getSource() == optionPane)
537+
// && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
538+
// discoverDialog.setVisible(false);
539+
// }
540+
// });
541+
if (inetAddress != null) {
542+
Logger.trace("Discovered host " + inetAddress.getHostAddress() + " for " + commandStation.getDescription());
543+
commandStation.setIpAddress(inetAddress.getHostAddress());
515544
}
545+
546+
java.awt.EventQueue.invokeLater(() -> {
547+
setComponents();
548+
discoverDialog.setVisible(false);
549+
discoverDialog.dispose();
550+
});
551+
552+
return inetAddress;
516553
}
517-
518-
519-
554+
520555
/**
521556
* @param args the command line arguments
522557
*/

0 commit comments

Comments
 (0)