code clean: Device, DeviceService, DeviceController
This commit is contained in:
parent
e3d5523ac7
commit
8e3e763e59
@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
|||||||
public class EntityNotFound extends RuntimeException {
|
public class EntityNotFound extends RuntimeException {
|
||||||
|
|
||||||
public EntityNotFound(@NonNull final String key, @NonNull final Object value) {
|
public EntityNotFound(@NonNull final String key, @NonNull final Object value) {
|
||||||
super("Device not found: %s=%s".formatted(key, value));
|
super("Entity not found: %s=%s".formatted(key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,12 +28,12 @@ public class Device {
|
|||||||
@Setter
|
@Setter
|
||||||
@NonNull
|
@NonNull
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String stateProperty;
|
private String statePropertyId;
|
||||||
|
|
||||||
public Device(@NonNull final String name, @NonNull final String slug, @NonNull final String stateProperty) {
|
public Device(@NonNull final String name, @NonNull final String slug, @NonNull final String statePropertyId) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.slug = slug;
|
this.slug = slug;
|
||||||
this.stateProperty = stateProperty;
|
this.statePropertyId = statePropertyId;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,14 +40,14 @@ public class DeviceController {
|
|||||||
@GetMapping("get/{uuidOrSlug}")
|
@GetMapping("get/{uuidOrSlug}")
|
||||||
private DeviceDto get(@PathVariable @NonNull final String uuidOrSlug, @NonNull final HttpServletRequest request) {
|
private DeviceDto get(@PathVariable @NonNull final String uuidOrSlug, @NonNull final HttpServletRequest request) {
|
||||||
log.debug("get: path={}", request.getServletPath());
|
log.debug("get: path={}", request.getServletPath());
|
||||||
return deviceService.toDto(uuidOrSlug);
|
return deviceService.getByUuidOrSlugDto(uuidOrSlug);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@GetMapping("getState/{uuidOrSlug}")
|
@GetMapping("getState/{uuidOrSlug}")
|
||||||
private Boolean getState(@PathVariable @NonNull final String uuidOrSlug, @NonNull final HttpServletRequest request) throws PropertyTypeMismatch {
|
private Boolean getState(@PathVariable @NonNull final String uuidOrSlug, @NonNull final HttpServletRequest request) throws PropertyTypeMismatch {
|
||||||
log.debug("getState: path={}", request.getServletPath());
|
log.debug("getState: path={}", request.getServletPath());
|
||||||
return deviceService.toDto(uuidOrSlug).getStateValue();
|
return deviceService.getByUuidOrSlugDto(uuidOrSlug).getStateValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("setState/{uuidOrSlug}/{state}")
|
@GetMapping("setState/{uuidOrSlug}/{state}")
|
||||||
|
|||||||
@ -36,7 +36,7 @@ public class DeviceDto implements IWebSocketMessage {
|
|||||||
this.uuid = device.getUuid();
|
this.uuid = device.getUuid();
|
||||||
this.name = device.getName();
|
this.name = device.getName();
|
||||||
this.slug = device.getSlug();
|
this.slug = device.getSlug();
|
||||||
this.statePropertyId = device.getStateProperty();
|
this.statePropertyId = device.getStatePropertyId();
|
||||||
this.stateProperty = stateProperty;
|
this.stateProperty = stateProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -34,25 +34,25 @@ public class DeviceService {
|
|||||||
public void setState(@NonNull final String uuidOrSlug, final boolean state) throws PropertyNotFound, PropertyNotWritable, PropertyTypeMismatch {
|
public void setState(@NonNull final String uuidOrSlug, final boolean state) throws PropertyNotFound, PropertyNotWritable, PropertyTypeMismatch {
|
||||||
log.debug("setState: uuidOrSlug={}, state={}", uuidOrSlug, state);
|
log.debug("setState: uuidOrSlug={}, state={}", uuidOrSlug, state);
|
||||||
final Device device = getByUuidOrSlug(uuidOrSlug);
|
final Device device = getByUuidOrSlug(uuidOrSlug);
|
||||||
propertyService.write(device.getStateProperty(), state, Boolean.class);
|
propertyService.write(device.getStatePropertyId(), state, Boolean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public DeviceDto toDto(final @NonNull String uuidOrSlug) {
|
public DeviceDto getByUuidOrSlugDto(final @NonNull String uuidOrSlug) {
|
||||||
return toDto(getByUuidOrSlug(uuidOrSlug));
|
return toDto(getByUuidOrSlug(uuidOrSlug));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
|
||||||
public DeviceDto toDto(@NonNull final Device device) {
|
|
||||||
final PropertyDto<Boolean> state = propertyService.dtoByIdAndTypeOrNull(device.getStateProperty(), Boolean.class);
|
|
||||||
return new DeviceDto(device, state);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private Device getByUuidOrSlug(@NonNull final String uuidOrSlug) {
|
private Device getByUuidOrSlug(@NonNull final String uuidOrSlug) {
|
||||||
return deviceRepository.findByUuidOrSlug(uuidOrSlug, uuidOrSlug).orElseThrow(() -> new EntityNotFound("uuidOrSlug", uuidOrSlug));
|
return deviceRepository.findByUuidOrSlug(uuidOrSlug, uuidOrSlug).orElseThrow(() -> new EntityNotFound("uuidOrSlug", uuidOrSlug));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public DeviceDto toDto(@NonNull final Device device) {
|
||||||
|
final PropertyDto<Boolean> state = propertyService.dtoByIdAndTypeOrNull(device.getStatePropertyId(), Boolean.class);
|
||||||
|
return new DeviceDto(device, state);
|
||||||
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private Device getByUuid(@NonNull final String uuid) {
|
private Device getByUuid(@NonNull final String uuid) {
|
||||||
return deviceRepository.findById(uuid).orElseThrow(() -> new EntityNotFound("uuid", uuid));
|
return deviceRepository.findById(uuid).orElseThrow(() -> new EntityNotFound("uuid", uuid));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user