From 7a77e53c598e2f8f735875d0915e66682cc33709 Mon Sep 17 00:00:00 2001 From: rusefi Date: Wed, 15 Jul 2020 00:23:43 -0400 Subject: [PATCH] RE TS plugin to have continues tune auto-upload feature #1605 --- .../com/rusefi/ts_plugin/MetaDataCache.java | 36 +++++++++++++++++++ .../src/com/rusefi/ts_plugin/PluginEntry.java | 20 +++++------ .../src/com/rusefi/ts_plugin/TuneUploder.java | 36 +++++++++++++------ .../src/com/rusefi/ts_plugin/UploadQueue.java | 10 ++++++ .../src/com/rusefi/ts_plugin/UploadView.java | 3 +- .../com/rusefi/ts_plugin/TsPluginBody.java | 3 +- 6 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 java_tools/ts_plugin/src/com/rusefi/ts_plugin/MetaDataCache.java create mode 100644 java_tools/ts_plugin/src/com/rusefi/ts_plugin/UploadQueue.java diff --git a/java_tools/ts_plugin/src/com/rusefi/ts_plugin/MetaDataCache.java b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/MetaDataCache.java new file mode 100644 index 0000000000..776ca9a7d5 --- /dev/null +++ b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/MetaDataCache.java @@ -0,0 +1,36 @@ +package com.rusefi.ts_plugin; + +import com.opensr5.ini.IniFileMetaInfo; +import com.opensr5.ini.RawIniFile; +import com.rusefi.TsTuneReader; +import org.jetbrains.annotations.Nullable; + +import java.io.FileNotFoundException; + +public class MetaDataCache { + + private static String cachedProjectName; + private static IniFileMetaInfo cache; + + @Nullable + public synchronized static IniFileMetaInfo getModel(String projectName) { + if (projectName == null) + return null; + if (!projectName.equals(cachedProjectName)) { + cache = null; + } + if (cache == null) { + System.out.println("Reading meta " + projectName); + String modeFileName = TsTuneReader.getProjectModeFileName(projectName); + try { + cache = new IniFileMetaInfo(RawIniFile.read(modeFileName)); + } catch (FileNotFoundException e) { + System.out.println("No luck reading " + modeFileName + ": " + e); + return null; + } + cachedProjectName = projectName; + } + return cache; + } + +} diff --git a/java_tools/ts_plugin/src/com/rusefi/ts_plugin/PluginEntry.java b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/PluginEntry.java index aae6e2fe02..9fec24ef0f 100644 --- a/java_tools/ts_plugin/src/com/rusefi/ts_plugin/PluginEntry.java +++ b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/PluginEntry.java @@ -14,7 +14,6 @@ import com.rusefi.ts_plugin.util.ManifestHelper; import com.rusefi.tune.xml.Constant; import com.rusefi.tune.xml.Msq; import com.rusefi.ui.AuthTokenPanel; -import com.rusefi.ui.storage.PersistentConfiguration; import com.rusefi.ui.util.URLLabel; import org.apache.http.concurrent.FutureCallback; import org.putgemin.VerticalFlowLayout; @@ -50,13 +49,7 @@ public class PluginEntry implements TsPluginBody { } }); - ControllerParameterChangeListener listener = new ControllerParameterChangeListener() { - @Override - public void parameterValueChanged(String parameterName) { - System.out.println("Parameter value changed " + parameterName); - timer.restart(); - } - }; + private final ControllerParameterChangeListener listener; /** * the real constructor - this one is invoked via reflection @@ -70,6 +63,13 @@ public class PluginEntry implements TsPluginBody { timer.stop(); timer.setRepeats(false); this.controllerAccessSupplier = controllerAccessSupplier; + listener = parameterName -> { + // System.out.println("Parameter value changed " + parameterName); + timer.restart(); + if (UploadView.isAutoUpload()) { + UploadQueue.enqueue(controllerAccessSupplier.get(), currentConfiguration); + } + }; upload.setBackground(new Color(0x90EE90)); new Thread(new Runnable() { @@ -205,11 +205,11 @@ public class PluginEntry implements TsPluginBody { public JComponent getContent() { return content; } - +/* public void close() { PersistentConfiguration.getConfig().save(); } - +*/ private String getConfigurationName() { ControllerAccess controllerAccess = controllerAccessSupplier.get(); if (controllerAccess == null) { diff --git a/java_tools/ts_plugin/src/com/rusefi/ts_plugin/TuneUploder.java b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/TuneUploder.java index e50f94c5fd..06b0c3cc8d 100644 --- a/java_tools/ts_plugin/src/com/rusefi/ts_plugin/TuneUploder.java +++ b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/TuneUploder.java @@ -4,12 +4,14 @@ import com.efiAnalytics.plugin.ecu.ControllerAccess; import com.efiAnalytics.plugin.ecu.ControllerException; import com.efiAnalytics.plugin.ecu.ControllerParameter; import com.efiAnalytics.plugin.ecu.servers.ControllerParameterServer; +import com.opensr5.ini.IniFileMetaInfo; import com.rusefi.TsTuneReader; import com.rusefi.tools.online.Online; import com.rusefi.tune.xml.Constant; import com.rusefi.tune.xml.Msq; import com.rusefi.tune.xml.Page; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.xml.bind.JAXBException; import java.io.IOException; @@ -17,12 +19,29 @@ import java.util.Collections; import java.util.Map; import java.util.Objects; import java.util.TreeMap; -import com.rusefi.config.generated.Fields; public class TuneUploder { static Msq writeCurrentTune(ControllerAccess controllerAccess, String configurationName) { + Msq msq = grabTune(controllerAccess, configurationName); + if (msq == null) + return null; + try { + String fileName = Online.outputXmlFileName; + msq.writeXmlFile(fileName); + return msq; + } catch (JAXBException | IOException e) { + System.out.println("Error writing XML: " + e); + return null; + } + } + + @Nullable + public static Msq grabTune(ControllerAccess controllerAccess, String configurationName) { Objects.requireNonNull(controllerAccess, "controllerAccess"); - Msq msq = Msq.create(Fields.TOTAL_CONFIG_SIZE, Fields.TS_SIGNATURE); + IniFileMetaInfo meta = MetaDataCache.getModel(configurationName); + if (meta == null) + return null; + Msq msq = Msq.create(meta.getTotalSize(), meta.getSignature()); ControllerParameterServer controllerParameterServer = controllerAccess.getControllerParameterServer(); Objects.requireNonNull(controllerParameterServer, "controllerParameterServer"); @@ -31,16 +50,13 @@ public class TuneUploder { try { String[] parameterNames = controllerParameterServer.getParameterNames(configurationName); for (String parameterName : parameterNames) { - handleParameter(configurationName, msq, controllerParameterServer, fileSystemValues, parameterName); + applyParameterValue(configurationName, msq, controllerParameterServer, fileSystemValues, parameterName); } - - String fileName = Online.outputXmlFileName; - msq.writeXmlFile(fileName); - return msq; - } catch (JAXBException | IOException | ControllerException e) { - System.out.println("Error writing XML: " + e); + } catch (ControllerException e) { + System.out.println("Error saving configuration: " + e); return null; } + return msq; } @NotNull @@ -60,7 +76,7 @@ public class TuneUploder { return byName; } - private static void handleParameter(String configurationName, Msq msq, ControllerParameterServer controllerParameterServer, Map byName, String parameterName) throws ControllerException { + private static void applyParameterValue(String configurationName, Msq msq, ControllerParameterServer controllerParameterServer, Map byName, String parameterName) throws ControllerException { ControllerParameter cp = controllerParameterServer.getControllerParameter(configurationName, parameterName); Objects.requireNonNull(cp, "ControllerParameter"); String type = cp.getParamClass(); diff --git a/java_tools/ts_plugin/src/com/rusefi/ts_plugin/UploadQueue.java b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/UploadQueue.java new file mode 100644 index 0000000000..a7bd7e166a --- /dev/null +++ b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/UploadQueue.java @@ -0,0 +1,10 @@ +package com.rusefi.ts_plugin; + +import com.efiAnalytics.plugin.ecu.ControllerAccess; +import com.rusefi.tune.xml.Msq; + +public class UploadQueue { + public static void enqueue(ControllerAccess controllerAccess, String configurationName) { + Msq msq = TuneUploder.grabTune(controllerAccess, configurationName); + } +} diff --git a/java_tools/ts_plugin/src/com/rusefi/ts_plugin/UploadView.java b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/UploadView.java index ae529f4074..ffc397207b 100644 --- a/java_tools/ts_plugin/src/com/rusefi/ts_plugin/UploadView.java +++ b/java_tools/ts_plugin/src/com/rusefi/ts_plugin/UploadView.java @@ -2,7 +2,6 @@ package com.rusefi.ts_plugin; import com.rusefi.tools.online.UploadResult; import com.rusefi.ui.storage.PersistentConfiguration; -import org.json.simple.JSONArray; import org.putgemin.VerticalFlowLayout; import javax.swing.*; @@ -34,7 +33,7 @@ public class UploadView { uploadState.setVisible(false); } - private static boolean isAutoUpload() { + public static boolean isAutoUpload() { return PersistentConfiguration.getConfig().getRoot().getBoolProperty(AUTO_UPLOAD, false); } diff --git a/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/TsPluginBody.java b/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/TsPluginBody.java index 7a48b3d074..2757e173f7 100644 --- a/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/TsPluginBody.java +++ b/java_tools/ts_plugin_launcher/src/com/rusefi/ts_plugin/TsPluginBody.java @@ -6,6 +6,7 @@ public interface TsPluginBody { String GET_VERSION = "getVersion"; JComponent getContent(); - +/* void close(); + */ }