Implemented PropertyController

This commit is contained in:
Patrick Haßel 2021-10-04 13:16:45 +02:00
parent caae0539fa
commit 4d894f2fb4
15 changed files with 141 additions and 6 deletions

View File

@ -1,5 +1,7 @@
package de.ph87.homeautomation.device;
import de.ph87.homeautomation.device.devices.Device;
public abstract class DeviceDto {
public final long id;

View File

@ -1,5 +1,6 @@
package de.ph87.homeautomation.device;
import de.ph87.homeautomation.device.devices.*;
import de.ph87.homeautomation.property.PropertyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -27,6 +28,9 @@ public class DeviceReadService {
if (device instanceof DeviceSwitch) {
final DeviceSwitch deviceSwitch = (DeviceSwitch) device;
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 + ")");
}

View File

@ -1,5 +1,6 @@
package de.ph87.homeautomation.device;
import de.ph87.homeautomation.device.devices.Device;
import org.springframework.data.repository.CrudRepository;
import java.util.List;

View File

@ -1,6 +1,7 @@
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.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -16,14 +17,17 @@ public class DeviceWriteService {
private final DeviceRepository deviceRepository;
private final PropertyService propertyService;
@PostConstruct
public void postConstruct() {
final DeviceSwitch deviceSwitch = new DeviceSwitch();
deviceSwitch.setName("TEST");
deviceSwitch.setStatePropertyName("knx.group.0/3/6");
deviceRepository.save(deviceSwitch);
final DeviceNumber deviceNumber = new DeviceNumber();
deviceNumber.setName("Helligkeit");
deviceNumber.setValuePropertyName("knx.group.0/5/6");
deviceRepository.save(deviceNumber);
}
}

View File

@ -1,4 +1,4 @@
package de.ph87.homeautomation.device;
package de.ph87.homeautomation.device.devices;
import lombok.AccessLevel;
import lombok.Getter;

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
package de.ph87.homeautomation.device;
package de.ph87.homeautomation.device.devices;
import lombok.Getter;
import lombok.Setter;

View File

@ -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 {

View File

@ -2,6 +2,7 @@ package de.ph87.homeautomation.knx.group;
import de.ph87.homeautomation.knx.KnxThreadService;
import de.ph87.homeautomation.property.IPropertyOwner;
import de.ph87.homeautomation.property.PropertyDto;
import de.ph87.homeautomation.property.PropertySetException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@ -11,8 +12,10 @@ import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import tuwien.auto.calimero.GroupAddress;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
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);
}
@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) {
final Matcher matcher = propertyNamePattern.matcher(propertyName);
if (matcher.matches()) {

View File

@ -1,5 +1,6 @@
package de.ph87.homeautomation.property;
import java.util.List;
import java.util.regex.Pattern;
public interface IPropertyOwner {
@ -10,4 +11,8 @@ public interface IPropertyOwner {
Boolean readBoolean(String propertyName);
Number readNumber(String propertyName);
List<PropertyDto> findAllProperties();
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -1,9 +1,12 @@
package de.ph87.homeautomation.property;
import de.ph87.homeautomation.shared.Helpers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Slf4j
@ -22,6 +25,10 @@ public class PropertyService {
return getOwnerOrThrow(propertyName).readBoolean(propertyName);
}
public Number readNumber(final String propertyName) {
return getOwnerOrThrow(propertyName).readNumber(propertyName);
}
private IPropertyOwner getOwnerOrThrow(final String propertyName) {
return propertyOwners.stream()
.filter(iPropertyOwner -> iPropertyOwner.getPropertyNamePattern().matcher(propertyName).matches())
@ -29,4 +36,8 @@ public class PropertyService {
.orElseThrow(() -> new RuntimeException("No IPropertyOwner found for propertyName: " + propertyName));
}
public List<PropertyDto> findAll() {
return propertyOwners.stream().map(IPropertyOwner::findAllProperties).reduce(new ArrayList<>(), Helpers::merge);
}
}

View File

@ -1,5 +1,8 @@
package de.ph87.homeautomation.shared;
import java.util.ArrayList;
import java.util.List;
public class Helpers {
public static String quoteOrNull(final String value) {
@ -30,4 +33,10 @@ public class Helpers {
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;
}
}