use token for sensor listener unsubscribe (#2120)

* api

* ioutil
This commit is contained in:
Matthew Kennedy 2020-12-22 15:48:55 -08:00 committed by GitHub
parent b94c3588bc
commit 718cbff14b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 10 deletions

View File

@ -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));
}

View File

@ -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);

View File

@ -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