1
0

read csv file and add some comments

This commit is contained in:
Nils Ritler 2023-05-30 11:31:01 +02:00
parent 0d105fb64e
commit 55fa80c4d4
7 changed files with 99 additions and 22 deletions

View File

@ -35,7 +35,6 @@ public abstract class DataPoint{
}
}
/**
* Just get the label of this DataPoint
* @return the label in a string

View File

@ -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()));

View File

@ -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();

View File

@ -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);

View File

@ -20,7 +20,6 @@ public class ModbusAccessor {
* private constructor
* */
private ModbusAccessor(){
}
/**

View File

@ -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<DataPoint, ModbusRegister> 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<DataPoint, ModbusRegister> 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

View File

@ -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);
/*