RE TS plugin to have continues tune auto-upload feature #1605
This commit is contained in:
parent
a78e8f64e1
commit
dcb5b34338
|
@ -21,7 +21,11 @@ public class UploadResult {
|
|||
return isError;
|
||||
}
|
||||
|
||||
public JSONArray getMessage() {
|
||||
public JSONArray getMessageArray() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String getFirstMessage() {
|
||||
return message.get(0).toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,6 +63,7 @@ public class PluginEntry implements TsPluginBody {
|
|||
timer.stop();
|
||||
timer.setRepeats(false);
|
||||
this.controllerAccessSupplier = controllerAccessSupplier;
|
||||
UploadQueue.start();
|
||||
listener = parameterName -> {
|
||||
// System.out.println("Parameter value changed " + parameterName);
|
||||
timer.restart();
|
||||
|
|
|
@ -3,12 +3,14 @@ package com.rusefi.ts_plugin;
|
|||
import com.efiAnalytics.plugin.ecu.ControllerAccess;
|
||||
import com.rusefi.shared.FileUtil;
|
||||
import com.rusefi.tools.online.Online;
|
||||
import com.rusefi.tools.online.UploadResult;
|
||||
import com.rusefi.tune.xml.Msq;
|
||||
import com.rusefi.ui.AuthTokenPanel;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
public class UploadQueue {
|
||||
|
@ -17,20 +19,36 @@ public class UploadQueue {
|
|||
|
||||
private static boolean isStarted;
|
||||
|
||||
private synchronized static void start() {
|
||||
public synchronized static void start() {
|
||||
if (isStarted)
|
||||
return;
|
||||
isStarted = true;
|
||||
readOutbox();
|
||||
new Thread(() -> {
|
||||
try {
|
||||
postingLoop();
|
||||
uploadLoop();
|
||||
} catch (InterruptedException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}, "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) {
|
||||
Msq msq = queue.take();
|
||||
|
||||
|
@ -38,13 +56,32 @@ public class UploadQueue {
|
|||
String fileName = OUTBOX_FOLDER + File.separator + System.currentTimeMillis() + ".msq";
|
||||
try {
|
||||
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) {
|
||||
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) {
|
||||
start();
|
||||
if (queue.size() > 100) {
|
||||
|
|
|
@ -29,6 +29,7 @@ public class UploadView {
|
|||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
PersistentConfiguration.getConfig().getRoot().setProperty(AUTO_UPLOAD, autoUpload.isSelected());
|
||||
PersistentConfiguration.getConfig().save();
|
||||
}
|
||||
});
|
||||
uploadState.setVisible(false);
|
||||
|
@ -39,7 +40,7 @@ public class UploadView {
|
|||
}
|
||||
|
||||
public void setResult(UploadResult result) {
|
||||
uploadState.setText(result.getMessage().get(0).toString());
|
||||
uploadState.setText(result.getFirstMessage());
|
||||
uploadState.setVisible(true);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue