94 lines
3.3 KiB
Java
94 lines
3.3 KiB
Java
package de.ph87.homeautomation.property;
|
|
|
|
import de.ph87.homeautomation.channel.ChannelService;
|
|
import de.ph87.homeautomation.shared.ISearchController;
|
|
import de.ph87.homeautomation.shared.SearchResult;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
@RestController
|
|
@RequestMapping("property")
|
|
@RequiredArgsConstructor
|
|
public class PropertyController implements ISearchController {
|
|
|
|
private final PropertyWriteService propertyWriteService;
|
|
|
|
private final PropertyReader propertyReader;
|
|
|
|
private final ChannelService channelService;
|
|
|
|
@GetMapping("create")
|
|
public PropertyDto create() {
|
|
return propertyWriteService.create();
|
|
}
|
|
|
|
@GetMapping("findAll")
|
|
public List<PropertyDto> findAll() {
|
|
return propertyReader.findAllDto();
|
|
}
|
|
|
|
@PostMapping("set/{id}/type")
|
|
public PropertyDto setPropertyType(@PathVariable final long id, @RequestBody final String propertyType) {
|
|
return propertyWriteService.set(id, Property::setType, PropertyType.valueOf(propertyType));
|
|
}
|
|
|
|
@PostMapping("set/{id}/title")
|
|
public PropertyDto setPropertyTitle(@PathVariable final long id, @RequestBody final String propertyTitle) {
|
|
return propertyWriteService.set(id, Property::setTitle, propertyTitle);
|
|
}
|
|
|
|
@PostMapping("set/{id}/value")
|
|
public PropertyDto setValue(@PathVariable final long id, @RequestBody final double value) {
|
|
return propertyWriteService.set(id, propertyWriteService::writeToChannel, value);
|
|
}
|
|
|
|
@PostMapping("set/{id}/readChannel")
|
|
public PropertyDto setReadChannel(@PathVariable final long id, @RequestBody(required = false) final Long channelId) {
|
|
return propertyWriteService.set(id, (p, v) -> p.setReadChannel(channelId == null ? null : channelService.getById(v)), channelId);
|
|
}
|
|
|
|
@PostMapping("set/{id}/writeChannel")
|
|
public PropertyDto setWriteChannel(@PathVariable final long id, @RequestBody(required = false) final Long channelId) {
|
|
return propertyWriteService.set(id, (p, v) -> p.setWriteChannel(channelId == null ? null : channelService.getById(v)), channelId);
|
|
}
|
|
|
|
@DeleteMapping("{id}")
|
|
public void delete(@PathVariable final long id) {
|
|
propertyWriteService.delete(id);
|
|
}
|
|
|
|
@PostMapping("toggle/{id}")
|
|
public PropertyDto setValue(@PathVariable final long id) {
|
|
final boolean oldState = getOldStateBoolean(id, false);
|
|
return propertyWriteService.set(id, propertyWriteService::writeToChannel, oldState ? 0.0 : 1.0);
|
|
}
|
|
|
|
private boolean getOldStateBoolean(final long id, final boolean orElse) {
|
|
final Double oldValue = propertyReader.getDtoById(id).getValue();
|
|
if (oldValue == null || oldValue.isNaN()) {
|
|
return orElse;
|
|
}
|
|
return oldValue > 0.0;
|
|
}
|
|
|
|
@Override
|
|
@GetMapping("searchById/{id}")
|
|
public SearchResult searchById(@PathVariable final long id) {
|
|
return toSearchResult(propertyReader.getDtoById(id));
|
|
}
|
|
|
|
@Override
|
|
@PostMapping("searchLike")
|
|
public List<SearchResult> searchLike(@RequestBody final String term) {
|
|
return propertyReader.findAllDtoLike("%" + term + "%").stream().map(this::toSearchResult).collect(Collectors.toList());
|
|
}
|
|
|
|
private SearchResult toSearchResult(final PropertyDto propertyDto) {
|
|
return new SearchResult(Property.class, propertyDto.getId(), propertyDto.getTitle());
|
|
}
|
|
|
|
}
|