Skip to content

Commit 7421953

Browse files
committed
sensorDb: Add ability to parse "Delta Time" lines in sample files. Add
DbApiMemory to help testing features with a memory database (mock database).
1 parent bfc90dd commit 7421953

File tree

9 files changed

+651
-8
lines changed

9 files changed

+651
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
package ca.carleton.gcrc.sensorDb.dbapi.memory;
2+
3+
import java.util.Collection;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
import java.util.Vector;
10+
11+
import ca.carleton.gcrc.sensorDb.dbapi.DbAPI;
12+
import ca.carleton.gcrc.sensorDb.dbapi.Device;
13+
import ca.carleton.gcrc.sensorDb.dbapi.DeviceLocation;
14+
import ca.carleton.gcrc.sensorDb.dbapi.DeviceSensorProfile;
15+
import ca.carleton.gcrc.sensorDb.dbapi.ImportRecord;
16+
import ca.carleton.gcrc.sensorDb.dbapi.Location;
17+
import ca.carleton.gcrc.sensorDb.dbapi.LogRecord;
18+
import ca.carleton.gcrc.sensorDb.dbapi.Observation;
19+
import ca.carleton.gcrc.sensorDb.dbapi.ObservationReader;
20+
import ca.carleton.gcrc.sensorDb.dbapi.Sensor;
21+
22+
/**
23+
* Implements a DbAPI in memory. Should only be used for testing.
24+
*
25+
*/
26+
public class DbApiMemory implements DbAPI {
27+
28+
static private int g_nextUuidInt = 1;
29+
30+
static synchronized private String getNextUUID(){
31+
String uuid = String.format("%08x", g_nextUuidInt);
32+
++g_nextUuidInt;
33+
return uuid;
34+
}
35+
36+
private Map<String,DeviceSensorProfile> deviceSensorProfilesById = new HashMap<String,DeviceSensorProfile>();
37+
private Map<String,Sensor> sensorsById = new HashMap<String,Sensor>();
38+
private Map<String,Device> devicesById = new HashMap<String,Device>();
39+
private Map<String,DeviceLocation> deviceLocationsById = new HashMap<String,DeviceLocation>();
40+
private Map<String,Location> locationsById = new HashMap<String,Location>();
41+
private Map<String,Observation> observationsById = new HashMap<String,Observation>();
42+
private Map<String,ImportRecord> importRecordsById = new HashMap<String,ImportRecord>();
43+
private Map<String,LogRecord> logRecordsById = new HashMap<String,LogRecord>();
44+
45+
@Override
46+
public Collection<DeviceSensorProfile> getDeviceSensorProfilesFromManufacturerDeviceName(
47+
String manufacturerDeviceName
48+
) throws Exception {
49+
50+
List<DeviceSensorProfile> profiles = new Vector<DeviceSensorProfile>();
51+
52+
for(DeviceSensorProfile profile : deviceSensorProfilesById.values()){
53+
if( manufacturerDeviceName.equals(profile.getManufacturerDeviceName()) ){
54+
profiles.add(profile);
55+
}
56+
}
57+
58+
return profiles;
59+
}
60+
61+
@Override
62+
public Collection<DeviceSensorProfile> getDeviceSensorProfiles() throws Exception {
63+
List<DeviceSensorProfile> profiles = new Vector<DeviceSensorProfile>( deviceSensorProfilesById.values() );
64+
65+
return profiles;
66+
}
67+
68+
@Override
69+
public Sensor createSensor(Sensor sensor) throws Exception {
70+
Sensor dbSensor = new Sensor();
71+
72+
dbSensor.setId( getNextUUID() );
73+
dbSensor.setAccuracy( sensor.getAccuracy() );
74+
dbSensor.setDeviceId( sensor.getDeviceId() );
75+
dbSensor.setHeightInMetres( sensor.getHeightInMetres() );
76+
dbSensor.setLabel( sensor.getLabel() );
77+
dbSensor.setPrecision( sensor.getPrecision() );
78+
dbSensor.setSerialNumber( sensor.getSerialNumber() );
79+
dbSensor.setTypeOfMeasurement( sensor.getTypeOfMeasurement() );
80+
dbSensor.setUnitOfMeasurement( sensor.getUnitOfMeasurement() );
81+
82+
sensorsById.put(dbSensor.getId(), dbSensor);
83+
84+
return dbSensor;
85+
}
86+
87+
@Override
88+
public Collection<Sensor> getSensors() throws Exception {
89+
List<Sensor> sensors = new Vector<Sensor>( sensorsById.values() );
90+
91+
return sensors;
92+
}
93+
94+
@Override
95+
public List<Sensor> getSensorsFromDeviceId(String device_id) throws Exception {
96+
List<Sensor> sensors = new Vector<Sensor>();
97+
98+
for(Sensor sensor : sensorsById.values()){
99+
if( device_id.equals( sensor.getDeviceId() ) ){
100+
sensors.add(sensor);
101+
}
102+
}
103+
104+
return sensors;
105+
}
106+
107+
@Override
108+
public Device createDevice(Device device) throws Exception {
109+
Device dbDevice = new Device();
110+
111+
dbDevice.setId( getNextUUID() );
112+
dbDevice.setAccessCode( device.getAccessCode() );
113+
dbDevice.setAcquiredOn( device.getAcquiredOn() );
114+
dbDevice.setDeviceType( device.getDeviceType() );
115+
dbDevice.setManufacturer( device.getManufacturer() );
116+
dbDevice.setManufacturerDeviceName( device.getManufacturerDeviceName() );
117+
dbDevice.setNotes( device.getNotes() );
118+
dbDevice.setSerialNumber( device.getSerialNumber() );
119+
120+
devicesById.put(dbDevice.getId(), dbDevice);
121+
122+
return dbDevice;
123+
}
124+
125+
@Override
126+
public Collection<Device> getDevices() throws Exception {
127+
List<Device> devices = new Vector<Device>( devicesById.values() );
128+
129+
return devices;
130+
}
131+
132+
@Override
133+
public Device getDeviceFromId(String id) throws Exception {
134+
return devicesById.get(id);
135+
}
136+
137+
@Override
138+
public Device getDeviceFromSerialNumber(String serialNumber) throws Exception {
139+
for(Device device : devicesById.values()){
140+
if( serialNumber.equals(device.getSerialNumber()) ){
141+
return device;
142+
}
143+
}
144+
145+
return null;
146+
}
147+
148+
@Override
149+
public DeviceLocation createDeviceLocation(DeviceLocation deviceLocation) throws Exception {
150+
DeviceLocation dbDeviceLocation = new DeviceLocation();
151+
152+
dbDeviceLocation.setId( getNextUUID() );
153+
dbDeviceLocation.setDeviceId( deviceLocation.getDeviceId() );
154+
dbDeviceLocation.setLocationId( deviceLocation.getLocationId() );
155+
dbDeviceLocation.setNotes( deviceLocation.getNotes() );
156+
dbDeviceLocation.setTimestamp( deviceLocation.getTimestamp() );
157+
158+
deviceLocationsById.put(dbDeviceLocation.getId(), dbDeviceLocation);
159+
160+
return dbDeviceLocation;
161+
}
162+
163+
@Override
164+
public List<DeviceLocation> getDeviceLocations() throws Exception {
165+
List<DeviceLocation> deviceLocations = new Vector<DeviceLocation>( deviceLocationsById.values() );
166+
167+
return deviceLocations;
168+
}
169+
170+
@Override
171+
public List<DeviceLocation> getDeviceLocationsFromDeviceId(String device_id) throws Exception {
172+
List<DeviceLocation> deviceLocations = new Vector<DeviceLocation>();
173+
174+
for(DeviceLocation deviceLocation : deviceLocationsById.values()){
175+
if( device_id.equals(deviceLocation.getDeviceId()) ){
176+
deviceLocations.add(deviceLocation);
177+
}
178+
}
179+
180+
return deviceLocations;
181+
}
182+
183+
@Override
184+
public List<Location> getLocationsFromDeviceLocations(List<DeviceLocation> deviceLocations) throws Exception {
185+
List<Location> locations = new Vector<Location>();
186+
187+
// Accumulate all location ids
188+
Set<String> locationIds = new HashSet<String>();
189+
for(DeviceLocation deviceLocation : deviceLocations){
190+
String locationId = deviceLocation.getLocationId();
191+
if( null != locationId ){
192+
locationIds.add(locationId);
193+
}
194+
}
195+
196+
for(String locationId : locationIds){
197+
Location location = getLocationFromLocationId(locationId);
198+
locations.add(location);
199+
}
200+
201+
return locations;
202+
}
203+
204+
@Override
205+
public Location createLocation(Location location) throws Exception {
206+
Location dbLocation = new Location();
207+
208+
dbLocation.setId( getNextUUID() );
209+
dbLocation.setAccuracy( location.getAccuracy() );
210+
dbLocation.setComment( location.getComment() );
211+
dbLocation.setElevation( location.getElevation() );
212+
dbLocation.setGeometry( location.getGeometry() );
213+
dbLocation.setName( location.getName() );
214+
dbLocation.setRecordingObservations( location.isRecordingObservations() );
215+
216+
locationsById.put(dbLocation.getId(), dbLocation);
217+
218+
return dbLocation;
219+
}
220+
221+
@Override
222+
public Location getLocationFromLocationId(String locationId) throws Exception {
223+
return locationsById.get(locationId);
224+
}
225+
226+
@Override
227+
public Collection<Location> getLocations() throws Exception {
228+
List<Location> locations = new Vector<Location>( locationsById.values() );
229+
230+
return locations;
231+
}
232+
233+
@Override
234+
public Observation createObservation(Observation observation) throws Exception {
235+
Observation dbObservation = new Observation();
236+
237+
dbObservation.setId( getNextUUID() );
238+
dbObservation.setAccuracy( observation.getAccuracy() );
239+
dbObservation.setCorrectedTime( observation.getCorrectedTime() );
240+
dbObservation.setDeviceId( observation.getDeviceId() );
241+
dbObservation.setElevation( observation.getElevation() );
242+
dbObservation.setImportId( observation.getImportId() );
243+
dbObservation.setImportKey( observation.getImportKey() );
244+
dbObservation.setLocation( observation.getLocation() );
245+
dbObservation.setLoggedTime( observation.getLoggedTime() );
246+
dbObservation.setMaxHeight( observation.getMaxHeight() );
247+
dbObservation.setMinHeight( observation.getMinHeight() );
248+
dbObservation.setNumericValue( observation.getNumericValue() );
249+
dbObservation.setObservationType( observation.getObservationType() );
250+
dbObservation.setPrecision( observation.getPrecision() );
251+
dbObservation.setSensorId( observation.getSensorId() );
252+
dbObservation.setTextValue( observation.getTextValue() );
253+
dbObservation.setUnitOfMeasure( observation.getUnitOfMeasure() );
254+
255+
observationsById.put(dbObservation.getId(), dbObservation);
256+
257+
return dbObservation;
258+
}
259+
260+
@Override
261+
public ObservationReader getObservationsFromImportId(String importId) throws Exception {
262+
List<Observation> observations = new Vector<Observation>();
263+
264+
for(Observation observation : observationsById.values()){
265+
if( importId.equals(observation.getImportId()) ){
266+
observations.add(observation);
267+
}
268+
}
269+
270+
ObservationReaderMemory obsReader = new ObservationReaderMemory(observations);
271+
272+
return obsReader;
273+
}
274+
275+
@Override
276+
public Observation getObservationFromImportKey(String importKey) throws Exception {
277+
for(Observation observation : observationsById.values()){
278+
if( importKey.equals(observation.getImportKey()) ){
279+
return observation;
280+
}
281+
}
282+
return null;
283+
}
284+
285+
@Override
286+
public ImportRecord createImportRecord(ImportRecord importRecord) throws Exception {
287+
ImportRecord dbImportRecord = new ImportRecord();
288+
289+
dbImportRecord.setId( getNextUUID() );
290+
dbImportRecord.setFileName( importRecord.getFileName() );
291+
dbImportRecord.setImportParameters( importRecord.getImportParameters() );
292+
dbImportRecord.setImportTime( importRecord.getImportTime() );
293+
294+
importRecordsById.put(dbImportRecord.getId(), dbImportRecord);
295+
296+
return dbImportRecord;
297+
}
298+
299+
@Override
300+
public List<ImportRecord> getImportRecords() throws Exception {
301+
List<ImportRecord> importRecords = new Vector<ImportRecord>( importRecordsById.values() );
302+
303+
return importRecords;
304+
}
305+
306+
@Override
307+
public ImportRecord getImportRecordFromImportId(String importId) throws Exception {
308+
return importRecordsById.get(importId);
309+
}
310+
311+
@Override
312+
public LogRecord createLogRecord(LogRecord logRecord) throws Exception {
313+
LogRecord dbLogRecord = new LogRecord();
314+
315+
dbLogRecord.setId( getNextUUID() );
316+
dbLogRecord.setLog( logRecord.getLog() );
317+
dbLogRecord.setTimestamp( logRecord.getTimestamp() );
318+
319+
logRecordsById.put(dbLogRecord.getId(), dbLogRecord);
320+
321+
return dbLogRecord;
322+
}
323+
324+
@Override
325+
public List<LogRecord> getLogRecords() throws Exception {
326+
List<LogRecord> logRecords = new Vector<LogRecord>( logRecordsById.values() );
327+
return logRecords;
328+
}
329+
330+
@Override
331+
public LogRecord getLogRecordFromId(String logId) throws Exception {
332+
return logRecordsById.get(logId);
333+
}
334+
335+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ca.carleton.gcrc.sensorDb.dbapi.memory;
2+
3+
import java.util.List;
4+
5+
import ca.carleton.gcrc.sensorDb.dbapi.Observation;
6+
import ca.carleton.gcrc.sensorDb.dbapi.ObservationReader;
7+
8+
public class ObservationReaderMemory implements ObservationReader {
9+
10+
private int index = 0;
11+
private List<Observation> observations = null;
12+
13+
public ObservationReaderMemory(List<Observation> observations){
14+
this.observations = observations;
15+
}
16+
17+
@Override
18+
public Observation read() throws Exception {
19+
if( index >= observations.size() ){
20+
return null;
21+
}
22+
23+
Observation observation = observations.get(index);
24+
++index;
25+
return observation;
26+
}
27+
28+
@Override
29+
public void close() throws Exception {
30+
}
31+
32+
}

0 commit comments

Comments
 (0)