only:refactoring: extract part of functionality into `JobType` enumeration #6791

This commit is contained in:
kifir 2024-09-19 23:09:13 +03:00 committed by rusEFI LLC
parent de4618d60b
commit 0205725d97
4 changed files with 55 additions and 41 deletions

View File

@ -0,0 +1,40 @@
package com.rusefi.maintenance;
import java.util.Optional;
public enum JobType {
DFU_AUTO("DFU update", "Auto DFU Update"),
DFU_MANUAL("DFU update", "Manual DFU Update"),
INSTALL_OPENBLT("OpenBLT Initial Programming"/*, "Install OpenBLT"*/),
ST_LINK(""/*, "ST-LINK Update"*/),
DFU_SWITCH("DFU switch", "Switch to DFU Mode"),
OPENBLT_SWITCH("OpenBLT switch", "Switch to OpenBLT Mode"),
OPENBLT_CAN("OpenBLT via CAN", "OpenBLT via CAN"),
OPENBLT_MANUAL("OpenBLT via Serial", "Manual OpenBLT Update"),
OPENBLT_AUTO("OpenBLT via Serial", "Auto OpenBLT Update"),
DFU_ERASE("DFU erase", "Full DFU Erase"),
UPDATE_CALIBRATIONS("Update calibrations");
final String jobName;
final String persistedValue;
JobType(final String jobName, final String persistedValue) {
this.jobName = jobName;
this.persistedValue = persistedValue;
}
JobType(final String jobName) {
this.jobName = jobName;
this.persistedValue = null;
}
static Optional<JobType> parsePersistedValue(final String persistedValue) {
for (final JobType value: values()) {
final String valuePersistedValue = value.persistedValue;
if ((valuePersistedValue != null) && valuePersistedValue.equals(persistedValue)) {
return Optional.of(value);
}
}
return Optional.empty();
}
}

View File

