only:refactoring: part of functionality is extracted into `JobWithSuspendedSerialPortScanner` class

This commit is contained in:
kifir23917 2024-09-21 03:05:26 +03:00 committed by rusefillc
parent 964d28a9f6
commit 9c397d07f1
3 changed files with 80 additions and 57 deletions

View File

@ -23,71 +23,55 @@ public enum CalibrationsUpdater {
) {
if (calibrationsImage != null) {
final int calibrationsImageSize = calibrationsImage.getSize();
try {
callbacks.logLine("Suspending port scanning...");
try (LinkManager linkManager = new LinkManager()
.setNeedPullText(false)
.setNeedPullLiveData(true)) {
callbacks.logLine(String.format("Connecting to port %s...", port));
try {
SerialPortScanner.INSTANCE.suspend().await(1, TimeUnit.MINUTES);
callbacks.logLine("Port scanning is suspended.");
linkManager.connect(port).await(60, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
callbacks.logLine("Failed to suspend port scanning in a minute.");
final String errorMsg = String.format("Failed to connect to port %s", port);
log.error(errorMsg, e);
callbacks.logLine(errorMsg);
callbacks.error();
return;
}
try (LinkManager linkManager = new LinkManager()
.setNeedPullText(false)
.setNeedPullLiveData(true)) {
callbacks.logLine(String.format("Connecting to port %s...", port));
try {
linkManager.connect(port).await(60, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
final String errorMsg = String.format("Failed to connect to port %s", port);
log.error(errorMsg, e);
callbacks.logLine(errorMsg);
callbacks.error();
return;
}
callbacks.logLine(String.format(
"Updating configuration image (%d bytes) to port %s...",
calibrationsImageSize,
port
));
final CountDownLatch latch = new CountDownLatch(1);
linkManager.execute(() -> {
linkManager.getBinaryProtocol().uploadChanges(calibrationsImage);
latch.countDown();
});
try {
if (!latch.await(1, TimeUnit.MINUTES)) {
callbacks.logLine(String.format(
"Failed to update configuration image (%d bytes) to port %s in a minute",
calibrationsImageSize,
port
));
callbacks.error();
} else {
callbacks.logLine(String.format(
"Configuration image (%d bytes) has been uploaded to port %s",
calibrationsImageSize,
port
));
callbacks.done();
}
} catch (final InterruptedException e) {
final String errorMsg = String.format(
"Updating calibrations to port %s was interrupted",
callbacks.logLine(String.format(
"Updating configuration image (%d bytes) to port %s...",
calibrationsImageSize,
port
));
final CountDownLatch latch = new CountDownLatch(1);
linkManager.execute(() -> {
linkManager.getBinaryProtocol().uploadChanges(calibrationsImage);
latch.countDown();
});
try {
if (!latch.await(1, TimeUnit.MINUTES)) {
callbacks.logLine(String.format(
"Failed to update configuration image (%d bytes) to port %s in a minute",
calibrationsImageSize,
port
);
log.error(errorMsg, e);
callbacks.logLine(errorMsg);
));
callbacks.error();
} else {
callbacks.logLine(String.format(
"Configuration image (%d bytes) has been uploaded to port %s",
calibrationsImageSize,
port
));
callbacks.done();
}
} catch (final InterruptedException e) {
final String errorMsg = String.format(
"Updating calibrations to port %s was interrupted",
port
);
log.error(errorMsg, e);
callbacks.logLine(errorMsg);
callbacks.error();
}
} finally {
callbacks.logLine("Resuming port scanning...");
SerialPortScanner.INSTANCE.resume();
callbacks.logLine("Port scanning is resumed.");
}
} else {
callbacks.logLine("ERROR: Calibrations to update are undefined");

View File

@ -0,0 +1,36 @@
package com.rusefi.maintenance.jobs;
import com.rusefi.SerialPortScanner;
import com.rusefi.io.UpdateOperationCallbacks;
import java.util.concurrent.TimeUnit;
public class JobWithSuspendedSerialPortScanner extends AsyncJob {
private final AsyncJob innerJob;
public JobWithSuspendedSerialPortScanner(final AsyncJob innerJob) {
super(innerJob.getName());
this.innerJob = innerJob;
}
@Override
public void doJob(UpdateOperationCallbacks callbacks) {
try {
callbacks.logLine("Suspending port scanning...");
try {
SerialPortScanner.INSTANCE.suspend().await(1, TimeUnit.MINUTES);
callbacks.logLine("Port scanning is suspended.");
} catch (final InterruptedException e) {
callbacks.logLine("Failed to suspend port scanning in a minute.");
callbacks.error();
return;
}
innerJob.doJob(callbacks);
} finally {
callbacks.logLine("Resuming port scanning...");
SerialPortScanner.INSTANCE.resume();
callbacks.logLine("Port scanning is resumed.");
}
}
}

View File

@ -6,6 +6,7 @@ import com.opensr5.io.ConfigurationImageFile;
import com.rusefi.SerialPortScanner;
import com.rusefi.core.preferences.storage.PersistentConfiguration;
import com.rusefi.maintenance.ProgramSelector;
import com.rusefi.maintenance.jobs.JobWithSuspendedSerialPortScanner;
import com.rusefi.maintenance.jobs.UpdateCalibrationsJob;
import javax.swing.*;
@ -31,7 +32,9 @@ public class UpdateCalibrations {
final ConfigurationImage calibrationsImage = ConfigurationImageFile.readFromFile(
selectedFile.getAbsolutePath()
);
ProgramSelector.executeJob(new UpdateCalibrationsJob(port, calibrationsImage));
ProgramSelector.executeJob(
new JobWithSuspendedSerialPortScanner(new UpdateCalibrationsJob(port, calibrationsImage))
);
} catch (final IOException e) {
final String errorMsg = String.format(
"Failed to load calibrations from file %s",