From 86aeae8b6fc499897754a2fbf745a942d3574219 Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Wed, 17 May 2023 16:08:18 +0200 Subject: [PATCH 01/13] ModbusAccessor finished and checked --- src/main/java/ch/hevs/isi/field/ModbusAccessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java index d5612ae..5e9f96a 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java +++ b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java @@ -137,7 +137,7 @@ public class ModbusAccessor { test.writeBoolean(401,false); //REMOTE_SOLAR_SW //check the factory SetPoint - System.out.println("Factory Setpoint is: " + factory_st); + System.out.println("Factory SetPoint is: " + factory_st); //check the solar SetPoint if(solar_connect_st){ From c3f15804b6ec4389e14aa3dd33af2c4ffcc28999 Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Tue, 23 May 2023 16:02:16 +0200 Subject: [PATCH 02/13] read and write Field done. pushToField to do --- .../ch/hevs/isi/field/BooleanRegister.java | 23 ++++++ .../ch/hevs/isi/field/FieldConnector.java | 20 +++--- .../java/ch/hevs/isi/field/FloatRegister.java | 23 ++++++ .../ch/hevs/isi/field/ModbusAccessor.java | 71 ++++++++++++------- .../ch/hevs/isi/field/ModbusRegister.java | 29 ++++++++ src/main/java/ch/hevs/isi/field/PollTask.java | 10 +++ 6 files changed, 143 insertions(+), 33 deletions(-) create mode 100644 src/main/java/ch/hevs/isi/field/BooleanRegister.java create mode 100644 src/main/java/ch/hevs/isi/field/FloatRegister.java create mode 100644 src/main/java/ch/hevs/isi/field/ModbusRegister.java create mode 100644 src/main/java/ch/hevs/isi/field/PollTask.java diff --git a/src/main/java/ch/hevs/isi/field/BooleanRegister.java b/src/main/java/ch/hevs/isi/field/BooleanRegister.java new file mode 100644 index 0000000..64c05c4 --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/BooleanRegister.java @@ -0,0 +1,23 @@ +package ch.hevs.isi.field; + +import ch.hevs.isi.core.BooleanDataPoint; + +public class BooleanRegister extends ModbusRegister{ + private boolean value; + private BooleanDataPoint bdp; + + public BooleanRegister(String label, boolean isOutput, int address){ + this.bdp = new BooleanDataPoint(label, isOutput); + updateMapOfRegisters(bdp, address); + } + + @Override + public void read() { + bdp.setValue(ModbusAccessor.getMySelf().readBoolean(address)); + } + + @Override + public void write() { + ModbusAccessor.getMySelf().writeBoolean(address, 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 3180d21..f0730b0 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -4,31 +4,33 @@ import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPointListener; import ch.hevs.isi.core.FloatDataPoint; +import java.util.Timer; + 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){ - + ModbusAccessor mbA = ModbusAccessor.getMySelf(); + mbA.connect(host, port); } - private void pushToField(DataPoint dp){ - System.out.println(dp.toString() + " -> Field"); } - @Override public void onNewValue(DataPoint dp) { pushToField(dp); + ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); + mr.write(); + } + public void periodicalPolling(){ + Timer pollTimer = new Timer(); + PollTask pollTask = new PollTask(); + pollTimer.scheduleAtFixedRate(pollTask,0,2000); } } diff --git a/src/main/java/ch/hevs/isi/field/FloatRegister.java b/src/main/java/ch/hevs/isi/field/FloatRegister.java new file mode 100644 index 0000000..9af7bdd --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/FloatRegister.java @@ -0,0 +1,23 @@ +package ch.hevs.isi.field; + +import ch.hevs.isi.core.FloatDataPoint; + +public class FloatRegister extends ModbusRegister{ + private Float value; + private FloatDataPoint dataPoint; + + public FloatRegister(String label, boolean isOutPut, int address) { + this.dataPoint = new FloatDataPoint(label, isOutPut); + updateMapOfRegisters(dataPoint,address); + } + + @Override + public void read() { + dataPoint.setValue(ModbusAccessor.getMySelf().readFloat(address)); + } + + @Override + public void write() { + ModbusAccessor.getMySelf().writeFloat(address, dataPoint.getValue()); + } +} \ No newline at end of file diff --git a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java index 5e9f96a..f54253d 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java +++ b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java @@ -9,6 +9,7 @@ import com.serotonin.modbus4j.exception.ModbusTransportException; import com.serotonin.modbus4j.ip.IpParameters; import com.serotonin.modbus4j.ip.tcp.TcpMaster; import com.serotonin.modbus4j.locator.BaseLocator; +import com.sun.org.apache.xpath.internal.operations.Mod; public class ModbusAccessor { @@ -63,27 +64,39 @@ public class ModbusAccessor { * method to write a boolean value in the correct modbus register * @param register address of the register * @param value the desired boolean value - * @throws ModbusTransportException - * @throws ErrorResponseException */ - public void writeBoolean (int register, boolean value) throws ModbusTransportException, ErrorResponseException{ + public void writeBoolean (int register, boolean value){ //first parameter = slaveID = always 1 because we only have one //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address - this.master.setValue(BaseLocator.coilStatus(1,register), value); //set the desired value in the correct register + try{ + this.master.setValue(BaseLocator.coilStatus(1,register), value); //set the desired value in the correct register + } + catch (ModbusTransportException e){ + e.printStackTrace(); + } + catch (ErrorResponseException e){ + e.printStackTrace(); + } } /** * method to write a float value in the correct modbus register * @param register address of the register * @param value the desired float value - * @throws ModbusTransportException - * @throws ErrorResponseException */ - public void writeFloat (int register, float value) throws ModbusTransportException, ErrorResponseException{ + public void writeFloat (int register, float value){ //first parameter = slaveID = always 1 because we only have one //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address //third parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte - this.master.setValue(BaseLocator.holdingRegister(1,register,DataType.FOUR_BYTE_FLOAT), value); + try{ + this.master.setValue(BaseLocator.holdingRegister(1,register,DataType.FOUR_BYTE_FLOAT), value); + } + catch (ModbusTransportException e){ + e.printStackTrace(); + } + catch (ErrorResponseException e){ + e.printStackTrace(); + } } //methods to read /** @@ -91,41 +104,55 @@ public class ModbusAccessor { * get the coil status of the register. * @param register address of register * @return boolean value of the desired register - * @throws ModbusTransportException - * @throws ErrorResponseException */ - public boolean readBoolean(int register) throws ModbusTransportException, ErrorResponseException { + public boolean readBoolean(int register){ //first parameter = slaveID = always 1 because we only have one //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address - boolean booleanValue = this.master.getValue(BaseLocator.coilStatus(1,register)); - return booleanValue; + try{ + boolean booleanValue = this.master.getValue(BaseLocator.coilStatus(1,register)); + return booleanValue; + } + catch (ModbusTransportException e){ + e.printStackTrace(); + } + catch (ErrorResponseException e){ + e.printStackTrace(); + } + return false; } /** * method to read a boolean value in the correct modbus register * get the value from the holding register * @param register address of the register * @return float value of the desired register - * @throws ModbusTransportException - * @throws ErrorResponseException */ - public float readFloat(int register)throws ModbusTransportException, ErrorResponseException{ + public float readFloat(int register){ //first parameter = slaveID = always 1 because we only have one //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address //third parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte - float floatValue = (float) this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT)); - return floatValue; + try { + float floatValue = (float) this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT)); + return floatValue; + } + catch (ModbusTransportException e){ + e.printStackTrace(); + } + catch (ErrorResponseException e){ + e.printStackTrace(); + } + return 0F; } /** * this main method is only for testing the ModbusAccessor class * @param args */ + /* public static void main(String[] args) { //create an instance of ModbusAccessor and connect it with the server ModbusAccessor test = ModbusAccessor.getMySelf(); test.connect("LocalHost", 1502); //do this all the time while(true){ - try{ //get a boolean value => solar SetPoint boolean solar_connect_st = test.readBoolean(609); //SOLAR_CONNECT_ST //get a float value => factory SetPoint @@ -145,13 +172,9 @@ public class ModbusAccessor { }else{ System.out.println("Solar is disconnected"); } - }catch (ModbusTransportException e){ - - }catch (ErrorResponseException e){ - - } } } + */ } diff --git a/src/main/java/ch/hevs/isi/field/ModbusRegister.java b/src/main/java/ch/hevs/isi/field/ModbusRegister.java new file mode 100644 index 0000000..27f7924 --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/ModbusRegister.java @@ -0,0 +1,29 @@ +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<>(); + public void updateMapOfRegisters(DataPoint dp, int address){ + this.address = address; + map.put(dp,this); + } + public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ + return map.get(dp); + } + public int getAddress() { + return address; + } + + public static void poll(){ + for (ModbusRegister mr : map.values()){ + mr.read(); //read all values (registers) of the map + } + } + public abstract void read(); //abstract prototype of the method read + public abstract void write(); //abstract prototype of the method read +} diff --git a/src/main/java/ch/hevs/isi/field/PollTask.java b/src/main/java/ch/hevs/isi/field/PollTask.java new file mode 100644 index 0000000..a01ccc2 --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/PollTask.java @@ -0,0 +1,10 @@ +package ch.hevs.isi.field; + +import java.util.TimerTask; + +public class PollTask extends TimerTask { + @Override + public void run() { + ModbusRegister.poll(); + } +} From 4b3a0bedd49b580226de447c541f9d1acd9806bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= <63239207+Klagarge@users.noreply.github.com> Date: Thu, 25 May 2023 21:17:11 +0200 Subject: [PATCH 03/13] Change update DataPoints by a subscription way --- src/main/java/ch/hevs/isi/core/DataPoint.java | 6 +++--- .../java/ch/hevs/isi/core/DataPointListener.java | 14 ++++++++++++++ .../java/ch/hevs/isi/db/DatabaseConnector.java | 3 +++ .../java/ch/hevs/isi/field/FieldConnector.java | 2 ++ src/main/java/ch/hevs/isi/web/WebConnector.java | 2 ++ src/test/java/Database.java | 4 ++++ 6 files changed, 28 insertions(+), 3 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..fc6287e 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -22,9 +22,9 @@ 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) ){ 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..0f094f6 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -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); } /** diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index 3180d21..a9c85ea 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -10,6 +10,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..9d9250a 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -3,6 +3,8 @@ 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 ch.hevs.isi.field.FieldConnector; +import ch.hevs.isi.web.WebConnector; import java.io.FileInputStream; import java.io.InputStream; @@ -16,6 +18,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); From c5a1e49e60c96121bb468667a061a4992c5fac24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= <63239207+Klagarge@users.noreply.github.com> Date: Thu, 25 May 2023 21:20:20 +0200 Subject: [PATCH 04/13] Remove unused import --- src/main/java/ch/hevs/isi/core/DataPoint.java | 4 ---- src/main/java/ch/hevs/isi/db/DatabaseConnector.java | 4 ++-- src/main/java/ch/hevs/isi/field/FieldConnector.java | 1 - src/test/java/Database.java | 5 ----- 4 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index fc6287e..cfc5543 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; diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index 0f094f6..0b17712 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -24,7 +24,7 @@ public class DatabaseConnector implements DataPointListener { 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 @@ -132,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 a9c85ea..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 { diff --git a/src/test/java/Database.java b/src/test/java/Database.java index 9d9250a..3ea14f7 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -1,15 +1,10 @@ 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 ch.hevs.isi.field.FieldConnector; import ch.hevs.isi.web.WebConnector; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.Properties; - public class Database { public static void main(String[] args) { MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true; From df941eea489514725f7f0e58b56ee0bf58c63660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= <63239207+Klagarge@users.noreply.github.com> Date: Thu, 25 May 2023 21:22:59 +0200 Subject: [PATCH 05/13] small polish --- src/main/java/ch/hevs/isi/core/DataPoint.java | 3 +-- src/main/java/ch/hevs/isi/db/DatabaseConnector.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index cfc5543..4259189 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -26,8 +26,7 @@ public abstract class DataPoint{ 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/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index 0b17712..163d6d2 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -19,7 +19,7 @@ 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 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 06/13] 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()); + */ + } + } +} From 55fa80c4d4200948b11f058eb0c72250e6c39fcd Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Tue, 30 May 2023 11:31:01 +0200 Subject: [PATCH 07/13] read csv file and add some comments --- src/main/java/ch/hevs/isi/core/DataPoint.java | 1 - .../ch/hevs/isi/field/BooleanRegister.java | 2 +- .../ch/hevs/isi/field/FieldConnector.java | 76 +++++++++++++++++-- .../java/ch/hevs/isi/field/FloatRegister.java | 6 +- .../ch/hevs/isi/field/ModbusAccessor.java | 1 - .../ch/hevs/isi/field/ModbusRegister.java | 31 +++++--- src/test/java/Field.java | 4 +- 7 files changed, 99 insertions(+), 22 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 30c15c6..6923ae4 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -35,7 +35,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 b33c86e..a71e5f0 100644 --- a/src/main/java/ch/hevs/isi/field/BooleanRegister.java +++ b/src/main/java/ch/hevs/isi/field/BooleanRegister.java @@ -1,6 +1,7 @@ package ch.hevs.isi.field; import ch.hevs.isi.core.BooleanDataPoint; +import ch.hevs.isi.core.DataPoint; public class BooleanRegister extends ModbusRegister{ private boolean value; @@ -11,7 +12,6 @@ public class BooleanRegister extends ModbusRegister{ value = bdp.getValue(); updateMapOfRegisters(bdp, address); } - @Override public void read() { bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index e146266..3d26045 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -4,12 +4,24 @@ import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPointListener; import ch.hevs.isi.core.FloatDataPoint; +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.util.Timer; public class FieldConnector implements DataPointListener { private static FieldConnector mySelf = null; private FieldConnector(){ + initialize("LocalHost",1502, "C:/Nils/Hesso/4_Semester/SIN/Minecraft_Electrical_Age_Project/ModbusMap.csv"); } + + /** + * static method to create a singleton pattern of the class + * checks if an instance of the class is already made + * if not, it creates an instance of the class FieldConnector + * @return instance of FieldConnector + */ public static FieldConnector getMySelf(){ if (mySelf == null){ mySelf = new FieldConnector(); @@ -17,18 +29,70 @@ public class FieldConnector implements DataPointListener { return mySelf; } - public void initialize(String host, int port){ - + /** + * read the csv-file of the ModbusMap + * @param pathToFile path to the file of all modbus registers (C:/.../ModbusMap.csv) + */ + public static void createRegister(String pathToFile){ + try{ + BufferedReader csvFile = new BufferedReader(new FileReader(pathToFile)); + csvFile.readLine(); + + while(csvFile.ready()){ + String line = csvFile.readLine(); // read one line + String[] splitLine = line.split(";"); // split line between ";" + + String label = splitLine[0]; // first split is the label of the register + boolean isOutput = splitLine[3].equals("N"); // third split declares if it is an output + int address = new Integer((splitLine[4])); // fourth split is the address of the register + float range = new Float(splitLine[5]); // if it is a floatDatapoint, the fifth split is the range of the data + float offset = new Float(splitLine[6]); // if it is a floatDatapoint, the sixth split is the offset of the data + + /* + // create a float or a boolean register + if (splitLine[1].equals("B")){ + BooleanRegister b = new BooleanRegister(label, isOutput, address); + } else if(splitLine[1].equals("F")){ + FloatRegister f = new FloatRegister(label, isOutput, address, range, offset); + } + */ + } + } + catch(FileNotFoundException e){ + e.printStackTrace(); + } + catch (IOException e){ + e.printStackTrace(); + } } - private void pushToField(DataPoint dp){ - ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); - mr.write(); + /** + * this method initialize the fieldConnector + * it connects the ModbusAccessor, reads the csv-file of the ModbusMap + * and starts the periodical polling of the modbus registers + * @param host ip address of the server + * @param port port of the server + * @param pathToFile path to the file of all modbus registers (C:/.../ModbusMap.csv) + */ + public void initialize(String host, int port, String pathToFile){ + ModbusAccessor.getMySelf().connect(host,port); + createRegister(pathToFile); + startPeriodicalPolling(); + } + private void pushToField(String label, String value){ + System.out.println("Field: " + label + " " + value); } @Override public void onNewValue(DataPoint dp) { - pushToField(dp); + ModbusRegister mR = ModbusRegister.getRegisterFromDatapoint(dp); + mR.write(); + pushToField(dp.getLabel(),dp.toString()); } + + /** + * method to start a periodical task + * in our case it is the reading of the modbus registers + */ public void startPeriodicalPolling(){ Timer pollTimer = new Timer(); PollTask pollTask = new PollTask(); diff --git a/src/main/java/ch/hevs/isi/field/FloatRegister.java b/src/main/java/ch/hevs/isi/field/FloatRegister.java index 1f0ddde..214bcf0 100644 --- a/src/main/java/ch/hevs/isi/field/FloatRegister.java +++ b/src/main/java/ch/hevs/isi/field/FloatRegister.java @@ -1,12 +1,14 @@ package ch.hevs.isi.field; +import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.FloatDataPoint; +import java.util.HashMap; + public class FloatRegister extends ModbusRegister{ private Float value; private FloatDataPoint fdp; - - public FloatRegister(String label, boolean isOutPut, int address) { + public FloatRegister(String label, boolean isOutPut, int address, float range, float offset) { this.fdp = new FloatDataPoint(label, isOutPut); value = fdp.getValue(); updateMapOfRegisters(fdp,address); diff --git a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java index f54253d..bff52b3 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java +++ b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java @@ -20,7 +20,6 @@ public class ModbusAccessor { * private constructor * */ private ModbusAccessor(){ - } /** diff --git a/src/main/java/ch/hevs/isi/field/ModbusRegister.java b/src/main/java/ch/hevs/isi/field/ModbusRegister.java index 1511ea2..f963845 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusRegister.java +++ b/src/main/java/ch/hevs/isi/field/ModbusRegister.java @@ -1,25 +1,38 @@ package ch.hevs.isi.field; import ch.hevs.isi.core.DataPoint; +import ch.hevs.isi.core.FloatDataPoint; import java.util.HashMap; public abstract class ModbusRegister { private int address; - public static HashMap mapOfRegisters = new HashMap<>(); + + /** + * get the address of the modbus register + * @return address of the modbus register + */ + public int getAddress() { + return address; + } + private final static HashMap mapOfRegisters = new HashMap<>(); + + /** + * get the modbus register from the desired datapoint + * @param dp the desired datapoint + * @return modbus register + */ + public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ + return mapOfRegisters.get(dp); + } public void updateMapOfRegisters(DataPoint dp, int address){ this.address = address; mapOfRegisters.put(dp,this); } - public int getAddress() { - return address; - } - - public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ - return mapOfRegisters.get(dp); - } - + /** + * read periodically each modbus register + */ public static void poll(){ 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 index 7d7fff6..7cd5802 100644 --- a/src/test/java/Field.java +++ b/src/test/java/Field.java @@ -8,11 +8,11 @@ public class Field { public static void main(String[] args) { - FloatRegister fRI = new FloatRegister("GRID_U_FLOAT",false,89); + //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); + //FloatRegister fRO = new FloatRegister("REMOTE_FACTORY_SP",true,205); BooleanRegister bRO = new BooleanRegister("REMOTE_SOLAR_SW", true ,401); /* From be9c484001b245aab080b36421a94bcfee394545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 6 Jun 2023 13:38:49 +0200 Subject: [PATCH 08/13] add comment on Core --- .../ch/hevs/isi/core/BooleanDataPoint.java | 25 ++++++++++++---- src/main/java/ch/hevs/isi/core/DataPoint.java | 29 ++++++++++++++++--- .../java/ch/hevs/isi/core/FloatDataPoint.java | 25 ++++++++++++---- src/test/java/Database.java | 1 - src/test/java/Web.java | 8 ++--- 5 files changed, 69 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java b/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java index d18131f..dc78181 100644 --- a/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java +++ b/src/main/java/ch/hevs/isi/core/BooleanDataPoint.java @@ -1,27 +1,42 @@ 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; + /** + * Create a new DataPoint with a label and if is an Output or not + * @param label the label of this DataPoint + * @param isOutput true if this is an Output, false if it's an Input + */ public BooleanDataPoint(String label, boolean isOutput) { super(label, isOutput); } + + /** + * Set the value of this DataPoint and create it if it doesn't exist via the update method + * @param value the value to set + */ public void setValue(boolean value){ this.value = value; - if (getDataPointFromLabel(this.getLabel()) == null){ + if (getDataPointOnListFromLabel(this.getLabel()) == null){ this.update(true); } else { this.update(false); } } + + /** + * Get the value of this DataPoint + * @return the value of this DataPoint + */ public boolean getValue(){ return this.value; } + /** + * Convert this DataPoint to a string + * @return the string representation of this DataPoint + */ public String toString(){ String s; s = this.getLabel(); diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 22d211d..672a20a 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -4,13 +4,28 @@ import java.util.HashMap; import java.util.Map; public abstract class DataPoint{ - private static Map dataPointMap = new HashMap<>(); - private String label; - private boolean isOutput; + + + public static Map dataPointMap = new HashMap<>(); // Map of all DataPoints + private String label; // Label of this DataPoint + private boolean isOutput; // True if this DataPoint is an output, false if it's an input + + /** + * Constructor of DataPoint + * @param label label of this DataPoint + * @param isOutput true if this DataPoint is an output, false if it's an input + */ protected DataPoint(String label, boolean isOutput){ this.label = label; this.isOutput = isOutput; } + + + /** + * Update the value of this DataPoint and notify every connector + * If this is a new value (doesn't exist), add it to the dataPointMap + * @param isNewValue true if this is a new value, false if it's an update + */ protected void update(boolean isNewValue){ if(isNewValue){ @@ -22,7 +37,13 @@ public abstract class DataPoint{ // Update every connector DataPointListener.listeners.forEach(listener -> listener.onNewValue(this)); } - public static DataPoint getDataPointFromLabel(String label){ + + /** + * Get the DataPoint from the label + * @param label label of the DataPoint + * @return the DataPoint if it exists, null otherwise + */ + public static DataPoint getDataPointOnListFromLabel(String label){ if( !dataPointMap.containsKey(label) ){ return null; } else { diff --git a/src/main/java/ch/hevs/isi/core/FloatDataPoint.java b/src/main/java/ch/hevs/isi/core/FloatDataPoint.java index c168dd4..6941ff8 100644 --- a/src/main/java/ch/hevs/isi/core/FloatDataPoint.java +++ b/src/main/java/ch/hevs/isi/core/FloatDataPoint.java @@ -1,27 +1,42 @@ 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; + + /** + * Create a new DataPoint with a label and if is an Output or not + * @param label the label of this DataPoint + * @param isOutput true if this is an Output, false if it's an Input + */ public FloatDataPoint(String label, boolean isOutput) { super(label, isOutput); } + + /** + * Set the value of this DataPoint and create it if it doesn't exist via the update method + * @param value the value to set + */ public void setValue(float value){ this.value = value; - if (getDataPointFromLabel(this.getLabel()) == null){ + if (getDataPointOnListFromLabel(this.getLabel()) == null){ this.update(true); } else { this.update(false); } } + /** + * Get the value of this DataPoint + * @return the value of this DataPoint + */ public float getValue(){ return this.value; } + /** + * Convert this DataPoint to a string + * @return the string representation of this DataPoint + */ public String toString(){ String s; s = this.getLabel(); diff --git a/src/test/java/Database.java b/src/test/java/Database.java index 3ea14f7..f4f7553 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -13,7 +13,6 @@ 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); diff --git a/src/test/java/Web.java b/src/test/java/Web.java index f289052..ff0c5e6 100644 --- a/src/test/java/Web.java +++ b/src/test/java/Web.java @@ -24,16 +24,16 @@ public class Web { Thread.sleep(2000); if(WebConnector.getMySelf().wss.getConnections().size() > 0){ - FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointFromLabel("REMOTE_FACTORY_SP"); + FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_FACTORY_SP"); if(rfSP != null) new FloatDataPoint("FACTORY_ST", true).setValue(rfSP.getValue()); - FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointFromLabel("REMOTE_COAL_SP"); + FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_COAL_SP"); if(cfSP != null) new FloatDataPoint("COAL_ST", true).setValue(cfSP.getValue()); - BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointFromLabel("REMOTE_SOLAR_SW"); + BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_SOLAR_SW"); if(solar != null) new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(solar.getValue()); - BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointFromLabel("REMOTE_WIND_SW"); + BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_WIND_SW"); if(wind != null) new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(wind.getValue()); time += 0.1f; From fa54c5fbb372b480d52781b49a1292a46bcdfed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 6 Jun 2023 13:51:28 +0200 Subject: [PATCH 09/13] add web on main --- src/config.properties | 8 ++++--- .../java/ch/hevs/isi/MinecraftController.java | 21 ++++++++++++++++--- .../ch/hevs/isi/db/DatabaseConnector.java | 8 +++---- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/config.properties b/src/config.properties index bba40f7..8f4c695 100644 --- a/src/config.properties +++ b/src/config.properties @@ -1,3 +1,5 @@ -URL = https://influx.sdi.hevs.ch -ORG = SIn15 -BUCKET = SIn15 \ No newline at end of file +DB.URL = https://influx.sdi.hevs.ch +DB.ORG = SIn15 +DB.BUCKET = SIn15 +WEB.URL = localhost +WEB.PORT = 8888 \ No newline at end of file diff --git a/src/main/java/ch/hevs/isi/MinecraftController.java b/src/main/java/ch/hevs/isi/MinecraftController.java index 6bc5059..2567af5 100644 --- a/src/main/java/ch/hevs/isi/MinecraftController.java +++ b/src/main/java/ch/hevs/isi/MinecraftController.java @@ -1,13 +1,17 @@ package ch.hevs.isi; -import ch.hevs.isi.core.BooleanDataPoint; -import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.db.DatabaseConnector; import ch.hevs.isi.utils.Utility; +import ch.hevs.isi.web.WebConnector; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.Properties; public class MinecraftController { public static boolean ERASE_PREVIOUS_DATA_INB_DB = false; + private static final Properties properties = new Properties(); // Properties of the config.properties file public static void usage() { System.out.println(); @@ -75,7 +79,12 @@ public class MinecraftController { // ------------------------------------ /DO NOT CHANGE THE FOLLOWING LINES ------------------------------------- - // Start coding here ... + // Read the config.properties file + try (InputStream input = new FileInputStream("src/config.properties")) { + properties.load(input); + } catch (Exception e) { + e.printStackTrace(); + } // Initialize the database connector if((dbProtocol != null) && (dbHostName != null)){ @@ -92,5 +101,11 @@ public class MinecraftController { // Initialize the Modbus TCP connector + // Initialize the web server + String host = properties.getProperty("WEB.URL"); + int port = Integer.parseInt(properties.getProperty("WEB.PORT")); + WebConnector.getMySelf().initialize(host, port); + + } } diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index 163d6d2..d87b584 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -48,13 +48,13 @@ public class DatabaseConnector implements DataPointListener { // Get the URL, the organization and the bucket from the config.properties file if their are null if (url == null){ - url = properties.getProperty("URL"); + url = properties.getProperty("DB.URL"); } if (org == null){ - org = properties.getProperty("ORG"); + org = properties.getProperty("DB.ORG"); } if (bucket == null){ - bucket = properties.getProperty("BUCKET"); + bucket = properties.getProperty("DB.BUCKET"); } // Subscribe to the update of DataPoints @@ -80,7 +80,7 @@ public class DatabaseConnector implements DataPointListener { try{ if(urlForWrite == null){ if(url == null){ - url = properties.getProperty("URL"); + url = properties.getProperty("DB.URL"); } fullURL = url + "/api/v2/write?org=" + org + "&bucket=" + bucket; Utility.pDebug("URL: " + fullURL); From 6f51ae034124181ffd7e380217cc62eb7b39ef54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 6 Jun 2023 13:54:53 +0200 Subject: [PATCH 10/13] fix some warnings --- src/main/java/ch/hevs/isi/core/DataPoint.java | 4 ++-- src/main/java/ch/hevs/isi/db/DatabaseConnector.java | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 672a20a..636ab60 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -7,8 +7,8 @@ public abstract class DataPoint{ public static Map dataPointMap = new HashMap<>(); // Map of all DataPoints - private String label; // Label of this DataPoint - private boolean isOutput; // True if this DataPoint is an output, false if it's an input + private final String label; // Label of this DataPoint + private final boolean isOutput; // True if this DataPoint is an output, false if it's an input /** * Constructor of DataPoint diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index d87b584..c6e5c5e 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -15,12 +15,13 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Properties; public class DatabaseConnector implements DataPointListener { 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 @@ -40,7 +41,7 @@ public class DatabaseConnector implements DataPointListener { */ private DatabaseConnector (){ // Read the config.properties file - try (InputStream input = new FileInputStream("src/config.properties")) { + try (InputStream input = Files.newInputStream(Paths.get("src/config.properties"))) { properties.load(input); } catch (Exception e) { e.printStackTrace(); @@ -77,6 +78,8 @@ public class DatabaseConnector implements DataPointListener { * @param url URL of the database. If null take the URL from the config.properties file */ public void initialize(String url){ + // Full URL of the InfluxDB server + String fullURL = null; try{ if(urlForWrite == null){ if(url == null){ @@ -106,10 +109,6 @@ public class DatabaseConnector implements DataPointListener { con.setRequestProperty("Content-Type", "text/plain"); con.setRequestProperty("Accept", "application/json"); con.setDoOutput(true); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } catch (ProtocolException e) { - throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } @@ -122,7 +121,7 @@ public class DatabaseConnector implements DataPointListener { data += "}"; Utility.pDebug(data); sendDataToDatabase(data); - fullURL=null; // Reset the full URL + fullURL =null; // Reset the full URL } } From e973693faa13c0497aa15917ef7febc71fbdf05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 6 Jun 2023 13:55:53 +0200 Subject: [PATCH 11/13] optimize imports --- src/main/java/ch/hevs/isi/db/DatabaseConnector.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index c6e5c5e..cd66e31 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -7,13 +7,11 @@ import ch.hevs.isi.core.DataPointListener; import ch.hevs.isi.core.FloatDataPoint; import ch.hevs.isi.utils.Utility; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.ProtocolException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; From c478c0a7bf0b76166f11d55c1f1e76f2213da233 Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Tue, 6 Jun 2023 17:50:17 +0200 Subject: [PATCH 12/13] changed the field component, made some test and added some comments --- src/main/java/ch/hevs/isi/core/DataPoint.java | 4 +- .../ch/hevs/isi/field/BooleanRegister.java | 20 +++++++--- .../ch/hevs/isi/field/FieldConnector.java | 39 ++++++++++++------- .../java/ch/hevs/isi/field/FloatRegister.java | 23 ++++++++--- .../ch/hevs/isi/field/ModbusRegister.java | 23 ++++++----- src/main/java/ch/hevs/isi/field/PollTask.java | 2 + src/main/resources/ModbusMap.csv | 27 +++++++++++++ src/test/java/Field.java | 30 +++++--------- 8 files changed, 111 insertions(+), 57 deletions(-) create mode 100644 src/main/resources/ModbusMap.csv diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 6923ae4..1a87291 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -22,8 +22,8 @@ public abstract class DataPoint{ } else { dataPointMap.replace(this.label, this); } - DatabaseConnector.getMySelf().onNewValue(this); - WebConnector.getMySelf().onNewValue(this); + //DatabaseConnector.getMySelf().onNewValue(this); + //WebConnector.getMySelf().onNewValue(this); FieldConnector.getMySelf().onNewValue(this); } public static DataPoint getDataPointFromLabel(String label){ diff --git a/src/main/java/ch/hevs/isi/field/BooleanRegister.java b/src/main/java/ch/hevs/isi/field/BooleanRegister.java index a71e5f0..71716a0 100644 --- a/src/main/java/ch/hevs/isi/field/BooleanRegister.java +++ b/src/main/java/ch/hevs/isi/field/BooleanRegister.java @@ -7,18 +7,26 @@ public class BooleanRegister extends ModbusRegister{ private boolean value; private BooleanDataPoint bdp; + /** + * public constructor of the booleanRegister + * + * @param label label of the datapoint + * @param isOutput true if it is an output datapoint + * @param address modbus address of the datapoint + */ public BooleanRegister(String label, boolean isOutput, int address){ - this.bdp = new BooleanDataPoint(label, isOutput); - value = bdp.getValue(); - updateMapOfRegisters(bdp, address); + this.bdp = new BooleanDataPoint(label, isOutput); //create an empty datapoint for the NullPointerException + updateMapOfRegisters(label, address); //add the address to the map } @Override public void read() { - bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); + bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); //read the value } @Override - public void write() { - ModbusAccessor.getMySelf().writeBoolean(this.getAddress(), bdp.getValue()); + public void write(DataPoint dp) { + bdp = (BooleanDataPoint) dp; //make the empty datapoint to the desired datapoint, which is to be written on the address + value = bdp.getValue(); //store the value of the datapoint + ModbusAccessor.getMySelf().writeBoolean(this.getAddress(),bdp.getValue()); //write the desired value on the modbus address } } diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index 3d26045..2991a18 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -12,8 +12,9 @@ import java.util.Timer; public class FieldConnector implements DataPointListener { private static FieldConnector mySelf = null; + private FieldConnector(){ - initialize("LocalHost",1502, "C:/Nils/Hesso/4_Semester/SIN/Minecraft_Electrical_Age_Project/ModbusMap.csv"); + initialize("LocalHost",1502, "src/main/resources/ModbusMap.csv"); } /** @@ -48,14 +49,15 @@ public class FieldConnector implements DataPointListener { float range = new Float(splitLine[5]); // if it is a floatDatapoint, the fifth split is the range of the data float offset = new Float(splitLine[6]); // if it is a floatDatapoint, the sixth split is the offset of the data - /* - // create a float or a boolean register + + // create a float or a boolean register and put it in the map of registers if (splitLine[1].equals("B")){ BooleanRegister b = new BooleanRegister(label, isOutput, address); + b.updateMapOfRegisters(label, address); //save the register in map, the key is the label of the datapoint } else if(splitLine[1].equals("F")){ FloatRegister f = new FloatRegister(label, isOutput, address, range, offset); + f.updateMapOfRegisters(label, address); //save the register in map, the key is the label of the datapoint } - */ } } catch(FileNotFoundException e){ @@ -75,18 +77,29 @@ public class FieldConnector implements DataPointListener { * @param pathToFile path to the file of all modbus registers (C:/.../ModbusMap.csv) */ public void initialize(String host, int port, String pathToFile){ - ModbusAccessor.getMySelf().connect(host,port); - createRegister(pathToFile); - startPeriodicalPolling(); + ModbusAccessor.getMySelf().connect(host,port); //connect to the modbus server + createRegister(pathToFile); //create the all needed modbus registers and save them in a map + startPeriodicalPolling(); //start periodical reading of the values } private void pushToField(String label, String value){ System.out.println("Field: " + label + " " + value); } @Override public void onNewValue(DataPoint dp) { - ModbusRegister mR = ModbusRegister.getRegisterFromDatapoint(dp); - mR.write(); - pushToField(dp.getLabel(),dp.toString()); + ModbusRegister mR = ModbusRegister.getRegisterFromDatapoint(dp); //search the corresponding register to the datapoint + if(dp.isOutput()){ //write only on the datapoints, which are outputs + if(dp.getLabel().equals("REMOTE_SOLAR_SW") //write only boolean outputs + ||dp.getLabel().equals("REMOTE_WIND_SW") + ||dp.getLabel().equals("SOLAR_CONNECT_ST") + ||dp.getLabel().equals("WIND_CONNECT_ST")){ + BooleanRegister br = (BooleanRegister) ModbusRegister.getRegisterFromDatapoint(dp); //create a booleanregister, because they are only boolean datapoints + br.write(dp); //write with the correct dp value + }else{ //write only float outputs + FloatRegister fr = (FloatRegister) ModbusRegister.getRegisterFromDatapoint(dp); //create a floatregister, because they are only float datapoints + fr.write(dp); //write with the correct dp value + } + pushToField(dp.getLabel(),dp.toString()); + } } /** @@ -94,8 +107,8 @@ public class FieldConnector implements DataPointListener { * in our case it is the reading of the modbus registers */ public void startPeriodicalPolling(){ - Timer pollTimer = new Timer(); - PollTask pollTask = new PollTask(); - pollTimer.scheduleAtFixedRate(pollTask,0,100); + Timer pollTimer = new Timer(); //create a new timer, + PollTask pollTask = new PollTask(); //create a new task to do every period. + pollTimer.scheduleAtFixedRate(pollTask,0,100); //do the run-function of the polltask every period } } diff --git a/src/main/java/ch/hevs/isi/field/FloatRegister.java b/src/main/java/ch/hevs/isi/field/FloatRegister.java index 214bcf0..cf27828 100644 --- a/src/main/java/ch/hevs/isi/field/FloatRegister.java +++ b/src/main/java/ch/hevs/isi/field/FloatRegister.java @@ -8,19 +8,30 @@ import java.util.HashMap; public class FloatRegister extends ModbusRegister{ private Float value; private FloatDataPoint fdp; + + /** + * public constructor of the Float Register + * + * @param label label of the datapoint + * @param isOutPut true if it is an output datapoint + * @param address modbus address of the datapoint + * @param range range of the datapoint value + * @param offset offset of the datapoint value + */ public FloatRegister(String label, boolean isOutPut, int address, float range, float offset) { - this.fdp = new FloatDataPoint(label, isOutPut); - value = fdp.getValue(); - updateMapOfRegisters(fdp,address); + this.fdp = new FloatDataPoint(label, isOutPut); //create an empty datapoint for the NullPointerException + updateMapOfRegisters(label,address); //add the address to the map } @Override public void read() { - fdp.setValue(ModbusAccessor.getMySelf().readFloat(this.getAddress())); + fdp.setValue(ModbusAccessor.getMySelf().readFloat(this.getAddress())); //read the value } @Override - public void write() { - ModbusAccessor.getMySelf().writeFloat(this.getAddress(), fdp.getValue()); + public void write(DataPoint dp) { + fdp = (FloatDataPoint) dp; //make the empty datapoint to the desired datapoint, which is to be written on the address + value = fdp.getValue(); //store the value of the datapoint + ModbusAccessor.getMySelf().writeFloat(this.getAddress(), fdp.getValue()); //write the desired value on the modbus address } } \ 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 f963845..6c87cff 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusRegister.java +++ b/src/main/java/ch/hevs/isi/field/ModbusRegister.java @@ -7,7 +7,6 @@ import java.util.HashMap; public abstract class ModbusRegister { private int address; - /** * get the address of the modbus register * @return address of the modbus register @@ -15,29 +14,35 @@ public abstract class ModbusRegister { public int getAddress() { return address; } - private final static HashMap mapOfRegisters = new HashMap<>(); + private final static HashMap mapOfRegisters = new HashMap<>(); /** * get the modbus register from the desired datapoint * @param dp the desired datapoint - * @return modbus register + * @return modbus adress of the datapoint */ public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ - return mapOfRegisters.get(dp); + return mapOfRegisters.get(dp.getLabel()); //search the modbus address of the datapoint-label } - public void updateMapOfRegisters(DataPoint dp, int address){ + + /** + * add an register to the map of the modbus addresses + * @param label label of the datapoint + * @param address modbus address of the datapoint + */ + public void updateMapOfRegisters(String label, int address){ this.address = address; - mapOfRegisters.put(dp,this); + mapOfRegisters.put(label,this); //add an address to the map with the corresponding datapoint-label } /** * read periodically each modbus register */ public static void poll(){ - for (ModbusRegister mr : mapOfRegisters.values()){ - mr.read(); //read all values (registers) of the map + for (ModbusRegister mr : mapOfRegisters.values()){ //read through all the values (modbus addresses) of the map + mr.read(); //read each datapoint, which address is stored in the map } } public abstract void read(); //abstract prototype of the method read - public abstract void write(); //abstract prototype of the method read + public abstract void write(DataPoint dp); //abstract prototype of the method read } diff --git a/src/main/java/ch/hevs/isi/field/PollTask.java b/src/main/java/ch/hevs/isi/field/PollTask.java index a01ccc2..8fe9204 100644 --- a/src/main/java/ch/hevs/isi/field/PollTask.java +++ b/src/main/java/ch/hevs/isi/field/PollTask.java @@ -1,5 +1,7 @@ package ch.hevs.isi.field; +import ch.hevs.isi.core.DataPoint; + import java.util.TimerTask; public class PollTask extends TimerTask { diff --git a/src/main/resources/ModbusMap.csv b/src/main/resources/ModbusMap.csv new file mode 100644 index 0000000..dc684e3 --- /dev/null +++ b/src/main/resources/ModbusMap.csv @@ -0,0 +1,27 @@ +Label;Type [FLOAT|Boolean];Input;Output;Address;Range;Offset +GRID_U_FLOAT;F;Y;N;89;1000;0 +BATT_P_FLOAT;F;Y;N;57;6000;-3000 +BATT_CHRG_FLOAT;F;Y;N;49;1;0 +SOLAR_P_FLOAT;F;Y;N;61;1500;0 +WIND_P_FLOAT;F;Y;N;53;1000;0 +COAL_P_FLOAT;F;Y;N;81;600;0 +COAL_AMOUNT;F;Y;N;65;1;0 +HOME_P_FLOAT;F;Y;N;101;1000;0 +PUBLIC_P_FLOAT;F;Y;N;97;500;0 +FACTORY_P_FLOAT;F;Y;N;105;2000;0 +BUNKER_P_FLOAT;F;Y;N;93;500;0 +WIND_FLOAT;F;Y;N;301;1;0 +WEATHER_FLOAT;F;Y;N;305;1;0 +WEATHER_FORECAST_FLOAT;F;Y;N;309;1;0 +WEATHER_COUNTDOWN_FLOAT;F;Y;;313;600;0 +CLOCK_FLOAT;F;Y;N;317;1;0 +REMOTE_COAL_SP;F;Y;Y;209;1;0 +REMOTE_FACTORY_SP;F;Y;Y;205;1;0 +REMOTE_SOLAR_SW;B;Y;Y;401;1;0 +REMOTE_WIND_SW;B;Y;Y;405;1;0 +FACTORY_ENERGY;F;Y;N;341;3600000;0 +SCORE;F;Y;N;345;3600000;0 +COAL_ST;F;Y;N;601;1;0 +FACTORY_ST;F;Y;N;605;1;0 +SOLAR_CONNECT_ST;B;Y;N;609;1;0 +WIND_CONNECT_ST;B;Y;N;613;1;0 diff --git a/src/test/java/Field.java b/src/test/java/Field.java index 7cd5802..fe282c5 100644 --- a/src/test/java/Field.java +++ b/src/test/java/Field.java @@ -7,28 +7,16 @@ import ch.hevs.isi.field.FloatRegister; public class Field { public static void main(String[] args) { + //create an FieldConnector + //reads the datapoint values and displays them in the consol + FieldConnector.getMySelf().initialize("LocalHost", 1502,"src/main/resources/ModbusMap.csv"); - //FloatRegister fRI = new FloatRegister("GRID_U_FLOAT",false,89); - BooleanRegister bRI = new BooleanRegister("SOLAR_CONNECT_ST", false ,609); - FieldConnector.getMySelf().startPeriodicalPolling(); + //create two datapoint to write a new value to them + BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true); + FloatDataPoint remoteFactorySp = new FloatDataPoint("REMOTE_FACTORY_SP", true); - //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()); - */ - } + //write to values + remoteFactorySp.setValue(0.74f); + solarPanel.setValue(true); } } From 58787501098a9b426f3493ca1509dbe0d0dfe658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 7 Jun 2023 13:42:14 +0200 Subject: [PATCH 13/13] fix everything for merge Field --- src/main/java/ch/hevs/isi/MinecraftController.java | 13 +++++++++---- .../java/ch/hevs/isi/db/DatabaseConnector.java | 2 +- .../java/ch/hevs/isi/field/BooleanRegister.java | 3 +++ .../java/ch/hevs/isi/field/FieldConnector.java | 7 ++++--- src/main/java/ch/hevs/isi/field/FloatRegister.java | 14 ++++++++++++-- src/main/resources/ModbusMap.csv | 2 +- src/test/java/Database.java | 3 +-- 7 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/hevs/isi/MinecraftController.java b/src/main/java/ch/hevs/isi/MinecraftController.java index 2567af5..1e6b4ac 100644 --- a/src/main/java/ch/hevs/isi/MinecraftController.java +++ b/src/main/java/ch/hevs/isi/MinecraftController.java @@ -1,6 +1,7 @@ package ch.hevs.isi; import ch.hevs.isi.db.DatabaseConnector; +import ch.hevs.isi.field.FieldConnector; import ch.hevs.isi.utils.Utility; import ch.hevs.isi.web.WebConnector; @@ -87,18 +88,22 @@ public class MinecraftController { } // Initialize the database connector - if((dbProtocol != null) && (dbHostName != null)){ - DatabaseConnector.url = dbProtocol+ "://" + dbHostName; - DatabaseConnector.getMySelf().initialize(dbProtocol+ "://" + dbHostName); - } if(dbName != null){ DatabaseConnector.org = dbName; } if(dbUserName != null){ DatabaseConnector.bucket = dbUserName; } + if((dbProtocol != null) && (dbHostName != null)){ + DatabaseConnector.url = dbProtocol+ "://" + dbHostName; + Utility.pDebug("Database URL: " + DatabaseConnector.url); + Utility.pDebug("Config: " + properties.getProperty("DB.URL")); + DatabaseConnector.getMySelf().initialize(DatabaseConnector.url); + } // Initialize the Modbus TCP connector + FieldConnector.getMySelf().initialize(modbusTcpHost, modbusTcpPort,"src/main/resources/ModbusMap.csv"); + // Initialize the web server diff --git a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java index cd66e31..63eb65a 100644 --- a/src/main/java/ch/hevs/isi/db/DatabaseConnector.java +++ b/src/main/java/ch/hevs/isi/db/DatabaseConnector.java @@ -178,7 +178,7 @@ public class DatabaseConnector implements DataPointListener { */ @Override public void onNewValue(DataPoint dp) { - if(dp.getLabel().equals("CLOAK_FLOAT")) { + if(dp.getLabel().equals("CLOCK_FLOAT")) { FloatDataPoint fdp = (FloatDataPoint) dp; _timeManager.setTimestamp(fdp.getValue()); _timestamp = _timeManager.getNanosForDB(); diff --git a/src/main/java/ch/hevs/isi/field/BooleanRegister.java b/src/main/java/ch/hevs/isi/field/BooleanRegister.java index 71716a0..205f596 100644 --- a/src/main/java/ch/hevs/isi/field/BooleanRegister.java +++ b/src/main/java/ch/hevs/isi/field/BooleanRegister.java @@ -20,6 +20,9 @@ public class BooleanRegister extends ModbusRegister{ } @Override public void read() { + if(bdp.isOutput()){ + return; //if it is an output datapoint, it is not read + } bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); //read the value } diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index 2991a18..411069f 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -15,6 +15,7 @@ public class FieldConnector implements DataPointListener { private FieldConnector(){ initialize("LocalHost",1502, "src/main/resources/ModbusMap.csv"); + DataPointListener.subscribeUpdate(this); } /** @@ -44,10 +45,10 @@ public class FieldConnector implements DataPointListener { String[] splitLine = line.split(";"); // split line between ";" String label = splitLine[0]; // first split is the label of the register - boolean isOutput = splitLine[3].equals("N"); // third split declares if it is an output + boolean isOutput = splitLine[3].equals("Y"); // third split declares if it is an output int address = new Integer((splitLine[4])); // fourth split is the address of the register - float range = new Float(splitLine[5]); // if it is a floatDatapoint, the fifth split is the range of the data - float offset = new Float(splitLine[6]); // if it is a floatDatapoint, the sixth split is the offset of the data + int range = new Integer(splitLine[5]); // if it is a floatDatapoint, the fifth split is the range of the data + int offset = new Integer(splitLine[6]); // if it is a floatDatapoint, the sixth split is the offset of the data // create a float or a boolean register and put it in the map of registers diff --git a/src/main/java/ch/hevs/isi/field/FloatRegister.java b/src/main/java/ch/hevs/isi/field/FloatRegister.java index cf27828..56f5e03 100644 --- a/src/main/java/ch/hevs/isi/field/FloatRegister.java +++ b/src/main/java/ch/hevs/isi/field/FloatRegister.java @@ -8,6 +8,8 @@ import java.util.HashMap; public class FloatRegister extends ModbusRegister{ private Float value; private FloatDataPoint fdp; + private int range; + private int offset; /** * public constructor of the Float Register @@ -18,14 +20,22 @@ public class FloatRegister extends ModbusRegister{ * @param range range of the datapoint value * @param offset offset of the datapoint value */ - public FloatRegister(String label, boolean isOutPut, int address, float range, float offset) { + public FloatRegister(String label, boolean isOutPut, int address, int range, int offset) { this.fdp = new FloatDataPoint(label, isOutPut); //create an empty datapoint for the NullPointerException updateMapOfRegisters(label,address); //add the address to the map + this.range = range; + this.offset = offset; } @Override public void read() { - fdp.setValue(ModbusAccessor.getMySelf().readFloat(this.getAddress())); //read the value + if(fdp.isOutput()){ + return; //if it is an output datapoint, it is not read + } + Float value = ModbusAccessor.getMySelf().readFloat(this.getAddress()); //read the value + value = value * range; + value = value + offset; + fdp.setValue(value); } @Override diff --git a/src/main/resources/ModbusMap.csv b/src/main/resources/ModbusMap.csv index dc684e3..0e6984d 100644 --- a/src/main/resources/ModbusMap.csv +++ b/src/main/resources/ModbusMap.csv @@ -13,7 +13,7 @@ BUNKER_P_FLOAT;F;Y;N;93;500;0 WIND_FLOAT;F;Y;N;301;1;0 WEATHER_FLOAT;F;Y;N;305;1;0 WEATHER_FORECAST_FLOAT;F;Y;N;309;1;0 -WEATHER_COUNTDOWN_FLOAT;F;Y;;313;600;0 +WEATHER_COUNTDOWN_FLOAT;F;Y;N;313;600;0 CLOCK_FLOAT;F;Y;N;317;1;0 REMOTE_COAL_SP;F;Y;Y;209;1;0 REMOTE_FACTORY_SP;F;Y;Y;205;1;0 diff --git a/src/test/java/Database.java b/src/test/java/Database.java index f4f7553..cae80de 100644 --- a/src/test/java/Database.java +++ b/src/test/java/Database.java @@ -8,12 +8,11 @@ import ch.hevs.isi.web.WebConnector; public class Database { public static void main(String[] args) { MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true; - FloatDataPoint clock = new FloatDataPoint("CLOAK_FLOAT", false); + FloatDataPoint clock = new FloatDataPoint("CLOCK_FLOAT", false); FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true); BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true); DatabaseConnector.getMySelf().initialize(null); - WebConnector.getMySelf().initialize(null, 0); clock.setValue(0f);