parent
b94c3588bc
commit
718cbff14b
|
@ -3,6 +3,7 @@ package com.rusefi;
|
|||
import com.devexperts.logging.Logging;
|
||||
import com.rusefi.config.generated.Fields;
|
||||
import com.rusefi.core.EngineState;
|
||||
import com.rusefi.core.ISensorCentral;
|
||||
import com.rusefi.core.Sensor;
|
||||
import com.rusefi.core.SensorCentral;
|
||||
import com.rusefi.io.CommandQueue;
|
||||
|
@ -73,18 +74,21 @@ public class IoUtil {
|
|||
long time = System.currentTimeMillis();
|
||||
|
||||
final CountDownLatch rpmLatch = new CountDownLatch(1);
|
||||
SensorCentral.SensorListener listener = value -> {
|
||||
double actualRpm = SensorCentral.getInstance().getValue(Sensor.RPM);
|
||||
|
||||
SensorCentral.ListenerToken listenerToken = SensorCentral.getInstance().addListener(Sensor.RPM, actualRpm -> {
|
||||
if (isCloseEnough(rpm, actualRpm))
|
||||
rpmLatch.countDown();
|
||||
};
|
||||
SensorCentral.getInstance().addListener(Sensor.RPM, listener);
|
||||
});
|
||||
|
||||
// Wait for RPM to change
|
||||
try {
|
||||
rpmLatch.await(40, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
SensorCentral.getInstance().removeListener(Sensor.RPM, listener);
|
||||
|
||||
// We don't need to listen to RPM any more
|
||||
listenerToken.remove();
|
||||
|
||||
double actualRpm = SensorCentral.getInstance().getValue(Sensor.RPM);
|
||||
|
||||
|
@ -97,11 +101,11 @@ public class IoUtil {
|
|||
static void waitForFirstResponse() throws InterruptedException {
|
||||
log.info("Let's give it some time to start...");
|
||||
final CountDownLatch startup = new CountDownLatch(1);
|
||||
SensorCentral.SensorListener listener = value -> startup.countDown();
|
||||
long waitStart = System.currentTimeMillis();
|
||||
SensorCentral.getInstance().addListener(Sensor.RPM, listener);
|
||||
|
||||
ISensorCentral.ListenerToken listener = SensorCentral.getInstance().addListener(Sensor.RPM, value -> startup.countDown());
|
||||
startup.await(5, TimeUnit.SECONDS);
|
||||
SensorCentral.getInstance().removeListener(Sensor.RPM, listener);
|
||||
listener.remove();
|
||||
FileLog.MAIN.logLine("Got first signal in " + (System.currentTimeMillis() - waitStart));
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,23 @@ package com.rusefi.core;
|
|||
public interface ISensorCentral extends ISensorHolder {
|
||||
void setAnySensorListener(SensorCentral.SensorListener2 anySensorListener);
|
||||
|
||||
void addListener(Sensor sensor, SensorCentral.SensorListener listener);
|
||||
public class ListenerToken {
|
||||
private ISensorCentral sensorCentralInstance;
|
||||
private Sensor sensor;
|
||||
private SensorCentral.SensorListener listener;
|
||||
|
||||
public ListenerToken(ISensorCentral instance, Sensor sensor, SensorCentral.SensorListener listener) {
|
||||
this.sensorCentralInstance = instance;
|
||||
this.sensor = sensor;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
sensorCentralInstance.removeListener(sensor, listener);
|
||||
}
|
||||
}
|
||||
|
||||
SensorCentral.ListenerToken addListener(Sensor sensor, SensorCentral.SensorListener listener);
|
||||
|
||||
void removeListener(Sensor sensor, SensorCentral.SensorListener listener);
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ public class SensorCentral implements ISensorCentral {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void addListener(Sensor sensor, SensorListener listener) {
|
||||
public ListenerToken addListener(Sensor sensor, SensorListener listener) {
|
||||
List<SensorListener> listeners;
|
||||
synchronized (allListeners) {
|
||||
listeners = allListeners.get(sensor);
|
||||
|
@ -71,6 +71,8 @@ public class SensorCentral implements ISensorCentral {
|
|||
allListeners.put(sensor, listeners);
|
||||
}
|
||||
listeners.add(listener);
|
||||
|
||||
return new SensorCentral.ListenerToken(this, sensor, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue