1
0

changed the field component, made some test and added some comments

This commit is contained in:
Nils Ritler
2023-06-06 17:50:17 +02:00
parent 55fa80c4d4
commit c478c0a7bf
8 changed files with 111 additions and 57 deletions

View File

@@ -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
}
}