Thermostats fixed (for scenes)

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-02 13:58:28 +02:00
parent e0ab831be9
commit c3d6590507
4 changed files with 26 additions and 13 deletions

View File

@ -68,7 +68,7 @@ public class SceneController {
.findByIdAndUsername(id, principal.getName())
.orElseThrow(NotFoundException::new);
return sceneService.apply(newScene);
return sceneService.apply(newScene, principal.getName());
}
@PutMapping("/{id}")

View File

@ -6,9 +6,11 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import org.springframework.stereotype.Component;
/** A thermostat capable of controlling cooling and heating. */
@Entity
@Component
public class Thermostat extends Switchable implements BooleanTriggerable {
@Override
@ -34,6 +36,12 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
BigDecimal measured = this.getMeasuredTemperature();
BigDecimal target = this.getTargetTemperature();
if (measured == null) {
this.setMode(Thermostat.Mode.IDLE);
return true;
}
BigDecimal delta = target.subtract(measured);
if (delta.abs().doubleValue() < 0.25) {

View File

@ -37,7 +37,7 @@ public class DeviceService {
}
}
private void triggerTriggers(Device device) {
private void triggerTriggers(Device device, final String username) {
final long deviceId = device.getId();
@ -57,7 +57,7 @@ public class DeviceService {
sceneRepository
.findById(t.getSceneId())
.orElseThrow(IllegalStateException::new))
.forEach(sceneService::apply);
.forEach((s) -> sceneService.apply(s, username));
}
public List<Device> findAll(Long hostId, String username) throws NotFoundException {
@ -97,11 +97,7 @@ public class DeviceService {
}
}
for (Device d : devices) {
if (d instanceof Thermostat) {
thermostatService.populateMeasuredTemperature((Thermostat) d);
}
}
populateComputedFields(devices);
return toList(devices);
} catch (NotFoundException e) {
@ -110,6 +106,14 @@ public class DeviceService {
}
}
public void populateComputedFields(Iterable<Device> devices) {
for (Device d : devices) {
if (d instanceof Thermostat) {
thermostatService.populateMeasuredTemperature((Thermostat) d);
}
}
}
public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId)
throws NotFoundException {
final User currentUser = userRepository.findByUsername(guestUsername);
@ -160,7 +164,7 @@ public class DeviceService {
devices.forEach((d) -> propagateUpdateAsOwner(d, username));
if (!fromScene) {
devices.forEach(this::triggerTriggers);
devices.forEach((d) -> triggerTriggers(d, username));
}
return toList(devices);
@ -172,7 +176,7 @@ public class DeviceService {
propagateUpdateAsOwner(device, username);
if (!fromScene) {
triggerTriggers(device);
triggerTriggers(device, username);
}
return device;

View File

@ -13,16 +13,17 @@ import org.springframework.stereotype.Component;
public class SceneService {
@Autowired private DeviceRepository<Device> deviceRepository;
@Autowired private DeviceService deviceService;
public List<Device> apply(Scene newScene) {
public List<Device> apply(Scene newScene, String username) {
final List<Device> updated = new ArrayList<>();
for (final State<?> s : newScene.getStates()) {
s.apply();
updated.add(s.getDevice());
}
deviceRepository.saveAll(updated);
deviceService.saveAllAsOwner(updated, username, true);
deviceService.populateComputedFields(updated);
return updated;
}
}