Create singleton and connect with datapoint
TODO some test
This commit is contained in:
parent
e0d0894040
commit
fe52981ba6
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,5 +3,5 @@
|
||||
/.idea/jarRepositories.xml
|
||||
/.idea/codeStyles
|
||||
/.idea/*.xml
|
||||
|
||||
/target/
|
||||
# TODO: add your build folder here
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
6
src/main/java/ch/hevs/isi/core/DataPointListener.java
Normal file
6
src/main/java/ch/hevs/isi/core/DataPointListener.java
Normal file
@ -0,0 +1,6 @@
|
||||
package ch.hevs.isi.core;
|
||||
|
||||
public interface DataPointListener {
|
||||
|
||||
void onNewValue(DataPoint dp);
|
||||
}
|
@ -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;
|
||||
|
32
src/main/java/ch/hevs/isi/db/DatabaseConnector.java
Normal file
32
src/main/java/ch/hevs/isi/db/DatabaseConnector.java
Normal 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);
|
||||
}
|
||||
}
|
39
src/main/java/ch/hevs/isi/field/FieldConnector.java
Normal file
39
src/main/java/ch/hevs/isi/field/FieldConnector.java
Normal 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);
|
||||
}
|
||||
}
|
31
src/main/java/ch/hevs/isi/web/WebConnector.java
Normal file
31
src/main/java/ch/hevs/isi/web/WebConnector.java
Normal 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
22
src/test/java/Core.java
Normal 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
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user