RE TS plugin to have continues tune auto-upload feature #1605

This commit is contained in:
rusefi 2020-07-15 21:00:09 -04:00
parent a78e8f64e1
commit dcb5b34338
4 changed files with 49 additions and 6 deletions

View File

@ -21,7 +21,11 @@ public class UploadResult {
return isError; return isError;
} }
public JSONArray getMessage() { public JSONArray getMessageArray() {
return message; return message;
} }
public String getFirstMessage() {
return message.get(0).toString();
}
} }

View File

@ -63,6 +63,7 @@ public class PluginEntry implements TsPluginBody {
timer.stop(); timer.stop();
timer.setRepeats(false); timer.setRepeats(false);
this.controllerAccessSupplier = controllerAccessSupplier; this.controllerAccessSupplier = controllerAccessSupplier;
UploadQueue.start();
listener = parameterName -> { listener = parameterName -> {
// System.out.println("Parameter value changed " + parameterName); // System.out.println("Parameter value changed " + parameterName);
timer.restart(); timer.restart();

View File

@ -3,12 +3,14 @@ package com.rusefi.ts_plugin;
import com.efiAnalytics.plugin.ecu.ControllerAccess; import com.efiAnalytics.plugin.ecu.ControllerAccess;
import com.rusefi.shared.FileUtil; import com.rusefi.shared.FileUtil;
import com.rusefi.tools.online.Online; import com.rusefi.tools.online.Online;
import com.rusefi.tools.online.UploadResult;
import com.rusefi.tune.xml.Msq; import com.rusefi.tune.xml.Msq;
import com.rusefi.ui.AuthTokenPanel; import com.rusefi.ui.AuthTokenPanel;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
public class UploadQueue { public class UploadQueue {
@ -17,20 +19,36 @@ public class UploadQueue {
private static boolean isStarted; private static boolean isStarted;
private synchronized static void start() { public synchronized static void start() {
if (isStarted) if (isStarted)
return; return;
isStarted = true; isStarted = true;
readOutbox();
new Thread(() -> { new Thread(() -> {
try { try {
postingLoop(); uploadLoop();
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
}, "Positing Thread").start(); }, "Positing Thread").start();
} }
private static void postingLoop() throws InterruptedException { private static void readOutbox() {
for (String file : Objects.requireNonNull(new File(OUTBOX_FOLDER).list((dir, name) -> name.endsWith(".msq")))) {
if (queue.size() > 90)
return;
System.out.println(UploadQueue.class.getSimpleName() + " readOutbox " + file);
try {
Msq msg = Msq.readTune(OUTBOX_FOLDER + File.separator + file);
queue.put(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(UploadQueue.class.getSimpleName() + " readOutbox got " + queue.size());
}
private static void uploadLoop() throws InterruptedException {
while (true) { while (true) {
Msq msq = queue.take(); Msq msq = queue.take();
@ -38,13 +56,32 @@ public class UploadQueue {
String fileName = OUTBOX_FOLDER + File.separator + System.currentTimeMillis() + ".msq"; String fileName = OUTBOX_FOLDER + File.separator + System.currentTimeMillis() + ".msq";
try { try {
msq.writeXmlFile(fileName); msq.writeXmlFile(fileName);
Online.upload(new File(fileName), AuthTokenPanel.getAuthToken()); UploadResult result = Online.upload(new File(fileName), AuthTokenPanel.getAuthToken());
System.out.println("isError " + result.isError());
System.out.println("first " + result.getFirstMessage());
if (result.isError() && result.getFirstMessage().contains("This file already exists")) {
System.out.println(UploadQueue.class.getSimpleName() + " No need to re-try this one");
delete(fileName);
// do not retry this error
continue;
}
if (result.isError()) {
System.out.println(UploadQueue.class.getSimpleName() + " Re-queueing " + msq);
queue.put(msq);
continue;
}
delete(fileName);
} catch (JAXBException | IOException e) { } catch (JAXBException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
private static void delete(String fileName) {
System.out.println(UploadQueue.class.getSimpleName() + " Deleting " + fileName);
new File(fileName).delete();
}
public static void enqueue(ControllerAccess controllerAccess, String configurationName) { public static void enqueue(ControllerAccess controllerAccess, String configurationName) {
start(); start();
if (queue.size() > 100) { if (queue.size() > 100) {

View File

@ -29,6 +29,7 @@ public class UploadView {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
PersistentConfiguration.getConfig().getRoot().setProperty(AUTO_UPLOAD, autoUpload.isSelected()); PersistentConfiguration.getConfig().getRoot().setProperty(AUTO_UPLOAD, autoUpload.isSelected());
PersistentConfiguration.getConfig().save();
} }
}); });
uploadState.setVisible(false); uploadState.setVisible(false);
@ -39,7 +40,7 @@ public class UploadView {
} }
public void setResult(UploadResult result) { public void setResult(UploadResult result) {
uploadState.setText(result.getMessage().get(0).toString()); uploadState.setText(result.getFirstMessage());
uploadState.setVisible(true); uploadState.setVisible(true);
} }