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] 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); } }