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.devexperts.logging.Logging;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.core.EngineState; import com.rusefi.core.EngineState;
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;
@ -73,18 +74,21 @@ public class IoUtil {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
final CountDownLatch rpmLatch = new CountDownLatch(1); 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)) if (isCloseEnough(rpm, actualRpm))
rpmLatch.countDown(); rpmLatch.countDown();
}; });
SensorCentral.getInstance().addListener(Sensor.RPM, listener);
// Wait for RPM to change
try { try {
rpmLatch.await(40, TimeUnit.SECONDS); rpmLatch.await(40, TimeUnit.SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new IllegalStateException(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); double actualRpm = SensorCentral.getInstance().getValue(Sensor.RPM);
@ -97,11 +101,11 @@ public class IoUtil {
static void waitForFirstResponse() throws InterruptedException { static void waitForFirstResponse() throws InterruptedException {
log.info("Let's give it some time to start..."); log.info("Let's give it some time to start...");
final CountDownLatch startup = new CountDownLatch(1); final CountDownLatch startup = new CountDownLatch(1);
SensorCentral.SensorListener listener = value -> startup.countDown();
long waitStart = System.currentTimeMillis(); 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); startup.await(5, TimeUnit.SECONDS);
SensorCentral.getInstance().removeListener(Sensor.RPM, listener); listener.remove();
FileLog.MAIN.logLine("Got first signal in " + (System.currentTimeMillis() - waitStart)); 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 { public interface ISensorCentral extends ISensorHolder {
void setAnySensorListener(SensorCentral.SensorListener2 anySensorListener); 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); void removeListener(Sensor sensor, SensorCentral.SensorListener listener);

View File

@ -62,7 +62,7 @@ public class SensorCentral implements ISensorCentral {
} }
@Override @Override
public void addListener(Sensor sensor, SensorListener listener) { public ListenerToken addListener(Sensor sensor, SensorListener listener) {
List<SensorListener> listeners; List<SensorListener> listeners;
synchronized (allListeners) { synchronized (allListeners) {
listeners = allListeners.get(sensor); listeners = allListeners.get(sensor);
@ -71,6 +71,8 @@ public class SensorCentral implements ISensorCentral {
allListeners.put(sensor, listeners); allListeners.put(sensor, listeners);
} }
listeners.add(listener); listeners.add(listener);
return new SensorCentral.ListenerToken(this, sensor, listener);
} }
@Override @Override