Skip to content

Locomotives are now refreshed in the main screen when added via locom… #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ Any value defined here will override the pom.xml file value but is only applicab
<com-junichi11-netbeans-changelf.use-project>true</com-junichi11-netbeans-changelf.use-project>
<com-junichi11-netbeans-changelf.lf-kind>LF</com-junichi11-netbeans-changelf.lf-kind>
<com-junichi11-netbeans-changelf.use-global>false</com-junichi11-netbeans-changelf.use-global>
<netbeans.hint.jdkPlatform>JDK_21_Temurin</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>
10 changes: 6 additions & 4 deletions src/main/java/jcs/JCS.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public class JCS extends Thread {

private static UICallback uiCallback;

//private final List<RefreshEventListener> refreshEventListeners;
private JCS() {
//refreshEventListeners = new ArrayList<>();
}

public static void logProgress(String message) {
Expand Down Expand Up @@ -237,6 +235,12 @@ public static void main(String[] args) {
JCS jcs = JCS.getInstance();

jcs.startGui();

//check the connection to the command station
if (!JCS.getJcsCommandStation().isConnected()) {
JCS.getJcsCommandStation().connectInBackground();
}

} else {
Logger.error("Could not obtain a Persistent store. Quitting....");
logProgress("Error! Can't Obtain a Persistent store!");
Expand Down Expand Up @@ -286,8 +290,6 @@ private void startGui() {
jcsFrame.setVisible(true);
jcsFrame.toFront();
jcsFrame.showOverviewPanel();
boolean con = "true".equalsIgnoreCase(System.getProperty("controller.autoconnect", "true"));
jcsFrame.connect(con);
});

JCS.logProgress("JCS started...");
Expand Down
41 changes: 25 additions & 16 deletions src/main/java/jcs/commandStation/JCSCommandStation.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,36 @@ private JCSCommandStation(boolean autoConnectController) {
}
}

public final boolean connectInBackground() {
executor.execute(() -> connect());

public final synchronized boolean connectInBackground() {
long now = System.currentTimeMillis();
long timemax = now + 2000;
long start = now;
long timemax = now + 3000;

executor.execute(() -> connect());

boolean con;
synchronized (this) {
boolean con = false;
if (decoderController != null) {
con = decoderController.isConnected();
while (!con && timemax < now) {
try {
wait(500);
} catch (InterruptedException ex) {
Logger.trace(ex);
}
now = System.currentTimeMillis();
}
if (!(timemax < now)) {
Logger.trace("Timeout connecting...");
} else {
Logger.trace("Can't connect as there is no DecoderController configured !");
}

while (!con && now < timemax) {
try {
wait(500);
} catch (InterruptedException ex) {
Logger.trace(ex);
}
now = System.currentTimeMillis();
con = decoderController.isConnected();
}

if (con) {
Logger.trace("Connected to " + decoderController.getCommandStationBean().getDescription() + " in " + (now - start) + " ms");
} else {
Logger.trace("Timeout connecting...");
}

return con;
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/jcs/entities/LocomotiveBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public class LocomotiveBean implements Serializable {
private String dispatcherDirection;
private String locomotiveDirection;

private Integer speedOne;
private Integer speedTwo;
private Integer speedThree;
private Integer speedFour;

private Image locIcon;
private CommandStationBean commandStationBean;

Expand Down Expand Up @@ -321,6 +326,42 @@ public void setCommandStationId(String commandStationId) {
this.commandStationId = commandStationId;
}

@Column(name = "speed_1")
public Integer getSpeedOne() {
return speedOne;
}

public void setSpeedOne(Integer speedOne) {
this.speedOne = speedOne;
}

@Column(name = "speed_2")
public Integer getSpeedTwo() {
return speedTwo;
}

public void setSpeedTwo(Integer speedTwo) {
this.speedTwo = speedTwo;
}

@Column(name = "speed_3")
public Integer getSpeedThree() {
return speedThree;
}

public void setSpeedThree(Integer speedThree) {
this.speedThree = speedThree;
}

@Column(name = "speed_4")
public Integer getSpeedFour() {
return speedFour;
}

public void setSpeedFour(Integer speedFour) {
this.speedFour = speedFour;
}

@Transient
public CommandStationBean getCommandStationBean() {
return commandStationBean;
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/jcs/entities/SensorBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,11 @@ public boolean equalsId(Object obj) {

@Override
public String toString() {
//return name;
return toLogString();
return name;
//return toLogString();
}

public String toLogString() {
// String ids;
// if (id == null) {
// ids = "(" + generateId() + ")";
// } else {
// ids = id;
// }

return "SensorBean{"
+ "id="
+ id
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/jcs/persistence/H2PersistenceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public FunctionBean getLocomotiveFunction(LocomotiveBean locomotive, Integer num

@Override
public FunctionBean getLocomotiveFunction(Long locomotiveId, Integer number) {
String commandStationId = getDefaultCommandStation().getId();
//String commandStationId = getDefaultCommandStation().getId();

FunctionBean fb = database.where("locomotive_id=? and f_number=?", locomotiveId, number).first(FunctionBean.class);
if (fb != null) {
Expand Down Expand Up @@ -970,15 +970,15 @@ public synchronized BlockBean persist(BlockBean block) {
database.insert(block);
}

changeSupport.firePropertyChange("data.blockr", prev, block);
changeSupport.firePropertyChange("data.block", prev, block);
return block;
}

@Override
public synchronized void remove(BlockBean block) {
int rows = this.database.delete(block).getRowsAffected();
int rows = database.delete(block).getRowsAffected();
Logger.trace(rows + " rows deleted");
changeSupport.firePropertyChange("data.sblock.deleted", block, null);
changeSupport.firePropertyChange("data.block.deleted", block, null);
}

@Override
Expand All @@ -990,7 +990,6 @@ public synchronized void removeAllBlocks() {

@Override
public List<CommandStationBean> getCommandStations() {
//List<CommandStationBean> commandStationBeans = database.where("enabled=true").results(CommandStationBean.class);
List<CommandStationBean> commandStationBeans = database.results(CommandStationBean.class);
return commandStationBeans;
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/jcs/ui/DispatcherStatusPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

import javax.swing.JPanel;
import jcs.commandStation.events.RefreshEvent;
import jcs.commandStation.events.RefreshEventListener;
import jcs.ui.util.LocomotiveSelectionChangedListener;

/**
*
*/
public class DispatcherStatusPanel extends JPanel implements RefreshEventListener {
public class DispatcherStatusPanel extends JPanel { //implements RefreshEventListener {

private static final long serialVersionUID = 6158244271104499799L;

Expand All @@ -43,15 +42,14 @@ public void refresh() {
locomotiveTablePanel.refresh();
}

@Override
public void onChange(RefreshEvent event) {
locomotiveTablePanel.onChange(event);
}
// @Override
// public void onChange(RefreshEvent event) {
// locomotiveTablePanel.onChange(event);
// }

public void addLocomotiveSelectionChangeListener(LocomotiveSelectionChangedListener listener) {
locomotiveTablePanel.addLocomotiveSelectionChangeListener(listener);
dispatcherTablePanel.addLocomotiveSelectionChangeListener(listener);

}

public void removeLocomotiveSelectionChangeListener(LocomotiveSelectionChangedListener listener) {
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/jcs/ui/JCSFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ private void showLocomotives() {
locomotiveDialog.setLocationRelativeTo(null);
}
locomotiveDialog.setVisible(true);

//Should add a listener here?
}

private void showAccessories() {
Expand Down Expand Up @@ -969,20 +971,16 @@ public void connect(boolean connect) {
if (JCS.getJcsCommandStation() != null) {
if (connect) {
String ip = JCS.getJcsCommandStation().getCommandStationBean().getIpAddress();
String name = JCS.getJcsCommandStation().getCommandStationBean().getDescription();
if (Ping.IsReachable(ip)) {
if ("AWT-EventQueue-0".equals(Thread.currentThread().getName())) {
JCS.getJcsCommandStation().connectInBackground();
} else {

JCS.getJcsCommandStation().connect();
}
} else {
Logger.debug("Can't reach ip " + ip + "...");


JOptionPane.showMessageDialog(this, "Can't connect. "+ip+" is not reachable.", "Can't Connect", JOptionPane.ERROR_MESSAGE, null);


JOptionPane.showMessageDialog(this, "Can't connect to " + name + ", " + ip + " is not reachable.", "Can't Connect", JOptionPane.ERROR_MESSAGE, null);
}

InfoBean info = JCS.getJcsCommandStation().getCommandStationInfo();
Expand Down Expand Up @@ -1147,7 +1145,7 @@ private void startAllLocomotives() {
private String getTitleString() {
String jcsVersion = VersionInfo.getVersion();

if (JCS.getJcsCommandStation() != null && JCS.getJcsCommandStation().getCommandStationInfo() != null) {
if (JCS.getJcsCommandStation() != null && JCS.getJcsCommandStation().getCommandStationInfo() != null && JCS.getJcsCommandStation().getCommandStationInfo().getProductName() != null) {
InfoBean info = JCS.getJcsCommandStation().getCommandStationInfo();
return "JCS " + "Connected to " + info.getProductName();
} else {
Expand Down Expand Up @@ -1207,6 +1205,14 @@ public void powerChanged(PowerEvent event) {
powerButton.setSelected(event.isPower());
}

public void refreshData() {
Logger.trace("Refresh data due to settings change...");
}

public void refreshLocomotives() {
this.dispatcherStatusPanel.refresh();
}


// Variables declaration - do not modify//GEN-BEGIN:variables
private JMenuItem aboutMI;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/jcs/ui/KeyboardSensorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
Expand Down
Loading
Loading