Working disconnect button in Console #4862

This commit is contained in:
rusefillc 2023-07-29 00:52:12 -04:00
parent c6401b447b
commit a6f527da62
5 changed files with 98 additions and 6 deletions

View File

@ -59,6 +59,7 @@ public class LinkManager implements Closeable {
private boolean needPullLiveData = true;
public final MessagesListener messageListener = (source, message) -> System.out.println(source + ": " + message);
private Thread communicationThread;
private boolean isDisconnectedByUser;
public LinkManager() {
Future<?> future = submit(() -> {
@ -178,6 +179,16 @@ public class LinkManager implements Closeable {
return this;
}
public void disconnect() {
isDisconnectedByUser = true;
close();
}
public void reconnect() {
isDisconnectedByUser = false;
restart();
}
public enum LogLevel {
INFO,
DEBUG,
@ -191,7 +202,6 @@ public class LinkManager implements Closeable {
public final LinkedBlockingQueue<Runnable> COMMUNICATION_QUEUE = new LinkedBlockingQueue<>();
/**
* All request/responses to underlying controller are happening on this single-threaded executor in a FIFO manner
*
*/
public final ExecutorService COMMUNICATION_EXECUTOR = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
@ -299,7 +309,8 @@ public class LinkManager implements Closeable {
}
public void restart() {
ConnectionStatusLogic.INSTANCE.setValue(ConnectionStatusValue.NOT_CONNECTED);
if (isDisconnectedByUser)
return;
close(); // Explicitly kill the connection (call connectors destructor??????)
String[] ports = getCommPorts();
@ -312,8 +323,10 @@ public class LinkManager implements Closeable {
@Override
public void close() {
if (connector != null)
ConnectionStatusLogic.INSTANCE.setValue(ConnectionStatusValue.NOT_CONNECTED);
if (connector != null) {
connector.stop();
}
isStarted = false; // Connector is dead and cant be in started state (Otherwise the Exception will raised)
}

View File

@ -6,7 +6,7 @@ import java.net.URL;
import java.util.concurrent.atomic.AtomicReference;
public class rusEFIVersion {
public static final int CONSOLE_VERSION = 20230701;
public static final int CONSOLE_VERSION = 20230728;
public static AtomicReference<String> firmwareVersion = new AtomicReference<>("N/A");
public static long classBuildTimeMillis() {

View File

@ -0,0 +1,79 @@
package com.rusefi;
import com.rusefi.io.ConnectionStatusLogic;
import com.rusefi.ui.UIContext;
import com.rusefi.ui.lua.TextEditor;
import org.putgemin.VerticalFlowLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import static javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW;
public class ConnectionTab {
private final UIContext uiContext;
private final JPanel content = new JPanel(new BorderLayout());
public ConnectionTab(UIContext uiContext) {
this.uiContext = uiContext;
JPanel vertical = new JPanel(new VerticalFlowLayout());
content.add(vertical, BorderLayout.CENTER);
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
KeyStroke disconnectKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK);
KeyStroke connectKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK);
vertical.add(new JLabel("Ctrl+C connect"));
vertical.add(connect);
vertical.add(new JLabel("Ctrl+R disconnect"));
vertical.add(disconnect);
ConnectionStatusLogic.INSTANCE.addListener(isConnected -> {
connect.setEnabled(!isConnected);
disconnect.setEnabled(isConnected);
});
TextEditor.installKeyAction(connectKeyStroke, "connectCommand", content, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
reconnect(uiContext);
}
});
TextEditor.installKeyAction(disconnectKeyStroke, "disconnectCommand", content, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
disconnect(uiContext);
}
});
disconnect.addActionListener(e -> disconnect(uiContext));
connect.addActionListener(e -> reconnect(uiContext));
}
private static void reconnect(UIContext uiContext) {
uiContext.getLinkManager().reconnect();
}
private static void disconnect(UIContext uiContext) {
uiContext.getLinkManager().disconnect();
}
public JPanel getContent() {
return content;
}
}

View File

@ -146,6 +146,7 @@ public class ConsoleUI {
MessagesCentral.getInstance().postMessage(ConsoleUI.class, "COMPOSITE_OFF_RPM=" + BinaryProtocolLogger.COMPOSITE_OFF_RPM);
tabbedPane.addTab("rusEFI Online", new OnlineTab(uiContext).getContent());
tabbedPane.addTab("Connection", new ConnectionTab(uiContext).getContent());
if (false) {
// this feature is not totally happy safer to disabke to reduce user confusion

View File

@ -123,8 +123,7 @@ public class TextEditor {
}
public static void installKeyAction(KeyStroke undoKeyStroke, String actionName, JComponent control, AbstractAction action) {
control.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(undoKeyStroke, actionName);
control.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(undoKeyStroke, actionName);
control.getActionMap().put(actionName, action);
}