refacotring - a bit of OOP
This commit is contained in:
parent
1921835f39
commit
adb41de7f8
|
@ -0,0 +1,19 @@
|
|||
package com.rusefi.core;
|
||||
|
||||
/**
|
||||
* 11/16/2017
|
||||
* (c) Andrey Belomutskiy
|
||||
*/
|
||||
public interface ISensorCentral {
|
||||
double getValue(Sensor sensor);
|
||||
|
||||
void setValue(double value, Sensor sensor);
|
||||
|
||||
void setAnySensorListener(SensorCentral.SensorListener2 anySensorListener);
|
||||
|
||||
void addListener(Sensor sensor, SensorCentral.SensorListener listener);
|
||||
|
||||
void removeListener(Sensor sensor, SensorCentral.SensorListener listener);
|
||||
|
||||
void initialize(EngineState es);
|
||||
}
|
|
@ -12,22 +12,23 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||
* Date: 1/6/13
|
||||
* (c) Andrey Belomutskiy
|
||||
*/
|
||||
public class SensorCentral {
|
||||
public class SensorCentral implements ISensorCentral {
|
||||
public static final String RPM_KEY = "rpm";
|
||||
private static final SensorCentral INSTANCE = new SensorCentral();
|
||||
|
||||
private final Map<Sensor, Double> values = new EnumMap<>(Sensor.class);
|
||||
|
||||
private final Map<Sensor, List<SensorListener>> allListeners = new EnumMap<>(Sensor.class);
|
||||
public SensorListener2 anySensorListener;
|
||||
private SensorListener2 anySensorListener;
|
||||
|
||||
public static SensorCentral getInstance() {
|
||||
public static ISensorCentral getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private SensorCentral() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getValue(Sensor sensor) {
|
||||
Double value = values.get(sensor);
|
||||
if (value == null)
|
||||
|
@ -35,6 +36,7 @@ public class SensorCentral {
|
|||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(double value, final Sensor sensor) {
|
||||
Double oldValue = values.get(sensor);
|
||||
boolean isUpdated = oldValue == null || !oldValue.equals(value);
|
||||
|
@ -58,6 +60,12 @@ public class SensorCentral {
|
|||
anySensorListener.onSensorUpdate(sensor, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnySensorListener(SensorListener2 anySensorListener) {
|
||||
this.anySensorListener = anySensorListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(Sensor sensor, SensorListener listener) {
|
||||
List<SensorListener> listeners;
|
||||
synchronized (allListeners) {
|
||||
|
@ -69,6 +77,7 @@ public class SensorCentral {
|
|||
listeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(Sensor sensor, SensorListener listener) {
|
||||
List<SensorListener> listeners;
|
||||
synchronized (allListeners) {
|
||||
|
@ -78,6 +87,7 @@ public class SensorCentral {
|
|||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(EngineState es) {
|
||||
addDoubleSensor(RPM_KEY, Sensor.RPM, es);
|
||||
addDoubleSensor("mat", Sensor.IAT, es);
|
||||
|
|
|
@ -75,7 +75,7 @@ public final class TableUpdateHandler {
|
|||
};
|
||||
|
||||
|
||||
SensorCentral.getInstance().anySensorListener = new SensorCentral.SensorListener2() {
|
||||
SensorCentral.getInstance().setAnySensorListener(new SensorCentral.SensorListener2() {
|
||||
@Override
|
||||
public void onSensorUpdate(final Sensor sensor, double value) {
|
||||
ResponseImpl r = new ResponseImpl();
|
||||
|
@ -131,7 +131,7 @@ public final class TableUpdateHandler {
|
|||
TableUpdateHandler.getInstance().handleDataUpdate(r);
|
||||
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private TableUpdateHandler() {
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.rusefi.autotune.stDataOnline;
|
|||
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocolHolder;
|
||||
import com.rusefi.config.Fields;
|
||||
import com.rusefi.core.ISensorCentral;
|
||||
import com.rusefi.core.Sensor;
|
||||
import com.rusefi.core.SensorCentral;
|
||||
import com.rusefi.ui.config.BaseConfigField;
|
||||
|
@ -282,7 +283,7 @@ public class FuelTunePane {
|
|||
}
|
||||
|
||||
public void showContent() {
|
||||
final SensorCentral sc = SensorCentral.getInstance();
|
||||
final ISensorCentral sc = SensorCentral.getInstance();
|
||||
sc.addListener(Sensor.RPM, new SensorCentral.SensorListener() {
|
||||
@Override
|
||||
public void onSensorUpdate(double value) {
|
||||
|
|
Loading…
Reference in New Issue