From 60ee5a3320d5ec73239e9f070dee383b83dc9a2b Mon Sep 17 00:00:00 2001 From: rusefillc Date: Fri, 15 Apr 2022 16:51:09 -0400 Subject: [PATCH] logging of live data structs was: data points #3614 --- .../com/rusefi/output/JavaFieldsConsumer.java | 16 ++-- .../rusefi/output/JavaSensorsConsumer.java | 81 +++++++++++++++++++ .../rusefi/test/JavaSensorsConsumerTest.java | 38 +++++++++ 3 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 java_tools/configuration_definition/src/main/java/com/rusefi/output/JavaSensorsConsumer.java create mode 100644 java_tools/configuration_definition/src/test/java/com/rusefi/test/JavaSensorsConsumerTest.java diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/output/JavaFieldsConsumer.java b/java_tools/configuration_definition/src/main/java/com/rusefi/output/JavaFieldsConsumer.java index 98be5ca2e7..97f310a255 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/output/JavaFieldsConsumer.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/output/JavaFieldsConsumer.java @@ -88,12 +88,8 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer { String[] tokens = custom.split(","); String stringSize = tokens[3].trim(); javaFieldsWriter.write(stringSize + ", FieldType.STRING"); - } else if (configField.getElementSize() == 1) { - javaFieldsWriter.write("FieldType.INT8"); - } else if (configField.getElementSize() == 2) { - javaFieldsWriter.write("FieldType.INT16"); } else { - javaFieldsWriter.write("FieldType.INT"); + javaFieldsWriter.write(getJavaType(configField.getElementSize())); } if (enumOptions != null) { javaFieldsWriter.write(", " + configField.getType()); @@ -107,6 +103,16 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer { return tsPosition; } + public static String getJavaType(int elementSize) { + if (elementSize == 1) { + return ("FieldType.INT8"); + } else if (elementSize == 2) { + return "FieldType.INT16"; + } else { + return "FieldType.INT"; + } + } + private boolean isStringField(ConfigField configField) { String custom = state.tsCustomLine.get(configField.getType()); return custom != null && custom.toLowerCase().startsWith(IniFileModel.FIELD_TYPE_STRING); diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/output/JavaSensorsConsumer.java b/java_tools/configuration_definition/src/main/java/com/rusefi/output/JavaSensorsConsumer.java new file mode 100644 index 0000000000..c574170983 --- /dev/null +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/output/JavaSensorsConsumer.java @@ -0,0 +1,81 @@ +package com.rusefi.output; + +import com.rusefi.ConfigField; +import com.rusefi.ReaderState; + +import java.io.IOException; +import java.util.List; + +public class JavaSensorsConsumer implements ConfigurationConsumer { + private final ReaderState state; + + private final StringBuilder sb = new StringBuilder(); + + public JavaSensorsConsumer(ReaderState state) { + + this.state = state; + } + + @Override + public void startFile() { + } + + @Override + public void endFile() throws IOException { + } + + @Override + public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException { + if (state.stack.isEmpty()) { + writeJavaFields(structure.tsFields, "", 0); + } + } + + private void writeJavaFields(List tsFields, String prefix, int tsPosition) { + FieldIterator iterator = new FieldIterator(tsFields); + for (int i = 0; i < tsFields.size(); i++) { + iterator.start(i); + tsPosition = writeOneField(iterator.cf, prefix, tsPosition, iterator.next, + iterator.bitState.get(), + iterator.getPrev()); + + iterator.end(); + } + } + + private int writeOneField(ConfigField configField, String prefix, int tsPosition, ConfigField next, int i, ConfigField prev) { + if (configField.isBit()) { + // do not support bits for now but need to account for size + tsPosition += configField.getSize(next); + return tsPosition; + } + sb.append(configField.getName()).append("("); + + sb.append(configField.getComment()).append(", "); + + sb.append("SensorCategory.SENSOR_INPUTS, "); + sb.append(JavaFieldsConsumer.getJavaType(configField.getElementSize())).append(", "); + sb.append(tsPosition).append(", "); + sb.append("1").append(", "); // scale + sb.append(configField.getMin()).append(", "); + sb.append(configField.getMax()).append(", "); + + sb.append(quote(configField.getUnits())).append(")"); + + + sb.append(","); + sb.append("\n"); + + + tsPosition += configField.getSize(next); + return tsPosition; + } + + private static String quote(String string) { + return "\"" + string + "\""; + } + + public String getContent() { + return sb.toString(); + } +} diff --git a/java_tools/configuration_definition/src/test/java/com/rusefi/test/JavaSensorsConsumerTest.java b/java_tools/configuration_definition/src/test/java/com/rusefi/test/JavaSensorsConsumerTest.java new file mode 100644 index 0000000000..5d59af96b5 --- /dev/null +++ b/java_tools/configuration_definition/src/test/java/com/rusefi/test/JavaSensorsConsumerTest.java @@ -0,0 +1,38 @@ +package com.rusefi.test; + +import com.rusefi.ReaderState; +import com.rusefi.output.JavaSensorsConsumer; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class JavaSensorsConsumerTest { + @Test + public void generateJavaSensors() throws IOException { + String outputChannels = "" + + "\n" + + "" + + "struct_no_prefix output_channels_s\n" + + "bit sd_present\n" + + "uint16_t autoscale RPMValue;GAUGE_NAME_RPM;\"RPM\",1, 0, 0, 8000, 0\n" + + "uint16_t rpmAcceleration;dRPM;\"RPM/s\",1, 0, 0, 5, 0\n" + + "\tuint16_t autoscale speedToRpmRatio;name;\"value\",{1/100}, 0, 0, 0, 0\n" + + "\tuint8_t autoscale vehicleSpeedKph;name2;\"kph\",1, 0, 0, 0, 0\n" + + "\tint8_t autoscale internalMcuTemperature;mcu;\"deg C\",1, 0, 0, 0, 0\n" + + "end_struct\n"; + ReaderState state = new ReaderState(); + JavaSensorsConsumer javaSensorsConsumer = new JavaSensorsConsumer(state); + state.readBufferedReader(outputChannels, javaSensorsConsumer); + + assertEquals("RPMValue(GAUGE_NAME_RPM, SensorCategory.SENSOR_INPUTS, FieldType.INT16, 4, 1, 0.0, 8000.0, \"RPM\"),\n" + + "rpmAcceleration(dRPM, SensorCategory.SENSOR_INPUTS, FieldType.INT16, 6, 1, 0.0, 5.0, \"RPM/s\"),\n" + + "speedToRpmRatio(name, SensorCategory.SENSOR_INPUTS, FieldType.INT16, 8, 1, 0.0, 0.0, \"value\"),\n" + + "vehicleSpeedKph(name2, SensorCategory.SENSOR_INPUTS, FieldType.INT8, 10, 1, 0.0, 0.0, \"kph\"),\n" + + "internalMcuTemperature(mcu, SensorCategory.SENSOR_INPUTS, FieldType.INT8, 11, 1, 0.0, 0.0, \"deg C\"),\n", + javaSensorsConsumer.getContent()); + + + } +}