75 lines
2.9 KiB
Java
75 lines
2.9 KiB
Java
package de.ph87.homeautomation.device;
|
|
|
|
import de.ph87.homeautomation.device.devices.Device;
|
|
import de.ph87.homeautomation.device.devices.DeviceDto;
|
|
import de.ph87.homeautomation.property.PropertyReader;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
import static de.ph87.homeautomation.shared.Helpers.mapOrNull;
|
|
|
|
@RestController
|
|
@RequestMapping("device")
|
|
@RequiredArgsConstructor
|
|
public class DeviceController {
|
|
|
|
private final DeviceReadService deviceReadService;
|
|
|
|
private final DeviceWriteService deviceWriteService;
|
|
|
|
private final PropertyReader propertyReader;
|
|
|
|
@GetMapping("findAll")
|
|
public List<DeviceDto> findAll() {
|
|
return deviceReadService.findAll();
|
|
}
|
|
|
|
@GetMapping("searchById/{id}")
|
|
public DeviceDto getById(@PathVariable final long id) {
|
|
return deviceReadService.getDtoById(id);
|
|
}
|
|
|
|
@PostMapping("create")
|
|
public DeviceDto create(@RequestBody final String typeString) {
|
|
return deviceWriteService.create(typeString);
|
|
}
|
|
|
|
@GetMapping("delete/{id}")
|
|
public void delete(@PathVariable final long id) {
|
|
deviceWriteService.delete(id);
|
|
}
|
|
|
|
@PostMapping("set/{id}/enabled")
|
|
public DeviceDto setEnabled(@PathVariable final long id, @RequestBody final boolean enabled) {
|
|
return deviceWriteService.set(id, Device::setEnabled, enabled);
|
|
}
|
|
|
|
@PostMapping("set/{id}/title")
|
|
public DeviceDto setName(@PathVariable final long id, @RequestBody final String title) {
|
|
return deviceWriteService.set(id, Device::setTitle, title);
|
|
}
|
|
|
|
@PostMapping("set/{id}/DeviceSwitch/stateProperty")
|
|
public DeviceDto setDeviceSwitchStateProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
|
|
return deviceWriteService.setDeviceSwitch(id, (device, v) -> device.setStateProperty(mapOrNull(v, propertyReader::getById)), propertyId);
|
|
}
|
|
|
|
@PostMapping("set/{id}/DeviceStateScene/stateProperty")
|
|
public DeviceDto setDeviceStateSceneStateProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
|
|
return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setStateProperty(mapOrNull(v, propertyReader::getById)), propertyId);
|
|
}
|
|
|
|
@PostMapping("set/{id}/DeviceStateScene/sceneProperty")
|
|
public DeviceDto setDeviceStateSceneSceneProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
|
|
return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setSceneProperty(mapOrNull(v, propertyReader::getById)), propertyId);
|
|
}
|
|
|
|
@PostMapping("set/{id}/DeviceShutter/positionProperty")
|
|
public DeviceDto setDeviceShutterPositionProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
|
|
return deviceWriteService.setDeviceShutter(id, (device, v) -> device.setPositionProperty(mapOrNull(v, propertyReader::getById)), propertyId);
|
|
}
|
|
|
|
}
|