1
0
This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.

102 lines
3.8 KiB
Java
Raw Normal View History

package ch.hevs.isi.field;
import ch.hevs.isi.core.DataPoint;
import ch.hevs.isi.core.DataPointListener;
import ch.hevs.isi.core.FloatDataPoint;
2023-05-30 11:31:01 +02:00
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(){
2023-05-30 11:31:01 +02:00
initialize("LocalHost",1502, "C:/Nils/Hesso/4_Semester/SIN/Minecraft_Electrical_Age_Project/ModbusMap.csv");
}
2023-05-30 11:31:01 +02:00
/**
* 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();
}
return mySelf;
}
2023-05-26 16:11:02 +02:00
2023-05-30 11:31:01 +02:00
/**
* 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();
}
}
2023-05-26 16:11:02 +02:00
2023-05-30 11:31:01 +02:00
/**
* 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) {
2023-05-30 11:31:01 +02:00
ModbusRegister mR = ModbusRegister.getRegisterFromDatapoint(dp);
mR.write();
pushToField(dp.getLabel(),dp.toString());
}
2023-05-30 11:31:01 +02:00
/**
* method to start a periodical task
* in our case it is the reading of the modbus registers
*/
2023-05-26 16:11:02 +02:00
public void startPeriodicalPolling(){
Timer pollTimer = new Timer();
PollTask pollTask = new PollTask();
2023-05-26 16:11:02 +02:00
pollTimer.scheduleAtFixedRate(pollTask,0,100);
}
}