auth token commands, better help

This commit is contained in:
rusefi 2020-05-31 10:36:20 -04:00
parent 63eb642ee0
commit 12befe92ab
6 changed files with 95 additions and 53 deletions

View File

@ -3,6 +3,6 @@ package com.rusefi;
import java.util.concurrent.atomic.AtomicReference;
public class rusEFIVersion {
public static final int CONSOLE_VERSION = 20200530;
public static final int CONSOLE_VERSION = 20200531;
public static AtomicReference<String> firmwareVersion = new AtomicReference<>("N/A");
}

View File

@ -25,6 +25,7 @@ import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@ -182,6 +183,9 @@ public class Launcher extends rusEFIVersion {
* @see StartupFrame if no parameters specified
*/
public static void main(final String[] args) throws Exception {
System.out.println("rusEfi UI console " + CONSOLE_VERSION);
System.out.println("Compiled " + new Date(ConsoleTools.classBuildTimeMillis()));
System.out.println("\n\n");
if (ConsoleTools.runTool(args)) {
return;
@ -189,11 +193,8 @@ public class Launcher extends rusEFIVersion {
ConsoleTools.printTools();
System.out.println("Starting rusEfi UI console " + CONSOLE_VERSION);
FileLog.MAIN.start();
getConfig().load();
FileLog.suspendLogging = getConfig().getRoot().getBoolProperty(GaugesPanel.DISABLE_LOGS);
Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler());

View File

@ -15,72 +15,59 @@ import com.rusefi.io.serial.SerialIoStreamJSerialComm;
import com.rusefi.maintenance.ExecHelper;
import com.rusefi.tools.online.Online;
import com.rusefi.tune.xml.Msq;
import com.rusefi.ui.OnlineTab;
import org.jetbrains.annotations.Nullable;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
public class ConsoleTools {
public static final String SET_AUTH_TOKEN = "set_auth_token";
private static Map<String, ConsoleTool> TOOLS = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private static Map<String, String> toolsHelp = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
static {
TOOLS.put("help", args -> printTools());
TOOLS.put("headless", ConsoleTools::runHeadless);
TOOLS.put("compile", ConsoleTools::invokeCompileExpressionTool);
TOOLS.put("ptrace_enums", ConsoleTools::runPerfTraceTool);
TOOLS.put("save_binary_configuration", ConsoleTools::saveBinaryConfig);
TOOLS.put("functional_test", ConsoleTools::runFunctionalTest);
TOOLS.put("compile_fsio_file", ConsoleTools::runCompileTool);
TOOLS.put("firing_order", ConsoleTools::runFiringOrderTool);
TOOLS.put("convert_binary_configuration_to_xml", ConsoleTools::convertBinaryToXml);
TOOLS.put("reboot_ecu", args -> sendCommand(Fields.CMD_REBOOT));
TOOLS.put(Fields.CMD_REBOOT_DFU, args -> sendCommand(Fields.CMD_REBOOT_DFU));
TOOLSput("help", args -> printTools(), "Print this help.");
TOOLSput("headless", ConsoleTools::runHeadless, "Connect to rusEFI controller and start saving logs.");
TOOLSput("ptrace_enums", ConsoleTools::runPerfTraceTool, "NOT A USER TOOL. Development tool to process pefrormance trace enums");
TOOLSput("firing_order", ConsoleTools::runFiringOrderTool, "NOT A USER TOOL. Development tool relating to adding new firing order into rusEFI firmware.");
TOOLSput("functional_test", ConsoleTools::runFunctionalTest, "NOT A USER TOOL. Development tool related to functional testing");
TOOLSput("convert_binary_configuration_to_xml", ConsoleTools::convertBinaryToXml, "NOT A USER TOOL. Development tool to convert binary configuration into XML form.");
TOOLSput("compile_fsio_line", ConsoleTools::invokeCompileExpressionTool, "Convert a line to RPN form.");
TOOLSput("compile_fsio_file", ConsoleTools::runCompileTool, "Convert all lines from a file to RPN form.");
TOOLSput("print_auth_token", args -> printAuthToken(), "Print current rusEFI Online authentication token.");
TOOLSput(SET_AUTH_TOKEN, ConsoleTools::setAuthToken, "Set rusEFI authentication token.");
TOOLSput("reboot_ecu", args -> sendCommand(Fields.CMD_REBOOT), "Sends a command to reboot rusEFI controller.");
TOOLSput(Fields.CMD_REBOOT_DFU, args -> sendCommand(Fields.CMD_REBOOT_DFU), "Sends a command to switch rusEFI controller into DFU mode.");
}
private static void TOOLSput(String command, ConsoleTool callback, String help) {
TOOLS.put(command, callback);
toolsHelp.put(command, help);
}
public static void printTools() {
for (String key : TOOLS.keySet()) {
System.out.println("Tool available: " + key);
}
}
private static void saveBinaryConfig(String[] args) throws IOException {
if (args.length < 2) {
System.out.println("Please specify output file name for binary configuration");
System.exit(-1);
}
String fileName = args[1];
String autoDetectedPort = autoDetectPort();
if (autoDetectedPort == null)
return;
LinkManager.startAndConnect(autoDetectedPort, new ConnectionStateListener() {
@Override
public void onConnectionEstablished() {
BinaryProtocol binaryProtocol = LinkManager.connector.getBinaryProtocol();
Objects.requireNonNull(binaryProtocol, "binaryProtocol");
ConfigurationImage configurationImage = binaryProtocol.getControllerConfiguration();
Objects.requireNonNull(configurationImage, "configurationImage");
try {
ConfigurationImageFile.saveToFile(configurationImage, fileName);
System.exit(0);
} catch (IOException e) {
System.out.println("While writing " + e);
System.exit(-1);
}
String help = toolsHelp.get(key);
if (help != null) {
System.out.println("\t" + help);
System.out.println("\n");
}
@Override
public void onConnectionFailed() {
System.out.println("onConnectionFailed");
System.exit(-1);
}
});
}
}
private static void sendCommand(String command) throws IOException {
@ -106,6 +93,21 @@ public class ConsoleTools {
System.exit(returnCode);
}
private static void setAuthToken(String[] args) {
String newToken = args[1];
getConfig().getRoot().setProperty(OnlineTab.AUTH_TOKEN, newToken);
}
private static void printAuthToken() {
String authToken = getConfig().getRoot().getProperty(OnlineTab.AUTH_TOKEN);
if (authToken.trim().isEmpty()) {
System.out.println("Auth token not defined. Please use " + SET_AUTH_TOKEN + " command");
System.out.println("\tPlease see https://github.com/rusefi/rusefi/wiki/Online");
return;
}
System.out.println("Auth token: " + authToken);
}
private static void runFunctionalTest(String[] args) throws InterruptedException {
// passing port argument if it was specified
String[] toolArgs = args.length == 1 ? new String[0] : new String[]{args[1]};
@ -214,6 +216,26 @@ public class ConsoleTools {
Online.upload(new File(Msq.outputXmlFileName), "x");
}
public static long classBuildTimeMillis() throws URISyntaxException, IllegalStateException, IllegalArgumentException {
Class<?> clazz = ConsoleTools.class;
URL resource = clazz.getResource(clazz.getSimpleName() + ".class");
if (resource == null) {
throw new IllegalStateException("Failed to find class file for class: " +
clazz.getName());
}
if (resource.getProtocol().equals("file")) {
return new File(resource.toURI()).lastModified();
} else if (resource.getProtocol().equals("jar")) {
String path = resource.getPath();
return new File(path.substring(5, path.indexOf("!"))).lastModified();
} else {
throw new IllegalArgumentException("Unhandled url protocol: " +
resource.getProtocol() + " for class: " +
clazz.getName() + " resource: " + resource.toString());
}
}
interface ConsoleTool {
void runTool(String args[]) throws Exception;
}

View File

@ -17,7 +17,7 @@ import java.io.IOException;
import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;
public class OnlineTab {
private static final String AUTH_TOKEN = "auth_token";
public static final String AUTH_TOKEN = "auth_token";
private static final String TOKEN_WARNING = "Please copy token from your forum profile";
private final JPanel content = new JPanel(new VerticalFlowLayout());

View File

@ -6,6 +6,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
/**
* @see PersistentConfiguration
*/
public class Node {
private String prefix;
private Map<String, Object> config = new HashMap<>();

View File

@ -13,34 +13,50 @@ public class PersistentConfiguration {
private static final String CONFIG_FILE_NAME = "rusefi_console_properties.xml";
private Map<String, Object> config = new HashMap<>();
private boolean isLoaded;
public static PersistentConfiguration getConfig() {
return INSTANCE;
}
private PersistentConfiguration() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> getConfig().save()));
}
@SuppressWarnings("unchecked")
public void load() {
try {
XMLDecoder e = new XMLDecoder(new BufferedInputStream(new FileInputStream(CONFIG_FILE_NAME)));
config = (Map<String, Object>) e.readObject();
e.close();
System.out.println("Got configuration from " + CONFIG_FILE_NAME);
} catch (Throwable e) {
FileLog.MAIN.logLine("Console configuration not found " + CONFIG_FILE_NAME + ", using defaults");
}
isLoaded = true;
}
/**
* todo: maybe the shutdown hook is the only place where this method should be invoked?
*/
public void save() {
if (!isLoaded) {
// settings not loaded, nothing to save
return;
}
try {
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(CONFIG_FILE_NAME)));
e.writeObject(config);
e.close();
System.out.println("Saved to " + CONFIG_FILE_NAME);
System.out.println("Saved settings to " + CONFIG_FILE_NAME);
} catch (FileNotFoundException e1) {
FileLog.MAIN.logLine("Error saving " + CONFIG_FILE_NAME);
}
}
public Node getRoot() {
if (!isLoaded)
load();
return new Node("root", config);
}
}