-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'gh320-lost-ambient-sensor' into dev (#320)
- Loading branch information
Showing
8 changed files
with
149 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
import net.sf.dz3r.device.actuator.economizer.EconomizerConfig; | ||
import net.sf.dz3r.model.Thermostat; | ||
import net.sf.dz3r.signal.Signal; | ||
import org.apache.logging.log4j.ThreadContext; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.time.Clock; | ||
|
@@ -21,6 +20,8 @@ | |
* More information: <a href="https://github.com/home-climate-control/dz/wiki/HVAC-Device:-Economizer">HVAC Device: Economizer</a> | ||
* | ||
* @param <A> Actuator device address type. | ||
* | ||
* @author Copyright © <a href="mailto:[email protected]">Vadim Tkachenko</a> 2001-2024 | ||
*/ | ||
public class PidEconomizer<A extends Comparable<A>> extends AbstractEconomizer { | ||
|
||
|
@@ -73,32 +74,35 @@ public PidEconomizer( | |
@Override | ||
protected Flux<Signal<Boolean, ProcessController.Status<Double>>> computeDeviceState(Flux<Signal<Double, Void>> pv) { | ||
|
||
ThreadContext.push("computeDeviceState"); | ||
|
||
try { | ||
|
||
// Compute the control signal to feed to the renderer. | ||
// Might want to make this available to outside consumers for instrumentation. | ||
var stage1 = controller | ||
.compute(pv) | ||
.doOnNext(e -> logger.debug("controller/{}: {}", getAddress(), e)); | ||
|
||
// Discard things the renderer doesn't understand. | ||
// Total failure is denoted by NaN by stage 1, it will get through. | ||
// The PID controller output value becomes the extra payload but is ignored at the moment (unlike Thermostat#compute()). | ||
var stage2 = stage1 | ||
.map(s -> new Signal<>(s.timestamp, s.getValue().signal, s.getValue(), s.status, s.error)); | ||
// Compute the control signal to feed to the renderer. | ||
// Might want to make this available to outside consumers for instrumentation. | ||
var stage1 = controller | ||
.compute(pv) | ||
.doOnNext(e -> logger.debug("controller/{}: {}", getAddress(), e)); | ||
|
||
// Interpret things the renderer doesn't understand | ||
var stage2 = stage1.map(this::computeRendererInput); | ||
|
||
// Deliver the signal | ||
// Might want to expose this as well | ||
return signalRenderer | ||
.compute(stage2) | ||
.doOnNext(e -> logger.debug("renderer/{}: {}", getAddress(), e)) | ||
.map(this::mapOutput); | ||
} | ||
|
||
// Deliver the signal | ||
// Might want to expose this as well | ||
return signalRenderer | ||
.compute(stage2) | ||
.doOnNext(e -> logger.debug("renderer/{}: {}", getAddress(), e)) | ||
.map(this::mapOutput); | ||
/** | ||
* Convert a possibly error signal from the computing pipeline into an actionable signal for the hysteresis controller. | ||
*/ | ||
private Signal<Double, ProcessController. Status<Double>> computeRendererInput(Signal<ProcessController.Status<Double>, Void> signal) { | ||
|
||
} finally { | ||
ThreadContext.pop(); | ||
if (signal.isOK()) { | ||
// PID controller output value becomes the extra payload but is ignored at the moment (unlike Thermostat#compute()). | ||
return new Signal<>(signal.timestamp, signal.getValue().signal, signal.getValue(), signal.status, signal.error); | ||
} | ||
|
||
// Any kind of errors at this point must be interpreted as "turn it off" | ||
return new Signal<>(signal.timestamp, -1d); | ||
} | ||
|
||
private Signal<Boolean, ProcessController.Status<Double>> mapOutput(Signal<ProcessController.Status<Double>, ProcessController.Status<Double>> source) { | ||
|
101 changes: 101 additions & 0 deletions
101
.../hcc-model/src/test/java/net/sf/dz3r/device/actuator/economizer/v2/PidEconomizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package net.sf.dz3r.device.actuator.economizer.v2; | ||
|
||
import net.sf.dz3r.device.DeviceState; | ||
import net.sf.dz3r.device.actuator.NullCqrsSwitch; | ||
import net.sf.dz3r.device.actuator.SwitchableHvacDevice; | ||
import net.sf.dz3r.device.actuator.economizer.EconomizerConfig; | ||
import net.sf.dz3r.device.actuator.economizer.EconomizerSettings; | ||
import net.sf.dz3r.model.HvacMode; | ||
import net.sf.dz3r.signal.Signal; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Sinks; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
import java.time.Clock; | ||
import java.time.Duration; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class PidEconomizerTest { | ||
|
||
private final Logger logger = LogManager.getLogger(); | ||
|
||
/** | ||
* Make sure the economizer stops upon loss of the ambient sensor signal. | ||
* | ||
* See <a href="https://github.com/home-climate-control/dz/issues/320">#320</a>. | ||
*/ | ||
@Test | ||
void ambientSensorLoss() throws InterruptedException { | ||
|
||
var actuator = new NullCqrsSwitch("test"); | ||
var hvac = new SwitchableHvacDevice( | ||
Clock.systemUTC(), | ||
"test", | ||
HvacMode.COOLING, | ||
actuator, | ||
false, | ||
null | ||
); | ||
|
||
Sinks.Many<Signal<Double, Void>> ambientSink = Sinks.many().multicast().onBackpressureBuffer(); | ||
Sinks.Many<Signal<Double, String>> indoorSink = Sinks.many().multicast().onBackpressureBuffer(); | ||
|
||
var ambientFlux = ambientSink.asFlux(); | ||
var indoorFlux = indoorSink.asFlux(); | ||
|
||
var eco = new PidEconomizer<>( | ||
Clock.systemUTC(), | ||
"test", | ||
new EconomizerConfig( | ||
HvacMode.COOLING, | ||
1.0, | ||
0.0000008, | ||
1.1, | ||
new EconomizerSettings( | ||
0, | ||
20, | ||
true, 1.0 | ||
) | ||
|
||
), | ||
ambientFlux, | ||
hvac, | ||
Duration.ofSeconds(10) | ||
); | ||
|
||
var output = eco.compute(indoorFlux); | ||
var start = Clock.systemUTC().instant(); | ||
var timeStep = 10L; | ||
var offset = new AtomicLong(0); | ||
|
||
output | ||
.publishOn(Schedulers.boundedElastic()) | ||
.subscribe(step -> logger.info("step: {}", step)); | ||
|
||
var deviceState = new LinkedBlockingQueue<Signal<DeviceState<Boolean>, String>>(); | ||
|
||
actuator.getFlux().subscribe(deviceState::add); | ||
|
||
// Setup complete, let's push data now | ||
|
||
// After receiving just the indoor signal (but not ambient), the economizer is off | ||
indoorSink.tryEmitNext(new Signal<>(start, 25.0, "indoor")); | ||
assertThat(deviceState.take().getValue().requested).isFalse(); | ||
|
||
// When both indoor and ambient signals are available, it is now on | ||
ambientSink.tryEmitNext(new Signal<>(start.plus(Duration.ofMillis(offset.addAndGet(timeStep))), 20.0)); | ||
assertThat(deviceState.take().getValue().requested).isTrue(); | ||
|
||
// It should turn off now | ||
ambientSink.tryEmitNext(new Signal<>(start.plus(Duration.ofMillis(offset.addAndGet(timeStep))), null, null,Signal.Status.FAILURE_TOTAL, new TimeoutException("oops"))); | ||
|
||
// ...and it does. | ||
assertThat(deviceState.take().getValue().requested).isFalse(); | ||
} | ||
} |