@ -33,39 +33,24 @@ import static com.devexperts.logging.Logging.getLogging;
import static com.rusefi.SerialPortScanner.SerialPortType.OpenBlt;
import static com.rusefi.core.ui.FrameHelper.appendBundleName;
import static com.rusefi.core.preferences.storage.PersistentConfiguration.getConfig;
import static com.rusefi.maintenance.JobType.*;
import static com.rusefi.ui.util.UiUtils.trueLayout;
public class ProgramSelector {
private static final Logging log = getLogging(ProgramSelector.class);
// todo: migrate to enum?
public static final String DFU_AUTO = "Auto DFU Update";
private static final String DFU_MANUAL = "Manual DFU Update";
private static final String DFU_SWITCH = "Switch to DFU Mode";
private static final String DFU_ERASE = "Full DFU Erase";
private static final String ST_LINK = "ST-LINK Update";
private static final String OPENBLT_SWITCH = "Switch to OpenBLT Mode";
public static final String OPENBLT_MANUAL = "Manual OpenBLT Update";
public static final String OPENBLT_AUTO = "Auto OpenBLT Update";
private static final String INSTALL_OPENBLT = "Install OpenBLT";
private static final String OPENBLT_CAN = "OpenBLT via CAN";
public static final String UPDATE_CALIBRATIONS = "Update Calibrations";
private final JPanel content = new JPanel(new BorderLayout());
private final JLabel noHardware = new JLabel("Nothing detected");
private final JPanel updateModeAndButton = new JPanel(new FlowLayout());
private final JComboBox<String> updateModeComboBox = new JComboBox<>();
private final JComboBox<JobType> updateModeComboBox = new JComboBox<>();
public ProgramSelector(JComboBox<PortResult> comboPorts) {
content.add(updateModeAndButton, BorderLayout.NORTH);
content.add(noHardware, BorderLayout.SOUTH);
String persistedMode = getConfig().getRoot().getProperty(getClass().getSimpleName());
if (Arrays.asList(DFU_AUTO, DFU_MANUAL, OPENBLT_CAN, OPENBLT_SWITCH, OPENBLT_MANUAL, OPENBLT_AUTO, DFU_ERASE, DFU_SWITCH).contains(persistedMode))
updateModeComboBox.setSelectedItem(persistedMode);
parsePersistedValue(persistedMode).ifPresent(updateModeComboBox::setSelectedItem);
JButton updateFirmwareButton = createUpdateFirmwareButton();
@ -76,32 +61,30 @@ public class ProgramSelector {
updateFirmwareButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final String selectedMode = (String) updateModeComboBox.getSelectedItem();
final JobType selectedMode = (JobType) updateModeComboBox.getSelectedItem();
final PortResult selectedPort = ((PortResult) comboPorts.getSelectedItem());
getConfig().getRoot().setProperty(getClass().getSimpleName(), selectedMode);
final String persistedValue = (selectedMode != null ? selectedMode.persistedValue : null);
getConfig().getRoot().setProperty(getClass().getSimpleName(), persistedValue);
executeJob(comboPorts, selectedMode, selectedPort);
}
});
}
public static void executeJob(JComponent parent, String selectedMode, PortResult selectedPort) {
public static void executeJob(JComponent parent, JobType selectedMode, PortResult selectedPort) {
log.info("ProgramSelector " + selectedMode + " " + selectedPort);
String jobName = null;
final String jobName = selectedMode.jobName;
Consumer<UpdateOperationCallbacks> job;
Objects.requireNonNull(selectedMode);
switch (selectedMode) {
case DFU_AUTO:
jobName = "DFU update";
job = (callbacks) -> DfuFlasher.doAutoDfu(parent, selectedPort.port, callbacks);
break;
case DFU_MANUAL:
jobName = "DFU update";
job = DfuFlasher::runDfuProgramming;
break;
case INSTALL_OPENBLT:
jobName = "OpenBLT Initial Programming";
job = DfuFlasher::runOpenBltInitialProgramming;
break;
case ST_LINK:
@ -111,31 +94,24 @@ public class ProgramSelector {
};
break;
case DFU_SWITCH:
jobName = "DFU switch";
job = (callbacks) -> rebootToDfu(parent, selectedPort.port, callbacks);
break;
case OPENBLT_SWITCH:
jobName = "OpenBLT switch";
job = (callbacks) -> rebootToOpenblt(parent, selectedPort.port, callbacks);
break;
case OPENBLT_CAN:
jobName = "OpenBLT via CAN";
job = (callbacks) -> flashOpenBltCan(parent, callbacks);
break;
case OPENBLT_MANUAL:
jobName = "OpenBLT via Serial";
job = (callbacks) -> flashOpenbltSerialJni(parent, selectedPort.port, callbacks);
break;
case OPENBLT_AUTO:
jobName = "OpenBLT via Serial";
job = (callbacks) -> flashOpenbltSerialAutomatic(parent, selectedPort, callbacks);
break;
case DFU_ERASE:
jobName = "DFU erase";
job = DfuFlasher::runDfuEraseAsync;
break;
case UPDATE_CALIBRATIONS:
jobName = "Update calibrations";
job = (callbacks) -> CalibrationsUpdater.INSTANCE.updateCalibrations(
selectedPort.port,
callbacks

View File

@ -30,6 +30,7 @@ import java.util.stream.Collectors;
import static com.devexperts.logging.Logging.getLogging;
import static com.rusefi.FileLog.isWindows;
import static com.rusefi.maintenance.JobType.*;
/**
* Focuses on firmware updater
@ -240,11 +241,11 @@ public class BasicStartupFrame {
if (isObfuscated) {
switch (port.type) {
case EcuWithOpenblt: {
ProgramSelector.executeJob(updateFirmwareButton, ProgramSelector.OPENBLT_AUTO, port);
ProgramSelector.executeJob(updateFirmwareButton, OPENBLT_AUTO, port);
break;
}
case OpenBlt: {
ProgramSelector.executeJob(updateFirmwareButton, ProgramSelector.OPENBLT_MANUAL, port);
ProgramSelector.executeJob(updateFirmwareButton, OPENBLT_MANUAL, port);
break;
}
default: {
@ -253,7 +254,7 @@ public class BasicStartupFrame {
}
}
} else {
ProgramSelector.executeJob(updateFirmwareButton, ProgramSelector.DFU_AUTO, port);
ProgramSelector.executeJob(updateFirmwareButton, DFU_AUTO, port);
}
}, () -> {
log.error("Port to update firmware is not defined.");

View File

@ -14,6 +14,7 @@ import java.io.File;
import java.io.IOException;
import static com.devexperts.logging.Logging.getLogging;
import static com.rusefi.maintenance.JobType.UPDATE_CALIBRATIONS;
public class UpdateCalibrations {
private static final Logging log = getLogging(UpdateCalibrations.class);
@ -32,11 +33,7 @@ public class UpdateCalibrations {
selectedFile.getAbsolutePath()
);
CalibrationsUpdater.INSTANCE.setCalibrationsToUpload(calibrationsImage);
ProgramSelector.executeJob(
parent,
ProgramSelector.UPDATE_CALIBRATIONS,
port
);
ProgramSelector.executeJob(parent, UPDATE_CALIBRATIONS, port);
} catch (final IOException e) {
final String errorMsg = String.format(
"Failed to load calibrations from file %s",