REO progress
This commit is contained in:
parent
6c75f48beb
commit
ddc96bbb25
|
@ -3,6 +3,7 @@ package com.rusefi.tune.xml;
|
|||
import com.opensr5.ConfigurationImage;
|
||||
import com.opensr5.ini.IniFileModel;
|
||||
import com.opensr5.ini.field.IniField;
|
||||
import com.rusefi.config.generated.Fields;
|
||||
import com.rusefi.ui.storage.PersistentConfiguration;
|
||||
import com.rusefi.xml.XmlUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -11,6 +12,7 @@ import javax.xml.bind.JAXBException;
|
|||
import javax.xml.bind.annotation.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
@XmlRootElement
|
||||
public class Msq {
|
||||
|
@ -25,7 +27,7 @@ public class Msq {
|
|||
}
|
||||
|
||||
@NotNull
|
||||
public static Msq toMsq(ConfigurationImage image) {
|
||||
public static Msq valueOf(ConfigurationImage image) {
|
||||
IniFileModel ini = IniFileModel.getInstance();
|
||||
Msq tune = new Msq();
|
||||
for (String key : ini.allIniFields.keySet())
|
||||
|
@ -33,6 +35,25 @@ public class Msq {
|
|||
return tune;
|
||||
}
|
||||
|
||||
public ConfigurationImage asImage(IniFileModel instance) {
|
||||
ConfigurationImage ci = new ConfigurationImage(Fields.TOTAL_CONFIG_SIZE);
|
||||
|
||||
for (Constant constant : getPage().constant) {
|
||||
if (constant.getName().startsWith("UNALLOCATED_SPACE")) {
|
||||
continue;
|
||||
}
|
||||
IniField field = instance.allIniFields.get(constant.getName());
|
||||
Objects.requireNonNull(field, "Field for " + constant.getName());
|
||||
System.out.println("Setting " + field);
|
||||
field.setValue(ci, constant);
|
||||
}
|
||||
return ci;
|
||||
}
|
||||
|
||||
public static Msq readTune(String fileName) throws Exception {
|
||||
return XmlUtil.readModel(Msq.class, fileName);
|
||||
}
|
||||
|
||||
public void writeXmlFile(String outputXmlFileName) throws JAXBException, IOException {
|
||||
XmlUtil.writeXml(this, Msq.class, outputXmlFileName);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,9 @@ import java.io.IOException;
|
|||
import java.io.StringWriter;
|
||||
|
||||
public class XmlUtil {
|
||||
static {
|
||||
XmlUtil.setParserImpl();
|
||||
}
|
||||
|
||||
public static void writeXml(Object instance, Class<?> modelClass, String fileName) throws JAXBException, IOException {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(modelClass);
|
||||
|
@ -37,7 +40,7 @@ public class XmlUtil {
|
|||
/**
|
||||
* See https://stackoverflow.com/questions/25644023/error-unmarshalling-xml-in-java-8-secure-processing-org-xml-sax-saxnotrecognize
|
||||
*/
|
||||
public static void setParserImpl() {
|
||||
private static void setParserImpl() {
|
||||
System.setProperty("org.xml.sax.driver", "com.sun.org.apache.xerces.internal.parsers.SAXParser");
|
||||
System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
|
||||
System.setProperty("javax.xml.parsers.SAXParserFactory","com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
|
||||
|
|
|
@ -345,7 +345,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
|
|||
}
|
||||
try {
|
||||
ConfigurationImageFile.saveToFile(image, CONFIGURATION_RUSEFI_BINARY);
|
||||
Msq tune = Msq.toMsq(image);
|
||||
Msq tune = Msq.valueOf(image);
|
||||
tune.writeXmlFile(CONFIGURATION_RUSEFI_XML);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Ignoring " + e);
|
||||
|
|
|
@ -224,7 +224,7 @@ public class ConsoleTools {
|
|||
ConfigurationImage image = ConfigurationImageFile.readFromFile(inputBinaryFileName);
|
||||
System.out.println("Got " + image.getSize() + " of configuration from " + inputBinaryFileName);
|
||||
|
||||
Msq tune = Msq.toMsq(image);
|
||||
Msq tune = Msq.valueOf(image);
|
||||
tune.writeXmlFile(Msq.outputXmlFileName);
|
||||
String authToken = AuthTokenPanel.getAuthToken();
|
||||
System.out.println("Using " + authToken);
|
||||
|
|
|
@ -25,7 +25,7 @@ public class OnlineTab {
|
|||
upload.addActionListener(new AbstractAction() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Msq tune = Msq.toMsq(LinkManager.connector.getBinaryProtocol().getControllerConfiguration());
|
||||
Msq tune = Msq.valueOf(LinkManager.connector.getBinaryProtocol().getControllerConfiguration());
|
||||
Online.uploadTune(tune, authTokenPanel, content);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -4,42 +4,63 @@ import com.opensr5.ConfigurationImage;
|
|||
import com.opensr5.ini.IniFileModel;
|
||||
import com.opensr5.ini.field.IniField;
|
||||
import com.opensr5.io.ConfigurationImageFile;
|
||||
import com.rusefi.config.generated.Fields;
|
||||
import com.rusefi.tune.xml.Constant;
|
||||
import com.rusefi.tune.xml.Msq;
|
||||
import com.rusefi.xml.XmlUtil;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* from IDEA this unit test needs to be exectuted with "empty" working directory
|
||||
* from IDEA this unit test needs to be executed with "empty" working directory
|
||||
*/
|
||||
public class TuneReadWriteTest {
|
||||
private static final String PATH = "ui/src/test/resources/frankenso/";
|
||||
private static final String TEST_INI = PATH + "mainController.ini";
|
||||
private static final String TEST_BINARY_FILE = PATH + "current_configuration.rusefi_binary";
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
IniFileModel.getInstance().readIniFile(TEST_INI);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadTsTune() throws Exception {
|
||||
XmlUtil.setParserImpl();
|
||||
|
||||
IniFileModel.getInstance().readIniFile(PATH + "mainController.ini");
|
||||
Msq tsTune = XmlUtil.readModel(Msq.class, PATH + "CurrentTune.msq");
|
||||
public void testCompareBinaryToTSTune() throws Exception {
|
||||
Msq tsTune = Msq.readTune(PATH + "CurrentTune.msq");
|
||||
System.out.println(tsTune);
|
||||
|
||||
ConfigurationImage tsBinaryData = makeBinaryTune(tsTune, IniFileModel.getInstance());
|
||||
ConfigurationImage tsBinaryData = tsTune.asImage(IniFileModel.getInstance());
|
||||
|
||||
System.out.println("Reading " + TEST_BINARY_FILE);
|
||||
ConfigurationImage fileBinaryData = ConfigurationImageFile.readFromFile(TEST_BINARY_FILE);
|
||||
|
||||
String binary = PATH + "current_configuration.rusefi_binary";
|
||||
System.out.println("Reading " + binary);
|
||||
ConfigurationImage fileBinaryData = ConfigurationImageFile.readFromFile(binary);
|
||||
int mismatchCounter = compareImages(tsBinaryData, fileBinaryData);
|
||||
// todo: why one byte mismatch? since it's in floats I kind of do not care, floats are weird
|
||||
assertEquals(1, mismatchCounter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteAndReadTSTune() throws Exception {
|
||||
ConfigurationImage originalBinaryData = ConfigurationImageFile.readFromFile(TEST_BINARY_FILE);
|
||||
|
||||
byte[] tsBinaryDataContent = tsBinaryData.getContent();
|
||||
byte[] fileBinaryDataContent = fileBinaryData.getContent();
|
||||
String fileName = Files.createTempFile("unit_test_", "xml").getFileName().toString();
|
||||
|
||||
// writing TS XML tune file with rusEFI code
|
||||
Msq tuneFromBinary = Msq.valueOf(originalBinaryData);
|
||||
tuneFromBinary.writeXmlFile(fileName);
|
||||
|
||||
// and now reading that XML back
|
||||
Msq tuneFromFile = Msq.readTune(fileName);
|
||||
|
||||
ConfigurationImage binaryDataFromXml = tuneFromFile.asImage(IniFileModel.getInstance());
|
||||
|
||||
// assertEquals(0, compareImages(originalBinaryData, binaryDataFromXml));
|
||||
}
|
||||
|
||||
private static int compareImages(ConfigurationImage image1, ConfigurationImage image2) {
|
||||
byte[] tsBinaryDataContent = image1.getContent();
|
||||
byte[] fileBinaryDataContent = image2.getContent();
|
||||
|
||||
int mismatchCounter = 0;
|
||||
|
||||
|
@ -53,22 +74,6 @@ public class TuneReadWriteTest {
|
|||
}
|
||||
}
|
||||
System.out.println("Total mismatch count " + mismatchCounter);
|
||||
// todo: why one byte mismatch? since it's in floats I kind of do not care, floats are weird
|
||||
assertEquals(1, mismatchCounter);
|
||||
}
|
||||
|
||||
private ConfigurationImage makeBinaryTune(Msq tsTune, IniFileModel instance) {
|
||||
ConfigurationImage ci = new ConfigurationImage(Fields.TOTAL_CONFIG_SIZE);
|
||||
|
||||
for (Constant constant : tsTune.getPage().constant) {
|
||||
if (constant.getName().startsWith("UNALLOCATED_SPACE")) {
|
||||
continue;
|
||||
}
|
||||
IniField field = instance.allIniFields.get(constant.getName());
|
||||
Objects.requireNonNull(field, "Field for " + constant.getName());
|
||||
System.out.println("Setting " + field);
|
||||
field.setValue(ci, constant);
|
||||
}
|
||||
return ci;
|
||||
return mismatchCounter;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue