Subscription (#7)
* Change update DataPoints by a subscription way * Remove unused import * small polish
This commit is contained in:
parent
9fdcda2a00
commit
d58097d723
@ -1,9 +1,5 @@
|
|||||||
package ch.hevs.isi.core;
|
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.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -22,16 +18,15 @@ public abstract class DataPoint{
|
|||||||
} else {
|
} else {
|
||||||
dataPointMap.replace(this.label, this);
|
dataPointMap.replace(this.label, this);
|
||||||
}
|
}
|
||||||
DatabaseConnector.getMySelf().onNewValue(this);
|
|
||||||
WebConnector.getMySelf().onNewValue(this);
|
// Update every connector
|
||||||
FieldConnector.getMySelf().onNewValue(this);
|
DataPointListener.listeners.forEach(listener -> listener.onNewValue(this));
|
||||||
}
|
}
|
||||||
public static DataPoint getDataPointFromLabel(String label){
|
public static DataPoint getDataPointFromLabel(String label){
|
||||||
if( !dataPointMap.containsKey(label) ){
|
if( !dataPointMap.containsKey(label) ){
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
DataPoint dp = dataPointMap.get(label);
|
return dataPointMap.get(label);
|
||||||
return dp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
package ch.hevs.isi.core;
|
package ch.hevs.isi.core;
|
||||||
|
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
public interface DataPointListener {
|
public interface DataPointListener {
|
||||||
|
|
||||||
|
// Vector of listeners
|
||||||
|
Vector<DataPointListener> listeners = new Vector<>();
|
||||||
|
|
||||||
void onNewValue(DataPoint dp);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,12 @@ import java.util.Properties;
|
|||||||
|
|
||||||
|
|
||||||
public class DatabaseConnector implements DataPointListener {
|
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 String fullURL = null; // Full URL of the InfluxDB server
|
||||||
private URL urlForWrite = null; // URL of the InfluxDB server
|
private URL urlForWrite = null; // URL of the InfluxDB server
|
||||||
private HttpURLConnection con = null; // Connection to 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 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
|
private long _timestamp = 0; // Timestamp of the data points
|
||||||
String default_token = System.getenv("SECRET_TOKEN"); // Token to access the InfluxDB server
|
String default_token = System.getenv("SECRET_TOKEN"); // Token to access the InfluxDB server
|
||||||
public static String url = null; // URL of 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){
|
if (bucket == null){
|
||||||
bucket = properties.getProperty("BUCKET");
|
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) {
|
private void pushToDatabase(DataPoint dp) {
|
||||||
// Initialize the database connector if not already done
|
// Initialize the database connector if not already done
|
||||||
if(initialized == false){
|
if(!initialized){
|
||||||
initialize(null);
|
initialize(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ package ch.hevs.isi.field;
|
|||||||
|
|
||||||
import ch.hevs.isi.core.DataPoint;
|
import ch.hevs.isi.core.DataPoint;
|
||||||
import ch.hevs.isi.core.DataPointListener;
|
import ch.hevs.isi.core.DataPointListener;
|
||||||
import ch.hevs.isi.core.FloatDataPoint;
|
|
||||||
|
|
||||||
public class FieldConnector implements DataPointListener {
|
public class FieldConnector implements DataPointListener {
|
||||||
|
|
||||||
@ -10,6 +9,8 @@ public class FieldConnector implements DataPointListener {
|
|||||||
|
|
||||||
private FieldConnector(){
|
private FieldConnector(){
|
||||||
|
|
||||||
|
// Subscribe to the update of DataPoints
|
||||||
|
DataPointListener.subscribeUpdate(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FieldConnector getMySelf(){
|
public static FieldConnector getMySelf(){
|
||||||
|
@ -8,6 +8,8 @@ public class WebConnector implements DataPointListener {
|
|||||||
|
|
||||||
private WebConnector (){
|
private WebConnector (){
|
||||||
|
|
||||||
|
// Subscribe to the update of DataPoints
|
||||||
|
DataPointListener.subscribeUpdate(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WebConnector getMySelf(){
|
public static WebConnector getMySelf(){
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
import ch.hevs.isi.MinecraftController;
|
import ch.hevs.isi.MinecraftController;
|
||||||
import ch.hevs.isi.core.BooleanDataPoint;
|
import ch.hevs.isi.core.BooleanDataPoint;
|
||||||
import ch.hevs.isi.core.DataPoint;
|
|
||||||
import ch.hevs.isi.core.FloatDataPoint;
|
import ch.hevs.isi.core.FloatDataPoint;
|
||||||
import ch.hevs.isi.db.DatabaseConnector;
|
import ch.hevs.isi.db.DatabaseConnector;
|
||||||
|
import ch.hevs.isi.field.FieldConnector;
|
||||||
import java.io.FileInputStream;
|
import ch.hevs.isi.web.WebConnector;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
public class Database {
|
public class Database {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -16,6 +13,8 @@ public class Database {
|
|||||||
BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true);
|
BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true);
|
||||||
|
|
||||||
DatabaseConnector.getMySelf().initialize(null);
|
DatabaseConnector.getMySelf().initialize(null);
|
||||||
|
FieldConnector.getMySelf().initialize(null, 0);
|
||||||
|
WebConnector.getMySelf().initialize(null, 0);
|
||||||
|
|
||||||
clock.setValue(0f);
|
clock.setValue(0f);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user