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

This commit is contained in:
rusefi 2020-07-15 20:02:09 -04:00
parent 75ec57a5d4
commit a78e8f64e1
4 changed files with 77 additions and 21 deletions

View File

@ -28,27 +28,30 @@ public class Online {
public static final String outputXmlFileName = FileUtil.RUSEFI_SETTINGS_FOLDER + File.separator + "output.msq";
private static final String url = "https://rusefi.com/online/upload.php";
public static UploadResult upload(File fileName, String authTokenValue) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
FileBody uploadFilePart = new FileBody(fileName);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("upload-file", uploadFilePart);
reqEntity.addPart("rusefi_token", new StringBody(authTokenValue));
httpPost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httpPost);
System.out.println("response=" + response);
System.out.println("code " + response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println("responseString=" + responseString);
JSONParser parser = new JSONParser();
/**
* blocking call for http file upload
*/
public static UploadResult upload(File fileName, String authTokenValue) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
FileBody uploadFilePart = new FileBody(fileName);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("upload-file", uploadFilePart);
reqEntity.addPart("rusefi_token", new StringBody(authTokenValue));
httpPost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httpPost);
System.out.println("response=" + response);
System.out.println("code " + response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println("responseString=" + responseString);
JSONParser parser = new JSONParser();
JSONObject object = (JSONObject) parser.parse(responseString);
System.out.println("object=" + object);
JSONArray info = (JSONArray) object.get("info");
@ -61,7 +64,7 @@ public class Online {
return new UploadResult(false, info);
}
} catch (ParseException e) {
} catch (IOException | ParseException e) {
return new UploadResult(true, "Error " + e);
}
}

View File

@ -67,6 +67,7 @@ public class PluginEntry implements TsPluginBody {
// System.out.println("Parameter value changed " + parameterName);
timer.restart();
if (UploadView.isAutoUpload()) {
System.out.println("enqueue tune");
UploadQueue.enqueue(controllerAccessSupplier.get(), currentConfiguration);
}
};

View File

@ -1,10 +1,61 @@
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.tune.xml.Msq;
import com.rusefi.ui.AuthTokenPanel;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingDeque;
public class UploadQueue {
public static final String OUTBOX_FOLDER = FileUtil.RUSEFI_SETTINGS_FOLDER + File.separator + "outbox";
private static LinkedBlockingDeque<Msq> queue = new LinkedBlockingDeque<>(128);
private static boolean isStarted;
private synchronized static void start() {
if (isStarted)
return;
isStarted = true;
new Thread(() -> {
try {
postingLoop();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}, "Positing Thread").start();
}
private static void postingLoop() throws InterruptedException {
while (true) {
Msq msq = queue.take();
new File(OUTBOX_FOLDER).mkdirs();
String fileName = OUTBOX_FOLDER + File.separator + System.currentTimeMillis() + ".msq";
try {
msq.writeXmlFile(fileName);
Online.upload(new File(fileName), AuthTokenPanel.getAuthToken());
} catch (JAXBException | IOException e) {
e.printStackTrace();
}
}
}
public static void enqueue(ControllerAccess controllerAccess, String configurationName) {
start();
if (queue.size() > 100) {
// too much pending drama
return;
}
Msq msq = TuneUploder.grabTune(controllerAccess, configurationName);
try {
queue.put(msq);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}

View File

@ -22,6 +22,7 @@ public class UploadView {
content.add(projectWarning);
content.add(tuneInfo);
content.add(uploadState);
content.add(autoUpload);
autoUpload.setSelected(isAutoUpload());
autoUpload.addActionListener(new AbstractAction() {