forked from ibwgr/messdaten-server
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMeasurementValueReader.java
More file actions
167 lines (142 loc) · 6.01 KB
/
MeasurementValueReader.java
File metadata and controls
167 lines (142 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package services;
import model.MeasurementValueXml;
import org.xml.sax.SAXException;
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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
/**
* Die Klasse dient als Schnittstelle zur Datenhaltung der Messwerte.
* Je nach konfiguriertem Protokoll wird die entsprechende Methode aufgerufen.
*/
public class MeasurementValueReader {
public static MeasurementValueXml getActualValue(String deviceName){
String protocol = DeviceMapperJson.getMeasurementValueProtocol(deviceName);
switch (protocol){
case "xml-1":
return Xml.getActualValue(deviceName);
case "txt-1":
return getActualValueFromTxt(deviceName);
case "xml-2":
return getActualValueFromXmlSax(deviceName);
default:
throw new ProtocolNotSupportedException(protocol);
}
}
/**
* Gibt einen Messwert mit Zeitstempel in Form einer Instanz von MeasurementValueXml zurueck,
* wenn das Protokoll xml-2 konfiguriert wurde.
*
* Wirft im Fehlerfall eine ReadWriteException
*
* @param deviceName
* @return
* @throws ReadWriteException
*/
public static MeasurementValueXml getActualValueFromXmlSax(String deviceName)throws ReadWriteException {
// Pfad der Messwerte-Files gemaess deviceName aus der Konfiguration lesen und ein File erstellen
String path = DeviceMapperJson.getMeasurementValuePath(deviceName);
long time = 0;
MeasurementValueXml actualValue = null;
SaxHandler handler = new SaxHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
ArrayList<MeasurementValueXml> measurementValues = (ArrayList<MeasurementValueXml>) handler.getMeasurementValues();
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(
new SequenceInputStream(
Collections.enumeration(Arrays.asList(
new InputStream[] {
new ByteArrayInputStream("<ChannelResult>".getBytes()),
new FileInputStream(path),
new ByteArrayInputStream("</ChannelResult>".getBytes()),
}))
), handler);
} 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 (FileNotFoundException e) {
throw new ReadWriteException("Datei konnte nicht gefunden werden " + path + "\n" + e.getMessage());
} catch (IOException e) {
throw new ReadWriteException("Fehler beim Lesen von " + path + "\n" + e.getMessage());
}
// Aktuellster Messwert zuweisen
for (MeasurementValueXml value : measurementValues){
if(value.getTime() > time){
actualValue = value;
time = value.getTime();
}
}
if(actualValue == null){
throw new ReadWriteException("Der Name " + deviceName + " wurde in " + path +" nicht gefunden");
}
//Bei Abbruch des Bluetooth-Signals der Messuhr wird NaN (Not_A_Number) als Messwert eingelesen
if(actualValue.getValue().equals("NaN")){
throw new ReadWriteException("Kein gültiger Messwert in " + path +" für " + deviceName + " gefunden");
}
return actualValue;
}
/**
* Gibt einen Messwert mit Zeitstempel in Form einer Instanz von MeasurementValueXml zurueck,
* wenn das Protokoll txt-1 konfiguriert wurde.
*
* Wirft im Fehlerfall eine ReadWriteException
*
* @param deviceName
* @return
* @throws ReadWriteException
*/
public static MeasurementValueXml getActualValueFromTxt(String deviceName)throws ReadWriteException{
FileInputStream in = null;
BufferedReader br = null;
String strLine = null;
String line = null;
String lastLine = null;
MeasurementValueXml measurementValue = new MeasurementValueXml();
// Pfad der Messwerte-Files gemaess deviceName aus der Konfiguration lesen und ein File erstellen
String path = DeviceMapperJson.getMeasurementValuePath(deviceName);
try {
in = new FileInputStream(path);
br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null)
{
strLine = line;
}
lastLine = strLine;
System.out.println(lastLine);
in.close();
} catch (FileNotFoundException ex) {
throw new ReadWriteException(ex.getMessage());
} catch (IOException ex) {
throw new ReadWriteException(ex.getMessage());
}
String[] tokens = lastLine.split(";");
measurementValue.setId(tokens[0]);
measurementValue.setValue(tokens[1]);
tokens[2].split(" ");
String pattern = "dd.MM.yyyy HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = null;
try {
date = simpleDateFormat.parse(tokens[2]);
} catch (ParseException e) {
e.printStackTrace();
}
measurementValue.setTime(date.getTime());
return measurementValue;
}
}