Skip to content

Commit

Permalink
Merge branch 'gh244-swing-eco' into reactive (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Apr 26, 2023
2 parents 5b4e18b + f4116c8 commit 7d0fc55
Show file tree
Hide file tree
Showing 19 changed files with 632 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ public void connect(UnitDirector.Feed feed) {
var zoneStatusFeed = new ZoneMetricsConverter(config.instance, feed.unit).compute(feed.aggregateZoneFlux);
var zoneControllerFeed = new ZoneControllerMetricsConverter(config.instance, feed.unit).compute(feed.zoneControllerFlux);
var unitControllerFeed = new UnitControllerMetricsConverter(config.instance, feed.unit).compute(feed.unitControllerFlux);
var hvacDeficeFeed = new HvacDeviceMetricsConverter(config.instance, feed.unit).compute(feed.hvacDeviceFlux);
var hvacDeviceFeed = new HvacDeviceMetricsConverter(config.instance, feed.unit).compute(feed.hvacDeviceFlux);

var all = Flux.merge(
sensorFeeds,
zoneSensorFeeds,
zoneStatusFeed,
zoneControllerFeed,
unitControllerFeed,
hvacDeficeFeed);
hvacDeviceFeed);

all.publishOn(Schedulers.boundedElastic()).subscribe(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ protected SingleStageUnitController(String name) {
@Override
public Flux<Signal<HvacCommand, Void>> compute(Flux<Signal<UnitControlSignal, Void>> in) {

logger.debug("compute()");

return in
.filter(Signal::isOK)
.map(s -> {
Expand Down
2 changes: 0 additions & 2 deletions dz3r-model/src/main/java/net/sf/dz3r/model/Thermostat.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ public double getError() {
@Override
public Flux<Signal<Status<CallingStatus>, Void>> compute(Flux<Signal<Double, Void>> pv) {

logger.debug("compute()");

// Compute the control signal to feed to the renderer.
// Might want to make this available to outside consumers for instrumentation.
var stage1 = controller
Expand Down
8 changes: 3 additions & 5 deletions dz3r-model/src/main/java/net/sf/dz3r/model/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ public ZoneSettings getSettings() {
@Override
public Flux<Signal<ZoneStatus, String>> compute(Flux<Signal<Double, String>> in) {

logger.debug("compute()");

var source = Optional.ofNullable(economizer)
.map(eco -> eco.compute(in))
.orElse(in);
Expand All @@ -106,16 +104,16 @@ public Flux<Signal<ZoneStatus, String>> compute(Flux<Signal<Double, String>> in)
// regardless of whether the zone is enabled
var stage1 = ts
.compute(stage0)
.doOnNext(e -> logger.trace("ts/{}: {}", getAddress(), e));
.doOnNext(e -> logger.trace("compute {}/ts: {}", getAddress(), e));

// Now, need to translate into a form that is easier manipulated
var stage2 = stage1.map(this::translate)
.doOnNext(e -> logger.debug("translated/{}: {}", getAddress(), e));
.doOnNext(e -> logger.debug("compute {}/translated: {}", getAddress(), e));

// Now, dampen the signal if the zone is disabled
var stage3 = stage2
.map(this::suppressIfNotEnabled)
.doOnNext(e -> logger.debug("isOn/{}: {} {}", getAddress(), settings.enabled ? "enabled" : "DISABLED", e));
.doOnNext(e -> logger.debug("compute {}/isOn: {} {}", getAddress(), settings.enabled ? "enabled" : "DISABLED", e));

// And finally, suppress if the economizer says so
return stage3.map(this::suppressEconomizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public ZoneController(Collection<Zone> zones) {
@Override
public Flux<Signal<UnitControlSignal, String>> compute(Flux<Signal<ZoneStatus, String>> in) {

logger.debug("compute()");

return in
.filter(this::isOurs)
.doOnNext(this::capture)
Expand Down
6 changes: 3 additions & 3 deletions dz3r-model/src/main/java/net/sf/dz3r/signal/Signal.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ public Throwable getError() {

@Override
public String toString() {
var result = "@" + timestamp + ", value={" + value + "}, " + printPayload()
var result = "{@" + timestamp + ", value={" + value + "}, " + printPayload()
+ "status=" + status
+ ", isOK=" + isOK() + ", isError=" + isError() ;

if (isOK()) {
return result;
return result + "}";
}

return result + ", error=" + error.getClass().getName() + "(" + error.getMessage() + ")";
return result + ", error=" + error.getClass().getName() + "(" + error.getMessage() + ")}";
}

private String printPayload() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public CallingStatus(Double sample, double demand, boolean calling) {

@Override
public String toString() {
return "{sample=" + sample + ",demand=" + demand + ", calling=" + calling + "}";
return "{sample=" + sample + ", demand=" + demand + ", calling=" + calling + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

class AbstractEconomizerTest {

/**
* Make sure that control signal is computed properly as the indoor temperature is approaching the {@link EconomizerSettings#targetTemperature}.
*/
@ParameterizedTest
@MethodSource("targetAdjustmentProvider")
void targetAdjustmentTest(TestData source) {
void targetAdjustmentTest(TargetAdjustmentTestData source) {

var settings = new EconomizerSettings(
source.mode,
Expand All @@ -41,8 +44,6 @@ private class TestEconomizer extends AbstractEconomizer<String> {
* <p>
* Note that only the {@code ambientFlux} argument is present, indoor flux is provided to {@link #compute(Flux)}.
*
* @param name
* @param settings
* @param ambientFlux Flux from the ambient temperature sensor.
* @param targetDevice Switch to control the economizer actuator.
*/
Expand All @@ -62,7 +63,7 @@ public double computeCombined(Double indoorTemperature, Double ambientTemperatur
return super.computeCombined(indoorTemperature, ambientTemperature);
}
}
private static final class TestData {
private static final class TargetAdjustmentTestData {

public final HvacMode mode;
public final double changeoverDelta;
Expand All @@ -71,7 +72,7 @@ private static final class TestData {
public final double ambientTemperature;
public final double expectedSignal;

private TestData(HvacMode mode, double changeoverDelta, double targetTemperature, double indoorTemperature, double ambientTemperature, double expectedSignal) {
private TargetAdjustmentTestData(HvacMode mode, double changeoverDelta, double targetTemperature, double indoorTemperature, double ambientTemperature, double expectedSignal) {
this.mode = mode;
this.changeoverDelta = changeoverDelta;
this.targetTemperature = targetTemperature;
Expand All @@ -82,16 +83,16 @@ private TestData(HvacMode mode, double changeoverDelta, double targetTemperature
}

/**
* @return Stream of {@link TestData} for {@link #targetAdjustmentTest(TestData)}.
* @return Stream of {@link TargetAdjustmentTestData} for {@link #targetAdjustmentTest(TargetAdjustmentTestData)}.
*/
private static Stream<TestData> targetAdjustmentProvider() {
private static Stream<TargetAdjustmentTestData> targetAdjustmentProvider() {

return Stream.of(
new TestData(HvacMode.COOLING, 1.0, 22.0, 25.0, 10.0, 14.0),
new TestData(HvacMode.COOLING, 1.0, 22.0, 23.0, 10.0, 12.0),
new TestData(HvacMode.COOLING, 1.0, 22.0, 22.5, 10.0, 5.75),
new TestData(HvacMode.COOLING, 1.0, 22.0, 22.0, 10.0, 0.0),
new TestData(HvacMode.COOLING, 1.0, 22.0, 21.0, 10.0, -10.0)
new TargetAdjustmentTestData(HvacMode.COOLING, 1.0, 22.0, 25.0, 10.0, 14.0),
new TargetAdjustmentTestData(HvacMode.COOLING, 1.0, 22.0, 23.0, 10.0, 12.0),
new TargetAdjustmentTestData(HvacMode.COOLING, 1.0, 22.0, 22.5, 10.0, 5.75),
new TargetAdjustmentTestData(HvacMode.COOLING, 1.0, 22.0, 22.0, 10.0, 0.0),
new TargetAdjustmentTestData(HvacMode.COOLING, 1.0, 22.0, 21.0, 10.0, -10.0)
);

}
Expand Down
2 changes: 1 addition & 1 deletion dz3r-swing/src/main/java/net/sf/dz3r/common/DataSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DataSet<T> {
/**
* The data set. The key is sampling time, the value is sample value.
*/
private final LinkedHashMap<Long, T> samples = new LinkedHashMap<>();
private final Map<Long, T> samples = new LinkedHashMap<>();

/**
* The expiration interval. Values older than the last key by this many
Expand Down
47 changes: 27 additions & 20 deletions dz3r-swing/src/main/java/net/sf/dz3r/view/swing/AbstractChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,14 @@ public abstract class AbstractChart<T, P> extends SwingSink<T, P> {
/**
* Horizontal grid spacing.
*
* Vertical grid lines will be painted every <code>timeSpacing</code>
* milliseconds. Default is 30 minutes.
* Vertical grid lines will be painted every {@code timeSpacing} milliseconds. Default is 30 minutes.
*/
protected static final Duration SPACING_TIME = Duration.of(30, ChronoUnit.MINUTES);

/**
* Vertical grid spacing.
*
* Horizontal grid lines will be painted every <code>valueSpacing</code>
* units. Default is 1.0.
* Horizontal grid lines will be painted every {@code valueSpacing} units. Default is 1.0.
*/
protected static final double SPACING_VALUE = 1.0;

Expand All @@ -85,23 +83,22 @@ public abstract class AbstractChart<T, P> extends SwingSink<T, P> {
/**
* Maximum known data value.
*/
protected Double dataMax = null;
protected Double dataMax;

/**
* Minimum known data value.
*/
protected Double dataMin = null;
protected Double dataMin ;

/**
* Timestamp on {@link #dataMin} or {@link #dataMax}, whichever is younger.
*
* @see #adjustVerticalLimits(long, double, double)
*/
private Long minmaxTime = null;
private Long minmaxTime ;

/**
* Amount of extra time to wait before {@link #recalculateVerticalLimits()
* recalculating} the limits.
* Amount of extra time to wait before {@link #recalculateVerticalLimits() recalculating} the limits.
*
* Chances are, new min/max values will be pretty close to old, so unless
* this value is used, recalculation will be happening more often than
Expand Down Expand Up @@ -169,7 +166,6 @@ public synchronized void paintComponent(Graphics g) {

try {


// Draw background
super.paintComponent(g);

Expand Down Expand Up @@ -197,10 +193,10 @@ public synchronized void paintComponent(Graphics g) {
paintValueGrid(g2d, boundary, insets, yScale, yOffset);
paintCharts(g2d, boundary, insets, now, xScale, xOffset, yScale, yOffset);

logger.debug("Painted in {}ms", (clock.instant().toEpochMilli() - startTime));
logger.debug("Painted in {}ms", clock.instant().toEpochMilli() - startTime);

} catch (Exception ex) {
logger.warn("Painted in {}ms (FAIL)", (clock.instant().toEpochMilli() - startTime));
logger.warn("Painted in {}ms (FAIL)", clock.instant().toEpochMilli() - startTime);
logger.warn("Unexpected exception, ignored", ex);
} finally {
ThreadContext.pop();
Expand Down Expand Up @@ -239,7 +235,7 @@ private void paintTimeGrid(Graphics2D g2d, Dimension boundary, Insets insets, lo

private void paintTimeGrid(Graphics2D g2d, double top, double bottom, Insets insets, long now, double xScale, long xOffset, Duration spacingTime) {

var originalStroke = (BasicStroke) g2d.getStroke();
var originalStroke = g2d.getStroke();
var gridStroke = setGridStroke(g2d);

g2d.setStroke(gridStroke);
Expand Down Expand Up @@ -279,7 +275,7 @@ private void paintValueGrid(

// VT: NOTE: squid:S107 - following this rule will hurt performance, so no.

var originalStroke = (BasicStroke) g2d.getStroke();
var originalStroke = g2d.getStroke();
var gridStroke = setGridStroke(g2d);

// The zero line gets painted with the default stroke
Expand All @@ -288,7 +284,7 @@ private void paintValueGrid(

var gridY = yOffset * yScale + insets.top;

Line2D gridLine = new Line2D.Double(
var gridLine = new Line2D.Double(
insets.left,
gridY,
(double)boundary.width - insets.right - 1,
Expand All @@ -300,7 +296,7 @@ private void paintValueGrid(

g2d.setStroke(gridStroke);

var halfWidth = (boundary.width - insets.right - 1) / 2d;
var halfWidth = (boundary.width - insets.right - 1) / 2.0;

for (var valueOffset = SPACING_VALUE; valueOffset < dataMax + PADDING; valueOffset += SPACING_VALUE) {

Expand Down Expand Up @@ -355,9 +351,9 @@ protected final void drawGradientLine(
// VT: NOTE: squid:S107 - following this rule will hurt performance, so no.

var gp = new GradientPaint(
(int) x0, (int) y0, startColor,
(int) x1, (int) y1, endColor);
Line2D line = new Line2D.Double(x0, y0, x1, y1);
(float) x0, (float) y0, startColor,
(float) x1, (float) y1, endColor);
var line = new Line2D.Double(x0, y0, x1, y1);

g2d.setPaint(gp);
g2d.setStroke(emphasize ? strokeDouble : strokeSingle);
Expand All @@ -370,13 +366,24 @@ protected final void drawGradientLine(
* @param timestamp Value timestamp.
* @param value Incoming data element.
* @param setpoint Incoming setpoint.
* @param ambient Ambient temperature, {@code null} if unavailable.
* @param target Incoming economizer target temperature, {@code null} if unavailable.
*
* @see #dataMax
* @see #dataMin
*/
protected final void adjustVerticalLimits(long timestamp, double value, double setpoint) {
protected final void adjustVerticalLimits(long timestamp, double value, double setpoint, Double ambient, Double target) {

adjustVerticalLimits(timestamp, value);
adjustVerticalLimitsExt(timestamp, setpoint);

if (ambient != null) {
adjustVerticalLimits(timestamp, ambient);
}

if (target != null) {
adjustVerticalLimitsExt(timestamp, target);
}
}

protected final void adjustVerticalLimits(long timestamp, double value) {
Expand Down
Loading

0 comments on commit 7d0fc55

Please sign in to comment.