2020-06-05 19:11:38 -07:00
|
|
|
package com.rusefi.ui.light;
|
|
|
|
|
2022-02-12 13:09:31 -08:00
|
|
|
import com.devexperts.logging.Logging;
|
2020-06-07 12:44:42 -07:00
|
|
|
import com.rusefi.*;
|
|
|
|
import com.rusefi.autodetect.PortDetector;
|
2020-06-16 19:26:19 -07:00
|
|
|
import com.rusefi.autoupdate.Autoupdate;
|
|
|
|
import com.rusefi.core.Sensor;
|
|
|
|
import com.rusefi.core.SensorCentral;
|
2022-10-15 11:36:41 -07:00
|
|
|
import com.rusefi.core.rusEFIVersion;
|
2020-06-07 12:44:42 -07:00
|
|
|
import com.rusefi.io.ConnectionStateListener;
|
|
|
|
import com.rusefi.io.ConnectionStatusLogic;
|
|
|
|
import com.rusefi.io.ConnectionWatchdog;
|
|
|
|
import com.rusefi.io.LinkManager;
|
2020-06-25 17:18:02 -07:00
|
|
|
import com.rusefi.ui.UIContext;
|
2020-06-05 19:11:38 -07:00
|
|
|
import com.rusefi.ui.util.FrameHelper;
|
2020-06-16 19:26:19 -07:00
|
|
|
import org.putgemin.VerticalFlowLayout;
|
2020-06-05 19:11:38 -07:00
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
|
2022-02-12 13:09:31 -08:00
|
|
|
import static com.devexperts.logging.Logging.getLogging;
|
2020-06-06 09:26:53 -07:00
|
|
|
import static com.rusefi.StartupFrame.createLogoLabel;
|
|
|
|
|
2020-06-05 19:11:38 -07:00
|
|
|
public class LightweightGUI {
|
2022-02-12 13:09:31 -08:00
|
|
|
private static final Logging log = getLogging(LightweightGUI.class);
|
2020-06-07 12:44:42 -07:00
|
|
|
|
2022-02-12 13:09:31 -08:00
|
|
|
private final JPanel connectedPanel = new JPanel();
|
|
|
|
private final JLabel connectedLabel = new JLabel();
|
2020-06-07 12:44:42 -07:00
|
|
|
|
2020-06-25 17:33:25 -07:00
|
|
|
public LightweightGUI(UIContext uiContext) {
|
2022-02-09 22:51:48 -08:00
|
|
|
final FrameHelper frameHelper = new FrameHelper();
|
2020-06-05 19:11:38 -07:00
|
|
|
frameHelper.getFrame().setTitle("rusEFI Lightweight " + rusEFIVersion.CONSOLE_VERSION);
|
|
|
|
|
|
|
|
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
2020-06-07 12:44:42 -07:00
|
|
|
|
|
|
|
connectedPanel.add(connectedLabel);
|
|
|
|
|
|
|
|
topPanel.add(connectedPanel);
|
|
|
|
|
2020-06-05 19:11:38 -07:00
|
|
|
topPanel.add(new InternetStatus().getContent());
|
|
|
|
|
2020-06-07 12:44:42 -07:00
|
|
|
|
2020-06-16 19:26:19 -07:00
|
|
|
JPanel leftPanel = new JPanel(new VerticalFlowLayout());
|
|
|
|
leftPanel.add(new JLabel(Autoupdate.readBundleFullName()));
|
|
|
|
|
|
|
|
|
|
|
|
JLabel firmwareVersion = new JLabel();
|
2020-12-22 17:12:50 -08:00
|
|
|
SensorCentral.getInstance().addListener(Sensor.FIRMWARE_VERSION, value -> firmwareVersion.setText(Integer.toString((int) value)));
|
2020-06-16 19:26:19 -07:00
|
|
|
leftPanel.add(firmwareVersion);
|
|
|
|
|
2022-02-09 22:51:48 -08:00
|
|
|
final JPanel content = new JPanel(new BorderLayout());
|
2020-06-05 19:11:38 -07:00
|
|
|
content.add(topPanel, BorderLayout.NORTH);
|
2020-06-16 19:26:19 -07:00
|
|
|
content.add(leftPanel, BorderLayout.WEST);
|
|
|
|
|
|
|
|
content.add(createLogoUrlPanel(), BorderLayout.EAST);
|
|
|
|
|
2020-06-07 12:44:42 -07:00
|
|
|
frameHelper.showFrame(content, true);
|
|
|
|
}
|
2020-06-06 09:26:53 -07:00
|
|
|
|
2020-06-07 12:44:42 -07:00
|
|
|
public static void start() {
|
2020-06-25 17:18:02 -07:00
|
|
|
|
|
|
|
UIContext uiContext = new UIContext();
|
|
|
|
|
2020-06-25 17:33:25 -07:00
|
|
|
LightweightGUI gui = new LightweightGUI(uiContext);
|
2020-06-06 09:26:53 -07:00
|
|
|
|
2020-06-07 12:44:42 -07:00
|
|
|
gui.setConnectedUI(false);
|
|
|
|
|
|
|
|
|
2020-06-25 17:33:25 -07:00
|
|
|
new Thread(() -> waitForDeviceAndStart(uiContext.getLinkManager())).start();
|
2020-06-07 12:44:42 -07:00
|
|
|
|
|
|
|
ConnectionStatusLogic.INSTANCE.addListener(new ConnectionStatusLogic.Listener() {
|
|
|
|
@Override
|
|
|
|
public void onConnectionStatus(boolean isConnected) {
|
2020-06-25 17:18:02 -07:00
|
|
|
uiContext.sensorLogger.init();
|
2020-06-07 12:44:42 -07:00
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
gui.setConnectedUI(isConnected);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-06-16 19:26:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static JPanel createLogoUrlPanel() {
|
|
|
|
JPanel panel = new JPanel(new BorderLayout());
|
|
|
|
|
|
|
|
panel.add(new JLabel(StartupFrame.LINK_TEXT), BorderLayout.SOUTH);
|
|
|
|
JLabel logo = createLogoLabel();
|
|
|
|
if (logo != null) {
|
|
|
|
panel.add(logo, BorderLayout.CENTER);
|
|
|
|
}
|
2020-06-07 12:44:42 -07:00
|
|
|
|
2020-06-16 19:26:19 -07:00
|
|
|
return panel;
|
2020-06-07 12:44:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setConnectedUI(boolean isConnected) {
|
|
|
|
connectedLabel.setText(isConnected ? "Connected" : "NOT CONNECTED");
|
|
|
|
connectedPanel.setBackground(isConnected ? Color.green : Color.red);
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:33:25 -07:00
|
|
|
public static void waitForDeviceAndStart(LinkManager linkManager) {
|
2020-06-07 12:44:42 -07:00
|
|
|
String autoDetectedPort = detectPortUntilDetected();
|
|
|
|
System.out.println("First time port detected: " + autoDetectedPort);
|
|
|
|
|
2020-06-25 17:33:25 -07:00
|
|
|
linkManager.startAndConnect(autoDetectedPort, ConnectionStateListener.VOID);
|
2020-06-07 12:44:42 -07:00
|
|
|
|
2022-02-23 11:58:07 -08:00
|
|
|
ConnectionWatchdog.init(linkManager);
|
2020-06-07 12:44:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String detectPortUntilDetected() {
|
|
|
|
while (true) {
|
2021-09-25 14:51:47 -07:00
|
|
|
String port = PortDetector.autoDetectSerial(null).getSerialPort();
|
2020-06-07 12:44:42 -07:00
|
|
|
System.out.println("Detected " + port);
|
|
|
|
if (port != null)
|
|
|
|
return port;
|
|
|
|
IoUtil.sleepSeconds(1);
|
|
|
|
}
|
2020-06-05 19:11:38 -07:00
|
|
|
}
|
|
|
|
}
|