From 95d48643f513464875a4d5b27a07c3a89b983139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= <63239207+Klagarge@users.noreply.github.com> Date: Fri, 26 May 2023 09:38:27 +0200 Subject: [PATCH 1/3] Create maven.yml --- .github/workflows/maven.yml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/maven.yml diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..001e7b5 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,35 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Java CI with Maven + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn -B package --file pom.xml + + # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive + - name: Update dependency graph + uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 From d1c222887a825df2b54536af97602eacae1fd4db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= <63239207+Klagarge@users.noreply.github.com> Date: Fri, 26 May 2023 09:41:46 +0200 Subject: [PATCH 2/3] Revert "Create maven.yml" --- .github/workflows/maven.yml | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 .github/workflows/maven.yml diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml deleted file mode 100644 index 001e7b5..0000000 --- a/.github/workflows/maven.yml +++ /dev/null @@ -1,35 +0,0 @@ -# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven - -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. - -name: Java CI with Maven - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Set up JDK 11 - uses: actions/setup-java@v3 - with: - java-version: '11' - distribution: 'temurin' - cache: maven - - name: Build with Maven - run: mvn -B package --file pom.xml - - # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive - - name: Update dependency graph - uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 From d58097d72319e43dfbbcb8d56beb154f788a287e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= <63239207+Klagarge@users.noreply.github.com> Date: Fri, 26 May 2023 12:46:35 +0200 Subject: [PATCH 3/3] Subscription (#7) * Change update DataPoints by a subscription way * Remove unused import * small polish --- src/main/java/ch/hevs/isi/core/DataPoint.java | 13 ++++--------- .../java/ch/hevs/isi/core/DataPointListener.java | 14 ++++++++++++++ .../java/ch/hevs/isi/db/DatabaseConnector.java | 9 ++++++--- .../java/ch/hevs/isi/field/FieldConnector.java | 3 ++- src/main/java/ch/hevs/isi/web/WebConnector.java | 2 ++ src/test/java/Database.java | 9 ++++----- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 47f51e8..4259189 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -1,9 +1,5 @@ package ch.hevs.isi.core; -import ch.hevs.isi.db.DatabaseConnector; -import ch.hevs.isi.field.FieldConnector; -import ch.hevs.isi.web.WebConnector; - import java.util.HashMap; import java.util.Map; @@ -22,16 +18,15 @@ 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) ){ return null; } else { - DataPoint dp = dataPointMap.get(label); - return dp; + return dataPointMap.get(label); } } diff --git a/src/main/java/ch/hevs/isi/core/DataPointListener.java b/src/main/java/ch/hevs/isi/core/DataPointListener.java index d2164ee..fdeded5 100644 --- a/src/main/java/ch/hevs/isi/core/DataPointListener.java +++ b/src/main/java/ch/hevs/isi/core/DataPointListener.java @@ -1,6 +1,20 @@ package ch.hevs.isi.core; +import java.util.Vector; + public interface DataPointListener { + // Vector of listeners + Vector 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); + } } diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index e91edb6..163d6d2 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -19,12 +19,12 @@ import java.util.Properties; public class DatabaseConnector implements DataPointListener { - private Properties properties = new Properties(); // Properties of the config.properties file + private final Properties properties = new Properties(); // Properties of the config.properties file private String fullURL = null; // Full URL of the InfluxDB server private URL urlForWrite = null; // URL of the InfluxDB server private HttpURLConnection con = null; // Connection to the InfluxDB server private boolean initialized = false; // Boolean to know if the database connector is initialized - private TimeManager _timeManager = new TimeManager(3); // Time manager to manage the time of the data points + private final TimeManager _timeManager = new TimeManager(3); // Time manager to manage the time of the data points private long _timestamp = 0; // Timestamp of the data points String default_token = System.getenv("SECRET_TOKEN"); // Token to access the InfluxDB server public static String url = null; // URL of the InfluxDB server @@ -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); } /** @@ -129,7 +132,7 @@ public class DatabaseConnector implements DataPointListener { */ private void pushToDatabase(DataPoint dp) { // Initialize the database connector if not already done - if(initialized == false){ + if(!initialized){ initialize(null); } diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index 3180d21..2720929 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -2,7 +2,6 @@ package ch.hevs.isi.field; import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPointListener; -import ch.hevs.isi.core.FloatDataPoint; public class FieldConnector implements DataPointListener { @@ -10,6 +9,8 @@ public class FieldConnector implements DataPointListener { private FieldConnector(){ + // Subscribe to the update of DataPoints + DataPointListener.subscribeUpdate(this); } public static FieldConnector getMySelf(){ diff --git a/src/main/java/ch/hevs/isi/web/WebConnector.java b/src/main/java/ch/hevs/isi/web/WebConnector.java index fd59f71..449e011 100644 --- a/src/main/java/ch/hevs/isi/web/WebConnector.java +++ b/src/main/java/ch/hevs/isi/web/WebConnector.java @@ -8,6 +8,8 @@ public class WebConnector implements DataPointListener { private WebConnector (){ + // Subscribe to the update of DataPoints + DataPointListener.subscribeUpdate(this); } public static WebConnector getMySelf(){ diff --git a/src/test/java/Database.java b/src/test/java/Database.java index 70815ef..3ea14f7 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -1,12 +1,9 @@ import ch.hevs.isi.MinecraftController; 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 java.io.FileInputStream; -import java.io.InputStream; -import java.util.Properties; +import ch.hevs.isi.field.FieldConnector; +import ch.hevs.isi.web.WebConnector; public class Database { public static void main(String[] args) { @@ -16,6 +13,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);