Skip to content

Commit

Permalink
SMHE-832 Object Registration
Browse files Browse the repository at this point in the history
get files from resources

Signed-off-by: Natasja Nortier <natasja.nortier@alliander.com>
  • Loading branch information
natasja-n committed Jun 29, 2022
1 parent a23d013 commit 4a94c2d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,38 @@
*/
package org.opensmartgridplatform.domain.smartmetering.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import org.opensmartgridplatform.domain.smartmetering.config.CosemObject;
import org.opensmartgridplatform.domain.smartmetering.config.MeterConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

@Service
public class DlmsObjectService {

private final List<MeterConfig> meterConfigList;
private List<MeterConfig> meterConfigList;

@Autowired
public DlmsObjectService(final List<MeterConfig> meterConfigList) {

this.meterConfigList = meterConfigList;
}

public Map<DlmsObjectType, CosemObject> getCosemObjects(
final String protocolName, final String protocolVersion) {
final String protocolName, final String protocolVersion) throws IOException {

if (this.meterConfigList == null || this.meterConfigList.isEmpty()) {
this.meterConfigList = this.getMeterConfigListFromResources();
}

final Optional<MeterConfig> meterConfig =
this.meterConfigList.stream()
Expand Down Expand Up @@ -59,4 +69,28 @@ private Optional<Map<DlmsObjectType, CosemObject>> getCosemObjectFromMeterConfig
});
return Optional.of(cosemObjectMap);
}

private List<MeterConfig> getMeterConfigListFromResources() throws IOException {
final ClassPathResource c = new ClassPathResource("/");
final ObjectMapper objectMapper = new ObjectMapper();

final List<MeterConfig> meterConfigs = new ArrayList<>();
try (final Stream<Path> stream = Files.walk(c.getFile().toPath())) {
stream
.map(Path::normalize)
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().endsWith(".json"))
.map(Path::toFile)
.forEach(
file -> {
try {
final MeterConfig meterConfig = objectMapper.readValue(file, MeterConfig.class);
meterConfigs.add(meterConfig);
} catch (final IOException e) {
e.printStackTrace();
}
});
}
return meterConfigs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void setUp() throws IOException {
}

@Test
void testGetCosemObject() {
void testGetCosemObject() throws IOException {
final String protocolVersion52 = "5.2";
final String protocolVersion50 = "5.0";
final String protocolName = "SMR";
Expand All @@ -61,7 +61,7 @@ void testGetCosemObject() {
}

@Test
void testNoCosemObjectFound() {
void testNoCosemObjectFound() throws IOException {
final Map<DlmsObjectType, CosemObject> cosemObjects =
this.dlmsObjectService.getCosemObjects("ABC", "12");

Expand Down

0 comments on commit 4a94c2d

Please sign in to comment.