refacotring - a bit of OOP

This commit is contained in:
rusefi 2017-11-16 20:17:32 -05:00
parent 1921835f39
commit adb41de7f8
4 changed files with 36 additions and 6 deletions

View File

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

View File

@ -12,22 +12,23 @@ import java.util.concurrent.CopyOnWriteArrayList;
* Date: 1/6/13 * Date: 1/6/13
* (c) Andrey Belomutskiy * (c) Andrey Belomutskiy
*/ */
public class SensorCentral { public class SensorCentral implements ISensorCentral {
public static final String RPM_KEY = "rpm"; public static final String RPM_KEY = "rpm";
private static final SensorCentral INSTANCE = new SensorCentral(); private static final SensorCentral INSTANCE = new SensorCentral();
private final Map<Sensor, Double> values = new EnumMap<>(Sensor.class); private final Map<Sensor, Double> values = new EnumMap<>(Sensor.class);
private final Map<Sensor, List<SensorListener>> allListeners = 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; return INSTANCE;
} }
private SensorCentral() { private SensorCentral() {
} }
@Override
public double getValue(Sensor sensor) { public double getValue(Sensor sensor) {
Double value = values.get(sensor); Double value = values.get(sensor);
if (value == null) if (value == null)
@ -35,6 +36,7 @@ public class SensorCentral {
return value; return value;
} }
@Override
public void setValue(double value, final Sensor sensor) { public void setValue(double value, final Sensor sensor) {
Double oldValue = values.get(sensor); Double oldValue = values.get(sensor);
boolean isUpdated = oldValue == null || !oldValue.equals(value); boolean isUpdated = oldValue == null || !oldValue.equals(value);
@ -58,6 +60,12 @@ public class SensorCentral {
anySensorListener.onSensorUpdate(sensor, value); anySensorListener.onSensorUpdate(sensor, value);
} }
@Override
public void setAnySensorListener(SensorListener2 anySensorListener) {
this.anySensorListener = anySensorListener;
}
@Override
public void addListener(Sensor sensor, SensorListener listener) { public void addListener(Sensor sensor, SensorListener listener) {
List<SensorListener> listeners; List<SensorListener> listeners;
synchronized (allListeners) { synchronized (allListeners) {
@ -69,6 +77,7 @@ public class SensorCentral {
listeners.add(listener); listeners.add(listener);
} }
@Override
public void removeListener(Sensor sensor, SensorListener listener) { public void removeListener(Sensor sensor, SensorListener listener) {
List<SensorListener> listeners; List<SensorListener> listeners;
synchronized (allListeners) { synchronized (allListeners) {
@ -78,6 +87,7 @@ public class SensorCentral {
listeners.remove(listener); listeners.remove(listener);
} }
@Override
public void initialize(EngineState es) { public void initialize(EngineState es) {
addDoubleSensor(RPM_KEY, Sensor.RPM, es); addDoubleSensor(RPM_KEY, Sensor.RPM, es);
addDoubleSensor("mat", Sensor.IAT, es); addDoubleSensor("mat", Sensor.IAT, es);

View File

@ -75,7 +75,7 @@ public final class TableUpdateHandler {
}; };
SensorCentral.getInstance().anySensorListener = new SensorCentral.SensorListener2() { SensorCentral.getInstance().setAnySensorListener(new SensorCentral.SensorListener2() {
@Override @Override
public void onSensorUpdate(final Sensor sensor, double value) { public void onSensorUpdate(final Sensor sensor, double value) {
ResponseImpl r = new ResponseImpl(); ResponseImpl r = new ResponseImpl();
@ -131,7 +131,7 @@ public final class TableUpdateHandler {
TableUpdateHandler.getInstance().handleDataUpdate(r); TableUpdateHandler.getInstance().handleDataUpdate(r);
} }
}; });
} }
private TableUpdateHandler() { private TableUpdateHandler() {

View File

@ -15,6 +15,7 @@ import com.rusefi.autotune.stDataOnline;
import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.binaryprotocol.BinaryProtocolHolder; import com.rusefi.binaryprotocol.BinaryProtocolHolder;
import com.rusefi.config.Fields; import com.rusefi.config.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.ui.config.BaseConfigField; import com.rusefi.ui.config.BaseConfigField;
@ -282,7 +283,7 @@ public class FuelTunePane {
} }
public void showContent() { public void showContent() {
final SensorCentral sc = SensorCentral.getInstance(); final ISensorCentral sc = SensorCentral.getInstance();
sc.addListener(Sensor.RPM, new SensorCentral.SensorListener() { sc.addListener(Sensor.RPM, new SensorCentral.SensorListener() {
@Override @Override
public void onSensorUpdate(double value) { public void onSensorUpdate(double value) {