1
0

Create singleton and connect with datapoint

TODO some test
This commit is contained in:
Rémi Heredero 2023-05-05 15:54:03 +02:00
parent e0d0894040
commit fe52981ba6
9 changed files with 178 additions and 9 deletions

2
.gitignore vendored
View File

@ -3,5 +3,5 @@
/.idea/jarRepositories.xml
/.idea/codeStyles
/.idea/*.xml
/target/
# TODO: add your build folder here

View File

@ -1,4 +1,8 @@
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;
@ -8,14 +12,19 @@ public class BooleanDataPoint extends DataPoint{
}
public void setValue(boolean value){
this.value = value;
if (getDataPointFromLabel(this.getLabel()) == null){
this.update(true);
} else {
this.update(false);
}
}
public boolean getValue(){
return this.value;
}
public String toString(){
String s = null;
s += this.getLabel();
String s;
s = this.getLabel();
s += ": ";
s += this.getValue();
return s;

View File

@ -1,20 +1,41 @@
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.Map;
public class DataPoint {
private Map<String, DataPoint> dataPointMap;
public abstract class DataPoint{
private static Map<String, DataPoint> dataPointMap;
private String label;
private boolean isOutput;
protected DataPoint(String label, boolean isOutput){
if(dataPointMap.containsKey(label)){
}
this.label = label;
this.isOutput = isOutput;
}
protected void update(boolean isNewValue){
if(isNewValue){
dataPointMap.put(this.label, this);
} else {
dataPointMap.replace(this.label, this);
}
DatabaseConnector.getMySelf().onNewValue(this);
WebConnector.getMySelf().onNewValue(this);
FieldConnector.getMySelf().onNewValue(this);
}
public DataPoint getDataPointFromLabel(String label){
return null;
public static DataPoint getDataPointFromLabel(String label){
if( !dataPointMap.containsKey(label) ){
return null;
} else {
DataPoint dp = dataPointMap.get(label);
return dp;
}
}

View File

@ -0,0 +1,6 @@
package ch.hevs.isi.core;
public interface DataPointListener {
void onNewValue(DataPoint dp);
}

View File

@ -1,5 +1,9 @@
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;
public FloatDataPoint(String label, boolean isOutput) {
@ -7,6 +11,11 @@ public class FloatDataPoint extends DataPoint{
}
public void setValue(float value){
this.value = value;
if (getDataPointFromLabel(this.getLabel()) == null){
this.update(true);
} else {
this.update(false);
}
}
public float getValue(){
@ -14,8 +23,8 @@ public class FloatDataPoint extends DataPoint{
}
public String toString(){
String s = null;
s += this.getLabel();
String s;
s = this.getLabel();
s += ": ";
s += this.getValue();
return s;

View File

@ -0,0 +1,32 @@
package ch.hevs.isi.db;
import ch.hevs.isi.core.DataPoint;
import ch.hevs.isi.core.DataPointListener;
public class DatabaseConnector implements DataPointListener {
private static DatabaseConnector mySelf = null;
private DatabaseConnector (){
}
public static DatabaseConnector getMySelf(){
if (mySelf == null){
mySelf = new DatabaseConnector();
}
return mySelf;
}
public void initialize(String url){
}
private void pushToDatabase(DataPoint dp){
System.out.println("To Database: " + dp.toString());
}
@Override
public void onNewValue(DataPoint dp) {
pushToDatabase(dp);
}
}

View File

@ -0,0 +1,39 @@
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 {
private static FieldConnector mySelf = null;
private FieldConnector(){
}
public static FieldConnector getMySelf(){
if (mySelf == null){
mySelf = new FieldConnector();
}
return mySelf;
}
public void initialize(String host, int port){
}
public void uselessTest(){
FloatDataPoint dp = new FloatDataPoint("Voltage", false);
dp.setValue(5);
}
private void pushToField(DataPoint dp){
System.out.println("To Field: " + dp.toString());
}
@Override
public void onNewValue(DataPoint dp) {
pushToField(dp);
}
}

View File

@ -0,0 +1,31 @@
package ch.hevs.isi.web;
import ch.hevs.isi.core.DataPoint;
import ch.hevs.isi.core.DataPointListener;
public class WebConnector implements DataPointListener {
private static WebConnector mySelf = null;
private WebConnector (){
}
public static WebConnector getMySelf(){
if (mySelf == null){
mySelf = new WebConnector();
}
return mySelf;
}
public void initialize(String host, int port){
}
private void pushToWeb(DataPoint dp){
System.out.println("To Web: " + dp.toString());
}
@Override
public void onNewValue(DataPoint dp) {
pushToWeb(dp);
}
}

22
src/test/java/Core.java Normal file
View File

@ -0,0 +1,22 @@
import ch.hevs.isi.core.BooleanDataPoint;
import ch.hevs.isi.core.FloatDataPoint;
import ch.hevs.isi.field.FieldConnector;
public class Core {
public static void main(String[] args) {
/*
BooleanDataPoint bDp = new BooleanDataPoint("foo", true);
FloatDataPoint floatDp = new FloatDataPoint("bar", true);
System.out.println(bDp.toString());
System.out.println(floatDp.toString());
bDp.setValue(true);;
floatDp.setValue(42);
System.out.println(bDp.toString());
System.out.println(floatDp.toString());
*/
FieldConnector fc;
// TODO some test
}
}