1
0

Change update DataPoints by a subscription way

This commit is contained in:
Rémi Heredero 2023-05-25 21:17:11 +02:00
parent e780bc860f
commit 4b3a0bedd4
6 changed files with 28 additions and 3 deletions

View File

@ -22,9 +22,9 @@ public abstract class DataPoint{
} else {
dataPointMap.replace(this.label, this);
}
DatabaseConnector.getMySelf().onNewValue(this);
WebConnector.getMySelf().onNewValue(this);
FieldConnector.getMySelf().onNewValue(this);
// Update every connector
DataPointListener.listeners.forEach(listener -> listener.onNewValue(this));
}
public static DataPoint getDataPointFromLabel(String label){
if( !dataPointMap.containsKey(label) ){

View File

@ -1,6 +1,20 @@
package ch.hevs.isi.core;
import java.util.Vector;
public interface DataPointListener {
// Vector of listeners
Vector<DataPointListener> listeners = new Vector<>();
void onNewValue(DataPoint dp);
/**
* Subscribe to the update of the DataPoint
* @param listener the listener to subscribe
*/
static void subscribeUpdate(DataPointListener listener){
// Call by final class for subscribe update
listeners.add(listener);
}
}

View File

@ -56,6 +56,9 @@ public class DatabaseConnector implements DataPointListener {
if (bucket == null){
bucket = properties.getProperty("BUCKET");
}
// Subscribe to the update of DataPoints
DataPointListener.subscribeUpdate(this);
}
/**

View File

@ -10,6 +10,8 @@ public class FieldConnector implements DataPointListener {
private FieldConnector(){
// Subscribe to the update of DataPoints
DataPointListener.subscribeUpdate(this);
}
public static FieldConnector getMySelf(){

View File

@ -8,6 +8,8 @@ public class WebConnector implements DataPointListener {
private WebConnector (){
// Subscribe to the update of DataPoints
DataPointListener.subscribeUpdate(this);
}
public static WebConnector getMySelf(){

View File

@ -3,6 +3,8 @@ import ch.hevs.isi.core.BooleanDataPoint;
import ch.hevs.isi.core.DataPoint;
import ch.hevs.isi.core.FloatDataPoint;
import ch.hevs.isi.db.DatabaseConnector;
import ch.hevs.isi.field.FieldConnector;
import ch.hevs.isi.web.WebConnector;
import java.io.FileInputStream;
import java.io.InputStream;
@ -16,6 +18,8 @@ public class Database {
BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true);
DatabaseConnector.getMySelf().initialize(null);
FieldConnector.getMySelf().initialize(null, 0);
WebConnector.getMySelf().initialize(null, 0);
clock.setValue(0f);