Frist impl sdm reader
This commit is contained in:
1 parent
f4131286e6
commit
0b11b32d96
22 files changed
+340
-123
No files matched your search
Generated
+3
-3
@@ -1,7 +1,7 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<HCL-Terraform>
|
||||
<option name="PROPERTY_ALIGNMENT" value="2" />
|
||||
</HCL-Terraform>
|
||||
<ScalaCodeStyleSettings>
|
||||
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
|
||||
</ScalaCodeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
||||
Generated
+1
@@ -2,6 +2,7 @@
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile default="true" name="Default" enabled="true" />
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
|
||||
@@ -15,10 +15,45 @@
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.11.1</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>30.1.1-jre</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ghgande</groupId>
|
||||
<artifactId>j2mod</artifactId>
|
||||
<version>2.7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.22</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -3,6 +3,10 @@ package home.automation;
|
||||
import com.ghgande.j2mod.modbus.ModbusIOException;
|
||||
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
|
||||
import com.ghgande.j2mod.modbus.procimg.Register;
|
||||
import home.automation.modbus.ModbusRtuTcpRepository;
|
||||
import home.automation.modbus.Registry;
|
||||
import home.automation.modbus.SDMService;
|
||||
import home.automation.modbus.configuration.JsonFileBasedConfiguration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
@@ -10,32 +14,9 @@ import java.net.UnknownHostException;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
InetAddress byName = InetAddress.getByName("192.168.0.65");
|
||||
ModbusTCPMaster modbusRTUTCPTransport = new ModbusTCPMaster("192.168.0.65", 502);
|
||||
modbusRTUTCPTransport.connect();
|
||||
for (int i = 0; i < 1; i++) {
|
||||
int ref = 4065;
|
||||
Register[] regs = modbusRTUTCPTransport.readMultipleRegisters(1, 4065, 75);
|
||||
for (Register reg : regs) {
|
||||
float v = reg.toShort();
|
||||
if (v < 0) {
|
||||
System.out.println(ref + " " + -(v + 32768)/10);
|
||||
} else {
|
||||
System.out.println(ref + " " + reg.getValue()/10.0);
|
||||
}
|
||||
ref++;
|
||||
}
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ModbusIOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
public static void main(String[] args) throws Exception {
|
||||
SDMService service = new SDMService(new ModbusRtuTcpRepository(new JsonFileBasedConfiguration()));
|
||||
final Registry registry = service.get(4070);
|
||||
System.out.println(registry);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package home.automation;
|
||||
|
||||
public class ModbusRtuTcpRepository {
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package home.automation;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public class RegisterFactory {
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
Optional<RegistryMapping> create(String json) {
|
||||
try {
|
||||
return Optional.of(mapper.readValue(json, RegistryMapping.class));
|
||||
} catch (JsonProcessingException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package home.automation;
|
||||
|
||||
public class RegistryMapping {
|
||||
private final int registry;
|
||||
private final String desc;
|
||||
|
||||
public RegistryMapping(int registry, String desc) {
|
||||
this.registry = registry;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public int getRegistry() {
|
||||
return registry;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package home.automation;
|
||||
|
||||
public class RegistryReader {
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package home.automation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Root{
|
||||
public List<RegistryMapping> registry_mapping;
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package home.automation;
|
||||
|
||||
import com.ghgande.j2mod.modbus.ModbusException;
|
||||
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
|
||||
import com.ghgande.j2mod.modbus.procimg.Register;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
public class SDMService {
|
||||
|
||||
private final InetAddress address;
|
||||
private final ModbusTCPMaster modbusConnection;
|
||||
private final int port;
|
||||
|
||||
public SDMService(InetAddress address, int port) throws Exception {
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
this.modbusConnection = initConnection();
|
||||
}
|
||||
|
||||
public Register[] read(RegistryMapping mapping) {
|
||||
try {
|
||||
return modbusConnection.readMultipleRegisters(mapping.getRegistry(), 1);
|
||||
} catch (ModbusException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new Register[0];
|
||||
}
|
||||
|
||||
private ModbusTCPMaster initConnection() throws Exception {
|
||||
ModbusTCPMaster modbusRTUTCPTransport = new ModbusTCPMaster(address.getHostAddress(), port);
|
||||
modbusRTUTCPTransport.connect();
|
||||
return modbusRTUTCPTransport;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package home.automation.modbus;
|
||||
|
||||
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
|
||||
import com.ghgande.j2mod.modbus.procimg.Register;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import home.automation.modbus.configuration.ConfigurationStrategy;
|
||||
import home.automation.modbus.configuration.RegistryConfiguration;
|
||||
import home.automation.modbus.configuration.RegistryMapping;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ModbusRtuTcpRepository {
|
||||
private final LoadingCache<Integer, Registry> repositoryCache;
|
||||
private final ConfigurationStrategy config;
|
||||
|
||||
public ModbusRtuTcpRepository(ConfigurationStrategy config) throws Exception {
|
||||
this.config = config;
|
||||
|
||||
CacheLoader<Integer, Registry> loader = configureCacheLoader(config);
|
||||
repositoryCache = CacheBuilder.newBuilder()
|
||||
.refreshAfterWrite(5, TimeUnit.SECONDS)
|
||||
.build(loader);
|
||||
|
||||
final List<Integer> allRegisters = config.getConfig().get().getRegistry_mapping().stream().map(RegistryMapping::getRegistry).collect(Collectors.toList());
|
||||
//loader.loadAll(allRegisters);
|
||||
}
|
||||
|
||||
private CacheLoader<Integer, Registry> configureCacheLoader(ConfigurationStrategy config) {
|
||||
return new CacheLoader<>() {
|
||||
@Override
|
||||
public Registry load(Integer registerNumber) throws Exception {
|
||||
final RegistryConfiguration registryConfiguration = config.getConfig().get();
|
||||
final String address = registryConfiguration.getAddress();
|
||||
final int port = registryConfiguration.getPort();
|
||||
|
||||
final ModbusTCPMaster modbusTCPMaster = new ModbusTCPMaster(address, port);
|
||||
modbusTCPMaster.connect();
|
||||
final Register[] registers = modbusTCPMaster.readMultipleRegisters(registerNumber, 1);
|
||||
|
||||
|
||||
final Optional<Registry> registryMeasurement = Arrays.stream(registers).findFirst().map(register -> Registry.builder()
|
||||
.registry(registerNumber)
|
||||
.desc(registryConfiguration.getRegistry_mapping()
|
||||
.stream()
|
||||
.filter(elem -> elem.getRegistry().equals(registerNumber))
|
||||
.findFirst()
|
||||
.map(RegistryMapping::getDesc)
|
||||
.orElse("Uknow registry number"))
|
||||
.value(normalizeRegistryValue(register))
|
||||
.build());
|
||||
return registryMeasurement.orElse(null);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String normalizeRegistryValue(Register reg) {
|
||||
float v = reg.toShort();
|
||||
if (v < 0) {
|
||||
return String.valueOf(-(v + 32768) / 10);
|
||||
} else {
|
||||
return String.valueOf((reg.getValue() / 10.0));
|
||||
}
|
||||
}
|
||||
|
||||
public List<Registry> getAll() {
|
||||
return (List<Registry>) repositoryCache.asMap().values();
|
||||
}
|
||||
|
||||
public Registry get(int register) throws ExecutionException {
|
||||
return repositoryCache.get(register);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package home.automation.modbus;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
public class Registry {
|
||||
private Integer registry;
|
||||
private String desc;
|
||||
private String value;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package home.automation.modbus;
|
||||
|
||||
import home.automation.modbus.configuration.RegistryMapping;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class SDMService {
|
||||
|
||||
private final ModbusRtuTcpRepository repository;
|
||||
|
||||
public SDMService(ModbusRtuTcpRepository repository) throws Exception {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<Registry> readAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public Registry get(int register) throws ExecutionException {
|
||||
return repository.get(register);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package home.automation.modbus.configuration;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ConfigurationStrategy {
|
||||
static final ObjectMapper mapper = new ObjectMapper();
|
||||
public Optional<RegistryConfiguration> getConfig() throws IOException;
|
||||
|
||||
default Optional<RegistryConfiguration> create(String json) {
|
||||
try {
|
||||
return Optional.of(mapper.readValue(json, RegistryConfiguration.class));
|
||||
} catch (JsonProcessingException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package home.automation.modbus.configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
public class JsonFileBasedConfiguration implements ConfigurationStrategy {
|
||||
private final String CONFIGURATION_NAME = "registryMapping.json";
|
||||
|
||||
@Override
|
||||
public Optional<RegistryConfiguration> getConfig() throws IOException {
|
||||
final InputStream resource = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIGURATION_NAME);
|
||||
Objects.requireNonNull(resource, "Configuration file not present please check the path:" + System.getProperty("usr.dir"));
|
||||
String jsonConfiguration = new String(resource.readAllBytes(), UTF_8);
|
||||
return create(jsonConfiguration);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package home.automation.modbus.configuration;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.fasterxml.jackson.annotation.JsonInclude.Include.*;
|
||||
|
||||
@JsonInclude(NON_EMPTY)
|
||||
@ToString
|
||||
public class RegistryConfiguration {
|
||||
private String address;
|
||||
private Integer port;
|
||||
private List<RegistryMapping> registry_mapping;
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public List<RegistryMapping> getRegistry_mapping() {
|
||||
return registry_mapping;
|
||||
}
|
||||
|
||||
public void setRegistry_mapping(List<RegistryMapping> registry_mapping) {
|
||||
this.registry_mapping = registry_mapping;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package home.automation.modbus.configuration;
|
||||
|
||||
import lombok.ToString;
|
||||
|
||||
@ToString
|
||||
public class RegistryMapping {
|
||||
private Integer registry;
|
||||
private String desc;
|
||||
|
||||
public void setRegistry(Integer registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public Integer getRegistry() {
|
||||
return registry;
|
||||
}
|
||||
|
||||
public void setDesc(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
File renamed without changes.
+17
@@ -0,0 +1,17 @@
|
||||
package home.automation.modbus.configuration;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
class JsonFileBasedConfigurationTest {
|
||||
|
||||
@Test
|
||||
public void GivenFileConfigurationStrategy_When_getConfig_Then_MappedClassShoudlBeCreated() throws IOException {
|
||||
ConfigurationStrategy configuration = new JsonFileBasedConfiguration();
|
||||
final Optional<RegistryConfiguration> config = configuration.getConfig();
|
||||
assert (config.isPresent());
|
||||
System.out.println(config.get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"address": "192.168.0.62",
|
||||
"port": 502,
|
||||
"registry_mapping": [
|
||||
{
|
||||
"registry": 4065,
|
||||
"desc": "External temperature"
|
||||
},
|
||||
{
|
||||
"registry": 4070,
|
||||
"desc": "Temp in"
|
||||
},
|
||||
{
|
||||
"registry": 4071,
|
||||
"desc": "Temp out"
|
||||
},
|
||||
{
|
||||
"registry": 4072,
|
||||
"desc": "Temp pc out"
|
||||
},
|
||||
{
|
||||
"registry": 4067,
|
||||
"desc": "Temp co"
|
||||
},
|
||||
{
|
||||
"registry": 4202,
|
||||
"desc": "Set Temp co"
|
||||
},
|
||||
{
|
||||
"registry": 4069,
|
||||
"desc": "Temp cwu"
|
||||
},
|
||||
{
|
||||
"registry": 4203,
|
||||
"desc": "Set Temp cwu"
|
||||
},
|
||||
{
|
||||
"registry": 4066,
|
||||
"desc": "Tem wew"
|
||||
},
|
||||
{
|
||||
"registry": 4201,
|
||||
"desc": "Set Temp wew"
|
||||
},
|
||||
{
|
||||
"registry": 4199,
|
||||
"desc": "Temp zsr"
|
||||
}
|
||||
]
|
||||
}
|
||||
Binary file not shown.
@@ -1,2 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4" />
|
||||
Reference in new issue
Block a user