From be9c484001b245aab080b36421a94bcfee394545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 6 Jun 2023 13:38:49 +0200 Subject: [PATCH] add comment on Core --- .../ch/hevs/isi/core/BooleanDataPoint.java | 25 ++++++++++++---- src/main/java/ch/hevs/isi/core/DataPoint.java | 29 ++++++++++++++++--- .../java/ch/hevs/isi/core/FloatDataPoint.java | 25 ++++++++++++---- src/test/java/Database.java | 1 - src/test/java/Web.java | 8 ++--- 5 files changed, 69 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java b/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java index d18131f..dc78181 100644 --- a/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java +++ b/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java @@ -1,27 +1,42 @@ package ch.hevs.isi.core; -import ch.hevs.isi.core.DataPointListener; -import ch.hevs.isi.db.DatabaseConnector; -import ch.hevs.isi.field.FieldConnector; -import ch.hevs.isi.web.WebConnector; public class BooleanDataPoint extends DataPoint{ private boolean value; + /** + * Create a new DataPoint with a label and if is an Output or not + * @param label the label of this DataPoint + * @param isOutput true if this is an Output, false if it's an Input + */ public BooleanDataPoint(String label, boolean isOutput) { super(label, isOutput); } + + /** + * Set the value of this DataPoint and create it if it doesn't exist via the update method + * @param value the value to set + */ public void setValue(boolean value){ this.value = value; - if (getDataPointFromLabel(this.getLabel()) == null){ + if (getDataPointOnListFromLabel(this.getLabel()) == null){ this.update(true); } else { this.update(false); } } + + /** + * Get the value of this DataPoint + * @return the value of this DataPoint + */ public boolean getValue(){ return this.value; } + /** + * Convert this DataPoint to a string + * @return the string representation of this DataPoint + */ public String toString(){ String s; s = this.getLabel(); diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 22d211d..672a20a 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -4,13 +4,28 @@ import java.util.HashMap; import java.util.Map; public abstract class DataPoint{ - private static Map dataPointMap = new HashMap<>(); - private String label; - private boolean isOutput; + + + public static Map dataPointMap = new HashMap<>(); // Map of all DataPoints + private String label; // Label of this DataPoint + private boolean isOutput; // True if this DataPoint is an output, false if it's an input + + /** + * Constructor of DataPoint + * @param label label of this DataPoint + * @param isOutput true if this DataPoint is an output, false if it's an input + */ protected DataPoint(String label, boolean isOutput){ this.label = label; this.isOutput = isOutput; } + + + /** + * Update the value of this DataPoint and notify every connector + * If this is a new value (doesn't exist), add it to the dataPointMap + * @param isNewValue true if this is a new value, false if it's an update + */ protected void update(boolean isNewValue){ if(isNewValue){ @@ -22,7 +37,13 @@ public abstract class DataPoint{ // Update every connector DataPointListener.listeners.forEach(listener -> listener.onNewValue(this)); } - public static DataPoint getDataPointFromLabel(String label){ + + /** + * Get the DataPoint from the label + * @param label label of the DataPoint + * @return the DataPoint if it exists, null otherwise + */ + public static DataPoint getDataPointOnListFromLabel(String label){ if( !dataPointMap.containsKey(label) ){ return null; } else { diff --git a/src/main/java/ch/hevs/isi/core/FloatDataPoint.java b/src/main/java/ch/hevs/isi/core/FloatDataPoint.java index c168dd4..6941ff8 100644 --- a/src/main/java/ch/hevs/isi/core/FloatDataPoint.java +++ b/src/main/java/ch/hevs/isi/core/FloatDataPoint.java @@ -1,27 +1,42 @@ package ch.hevs.isi.core; -import ch.hevs.isi.db.DatabaseConnector; -import ch.hevs.isi.field.FieldConnector; -import ch.hevs.isi.web.WebConnector; - public class FloatDataPoint extends DataPoint{ private float value; + + /** + * Create a new DataPoint with a label and if is an Output or not + * @param label the label of this DataPoint + * @param isOutput true if this is an Output, false if it's an Input + */ public FloatDataPoint(String label, boolean isOutput) { super(label, isOutput); } + + /** + * Set the value of this DataPoint and create it if it doesn't exist via the update method + * @param value the value to set + */ public void setValue(float value){ this.value = value; - if (getDataPointFromLabel(this.getLabel()) == null){ + if (getDataPointOnListFromLabel(this.getLabel()) == null){ this.update(true); } else { this.update(false); } } + /** + * Get the value of this DataPoint + * @return the value of this DataPoint + */ public float getValue(){ return this.value; } + /** + * Convert this DataPoint to a string + * @return the string representation of this DataPoint + */ public String toString(){ String s; s = this.getLabel(); diff --git a/src/test/java/Database.java b/src/test/java/Database.java index 3ea14f7..f4f7553 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -13,7 +13,6 @@ 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); diff --git a/src/test/java/Web.java b/src/test/java/Web.java index f289052..ff0c5e6 100644 --- a/src/test/java/Web.java +++ b/src/test/java/Web.java @@ -24,16 +24,16 @@ public class Web { Thread.sleep(2000); if(WebConnector.getMySelf().wss.getConnections().size() > 0){ - FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointFromLabel("REMOTE_FACTORY_SP"); + FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_FACTORY_SP"); if(rfSP != null) new FloatDataPoint("FACTORY_ST", true).setValue(rfSP.getValue()); - FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointFromLabel("REMOTE_COAL_SP"); + FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_COAL_SP"); if(cfSP != null) new FloatDataPoint("COAL_ST", true).setValue(cfSP.getValue()); - BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointFromLabel("REMOTE_SOLAR_SW"); + BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_SOLAR_SW"); if(solar != null) new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(solar.getValue()); - BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointFromLabel("REMOTE_WIND_SW"); + BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_WIND_SW"); if(wind != null) new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(wind.getValue()); time += 0.1f;