From 0d105fb64ecaf180595faf488f9538be573acc48 Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Fri, 26 May 2023 16:11:02 +0200 Subject: [PATCH] field not finished, connection works --- src/main/java/ch/hevs/isi/core/DataPoint.java | 2 -- .../ch/hevs/isi/field/BooleanRegister.java | 5 +-- .../ch/hevs/isi/field/FieldConnector.java | 13 +++---- .../java/ch/hevs/isi/field/FloatRegister.java | 11 +++--- .../ch/hevs/isi/field/ModbusRegister.java | 17 +++++----- src/test/java/Field.java | 34 +++++++++++++++++++ 6 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 src/test/java/Field.java diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 47f51e8..30c15c6 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -36,8 +36,6 @@ public abstract class DataPoint{ } - - /** * Just get the label of this DataPoint * @return the label in a string diff --git a/src/main/java/ch/hevs/isi/field/BooleanRegister.java b/src/main/java/ch/hevs/isi/field/BooleanRegister.java index 64c05c4..b33c86e 100644 --- a/src/main/java/ch/hevs/isi/field/BooleanRegister.java +++ b/src/main/java/ch/hevs/isi/field/BooleanRegister.java @@ -8,16 +8,17 @@ public class BooleanRegister extends ModbusRegister{ public BooleanRegister(String label, boolean isOutput, int address){ this.bdp = new BooleanDataPoint(label, isOutput); + value = bdp.getValue(); updateMapOfRegisters(bdp, address); } @Override public void read() { - bdp.setValue(ModbusAccessor.getMySelf().readBoolean(address)); + bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); } @Override public void write() { - ModbusAccessor.getMySelf().writeBoolean(address, bdp.getValue()); + ModbusAccessor.getMySelf().writeBoolean(this.getAddress(), bdp.getValue()); } } diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index f0730b0..e146266 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -16,21 +16,22 @@ public class FieldConnector implements DataPointListener { } return mySelf; } + public void initialize(String host, int port){ - ModbusAccessor mbA = ModbusAccessor.getMySelf(); - mbA.connect(host, port); + } + private void pushToField(DataPoint dp){ + ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); + mr.write(); } @Override public void onNewValue(DataPoint dp) { pushToField(dp); - ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); - mr.write(); } - public void periodicalPolling(){ + public void startPeriodicalPolling(){ Timer pollTimer = new Timer(); PollTask pollTask = new PollTask(); - pollTimer.scheduleAtFixedRate(pollTask,0,2000); + pollTimer.scheduleAtFixedRate(pollTask,0,100); } } diff --git a/src/main/java/ch/hevs/isi/field/FloatRegister.java b/src/main/java/ch/hevs/isi/field/FloatRegister.java index 9af7bdd..1f0ddde 100644 --- a/src/main/java/ch/hevs/isi/field/FloatRegister.java +++ b/src/main/java/ch/hevs/isi/field/FloatRegister.java @@ -4,20 +4,21 @@ import ch.hevs.isi.core.FloatDataPoint; public class FloatRegister extends ModbusRegister{ private Float value; - private FloatDataPoint dataPoint; + private FloatDataPoint fdp; public FloatRegister(String label, boolean isOutPut, int address) { - this.dataPoint = new FloatDataPoint(label, isOutPut); - updateMapOfRegisters(dataPoint,address); + this.fdp = new FloatDataPoint(label, isOutPut); + value = fdp.getValue(); + updateMapOfRegisters(fdp,address); } @Override public void read() { - dataPoint.setValue(ModbusAccessor.getMySelf().readFloat(address)); + fdp.setValue(ModbusAccessor.getMySelf().readFloat(this.getAddress())); } @Override public void write() { - ModbusAccessor.getMySelf().writeFloat(address, dataPoint.getValue()); + ModbusAccessor.getMySelf().writeFloat(this.getAddress(), fdp.getValue()); } } \ No newline at end of file diff --git a/src/main/java/ch/hevs/isi/field/ModbusRegister.java b/src/main/java/ch/hevs/isi/field/ModbusRegister.java index 27f7924..1511ea2 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusRegister.java +++ b/src/main/java/ch/hevs/isi/field/ModbusRegister.java @@ -3,24 +3,25 @@ package ch.hevs.isi.field; import ch.hevs.isi.core.DataPoint; import java.util.HashMap; -import java.util.Map; public abstract class ModbusRegister { - protected int address; - public static HashMap map = new HashMap<>(); + private int address; + public static HashMap mapOfRegisters = new HashMap<>(); public void updateMapOfRegisters(DataPoint dp, int address){ this.address = address; - map.put(dp,this); - } - public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ - return map.get(dp); + mapOfRegisters.put(dp,this); } + public int getAddress() { return address; } + public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ + return mapOfRegisters.get(dp); + } + public static void poll(){ - for (ModbusRegister mr : map.values()){ + for (ModbusRegister mr : mapOfRegisters.values()){ mr.read(); //read all values (registers) of the map } } diff --git a/src/test/java/Field.java b/src/test/java/Field.java new file mode 100644 index 0000000..7d7fff6 --- /dev/null +++ b/src/test/java/Field.java @@ -0,0 +1,34 @@ +import ch.hevs.isi.core.BooleanDataPoint; +import ch.hevs.isi.core.FloatDataPoint; +import ch.hevs.isi.field.BooleanRegister; +import ch.hevs.isi.field.FieldConnector; +import ch.hevs.isi.field.FloatRegister; + +public class Field { + public static void main(String[] args) { + + + FloatRegister fRI = new FloatRegister("GRID_U_FLOAT",false,89); + BooleanRegister bRI = new BooleanRegister("SOLAR_CONNECT_ST", false ,609); + FieldConnector.getMySelf().startPeriodicalPolling(); + + FloatRegister fRO = new FloatRegister("REMOTE_FACTORY_SP",true,205); + BooleanRegister bRO = new BooleanRegister("REMOTE_SOLAR_SW", true ,401); + + /* + fRO.fdp.setValue(0.8F); + bRO.bdp.setValue(true); + */ + + while(true) + { + /* + float value = fRI.dataPoint.getValue()*1000; + System.out.println(value); + */ + /* + System.out.println(bRI.bdp.getValue()); + */ + } + } +}