forked from ibwgr/messdaten-server
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDeviceMapperJson.java
More file actions
200 lines (180 loc) · 6.82 KB
/
DeviceMapperJson.java
File metadata and controls
200 lines (180 loc) · 6.82 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package services;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.Device;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
/**
* Created by Nett on 27.05.2017.
*
* Die Klasse DeviceMapperJson stellt Methoden für die Konvertierung
* von Json nach List<Device> und umgekehrt zur Verfügung.
*/
public class DeviceMapperJson {
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
* Wirft eine IOException wenn die Liste nicht erstellt werden konnte
*
* @return
*/
public static List<Device> getDeviceListFromConfig() throws ReadWriteException {
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<List<Device>> listType = new TypeReference<List<Device>>() {
};
List<Device> devices;
String configPath = CONFIGPATH;
try {
devices = objectMapper.readValue(new File(configPath), listType);
} catch (IOException e) {
throw new ReadWriteException("Could not find devices in " + CONFIGPATH + "\n" + e.getMessage());
}
return devices;
}
/**
* Fuegt der List<Device> einen neuen Device hinzu, falls dieser valid ist.
* Mapped die Liste ins Json-Config-File
*
* Wirft eine ReadWriteException falls das Config-File nicht erstellt werden konnte
*
* @param newDevice
* @return
*/
public static Device addDeviceToConfig(Device newDevice)throws ReadWriteException{
ObjectMapper objectMapper = new ObjectMapper();
List<Device>devices = getDeviceListFromConfig();
if(DeviceValidator.validateDevice(devices,newDevice)){
devices.add(newDevice);
}else{
throw new ReadWriteException( "Device " + newDevice.getName() + " not valid to add to configuration");
}
try {
objectMapper.writeValue(new File(CONFIGPATH), devices);
} catch (IOException e) {
throw new ReadWriteException("Could not save new device-list to " + CONFIGPATH
+ ", after adding " + newDevice.getName() + " to list"+ "\n" + e.getMessage());
}
return newDevice;
}
/**
* Aktualisiert einen Device in der Konfiguration
*
* Wirft eine ReadWriteException falls das Config-File nicht erstellt werden konnte
*
* @param updDevice
* @return
*/
public static Device updateDeviceInConfig(Device updDevice)throws ReadWriteException{
ObjectMapper objectMapper = new ObjectMapper();
List<Device>devices = getDeviceListFromConfig();
if(DeviceValidator.isValidForUpdate(devices,updDevice)){
for(Device device : devices){
if(device.getName().equals(updDevice.getName())){
device.setHostIp(updDevice.getHostIp());
device.setDataSource(updDevice.getDataSource());
device.setGroup(updDevice.getGroup());
device.setProtocol(updDevice.getProtocol());
}
}
}else{
throw new ReadWriteException( "Device " + updDevice.getName() + " not valid to update in configuration");
}
try {
objectMapper.writeValue(new File(CONFIGPATH), devices);
} catch (IOException e) {
throw new ReadWriteException("Could not save new device-list to " + CONFIGPATH
+ ", after updating " + updDevice.getName() + " in list"+ "\n" + e.getMessage());
}
return updDevice;
}
/**
* Parsed das File DeviceConfiguration.json in eine JsonNode und gibt diese zurueck
* Wirft eine IOException wenn die Liste nicht erstellt werden konnte
*
* @return
*/
public static JsonNode getJsonNode() throws ReadWriteException{
File file = new File(CONFIGPATH);
JsonNode devicesJson;
try {
FileInputStream is = new FileInputStream(file);
devicesJson = new ObjectMapper().createParser(is).readValueAsTree();
} catch(IOException e){
throw new ReadWriteException("Could not find devices in " + CONFIGPATH + "\n" + e.getMessage());
}
return devicesJson;
}
/**
* Findet in der aktuellen Konfiguration einen Device ueber seinen Namen und gibt diesen zurueck.
* Wird kein Device gefunden gibt die Methode null zurueck.
*
* @param name
* @return
*/
public static Device findDevice(String name){
List<Device>devices = getDeviceListFromConfig();
for(Device device : devices){
if(device.getName().equals(name)){
return device;
}
}
return null;
}
/**
* Loescht einen Device aus der Konfiguration
*
* Wirft eine ReadWriteException falls das Config-File nicht erstellt werden konnte
*
* @param delDevice
* @return
*/
public static boolean deleteDeviceInConfig(Device delDevice) throws ReadWriteException{
ObjectMapper objectMapper = new ObjectMapper();
List<Device>devices = getDeviceListFromConfig();
boolean deleted = false;
deleted = devices.remove(delDevice);
if(!deleted){
throw new ReadWriteException("Could not delete device " + delDevice.getName() +" from list");
}
try {
objectMapper.writeValue(new File(CONFIGPATH), devices);
} catch (IOException e) {
throw new ReadWriteException("Could not save new device-list to " + CONFIGPATH
+ ", after removing " + delDevice.getName() + " from list"+ "\n" + e.getMessage());
}
return deleted;
}
/**
* Gibt den Pfad der Data-Source des gesuchen Devices gemaess Konfiguration zurueck
*
* @param deviceName
* @return
*/
public static String getMeasurementValuePath(String deviceName){
List<Device>devices = getDeviceListFromConfig();
for(Device device : devices){
if(device.getName().equals(deviceName)){
return device.getDataSource();
}
}
return null;
}
/**
* Gibt das Protokoll des gesuchen Devices gemaess Konfiguration zurueck
*
* @param deviceName
* @return
*/
public static String getMeasurementValueProtocol(String deviceName){
List<Device>devices = getDeviceListFromConfig();
for(Device device : devices){
if(device.getName().equals(deviceName)){
return device.getProtocol();
}
}
return null;
}
}