Implemented PropertyController
This commit is contained in:
parent
caae0539fa
commit
4d894f2fb4
@ -1,5 +1,7 @@
|
|||||||
package de.ph87.homeautomation.device;
|
package de.ph87.homeautomation.device;
|
||||||
|
|
||||||
|
import de.ph87.homeautomation.device.devices.Device;
|
||||||
|
|
||||||
public abstract class DeviceDto {
|
public abstract class DeviceDto {
|
||||||
|
|
||||||
public final long id;
|
public final long id;
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.homeautomation.device;
|
package de.ph87.homeautomation.device;
|
||||||
|
|
||||||
|
import de.ph87.homeautomation.device.devices.*;
|
||||||
import de.ph87.homeautomation.property.PropertyService;
|
import de.ph87.homeautomation.property.PropertyService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -27,6 +28,9 @@ public class DeviceReadService {
|
|||||||
if (device instanceof DeviceSwitch) {
|
if (device instanceof DeviceSwitch) {
|
||||||
final DeviceSwitch deviceSwitch = (DeviceSwitch) device;
|
final DeviceSwitch deviceSwitch = (DeviceSwitch) device;
|
||||||
return new DeviceSwitchDto(deviceSwitch, propertyService.readBoolean(deviceSwitch.getStatePropertyName()));
|
return new DeviceSwitchDto(deviceSwitch, propertyService.readBoolean(deviceSwitch.getStatePropertyName()));
|
||||||
|
} else if (device instanceof DeviceNumber) {
|
||||||
|
final DeviceNumber deviceNumber = (DeviceNumber) device;
|
||||||
|
return new DeviceNumberDto(deviceNumber, propertyService.readNumber(deviceNumber.getValuePropertyName()));
|
||||||
}
|
}
|
||||||
throw new RuntimeException("Not imeplemented: toDto(" + device + ")");
|
throw new RuntimeException("Not imeplemented: toDto(" + device + ")");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.homeautomation.device;
|
package de.ph87.homeautomation.device;
|
||||||
|
|
||||||
|
import de.ph87.homeautomation.device.devices.Device;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package de.ph87.homeautomation.device;
|
package de.ph87.homeautomation.device;
|
||||||
|
|
||||||
import de.ph87.homeautomation.property.PropertyService;
|
import de.ph87.homeautomation.device.devices.DeviceNumber;
|
||||||
|
import de.ph87.homeautomation.device.devices.DeviceSwitch;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -16,14 +17,17 @@ public class DeviceWriteService {
|
|||||||
|
|
||||||
private final DeviceRepository deviceRepository;
|
private final DeviceRepository deviceRepository;
|
||||||
|
|
||||||
private final PropertyService propertyService;
|
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void postConstruct() {
|
public void postConstruct() {
|
||||||
final DeviceSwitch deviceSwitch = new DeviceSwitch();
|
final DeviceSwitch deviceSwitch = new DeviceSwitch();
|
||||||
deviceSwitch.setName("TEST");
|
deviceSwitch.setName("TEST");
|
||||||
deviceSwitch.setStatePropertyName("knx.group.0/3/6");
|
deviceSwitch.setStatePropertyName("knx.group.0/3/6");
|
||||||
deviceRepository.save(deviceSwitch);
|
deviceRepository.save(deviceSwitch);
|
||||||
|
|
||||||
|
final DeviceNumber deviceNumber = new DeviceNumber();
|
||||||
|
deviceNumber.setName("Helligkeit");
|
||||||
|
deviceNumber.setValuePropertyName("knx.group.0/5/6");
|
||||||
|
deviceRepository.save(deviceNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package de.ph87.homeautomation.device;
|
package de.ph87.homeautomation.device.devices;
|
||||||
|
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package de.ph87.homeautomation.device.devices;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@ToString
|
||||||
|
@Entity
|
||||||
|
public class DeviceNumber extends Device {
|
||||||
|
|
||||||
|
private String valuePropertyName;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package de.ph87.homeautomation.device.devices;
|
||||||
|
|
||||||
|
import de.ph87.homeautomation.device.DeviceDto;
|
||||||
|
|
||||||
|
public class DeviceNumberDto extends DeviceDto {
|
||||||
|
|
||||||
|
public final Number value;
|
||||||
|
|
||||||
|
public DeviceNumberDto(final DeviceNumber device, final Number value) {
|
||||||
|
super(device);
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package de.ph87.homeautomation.device;
|
package de.ph87.homeautomation.device.devices;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -1,4 +1,6 @@
|
|||||||
package de.ph87.homeautomation.device;
|
package de.ph87.homeautomation.device.devices;
|
||||||
|
|
||||||
|
import de.ph87.homeautomation.device.DeviceDto;
|
||||||
|
|
||||||
public class DeviceSwitchDto extends DeviceDto {
|
public class DeviceSwitchDto extends DeviceDto {
|
||||||
|
|
||||||
@ -2,6 +2,7 @@ package de.ph87.homeautomation.knx.group;
|
|||||||
|
|
||||||
import de.ph87.homeautomation.knx.KnxThreadService;
|
import de.ph87.homeautomation.knx.KnxThreadService;
|
||||||
import de.ph87.homeautomation.property.IPropertyOwner;
|
import de.ph87.homeautomation.property.IPropertyOwner;
|
||||||
|
import de.ph87.homeautomation.property.PropertyDto;
|
||||||
import de.ph87.homeautomation.property.PropertySetException;
|
import de.ph87.homeautomation.property.PropertySetException;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@ -11,8 +12,10 @@ import org.springframework.context.event.EventListener;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import tuwien.auto.calimero.GroupAddress;
|
import tuwien.auto.calimero.GroupAddress;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static de.ph87.homeautomation.shared.Helpers.quoteOrNull;
|
import static de.ph87.homeautomation.shared.Helpers.quoteOrNull;
|
||||||
|
|
||||||
@ -55,6 +58,25 @@ public class KnxGroupSetService implements IPropertyOwner {
|
|||||||
return knxGroupRepository.findByAddressRaw(parseGroupAddress(propertyName).getRawAddress()).map(KnxGroup::getBooleanValue).orElse(null);
|
return knxGroupRepository.findByAddressRaw(parseGroupAddress(propertyName).getRawAddress()).map(KnxGroup::getBooleanValue).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Number readNumber(final String propertyName) {
|
||||||
|
return knxGroupRepository.findByAddressRaw(parseGroupAddress(propertyName).getRawAddress()).map(KnxGroup::getNumberValue).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PropertyDto> findAllProperties() {
|
||||||
|
return knxGroupRepository.findAll().stream().map(this::toPropertyDto).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private PropertyDto toPropertyDto(final KnxGroup knxGroup) {
|
||||||
|
return new PropertyDto(
|
||||||
|
knxGroup.getName(),
|
||||||
|
knxGroup.getBooleanValue(),
|
||||||
|
knxGroup.getNumberValue(),
|
||||||
|
knxGroup.getValueTimestamp()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private GroupAddress parseGroupAddress(final String propertyName) {
|
private GroupAddress parseGroupAddress(final String propertyName) {
|
||||||
final Matcher matcher = propertyNamePattern.matcher(propertyName);
|
final Matcher matcher = propertyNamePattern.matcher(propertyName);
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.homeautomation.property;
|
package de.ph87.homeautomation.property;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public interface IPropertyOwner {
|
public interface IPropertyOwner {
|
||||||
@ -10,4 +11,8 @@ public interface IPropertyOwner {
|
|||||||
|
|
||||||
Boolean readBoolean(String propertyName);
|
Boolean readBoolean(String propertyName);
|
||||||
|
|
||||||
|
Number readNumber(String propertyName);
|
||||||
|
|
||||||
|
List<PropertyDto> findAllProperties();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,22 @@
|
|||||||
|
package de.ph87.homeautomation.property;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("property")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class PropertyController {
|
||||||
|
|
||||||
|
private final PropertyService propertyService;
|
||||||
|
|
||||||
|
@GetMapping("findAll")
|
||||||
|
public List<PropertyDto> findAll() {
|
||||||
|
return propertyService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package de.ph87.homeautomation.property;
|
||||||
|
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
|
||||||
|
public class PropertyDto {
|
||||||
|
|
||||||
|
public final String name;
|
||||||
|
|
||||||
|
public final Boolean booleanValue;
|
||||||
|
|
||||||
|
public final Number numberValue;
|
||||||
|
|
||||||
|
public final ZonedDateTime timestamp;
|
||||||
|
|
||||||
|
public PropertyDto(final String name, final Boolean booleanValue, final Number numberValue, final ZonedDateTime timestamp) {
|
||||||
|
this.name = name;
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
this.numberValue = numberValue;
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,9 +1,12 @@
|
|||||||
package de.ph87.homeautomation.property;
|
package de.ph87.homeautomation.property;
|
||||||
|
|
||||||
|
import de.ph87.homeautomation.shared.Helpers;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -22,6 +25,10 @@ public class PropertyService {
|
|||||||
return getOwnerOrThrow(propertyName).readBoolean(propertyName);
|
return getOwnerOrThrow(propertyName).readBoolean(propertyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Number readNumber(final String propertyName) {
|
||||||
|
return getOwnerOrThrow(propertyName).readNumber(propertyName);
|
||||||
|
}
|
||||||
|
|
||||||
private IPropertyOwner getOwnerOrThrow(final String propertyName) {
|
private IPropertyOwner getOwnerOrThrow(final String propertyName) {
|
||||||
return propertyOwners.stream()
|
return propertyOwners.stream()
|
||||||
.filter(iPropertyOwner -> iPropertyOwner.getPropertyNamePattern().matcher(propertyName).matches())
|
.filter(iPropertyOwner -> iPropertyOwner.getPropertyNamePattern().matcher(propertyName).matches())
|
||||||
@ -29,4 +36,8 @@ public class PropertyService {
|
|||||||
.orElseThrow(() -> new RuntimeException("No IPropertyOwner found for propertyName: " + propertyName));
|
.orElseThrow(() -> new RuntimeException("No IPropertyOwner found for propertyName: " + propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<PropertyDto> findAll() {
|
||||||
|
return propertyOwners.stream().map(IPropertyOwner::findAllProperties).reduce(new ArrayList<>(), Helpers::merge);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
package de.ph87.homeautomation.shared;
|
package de.ph87.homeautomation.shared;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Helpers {
|
public class Helpers {
|
||||||
|
|
||||||
public static String quoteOrNull(final String value) {
|
public static String quoteOrNull(final String value) {
|
||||||
@ -30,4 +33,10 @@ public class Helpers {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> List<T> merge(final List<T> a, final List<T> b) {
|
||||||
|
final List<T> c = new ArrayList<>(a);
|
||||||
|
c.addAll(b);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user