More sensor listener lambdas (#2121)

* api

* ioutil

* more lambdas
This commit is contained in:
Matthew Kennedy 2020-12-22 17:12:50 -08:00 committed by GitHub
parent 5b7d6fc59e
commit f1faf16a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 36 additions and 68 deletions

View File

@ -6,6 +6,7 @@ import com.rusefi.IoUtil;
import com.rusefi.TestingUtils; import com.rusefi.TestingUtils;
import com.rusefi.Timeouts; import com.rusefi.Timeouts;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.core.ISensorCentral;
import com.rusefi.core.Sensor; import com.rusefi.core.Sensor;
import com.rusefi.core.SensorCentral; import com.rusefi.core.SensorCentral;
import com.rusefi.io.CommandQueue; import com.rusefi.io.CommandQueue;
@ -44,17 +45,18 @@ public class EcuTestHelper {
sleepSeconds(settleTime); sleepSeconds(settleTime);
AtomicReference<String> result = new AtomicReference<>(); AtomicReference<String> result = new AtomicReference<>();
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
SensorCentral.SensorListener listener = value -> {
double actualRpm = SensorCentral.getInstance().getValue(Sensor.RPM); ISensorCentral.ListenerToken listener = SensorCentral.getInstance().addListener(Sensor.RPM, actualRpm -> {
if (!isCloseEnough(rpm, actualRpm)) { if (!isCloseEnough(rpm, actualRpm)) {
long seconds = (System.currentTimeMillis() - start) / 1000; long seconds = (System.currentTimeMillis() - start) / 1000;
result.set("Got " + actualRpm + " while trying to stay at " + rpm + " after " + seconds + " seconds"); result.set("Got " + actualRpm + " while trying to stay at " + rpm + " after " + seconds + " seconds");
} }
}; });
SensorCentral.getInstance().addListener(Sensor.RPM, listener);
sleepSeconds(testDuration); sleepSeconds(testDuration);
callback.apply(result.get()); callback.apply(result.get());
SensorCentral.getInstance().removeListener(Sensor.RPM, listener);
listener.remove();
} }
@NotNull @NotNull
@ -131,7 +133,7 @@ public class EcuTestHelper {
sleepSeconds(2); sleepSeconds(2);
sendCommand("set " + Fields.CMD_ENGINE_TYPE + " " + type, COMPLEX_COMMAND_RETRY, Timeouts.SET_ENGINE_TIMEOUT); sendCommand("set " + Fields.CMD_ENGINE_TYPE + " " + type, COMPLEX_COMMAND_RETRY, Timeouts.SET_ENGINE_TIMEOUT);
// TODO: document the reason for this sleep?! // TODO: document the reason for this sleep?!
sleepSeconds(3); sleepSeconds(1);
sendCommand(getEnableCommand(Fields.CMD_PWM)); sendCommand(getEnableCommand(Fields.CMD_PWM));
sendCommand(getEnableCommand(Fields.CMD_SELF_STIMULATION)); sendCommand(getEnableCommand(Fields.CMD_SELF_STIMULATION));
// // we need to skip one chart since it might have been produced with previous engine type // // we need to skip one chart since it might have been produced with previous engine type

View File

@ -31,12 +31,7 @@ public class ConnectionStatusLogic {
private List<Listener> listeners = new CopyOnWriteArrayList<>(); private List<Listener> listeners = new CopyOnWriteArrayList<>();
private ConnectionStatusLogic() { private ConnectionStatusLogic() {
SensorCentral.getInstance().addListener(Sensor.TIME_SECONDS, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(Sensor.TIME_SECONDS, value -> markConnected());
@Override
public void onSensorUpdate(double value) {
markConnected();
}
});
MessagesCentral.getInstance().addListener(new MessagesCentral.MessageListener() { MessagesCentral.getInstance().addListener(new MessagesCentral.MessageListener() {
@Override @Override

View File

@ -48,36 +48,30 @@ public class SensorStats {
public static void startStandardDeviation(Sensor source, final Sensor destination) { public static void startStandardDeviation(Sensor source, final Sensor destination) {
final CyclicBuffer cb = new CyclicBuffer(30); final CyclicBuffer cb = new CyclicBuffer(30);
SensorCentral.getInstance().addListener(source, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(source,
@Override value -> {
public void onSensorUpdate(double value) { cb.add(value);
cb.add(value); if (cb.getSize() == cb.getMaxSize()) {
if (cb.getSize() == cb.getMaxSize()) { double stdDev = DataBuffer.getStandardDeviation(cb.getValues());
double stdDev = DataBuffer.getStandardDeviation(cb.getValues()); SensorCentral.getInstance().setValue(stdDev, destination);
SensorCentral.getInstance().setValue(stdDev, destination);
}
} }
} }
); );
} }
public static void startDelta(Sensor input1, final Sensor input2, final Sensor destination) { public static void startDelta(Sensor input1, final Sensor input2, final Sensor destination) {
SensorCentral.getInstance().addListener(input1, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(input1,
@Override value -> {
public void onSensorUpdate(double value) { double valueMs = (value - SensorCentral.getInstance().getValue(input2)) / EngineReport.ENGINE_SNIFFER_TICKS_PER_MS;
double valueMs = 1.0 * (value - SensorCentral.getInstance().getValue(input2)) / EngineReport.ENGINE_SNIFFER_TICKS_PER_MS;
SensorCentral.getInstance().setValue(valueMs, destination); SensorCentral.getInstance().setValue(valueMs, destination);
} });
});
} }
public static void startConversion(final Sensor source, final Sensor destination, final SensorConversion conversion) { public static void startConversion(final Sensor source, final Sensor destination, final SensorConversion conversion) {
SensorCentral.getInstance().addListener(source, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(source,
@Override value -> {
public void onSensorUpdate(double value) {
double converted = conversion.convertValue(value); double converted = conversion.convertValue(value);
SensorCentral.getInstance().setValue(converted, destination); SensorCentral.getInstance().setValue(converted, destination);
} });
});
} }
} }

View File

@ -96,15 +96,13 @@ public class SensorLogger {
return; return;
} }
isInitialized = true; isInitialized = true;
SensorCentral.getInstance().addListener(Sensor.TIME_SECONDS, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(Sensor.TIME_SECONDS,
@Override value -> {
public void onSensorUpdate(double value) {
if (ConnectionStatusLogic.INSTANCE.getValue() != ConnectionStatusValue.CONNECTED) if (ConnectionStatusLogic.INSTANCE.getValue() != ConnectionStatusValue.CONNECTED)
return; return;
for (SensorLog sensorLog : sensorLogs) for (SensorLog sensorLog : sensorLogs)
sensorLog.writeSensorLogLine(); sensorLog.writeSensorLogLine();
} });
});
} }
public double getSecondsSinceFileStart() { public double getSecondsSinceFileStart() {

View File

@ -285,9 +285,7 @@ public class FuelTunePane {
public void showContent() { public void showContent() {
final ISensorCentral sc = SensorCentral.getInstance(); final ISensorCentral sc = SensorCentral.getInstance();
sc.addListener(Sensor.RPM, new SensorCentral.SensorListener() { sc.addListener(Sensor.RPM, value -> {
@Override
public void onSensorUpdate(double value) {
if (!collect.isSelected()) if (!collect.isSelected())
return; return;
int rpm = (int) value; int rpm = (int) value;
@ -303,7 +301,7 @@ public class FuelTunePane {
incomingDataPoints.add(newPoint); incomingDataPoints.add(newPoint);
} }
} }
}); );
loadArray(veLoadBins, veLoadOffset); loadArray(veLoadBins, veLoadOffset);
loadArray(veRpmBins, veRpmOffset); loadArray(veRpmBins, veRpmOffset);

View File

@ -27,12 +27,7 @@ public class RpmModel {
} }
private RpmModel() { private RpmModel() {
SensorCentral.getInstance().addListener(Sensor.RPM, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(Sensor.RPM, value -> setValue((int) value));
@Override
public void onSensorUpdate(double value) {
setValue((int) value);
}
});
} }
public void setValue(int rpm) { public void setValue(int rpm) {

View File

@ -45,13 +45,7 @@ public class LightweightGUI {
JLabel firmwareVersion = new JLabel(); JLabel firmwareVersion = new JLabel();
SensorCentral.getInstance().addListener(Sensor.FIRMWARE_VERSION, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(Sensor.FIRMWARE_VERSION, value -> firmwareVersion.setText(Integer.toString((int) value)));
@Override
public void onSensorUpdate(double value) {
firmwareVersion.setText(Integer.toString((int) value));
}
});
leftPanel.add(firmwareVersion); leftPanel.add(firmwareVersion);
content.add(topPanel, BorderLayout.NORTH); content.add(topPanel, BorderLayout.NORTH);

View File

@ -123,12 +123,7 @@ public class LiveDocPanel {
} }
}); });
result.addControl(label); result.addControl(label);
SensorCentral.getInstance().addListener(sensor, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(sensor, value -> label.setText(niceToString(value, LIVE_DATA_PRECISION)));
@Override
public void onSensorUpdate(double value) {
label.setText(niceToString(value, LIVE_DATA_PRECISION));
}
});
} else if (r instanceof IfRequest) { } else if (r instanceof IfRequest) {
IfRequest request = (IfRequest) r; IfRequest request = (IfRequest) r;

View File

@ -16,11 +16,7 @@ public class IntGaugeLabel extends JLabel {
public IntGaugeLabel(final String shortName, Sensor sensor) { public IntGaugeLabel(final String shortName, Sensor sensor) {
if (sensor.getType() != FieldType.INT) if (sensor.getType() != FieldType.INT)
throw new IllegalArgumentException(sensor.name()); throw new IllegalArgumentException(sensor.name());
SensorCentral.getInstance().addListener(sensor, new SensorCentral.SensorListener() {
@Override SensorCentral.getInstance().addListener(sensor, value -> setText(shortName + ": " + (int)value));
public void onSensorUpdate(double value) {
IntGaugeLabel.this.setText(shortName + ": " + (int)value);
}
});
} }
} }

View File

@ -55,13 +55,14 @@ public class SensorGauge {
gauge.setBackgroundColor(BackgroundColor.LIGHT_GRAY); gauge.setBackgroundColor(BackgroundColor.LIGHT_GRAY);
SensorCentral.getInstance().addListener(sensor, new SensorCentral.SensorListener() { SensorCentral.getInstance().addListener(sensor,
public void onSensorUpdate(double value) { value -> {
if (GaugesPanel.IS_PAUSED) if (GaugesPanel.IS_PAUSED)
return; return;
gauge.setValue(sensor.translateValue(value)); gauge.setValue(sensor.translateValue(value));
} }
}); );
gauge.setValue(sensor.translateValue(SensorCentral.getInstance().getValue(sensor))); gauge.setValue(sensor.translateValue(SensorCentral.getInstance().getValue(sensor)));
gauge.setLcdDecimals(2); gauge.setLcdDecimals(2);