Merge pull request #18 from HEI-SYND-221-231-SIn/improvement
Merge Field
This commit is contained in:
commit
39370e3e54
@ -1,3 +1,5 @@
|
|||||||
URL = https://influx.sdi.hevs.ch
|
DB.URL = https://influx.sdi.hevs.ch
|
||||||
ORG = SIn15
|
DB.ORG = SIn15
|
||||||
BUCKET = SIn15
|
DB.BUCKET = SIn15
|
||||||
|
WEB.URL = localhost
|
||||||
|
WEB.PORT = 8888
|
@ -1,13 +1,18 @@
|
|||||||
package ch.hevs.isi;
|
package ch.hevs.isi;
|
||||||
|
|
||||||
import ch.hevs.isi.core.BooleanDataPoint;
|
|
||||||
import ch.hevs.isi.core.FloatDataPoint;
|
|
||||||
import ch.hevs.isi.db.DatabaseConnector;
|
import ch.hevs.isi.db.DatabaseConnector;
|
||||||
|
import ch.hevs.isi.field.FieldConnector;
|
||||||
import ch.hevs.isi.utils.Utility;
|
import ch.hevs.isi.utils.Utility;
|
||||||
|
import ch.hevs.isi.web.WebConnector;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
public class MinecraftController {
|
public class MinecraftController {
|
||||||
|
|
||||||
public static boolean ERASE_PREVIOUS_DATA_INB_DB = false;
|
public static boolean ERASE_PREVIOUS_DATA_INB_DB = false;
|
||||||
|
private static final Properties properties = new Properties(); // Properties of the config.properties file
|
||||||
|
|
||||||
public static void usage() {
|
public static void usage() {
|
||||||
System.out.println();
|
System.out.println();
|
||||||
@ -75,21 +80,36 @@ public class MinecraftController {
|
|||||||
|
|
||||||
// ------------------------------------ /DO NOT CHANGE THE FOLLOWING LINES -------------------------------------
|
// ------------------------------------ /DO NOT CHANGE THE FOLLOWING LINES -------------------------------------
|
||||||
|
|
||||||
// Start coding here ...
|
// Read the config.properties file
|
||||||
|
try (InputStream input = new FileInputStream("src/config.properties")) {
|
||||||
|
properties.load(input);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the database connector
|
// Initialize the database connector
|
||||||
if((dbProtocol != null) && (dbHostName != null)){
|
|
||||||
DatabaseConnector.url = dbProtocol+ "://" + dbHostName;
|
|
||||||
DatabaseConnector.getMySelf().initialize(dbProtocol+ "://" + dbHostName);
|
|
||||||
}
|
|
||||||
if(dbName != null){
|
if(dbName != null){
|
||||||
DatabaseConnector.org = dbName;
|
DatabaseConnector.org = dbName;
|
||||||
}
|
}
|
||||||
if(dbUserName != null){
|
if(dbUserName != null){
|
||||||
DatabaseConnector.bucket = dbUserName;
|
DatabaseConnector.bucket = dbUserName;
|
||||||
}
|
}
|
||||||
|
if((dbProtocol != null) && (dbHostName != null)){
|
||||||
|
DatabaseConnector.url = dbProtocol+ "://" + dbHostName;
|
||||||
|
Utility.pDebug("Database URL: " + DatabaseConnector.url);
|
||||||
|
Utility.pDebug("Config: " + properties.getProperty("DB.URL"));
|
||||||
|
DatabaseConnector.getMySelf().initialize(DatabaseConnector.url);
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the Modbus TCP connector
|
// Initialize the Modbus TCP connector
|
||||||
|
FieldConnector.getMySelf().initialize(modbusTcpHost, modbusTcpPort,"src/main/resources/ModbusMap.csv");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Initialize the web server
|
||||||
|
String host = properties.getProperty("WEB.URL");
|
||||||
|
int port = Integer.parseInt(properties.getProperty("WEB.PORT"));
|
||||||
|
WebConnector.getMySelf().initialize(host, port);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,42 @@
|
|||||||
package ch.hevs.isi.core;
|
package ch.hevs.isi.core;
|
||||||
import ch.hevs.isi.core.DataPointListener;
|
|
||||||
import ch.hevs.isi.db.DatabaseConnector;
|
|
||||||
import ch.hevs.isi.field.FieldConnector;
|
|
||||||
import ch.hevs.isi.web.WebConnector;
|
|
||||||
|
|
||||||
public class BooleanDataPoint extends DataPoint{
|
public class BooleanDataPoint extends DataPoint{
|
||||||
private boolean value;
|
private boolean value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new DataPoint with a label and if is an Output or not
|
||||||
|
* @param label the label of this DataPoint
|
||||||
|
* @param isOutput true if this is an Output, false if it's an Input
|
||||||
|
*/
|
||||||
public BooleanDataPoint(String label, boolean isOutput) {
|
public BooleanDataPoint(String label, boolean isOutput) {
|
||||||
super(label, isOutput);
|
super(label, isOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the value of this DataPoint and create it if it doesn't exist via the update method
|
||||||
|
* @param value the value to set
|
||||||
|
*/
|
||||||
public void setValue(boolean value){
|
public void setValue(boolean value){
|
||||||
this.value = value;
|
this.value = value;
|
||||||
if (getDataPointFromLabel(this.getLabel()) == null){
|
if (getDataPointOnListFromLabel(this.getLabel()) == null){
|
||||||
this.update(true);
|
this.update(true);
|
||||||
} else {
|
} else {
|
||||||
this.update(false);
|
this.update(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the value of this DataPoint
|
||||||
|
* @return the value of this DataPoint
|
||||||
|
*/
|
||||||
public boolean getValue(){
|
public boolean getValue(){
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert this DataPoint to a string
|
||||||
|
* @return the string representation of this DataPoint
|
||||||
|
*/
|
||||||
public String toString(){
|
public String toString(){
|
||||||
String s;
|
String s;
|
||||||
s = this.getLabel();
|
s = this.getLabel();
|
||||||
|
@ -1,16 +1,35 @@
|
|||||||
package ch.hevs.isi.core;
|
package ch.hevs.isi.core;
|
||||||
|
|
||||||
|
import ch.hevs.isi.db.DatabaseConnector;
|
||||||
|
import ch.hevs.isi.field.FieldConnector;
|
||||||
|
import ch.hevs.isi.web.WebConnector;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public abstract class DataPoint{
|
public abstract class DataPoint{
|
||||||
private static Map<String, DataPoint> dataPointMap = new HashMap<>();
|
|
||||||
private String label;
|
|
||||||
private boolean isOutput;
|
public static Map<String, DataPoint> dataPointMap = new HashMap<>(); // Map of all DataPoints
|
||||||
|
private final String label; // Label of this DataPoint
|
||||||
|
private final boolean isOutput; // True if this DataPoint is an output, false if it's an input
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor of DataPoint
|
||||||
|
* @param label label of this DataPoint
|
||||||
|
* @param isOutput true if this DataPoint is an output, false if it's an input
|
||||||
|
*/
|
||||||
protected DataPoint(String label, boolean isOutput){
|
protected DataPoint(String label, boolean isOutput){
|
||||||
this.label = label;
|
this.label = label;
|
||||||
this.isOutput = isOutput;
|
this.isOutput = isOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the value of this DataPoint and notify every connector
|
||||||
|
* If this is a new value (doesn't exist), add it to the dataPointMap
|
||||||
|
* @param isNewValue true if this is a new value, false if it's an update
|
||||||
|
*/
|
||||||
protected void update(boolean isNewValue){
|
protected void update(boolean isNewValue){
|
||||||
|
|
||||||
if(isNewValue){
|
if(isNewValue){
|
||||||
@ -22,7 +41,13 @@ public abstract class DataPoint{
|
|||||||
// Update every connector
|
// Update every connector
|
||||||
DataPointListener.listeners.forEach(listener -> listener.onNewValue(this));
|
DataPointListener.listeners.forEach(listener -> listener.onNewValue(this));
|
||||||
}
|
}
|
||||||
public static DataPoint getDataPointFromLabel(String label){
|
|
||||||
|
/**
|
||||||
|
* Get the DataPoint from the label
|
||||||
|
* @param label label of the DataPoint
|
||||||
|
* @return the DataPoint if it exists, null otherwise
|
||||||
|
*/
|
||||||
|
public static DataPoint getDataPointOnListFromLabel(String label){
|
||||||
if( !dataPointMap.containsKey(label) ){
|
if( !dataPointMap.containsKey(label) ){
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,27 +1,42 @@
|
|||||||
package ch.hevs.isi.core;
|
package ch.hevs.isi.core;
|
||||||
|
|
||||||
import ch.hevs.isi.db.DatabaseConnector;
|
|
||||||
import ch.hevs.isi.field.FieldConnector;
|
|
||||||
import ch.hevs.isi.web.WebConnector;
|
|
||||||
|
|
||||||
public class FloatDataPoint extends DataPoint{
|
public class FloatDataPoint extends DataPoint{
|
||||||
private float value;
|
private float value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new DataPoint with a label and if is an Output or not
|
||||||
|
* @param label the label of this DataPoint
|
||||||
|
* @param isOutput true if this is an Output, false if it's an Input
|
||||||
|
*/
|
||||||
public FloatDataPoint(String label, boolean isOutput) {
|
public FloatDataPoint(String label, boolean isOutput) {
|
||||||
super(label, isOutput);
|
super(label, isOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the value of this DataPoint and create it if it doesn't exist via the update method
|
||||||
|
* @param value the value to set
|
||||||
|
*/
|
||||||
public void setValue(float value){
|
public void setValue(float value){
|
||||||
this.value = value;
|
this.value = value;
|
||||||
if (getDataPointFromLabel(this.getLabel()) == null){
|
if (getDataPointOnListFromLabel(this.getLabel()) == null){
|
||||||
this.update(true);
|
this.update(true);
|
||||||
} else {
|
} else {
|
||||||
this.update(false);
|
this.update(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the value of this DataPoint
|
||||||
|
* @return the value of this DataPoint
|
||||||
|
*/
|
||||||
public float getValue(){
|
public float getValue(){
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert this DataPoint to a string
|
||||||
|
* @return the string representation of this DataPoint
|
||||||
|
*/
|
||||||
public String toString(){
|
public String toString(){
|
||||||
String s;
|
String s;
|
||||||
s = this.getLabel();
|
s = this.getLabel();
|
||||||
|
@ -7,20 +7,19 @@ import ch.hevs.isi.core.DataPointListener;
|
|||||||
import ch.hevs.isi.core.FloatDataPoint;
|
import ch.hevs.isi.core.FloatDataPoint;
|
||||||
import ch.hevs.isi.utils.Utility;
|
import ch.hevs.isi.utils.Utility;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.ProtocolException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
|
||||||
public class DatabaseConnector implements DataPointListener {
|
public class DatabaseConnector implements DataPointListener {
|
||||||
private final Properties properties = new Properties(); // Properties of the config.properties file
|
private final Properties properties = new Properties(); // Properties of the config.properties file
|
||||||
private String fullURL = null; // Full URL of the InfluxDB server
|
|
||||||
private URL urlForWrite = null; // URL of the InfluxDB server
|
private URL urlForWrite = null; // URL of the InfluxDB server
|
||||||
private HttpURLConnection con = null; // Connection to the InfluxDB server
|
private HttpURLConnection con = null; // Connection to the InfluxDB server
|
||||||
private boolean initialized = false; // Boolean to know if the database connector is initialized
|
private boolean initialized = false; // Boolean to know if the database connector is initialized
|
||||||
@ -40,7 +39,7 @@ public class DatabaseConnector implements DataPointListener {
|
|||||||
*/
|
*/
|
||||||
private DatabaseConnector (){
|
private DatabaseConnector (){
|
||||||
// Read the config.properties file
|
// Read the config.properties file
|
||||||
try (InputStream input = new FileInputStream("src/config.properties")) {
|
try (InputStream input = Files.newInputStream(Paths.get("src/config.properties"))) {
|
||||||
properties.load(input);
|
properties.load(input);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -48,13 +47,13 @@ public class DatabaseConnector implements DataPointListener {
|
|||||||
|
|
||||||
// Get the URL, the organization and the bucket from the config.properties file if their are null
|
// Get the URL, the organization and the bucket from the config.properties file if their are null
|
||||||
if (url == null){
|
if (url == null){
|
||||||
url = properties.getProperty("URL");
|
url = properties.getProperty("DB.URL");
|
||||||
}
|
}
|
||||||
if (org == null){
|
if (org == null){
|
||||||
org = properties.getProperty("ORG");
|
org = properties.getProperty("DB.ORG");
|
||||||
}
|
}
|
||||||
if (bucket == null){
|
if (bucket == null){
|
||||||
bucket = properties.getProperty("BUCKET");
|
bucket = properties.getProperty("DB.BUCKET");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe to the update of DataPoints
|
// Subscribe to the update of DataPoints
|
||||||
@ -77,10 +76,12 @@ public class DatabaseConnector implements DataPointListener {
|
|||||||
* @param url URL of the database. If null take the URL from the config.properties file
|
* @param url URL of the database. If null take the URL from the config.properties file
|
||||||
*/
|
*/
|
||||||
public void initialize(String url){
|
public void initialize(String url){
|
||||||
|
// Full URL of the InfluxDB server
|
||||||
|
String fullURL = null;
|
||||||
try{
|
try{
|
||||||
if(urlForWrite == null){
|
if(urlForWrite == null){
|
||||||
if(url == null){
|
if(url == null){
|
||||||
url = properties.getProperty("URL");
|
url = properties.getProperty("DB.URL");
|
||||||
}
|
}
|
||||||
fullURL = url + "/api/v2/write?org=" + org + "&bucket=" + bucket;
|
fullURL = url + "/api/v2/write?org=" + org + "&bucket=" + bucket;
|
||||||
Utility.pDebug("URL: " + fullURL);
|
Utility.pDebug("URL: " + fullURL);
|
||||||
@ -106,10 +107,6 @@ public class DatabaseConnector implements DataPointListener {
|
|||||||
con.setRequestProperty("Content-Type", "text/plain");
|
con.setRequestProperty("Content-Type", "text/plain");
|
||||||
con.setRequestProperty("Accept", "application/json");
|
con.setRequestProperty("Accept", "application/json");
|
||||||
con.setDoOutput(true);
|
con.setDoOutput(true);
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (ProtocolException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -122,7 +119,7 @@ public class DatabaseConnector implements DataPointListener {
|
|||||||
data += "}";
|
data += "}";
|
||||||
Utility.pDebug(data);
|
Utility.pDebug(data);
|
||||||
sendDataToDatabase(data);
|
sendDataToDatabase(data);
|
||||||
fullURL=null; // Reset the full URL
|
fullURL =null; // Reset the full URL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,7 +178,7 @@ public class DatabaseConnector implements DataPointListener {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onNewValue(DataPoint dp) {
|
public void onNewValue(DataPoint dp) {
|
||||||
if(dp.getLabel().equals("CLOAK_FLOAT")) {
|
if(dp.getLabel().equals("CLOCK_FLOAT")) {
|
||||||
FloatDataPoint fdp = (FloatDataPoint) dp;
|
FloatDataPoint fdp = (FloatDataPoint) dp;
|
||||||
_timeManager.setTimestamp(fdp.getValue());
|
_timeManager.setTimestamp(fdp.getValue());
|
||||||
_timestamp = _timeManager.getNanosForDB();
|
_timestamp = _timeManager.getNanosForDB();
|
||||||
|
@ -7,18 +7,29 @@ public class BooleanRegister extends ModbusRegister{
|
|||||||
private boolean value;
|
private boolean value;
|
||||||
private BooleanDataPoint bdp;
|
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){
|
public BooleanRegister(String label, boolean isOutput, int address){
|
||||||
this.bdp = new BooleanDataPoint(label, isOutput);
|
this.bdp = new BooleanDataPoint(label, isOutput); //create an empty datapoint for the NullPointerException
|
||||||
value = bdp.getValue();
|
updateMapOfRegisters(label, address); //add the address to the map
|
||||||
updateMapOfRegisters(bdp, address);
|
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void read() {
|
public void read() {
|
||||||
bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress()));
|
if(bdp.isOutput()){
|
||||||
|
return; //if it is an output datapoint, it is not read
|
||||||
|
}
|
||||||
|
bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); //read the value
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write() {
|
public void write(DataPoint dp) {
|
||||||
ModbusAccessor.getMySelf().writeBoolean(this.getAddress(), bdp.getValue());
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package ch.hevs.isi.field;
|
|||||||
|
|
||||||
import ch.hevs.isi.core.DataPoint;
|
import ch.hevs.isi.core.DataPoint;
|
||||||
import ch.hevs.isi.core.DataPointListener;
|
import ch.hevs.isi.core.DataPointListener;
|
||||||
|
import ch.hevs.isi.core.FloatDataPoint;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@ -11,13 +12,10 @@ import java.util.Timer;
|
|||||||
|
|
||||||
public class FieldConnector implements DataPointListener {
|
public class FieldConnector implements DataPointListener {
|
||||||
private static FieldConnector mySelf = null;
|
private static FieldConnector mySelf = null;
|
||||||
|
|
||||||
private FieldConnector(){
|
private FieldConnector(){
|
||||||
|
initialize("LocalHost",1502, "src/main/resources/ModbusMap.csv");
|
||||||
initialize("LocalHost",1502, "C:/Nils/Hesso/4_Semester/SIN/Minecraft_Electrical_Age_Project/ModbusMap.csv");
|
|
||||||
|
|
||||||
// Subscribe to the update of DataPoints
|
|
||||||
DataPointListener.subscribeUpdate(this);
|
DataPointListener.subscribeUpdate(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,19 +45,20 @@ public class FieldConnector implements DataPointListener {
|
|||||||
String[] splitLine = line.split(";"); // split line between ";"
|
String[] splitLine = line.split(";"); // split line between ";"
|
||||||
|
|
||||||
String label = splitLine[0]; // first split is the label of the register
|
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
|
boolean isOutput = splitLine[3].equals("Y"); // third split declares if it is an output
|
||||||
int address = new Integer((splitLine[4])); // fourth split is the address of the register
|
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
|
int range = new Integer(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
|
int offset = new Integer(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")){
|
if (splitLine[1].equals("B")){
|
||||||
BooleanRegister b = new BooleanRegister(label, isOutput, address);
|
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")){
|
} else if(splitLine[1].equals("F")){
|
||||||
FloatRegister f = new FloatRegister(label, isOutput, address, range, offset);
|
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){
|
catch(FileNotFoundException e){
|
||||||
@ -79,27 +78,38 @@ public class FieldConnector implements DataPointListener {
|
|||||||
* @param pathToFile path to the file of all modbus registers (C:/.../ModbusMap.csv)
|
* @param pathToFile path to the file of all modbus registers (C:/.../ModbusMap.csv)
|
||||||
*/
|
*/
|
||||||
public void initialize(String host, int port, String pathToFile){
|
public void initialize(String host, int port, String pathToFile){
|
||||||
ModbusAccessor.getMySelf().connect(host,port);
|
ModbusAccessor.getMySelf().connect(host,port); //connect to the modbus server
|
||||||
createRegister(pathToFile);
|
createRegister(pathToFile); //create the all needed modbus registers and save them in a map
|
||||||
startPeriodicalPolling();
|
startPeriodicalPolling(); //start periodical reading of the values
|
||||||
}
|
}
|
||||||
private void pushToField(String label, String value){
|
private void pushToField(String label, String value){
|
||||||
System.out.println("Field: " + label + " " + value);
|
System.out.println("Field: " + label + " " + value);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void onNewValue(DataPoint dp) {
|
public void onNewValue(DataPoint dp) {
|
||||||
ModbusRegister mR = ModbusRegister.getRegisterFromDatapoint(dp);
|
ModbusRegister mR = ModbusRegister.getRegisterFromDatapoint(dp); //search the corresponding register to the datapoint
|
||||||
mR.write();
|
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());
|
pushToField(dp.getLabel(),dp.toString());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* method to start a periodical task
|
* method to start a periodical task
|
||||||
* in our case it is the reading of the modbus registers
|
* in our case it is the reading of the modbus registers
|
||||||
*/
|
*/
|
||||||
public void startPeriodicalPolling(){
|
public void startPeriodicalPolling(){
|
||||||
Timer pollTimer = new Timer();
|
Timer pollTimer = new Timer(); //create a new timer,
|
||||||
PollTask pollTask = new PollTask();
|
PollTask pollTask = new PollTask(); //create a new task to do every period.
|
||||||
pollTimer.scheduleAtFixedRate(pollTask,0,100);
|
pollTimer.scheduleAtFixedRate(pollTask,0,100); //do the run-function of the polltask every period
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,19 +8,40 @@ import java.util.HashMap;
|
|||||||
public class FloatRegister extends ModbusRegister{
|
public class FloatRegister extends ModbusRegister{
|
||||||
private Float value;
|
private Float value;
|
||||||
private FloatDataPoint fdp;
|
private FloatDataPoint fdp;
|
||||||
public FloatRegister(String label, boolean isOutPut, int address, float range, float offset) {
|
private int range;
|
||||||
this.fdp = new FloatDataPoint(label, isOutPut);
|
private int offset;
|
||||||
value = fdp.getValue();
|
|
||||||
updateMapOfRegisters(fdp,address);
|
/**
|
||||||
|
* 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, int range, int offset) {
|
||||||
|
this.fdp = new FloatDataPoint(label, isOutPut); //create an empty datapoint for the NullPointerException
|
||||||
|
updateMapOfRegisters(label,address); //add the address to the map
|
||||||
|
this.range = range;
|
||||||
|
this.offset = offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read() {
|
public void read() {
|
||||||
fdp.setValue(ModbusAccessor.getMySelf().readFloat(this.getAddress()));
|
if(fdp.isOutput()){
|
||||||
|
return; //if it is an output datapoint, it is not read
|
||||||
|
}
|
||||||
|
Float value = ModbusAccessor.getMySelf().readFloat(this.getAddress()); //read the value
|
||||||
|
value = value * range;
|
||||||
|
value = value + offset;
|
||||||
|
fdp.setValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write() {
|
public void write(DataPoint dp) {
|
||||||
ModbusAccessor.getMySelf().writeFloat(this.getAddress(), fdp.getValue());
|
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 {
|
public abstract class ModbusRegister {
|
||||||
private int address;
|
private int address;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the address of the modbus register
|
* get the address of the modbus register
|
||||||
* @return address of the modbus register
|
* @return address of the modbus register
|
||||||
@ -15,29 +14,35 @@ public abstract class ModbusRegister {
|
|||||||
public int getAddress() {
|
public int getAddress() {
|
||||||
return address;
|
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
|
* get the modbus register from the desired datapoint
|
||||||
* @param dp the desired datapoint
|
* @param dp the desired datapoint
|
||||||
* @return modbus register
|
* @return modbus adress of the datapoint
|
||||||
*/
|
*/
|
||||||
public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){
|
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;
|
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
|
* read periodically each modbus register
|
||||||
*/
|
*/
|
||||||
public static void poll(){
|
public static void poll(){
|
||||||
for (ModbusRegister mr : mapOfRegisters.values()){
|
for (ModbusRegister mr : mapOfRegisters.values()){ //read through all the values (modbus addresses) of the map
|
||||||
mr.read(); //read all values (registers) 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 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;
|
package ch.hevs.isi.field;
|
||||||
|
|
||||||
|
import ch.hevs.isi.core.DataPoint;
|
||||||
|
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class PollTask extends TimerTask {
|
public class PollTask extends TimerTask {
|
||||||
|
27
src/main/resources/ModbusMap.csv
Normal file
27
src/main/resources/ModbusMap.csv
Normal file
@ -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;N;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
|
|
@ -8,13 +8,11 @@ import ch.hevs.isi.web.WebConnector;
|
|||||||
public class Database {
|
public class Database {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true;
|
MinecraftController.ERASE_PREVIOUS_DATA_INB_DB = true;
|
||||||
FloatDataPoint clock = new FloatDataPoint("CLOAK_FLOAT", false);
|
FloatDataPoint clock = new FloatDataPoint("CLOCK_FLOAT", false);
|
||||||
FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true);
|
FloatDataPoint gridVoltage = new FloatDataPoint("GRID_U_FLOAT", true);
|
||||||
BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true);
|
BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true);
|
||||||
|
|
||||||
DatabaseConnector.getMySelf().initialize(null);
|
DatabaseConnector.getMySelf().initialize(null);
|
||||||
FieldConnector.getMySelf().initialize(null, 0);
|
|
||||||
WebConnector.getMySelf().initialize(null, 0);
|
|
||||||
|
|
||||||
clock.setValue(0f);
|
clock.setValue(0f);
|
||||||
|
|
||||||
|
@ -7,28 +7,16 @@ import ch.hevs.isi.field.FloatRegister;
|
|||||||
public class Field {
|
public class Field {
|
||||||
public static void main(String[] args) {
|
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);
|
//create two datapoint to write a new value to them
|
||||||
BooleanRegister bRI = new BooleanRegister("SOLAR_CONNECT_ST", false ,609);
|
BooleanDataPoint solarPanel = new BooleanDataPoint("REMOTE_SOLAR_SW", true);
|
||||||
FieldConnector.getMySelf().startPeriodicalPolling();
|
FloatDataPoint remoteFactorySp = new FloatDataPoint("REMOTE_FACTORY_SP", true);
|
||||||
|
|
||||||
//FloatRegister fRO = new FloatRegister("REMOTE_FACTORY_SP",true,205);
|
//write to values
|
||||||
BooleanRegister bRO = new BooleanRegister("REMOTE_SOLAR_SW", true ,401);
|
remoteFactorySp.setValue(0.74f);
|
||||||
|
solarPanel.setValue(true);
|
||||||
/*
|
|
||||||
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());
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,16 +24,16 @@ public class Web {
|
|||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
|
|
||||||
if(WebConnector.getMySelf().wss.getConnections().size() > 0){
|
if(WebConnector.getMySelf().wss.getConnections().size() > 0){
|
||||||
FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointFromLabel("REMOTE_FACTORY_SP");
|
FloatDataPoint rfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_FACTORY_SP");
|
||||||
if(rfSP != null) new FloatDataPoint("FACTORY_ST", true).setValue(rfSP.getValue());
|
if(rfSP != null) new FloatDataPoint("FACTORY_ST", true).setValue(rfSP.getValue());
|
||||||
|
|
||||||
FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointFromLabel("REMOTE_COAL_SP");
|
FloatDataPoint cfSP = (FloatDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_COAL_SP");
|
||||||
if(cfSP != null) new FloatDataPoint("COAL_ST", true).setValue(cfSP.getValue());
|
if(cfSP != null) new FloatDataPoint("COAL_ST", true).setValue(cfSP.getValue());
|
||||||
|
|
||||||
BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointFromLabel("REMOTE_SOLAR_SW");
|
BooleanDataPoint solar = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_SOLAR_SW");
|
||||||
if(solar != null) new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(solar.getValue());
|
if(solar != null) new BooleanDataPoint("SOLAR_CONNECT_ST", true).setValue(solar.getValue());
|
||||||
|
|
||||||
BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointFromLabel("REMOTE_WIND_SW");
|
BooleanDataPoint wind = (BooleanDataPoint) DataPoint.getDataPointOnListFromLabel("REMOTE_WIND_SW");
|
||||||
if(wind != null) new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(wind.getValue());
|
if(wind != null) new BooleanDataPoint("WIND_CONNECT_ST", true).setValue(wind.getValue());
|
||||||
|
|
||||||
time += 0.1f;
|
time += 0.1f;
|
||||||
|
Reference in New Issue
Block a user