Lua to read tsOutputs #3376

This commit is contained in:
rusefillc 2021-12-12 22:42:46 -05:00
parent 793b6a214f
commit d0592fa20d
14 changed files with 77 additions and 50 deletions

View File

@ -257,7 +257,7 @@ public class ConfigDefinition {
List<ConfigurationConsumer> destinations = new ArrayList<>();
if (TS_OUTPUTS_DESTINATION != null) {
destinations.add(new OutputsSectionConsumer(TS_OUTPUTS_DESTINATION + File.separator + "generated/output_channels.ini", state));
destinations.add(new DataLogConsumer(TS_OUTPUTS_DESTINATION + File.separator + "generated/data_logs.ini", state));
destinations.add(new DataLogConsumer(TS_OUTPUTS_DESTINATION + File.separator + "generated/data_logs.ini"));
destinations.add(new GaugeConsumer(TS_OUTPUTS_DESTINATION + File.separator + "generated/gauges.ini", state));
}
if (tsInputFileFolder != null) {

View File

@ -46,6 +46,7 @@ public class ConfigField {
private final boolean hasAutoscale;
private final String trueName;
private final String falseName;
private boolean isFromIterate;
/**
* todo: one day someone should convert this into a builder
@ -328,5 +329,13 @@ public class ConfigField {
return token.substring(1, length - 1);
return token;
}
public void isFromIterate(boolean isFromIterate) {
this.isFromIterate = isFromIterate;
}
public boolean isFromIterate() {
return isFromIterate;
}
}

View File

@ -156,7 +156,7 @@ public class ReaderState {
structures.put(structure.getName(), structure);
for (ConfigurationConsumer consumer : consumers)
consumer.handleEndStruct(structure);
consumer.handleEndStruct(this, structure);
}
public void readBufferedReader(String inputString, List<ConfigurationConsumer> consumers) throws IOException {
@ -267,6 +267,7 @@ public class ReaderState {
for (int i = 1; i <= cf.getArraySizes()[0]; i++) {
ConfigField element = new ConfigField(state, cf.getName() + i, cf.getComment(), null,
cf.getType(), new int[0], cf.getTsInfo(), false, false, false, null, null);
element.isFromIterate(true);
structure.addTs(element);
}
} else if (cf.isDirective()) {

View File

@ -39,7 +39,7 @@ public abstract class BaseCHeaderConsumer extends AbstractConfigurationConsumer
}
@Override
public void handleEndStruct(ConfigStructure structure) {
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) {
if (structure.comment != null) {
content.append("/**" + EOL + ConfigDefinition.packComment(structure.comment, "") + EOL + "*/" + EOL);
}

View File

@ -1,5 +1,7 @@
package com.rusefi.output;
import com.rusefi.ReaderState;
import java.io.IOException;
public interface ConfigurationConsumer {
@ -7,5 +9,5 @@ public interface ConfigurationConsumer {
void endFile() throws IOException;
void handleEndStruct(ConfigStructure structure) throws IOException;
void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException;
}

View File

@ -18,19 +18,17 @@ import static org.abego.treelayout.internal.util.java.lang.string.StringUtil.quo
public class DataLogConsumer extends AbstractConfigurationConsumer {
public static final String UNUSED = "unused";
private final String fileName;
private final ReaderState state;
private final CharArrayWriter tsWriter = new CharArrayWriter();
private final TreeSet<String> comments = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
public DataLogConsumer(String fileName, ReaderState state) {
public DataLogConsumer(String fileName) {
this.fileName = fileName;
this.state = state;
}
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
if (state.stack.isEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(structure.tsFields, "",
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
if (readerState.stack.isEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(readerState, structure.tsFields, "",
this::handle);
iterator.loop();
String content = iterator.sb.toString();
@ -48,7 +46,7 @@ public class DataLogConsumer extends AbstractConfigurationConsumer {
}
}
private String handle(ConfigField configField, String prefix) {
private String handle(ReaderState state, ConfigField configField, String prefix) {
if (configField.getName().contains(UNUSED))
return "";

View File

@ -21,10 +21,10 @@ public class GaugeConsumer extends AbstractConfigurationConsumer {
}
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
if (state.stack.isEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(structure.tsFields, "",
this::handle);
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "",
(state, configField, prefix) -> handle(configField, prefix));
iterator.loop();
String content = iterator.sb.toString();
charArrayWriter.append(content);

View File

@ -1,6 +1,7 @@
package com.rusefi.output;
import com.rusefi.ConfigField;
import com.rusefi.ReaderState;
import org.jetbrains.annotations.Nullable;
import java.io.FileWriter;
@ -28,18 +29,12 @@ public class GetConfigValueConsumer extends AbstractConfigurationConsumer {
}
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
FieldIterator iterator = new FieldIterator(structure.cFields);
for (int i = 0; i < structure.cFields.size(); i++) {
iterator.start(i);
append(iterator.cf);
iterator.end();
public void handleEndStruct(ReaderState state, ConfigStructure structure) throws IOException {
if (state.stack.isEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "",
this::process, ".");
iterator.loop();
}
}
@Override
@ -55,12 +50,16 @@ public class GetConfigValueConsumer extends AbstractConfigurationConsumer {
writeStringToFile(outputFIleName, content.toString());
}
private void append(ConfigField cf) {
private String process(ReaderState readerState, ConfigField cf, String prefix) {
if (cf.getName().contains(UNUSED) || cf.getName().contains(ALIGNMENT_FILL_AT))
return;
return "";
content.append("\tif (strEqualCaseInsensitive(name, \"" + cf.getName() + "\"))\n");
content.append("\t\treturn engineConfiguration->" + cf.getName() + ";\n");
if (cf.isArray() || cf.isFromIterate())
return "";
content.append("\tif (strEqualCaseInsensitive(name, \"" + prefix + cf.getName() + "\"))\n");
content.append("\t\treturn engineConfiguration->" + prefix + cf.getName() + ";\n");
return "";
}
public String getContent() {

View File

@ -111,7 +111,7 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
return custom != null && custom.toLowerCase().startsWith(IniFileModel.FIELD_TYPE_STRING);
}
public void handleEndStruct(ConfigStructure structure) throws IOException {
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
if (state.stack.isEmpty()) {
writeJavaFields(structure.tsFields, "", 0);
}

View File

@ -21,7 +21,7 @@ public class OutputsSectionConsumer extends AbstractConfigurationConsumer {
}
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
System.out.println("handleEndStruct");
if (state.stack.isEmpty()) {

View File

@ -1,18 +1,27 @@
package com.rusefi.output;
import com.rusefi.ConfigField;
import com.rusefi.ReaderState;
import java.util.List;
class PerFieldWithStructuresIterator extends FieldIterator {
private final ReaderState state;
private final String prefix;
private final Strategy strategy;
private final String prefixSeparator;
StringBuilder sb = new StringBuilder();
public PerFieldWithStructuresIterator(List<ConfigField> fields, String prefix, Strategy strategy) {
public PerFieldWithStructuresIterator(ReaderState state, List<ConfigField> fields, String prefix, Strategy strategy, String prefixSeparator) {
super(fields);
this.state = state;
this.prefix = prefix;
this.strategy = strategy;
this.prefixSeparator = prefixSeparator;
}
public PerFieldWithStructuresIterator(ReaderState state, List<ConfigField> fields, String prefix, Strategy strategy) {
this(state, fields, prefix, strategy, "_");
}
@Override
@ -20,18 +29,18 @@ class PerFieldWithStructuresIterator extends FieldIterator {
ConfigStructure cs = cf.getState().structures.get(cf.getType());
String content;
if (cs != null) {
String extraPrefix = cs.withPrefix ? cf.getName() + "_" : "";
PerFieldWithStructuresIterator fieldIterator = new PerFieldWithStructuresIterator(cs.tsFields, extraPrefix, strategy);
String extraPrefix = cs.withPrefix ? cf.getName() + prefixSeparator : "";
PerFieldWithStructuresIterator fieldIterator = new PerFieldWithStructuresIterator(state, cs.tsFields, extraPrefix, strategy);
fieldIterator.loop();
content = fieldIterator.sb.toString();
} else {
content = strategy.process(cf, prefix);
content = strategy.process(state, cf, prefix);
}
sb.append(content);
super.end();
}
interface Strategy {
String process(ConfigField configField, String prefix);
String process(ReaderState state, ConfigField configField, String prefix);
}
}

View File

@ -19,7 +19,7 @@ public class SignatureConsumer extends AbstractConfigurationConsumer {
}
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
ConfigDefinition.writeDefinesToFile(registry, destHeader);
}
}

View File

@ -148,7 +148,7 @@ public class TSProjectConsumer implements ConfigurationConsumer {
}
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
state.variableRegistry.register(structure.name + "_size", structure.getTotalSize());
if (state.stack.isEmpty()) {
totalTsSize = tsOutput.writeTunerStudio(structure, "", tsWriter, 0);

View File

@ -99,7 +99,7 @@ public class OutputsTest {
state.variableRegistry.register("PACK_MULT_PERCENT", 100);
state.variableRegistry.register("GAUGE_NAME_FUEL_BASE", "hello");
DataLogConsumer dataLogConsumer = new DataLogConsumer(null, state);
DataLogConsumer dataLogConsumer = new DataLogConsumer(null);
state.readBufferedReader(test, Collections.singletonList(dataLogConsumer));
assertEquals(
"entry = issue_294_31, \"issue_294_31\", int, \"%d\"\n" +
@ -128,6 +128,7 @@ public class OutputsTest {
"float resistance_2;;\"Ohm\", 1, 0, 0, 200000, 1\n" +
"float resistance_3;;\"Ohm\", 1, 0, 0, 200000, 1\n" +
"\n" +
"\tint[12 iterate] ignitionPins;\n" +
"\tfloat bias_resistor;+Pull-up resistor value on your board;\"Ohm\", 1, 0, 0, 200000, 1\n" +
"end_struct\n" +
"struct ThermistorConf @brief Thermistor curve parameters\n" +
@ -171,18 +172,26 @@ public class OutputsTest {
"\t\treturn engineConfiguration->resistance_3;\n" +
"\tif (strEqualCaseInsensitive(name, \"bias_resistor\"))\n" +
"\t\treturn engineConfiguration->bias_resistor;\n" +
"\tif (strEqualCaseInsensitive(name, \"config\"))\n" +
"\t\treturn engineConfiguration->config;\n" +
"\tif (strEqualCaseInsensitive(name, \"adcChannel\"))\n" +
"\t\treturn engineConfiguration->adcChannel;\n" +
"\tif (strEqualCaseInsensitive(name, \"clt\"))\n" +
"\t\treturn engineConfiguration->clt;\n" +
"\tif (strEqualCaseInsensitive(name, \"iat\"))\n" +
"\t\treturn engineConfiguration->iat;\n" +
"\tif (strEqualCaseInsensitive(name, \"clt.adcChannel\"))\n" +
"\t\treturn engineConfiguration->clt.adcChannel;\n" +
"\tif (strEqualCaseInsensitive(name, \"tempC_1\"))\n" +
"\t\treturn engineConfiguration->tempC_1;\n" +
"\tif (strEqualCaseInsensitive(name, \"tempC_2\"))\n" +
"\t\treturn engineConfiguration->tempC_2;\n" +
"\tif (strEqualCaseInsensitive(name, \"tempC_3\"))\n" +
"\t\treturn engineConfiguration->tempC_3;\n" +
"\tif (strEqualCaseInsensitive(name, \"resistance_1\"))\n" +
"\t\treturn engineConfiguration->resistance_1;\n" +
"\tif (strEqualCaseInsensitive(name, \"resistance_2\"))\n" +
"\t\treturn engineConfiguration->resistance_2;\n" +
"\tif (strEqualCaseInsensitive(name, \"resistance_3\"))\n" +
"\t\treturn engineConfiguration->resistance_3;\n" +
"\tif (strEqualCaseInsensitive(name, \"bias_resistor\"))\n" +
"\t\treturn engineConfiguration->bias_resistor;\n" +
"\tif (strEqualCaseInsensitive(name, \"iat.adcChannel\"))\n" +
"\t\treturn engineConfiguration->iat.adcChannel;\n" +
"\tif (strEqualCaseInsensitive(name, \"issue_294_31\"))\n" +
"\t\treturn engineConfiguration->issue_294_31;\n" +
"\tif (strEqualCaseInsensitive(name, \"knock\"))\n" +
"\t\treturn engineConfiguration->knock;\n" +
"\tif (strEqualCaseInsensitive(name, \"baseFuel\"))\n" +
"\t\treturn engineConfiguration->baseFuel;\n" +
"\tif (strEqualCaseInsensitive(name, \"afr_type\"))\n" +
@ -214,7 +223,7 @@ public class OutputsTest {
"end_struct\n";
ReaderState state = new ReaderState();
DataLogConsumer dataLogConsumer = new DataLogConsumer(null, state);
DataLogConsumer dataLogConsumer = new DataLogConsumer(null);
GaugeConsumer gaugeConsumer = new GaugeConsumer(null, state);
state.readBufferedReader(test, Arrays.asList(dataLogConsumer, gaugeConsumer));
assertEquals(