Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/services/DeviceMapperJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public class DeviceMapperJson {

private static final String CONFIGPATH = DeviceMapperJson.class.getResource("/rsc/DeviceConfiguration.json").getFile();
private static final String CONFIGPATH = DeviceMapperJson.class.getResource("/rsc/DeviceConfiguration.json").getFile().replaceAll("%20", " ");

/**
* Mapped DeviceConfiguration.json nach List<Device>devices und gibt diese Liste zurueck
Expand Down
78 changes: 4 additions & 74 deletions src/main/java/services/MeasurementValueReader.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package services;

import model.MeasurementValueXml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -32,90 +25,27 @@
/**
* Die Klasse dient als Schnittstelle zur Datenhaltung der Messwerte.
* Je nach konfiguriertem Protokoll wird die entsprechende Methode aufgerufen.
*
* Created by Nett on 04.06.2017.
*/
public class MeasurementValueReader {


/**
* Dient als Weiche um die, gemaess Prokoll des Devices, passende Methode aufzurufen.
*
* @param deviceName
* @return
* @throws ReadWriteException
*/
public static MeasurementValueXml getActualValue(String deviceName)throws ReadWriteException{
public static MeasurementValueXml getActualValue(String deviceName){

String protocol = DeviceMapperJson.getMeasurementValueProtocol(deviceName);
switch (protocol){

case "xml-1":
return getActualValueFromXml(deviceName);
return Xml.getActualValue(deviceName);
case "txt-1":
return getActualValueFromTxt(deviceName);
case "xml-2":
return getActualValueFromXmlSax(deviceName);
default:
throw new ReadWriteException("Not possible to read protocol-type: " + protocol);
throw new ProtocolNotSupportedException(protocol);
}
}

/**
* Gibt einen Messwert mit Zeitstempel in Form einer Instanz von MeasurementValueXml zurueck,
* wenn das Protokoll xml-1 konfiguriert wurde.
*
* Wirft im Fehlerfall eine ReadWriteException
*
* @param deviceName
* @return
* @throws ReadWriteException
*/
public static MeasurementValueXml getActualValueFromXml(String deviceName)throws ReadWriteException{
MeasurementValueXml measurementValue = new MeasurementValueXml();
// Pfad der Messwerte-Files gemaess deviceName aus der Konfiguration lesen und ein File erstellen
String path = DeviceMapperJson.getMeasurementValuePath(deviceName);
InputStream xmlFile;
if(path != null){
xmlFile = MeasurementValueReader.class.getResourceAsStream(path);
}else{
throw new ReadWriteException("Path for DataSource from " + deviceName + " not found in configuration");
}

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Device");

for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String deviceId = eElement.getElementsByTagName("Id").item(0).getTextContent();
//Suchbegriff mit dem Wert des aktuellen Elements vergleichen
if(deviceId.equals(deviceName)){
// Messwert erstellen aus Xml-File
measurementValue.setId(deviceId);
measurementValue.setValue(eElement.getElementsByTagName("Value").item(0).getTextContent());
measurementValue.setTime(new File(path).lastModified());
break;
}
}
}
} catch (ParserConfigurationException e) {
throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage());
} catch (SAXException e) {
throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage());
} catch (IOException e) {
throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage());
}
if(measurementValue == null){
throw new ReadWriteException("Der Name " + deviceName + " wurde in " + path +" nicht gefunden");
}
return measurementValue;
}


/**
* Gibt einen Messwert mit Zeitstempel in Form einer Instanz von MeasurementValueXml zurueck,
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/services/ProtocolNotSupportedException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package services;

public class ProtocolNotSupportedException extends RuntimeException{

public ProtocolNotSupportedException(String protocol) {
super("Not supported protocol-type: " + protocol);
}
}
69 changes: 69 additions & 0 deletions src/main/java/services/Xml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package services;

import model.MeasurementValueXml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

public class Xml {

/**
* Gibt einen Messwert mit Zeitstempel in Form einer Instanz von MeasurementValueXml zurueck,
* wenn das Protokoll xml-1 konfiguriert wurde.
*/
public static MeasurementValueXml getActualValue(String deviceName) {
String path = DeviceMapperJson.getMeasurementValuePath(deviceName);
path = DeviceMapperJson.class.getResource(path).getFile().replaceAll("%20", " ");
return getMeasurementValueXml(deviceName, path);
}


private static MeasurementValueXml getMeasurementValueXml(String deviceName, String path) {
MeasurementValueXml measurementValue = new MeasurementValueXml();
NodeList nList = getNodeList(path);

for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String deviceId = eElement.getElementsByTagName("Id").item(0).getTextContent();
if (deviceId.equals(deviceName)) {
measurementValue.setId(deviceId);
measurementValue.setValue(eElement.getElementsByTagName("Value").item(0).getTextContent());
measurementValue.setTime(new File(path).lastModified());
return measurementValue;
}
}
}
throw new ReadWriteException("Gerät \"" + deviceName + "\" wurde nicht gefunden in: " + path);
}


private static NodeList getNodeList(String path) {
File xmlFile = new File(path);
Document doc = getDocument(xmlFile, path);
return doc.getElementsByTagName("Device");
}


private static Document getDocument(File xmlFile, String path) {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
return doc;
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage());
}
}

}