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()) .findByIdAndUsername(id, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
return sceneService.apply(newScene); return sceneService.apply(newScene, principal.getName());
} }
@PutMapping("/{id}") @PutMapping("/{id}")

View file

@ -6,9 +6,11 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Transient; import javax.persistence.Transient;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import org.springframework.stereotype.Component;
/** A thermostat capable of controlling cooling and heating. */ /** A thermostat capable of controlling cooling and heating. */
@Entity @Entity
@Component
public class Thermostat extends Switchable implements BooleanTriggerable { public class Thermostat extends Switchable implements BooleanTriggerable {
@Override @Override
@ -34,6 +36,12 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
BigDecimal measured = this.getMeasuredTemperature(); BigDecimal measured = this.getMeasuredTemperature();
BigDecimal target = this.getTargetTemperature(); BigDecimal target = this.getTargetTemperature();
if (measured == null) {
this.setMode(Thermostat.Mode.IDLE);
return true;
}
BigDecimal delta = target.subtract(measured); BigDecimal delta = target.subtract(measured);
if (delta.abs().doubleValue() < 0.25) { 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(); final long deviceId = device.getId();
@ -57,7 +57,7 @@ public class DeviceService {
sceneRepository sceneRepository
.findById(t.getSceneId()) .findById(t.getSceneId())
.orElseThrow(IllegalStateException::new)) .orElseThrow(IllegalStateException::new))
.forEach(sceneService::apply); .forEach((s) -> sceneService.apply(s, username));
} }
public List<Device> findAll(Long hostId, String username) throws NotFoundException { public List<Device> findAll(Long hostId, String username) throws NotFoundException {
@ -97,11 +97,7 @@ public class DeviceService {
} }
} }
for (Device d : devices) { populateComputedFields(devices);
if (d instanceof Thermostat) {
thermostatService.populateMeasuredTemperature((Thermostat) d);
}
}
return toList(devices); return toList(devices);
} catch (NotFoundException e) { } 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) public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId)
throws NotFoundException { throws NotFoundException {
final User currentUser = userRepository.findByUsername(guestUsername); final User currentUser = userRepository.findByUsername(guestUsername);
@ -160,7 +164,7 @@ public class DeviceService {
devices.forEach((d) -> propagateUpdateAsOwner(d, username)); devices.forEach((d) -> propagateUpdateAsOwner(d, username));
if (!fromScene) { if (!fromScene) {
devices.forEach(this::triggerTriggers); devices.forEach((d) -> triggerTriggers(d, username));
} }
return toList(devices); return toList(devices);
@ -172,7 +176,7 @@ public class DeviceService {
propagateUpdateAsOwner(device, username); propagateUpdateAsOwner(device, username);
if (!fromScene) { if (!fromScene) {
triggerTriggers(device); triggerTriggers(device, username);
} }
return device; return device;

View file

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