changed the field component, made some test and added some comments
This commit is contained in:
@ -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){
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
@ -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<DataPoint, ModbusRegister> mapOfRegisters = new HashMap<>();
|
||||
private final static HashMap<String, ModbusRegister> 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
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package ch.hevs.isi.field;
|
||||
|
||||
import ch.hevs.isi.core.DataPoint;
|
||||
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class PollTask extends TimerTask {
|
||||
|
Reference in New Issue
Block a user