Skip to content

Commit

Permalink
Rearranging more chairs (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Sep 7, 2023
1 parent 7fd7f06 commit 2e6adb2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 53 deletions.
29 changes: 16 additions & 13 deletions dz3r-model/src/main/java/net/sf/dz3r/device/actuator/HeatPump.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,6 @@ public class HeatPump extends AbstractHvacDevice {
*/
private HvacCommand requestedState = new HvacCommand(null, null, null);

/**
* Create an instance with all straight switches.
*
* @param name JMX name.
* @param switchMode Switch to pull to change the operating mode.
* @param switchRunning Switch to pull to turn on the compressor.
* @param switchFan Switch to pull to turn on the air handler.
*/
public HeatPump(String name, Switch<?> switchMode, Switch<?> switchRunning, Switch<?> switchFan) {
this(name, switchMode, false, switchRunning, false, switchFan, false);
}

/**
* Create an instance with some switches possibly reverse.
*
Expand Down Expand Up @@ -114,13 +102,28 @@ public HeatPump(
* @param reverseRunning {@code true} if the "off" running position corresponds to logical one.
* @param switchFan Switch to pull to turn on the air handler.
* @param reverseFan {@code true} if the "off" fan position corresponds to logical one.
* @param changeModeDelay Delay to observe while changing the {@link HvacMode operating mode}.
*/
public HeatPump(
String name,
Switch<?> switchMode, boolean reverseMode,
Switch<?> switchRunning, boolean reverseRunning,
Switch<?> switchFan, boolean reverseFan,
Duration changeModeDelay) {
this(name,
switchMode, reverseMode,
switchRunning, reverseRunning,
switchFan, reverseFan,
changeModeDelay,
Schedulers.newSingle("HeatPump(" + name + ")"));
}
public HeatPump(
String name,
Switch<?> switchMode, boolean reverseMode,
Switch<?> switchRunning, boolean reverseRunning,
Switch<?> switchFan, boolean reverseFan,
Duration changeModeDelay,
Scheduler scheduler) {

super(name);

Expand Down Expand Up @@ -148,7 +151,7 @@ public HeatPump(
return DEFAULT_MODE_CHANGE_DELAY;
});

scheduler = Schedulers.newSingle("HeatPump(" + getAddress() + ")");
this.scheduler = scheduler;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;
import reactor.tools.agent.ReactorDebugAgent;
Expand All @@ -23,26 +24,36 @@ class HeatPumpTest {
private final Logger logger = LogManager.getLogger();
private final Duration delay = Duration.ofMillis(500);

private final Scheduler scheduler = Schedulers.newSingle("heatpump-test-single");

@BeforeAll
static void init() {
ReactorDebugAgent.init();
}

private SwitchPack getSwitchPack() {

// VT: NOTE: Might need to use this for running parameterized tests with different schedulers
return new SwitchPack(
new NullSwitch("mode", scheduler),
new NullSwitch("running", scheduler),
new NullSwitch("fan", scheduler)
);
}

/**
* Verify that empty command sequence is executed (implementation will issue initialization and shutdown commands).
*/
@Test
void empty() { // NOSONAR It's not complex, it's just mundane

var switchMode = new NullSwitch("mode");
var switchRunning = new NullSwitch("running");
var switchFan = new NullSwitch("fan");

var switchPack = getSwitchPack();
var d = new HeatPump("hp-empty",
switchMode, false,
switchRunning, false,
switchFan, false,
delay);
switchPack.mode, false,
switchPack.running, false,
switchPack.fan, false,
delay,
scheduler);
Flux<Signal<HvacCommand, Void>> sequence = Flux.empty();

var result = d.compute(sequence).log();
Expand Down Expand Up @@ -72,15 +83,13 @@ void empty() { // NOSONAR It's not complex, it's just mundane
@Test
void demandBeforeMode() { // NOSONAR It's not complex, it's just mundane

var switchMode = new NullSwitch("mode");
var switchRunning = new NullSwitch("running");
var switchFan = new NullSwitch("fan");

var switchPack = getSwitchPack();
var d = new HeatPump("hp-initial-mode",
switchMode, false,
switchRunning, false,
switchFan, false,
delay);
switchPack.mode, false,
switchPack.running, false,
switchPack.fan, false,
delay,
scheduler);
var sequence = Flux.just(
// This will fail
new Signal<HvacCommand, Void>(Instant.now(), new HvacCommand(null, 0.8, null)),
Expand Down Expand Up @@ -137,15 +146,13 @@ void demandBeforeMode() { // NOSONAR It's not complex, it's just mundane
@Test
void setMode() { // NOSONAR It's not complex, it's just mundane

var switchMode = new NullSwitch("mode");
var switchRunning = new NullSwitch("running");
var switchFan = new NullSwitch("fan");

var switchPack = getSwitchPack();
var d = new HeatPump("hp-change-mode",
switchMode, false,
switchRunning, false,
switchFan, false,
delay);
switchPack.mode, false,
switchPack.running, false,
switchPack.fan, false,
delay,
scheduler);
var sequence = Flux.just(
new Signal<HvacCommand, Void>(Instant.now(), new HvacCommand(HvacMode.HEATING, 0.8, null))
);
Expand Down Expand Up @@ -188,15 +195,13 @@ void setMode() { // NOSONAR It's not complex, it's just mundane
@Test
void changeMode() { // NOSONAR It's not complex, it's just mundane

var switchMode = new NullSwitch("mode");
var switchRunning = new NullSwitch("running");
var switchFan = new NullSwitch("fan");

var switchPack = getSwitchPack();
var d = new HeatPump("hp-change-mode",
switchMode, false,
switchRunning, false,
switchFan, false,
delay);
switchPack.mode, false,
switchPack.running, false,
switchPack.fan, false,
delay,
scheduler);
var sequence = Flux.just(
new Signal<HvacCommand, Void>(Instant.now(), new HvacCommand(HvacMode.HEATING, 0.8, null)),
new Signal<HvacCommand, Void>(Instant.now(), new HvacCommand(HvacMode.COOLING, 0.7, null))
Expand Down Expand Up @@ -264,15 +269,13 @@ void changeMode() { // NOSONAR It's not complex, it's just mundane
@Test
void boot() { // NOSONAR It's not complex, it's just mundane

var switchMode = new NullSwitch("mode");
var switchRunning = new NullSwitch("running");
var switchFan = new NullSwitch("fan");

var switchPack = getSwitchPack();
var d = new HeatPump("hp-boot",
switchMode, false,
switchRunning, false,
switchFan, false,
delay);
switchPack.mode, false,
switchPack.running, false,
switchPack.fan, false,
delay,
scheduler);
var sequence = Flux.just(
new Signal<HvacCommand, Void>(Instant.now(), new HvacCommand(null, 0d, null)),
new Signal<HvacCommand, Void>(Instant.now(), new HvacCommand(HvacMode.COOLING, null, null)),
Expand Down Expand Up @@ -424,4 +427,10 @@ void delayElementsFromMono2() {
.assertNext(e -> assertThat(e).isEqualTo(5))
.verifyComplete();
}

private record SwitchPack(
Switch<?> mode,
Switch<?> running,
Switch<?> fan
) {}
}

0 comments on commit 2e6adb2

Please sign in to comment.