Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sagercaster] Reintroducing timestamp channel #11665

Merged
merged 4 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions bundles/org.openhab.binding.sagercaster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ The Sager Weathercaster is a scientific instrument for accurate prediction of th

* To operate, this binding will need to use channel values provided by other means (e.g. Weather Binding, Netatmo, a 1-Wire personal weather station...)

* This binding buffers readings for some hours before producing weather forecasts(wind direction and sea level pressure). SagerWeatherCaster needs an observation period of minimum 6 hours.
* This binding buffers readings for some hours before producing weather forecasts(wind direction and sea level pressure).
SagerWeatherCaster needs an observation period of minimum 6 hours.

For these reasons, this binding is not a binding in the usual sense.

Expand All @@ -24,9 +25,11 @@ The binding itself does not require any configuration.

| Name | Type | Description |
|--------------------|----------|--------------------------------------------------------------------------|
| location | Location | Latitude and longitude of the desired weather forecast. |
| location (*) | Location | Latitude and longitude of the desired weather forecast. |
| observation-period | int | Minimum delay (in hours) before producing forecasts. Defaulted to 6. |

(*) Only latitude is used by the algorithm.

## Channels
lolodomo marked this conversation as resolved.
Show resolved Hide resolved

The binding will use some input channels, that can be configured directly with profiles (sample below).
Expand All @@ -41,6 +44,8 @@ The binding will use some input channels, that can be configured directly with p
| wind-speed-beaufort | input |Number | Wind speed expressed using the Beaufort scale |
| pressure | input |Number:Pressure | Sea level pressure |
| wind-angle | input |Number:Angle | Wind direction |
| temperature | input |Number:Temperature | Outside temperature |
| timestamp | output |DateTime | Timestamp of the last forecast update |
| forecast | output |String | Description of the weather forecast |
| velocity | output |String | Description of the expected wind evolution |
| velocity-beaufort | output |Number | Expected wind evolution using the Beaufort scale |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class SagerCasterBindingConstants {
public static final String CHANNEL_WINDEVOLUTION = "wind-evolution";
public static final String CHANNEL_PRESSURETREND = "pressure-trend";
public static final String CHANNEL_TEMPERATURETREND = "temperature-trend";
public static final String CHANNEL_TIMESTAMP = "timestamp";

// Input channel ids
public static final String CHANNEL_CLOUDINESS = "cloudiness";
public static final String CHANNEL_IS_RAINING = "is-raining";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.sagercaster.internal.caster.SagerWeatherCaster;
import org.openhab.binding.sagercaster.internal.handler.SagerCasterHandler;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.sagercaster.internal.caster;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This class holds the result of the SagerCaster algorithm
*
* @author Gaël L'hopital - Initial contribution
*/
@NonNullByDefault
public class SagerPrediction {
private final String prediction;

public SagerPrediction(String sagerCode) {
this.prediction = sagerCode;
}

public String getSagerCode() {
return prediction;
}

public String getForecast() {
return Character.toString(prediction.charAt(0));
}

public String getWindVelocity() {
return Character.toString(prediction.charAt(1));
}

public String getWindDirection() {
return Character.toString(prediction.charAt(2));
}

public String getWindDirection2() {
return prediction.length() > 3 ? Character.toString(prediction.charAt(3)) : SagerWeatherCaster.UNDEF;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* 378 possible forecasts determined from 4996 dial codes.
*/

package org.openhab.binding.sagercaster.internal;
package org.openhab.binding.sagercaster.internal.caster;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -76,6 +76,7 @@
@Component(service = SagerWeatherCaster.class, scope = ServiceScope.SINGLETON)
@NonNullByDefault
public class SagerWeatherCaster {
public final static String UNDEF = "-";
// Northern Polar Zone & Northern Tropical Zone
private final static String[] NPZDIRECTIONS = { "S", "SW", "W", "NW", "N", "NE", "E", "SE" };
// Northern Temperate Zone
Expand All @@ -88,7 +89,7 @@ public class SagerWeatherCaster {
private final Logger logger = LoggerFactory.getLogger(SagerWeatherCaster.class);
private final Properties forecaster = new Properties();

private Optional<Prevision> prevision = Optional.empty();
private Optional<SagerPrediction> prevision = Optional.empty();
private String[] usedDirections = NTZDIRECTIONS; // Defaulted to Northern Zone

private int currentBearing = -1;
Expand Down Expand Up @@ -238,7 +239,7 @@ private String getCompass() {

private void updatePrediction() {
int zWind = Arrays.asList(usedDirections).indexOf(getCompass());
String d1 = "-";
String d1 = UNDEF;
switch (zWind) {
case 0:
if (windEvolution == 3) {
Expand Down Expand Up @@ -319,39 +320,27 @@ private void updatePrediction() {
}
String forecast = forecaster.getProperty(
d1 + String.valueOf(sagerPressure) + String.valueOf(pressureEvolution) + String.valueOf(nubes));
prevision = (forecast != null) ? Optional.of(new Prevision(forecast)) : Optional.empty();
prevision = Optional.ofNullable(forecast != null ? new SagerPrediction(forecast) : null);
}

public String getForecast() {
if (prevision.isPresent()) {
char forecast = prevision.get().zForecast;
return Character.toString(forecast);
}
return "-";
return prevision.map(p -> p.getForecast()).orElse(UNDEF);
}

public String getWindVelocity() {
if (prevision.isPresent()) {
char windVelocity = prevision.get().zWindVelocity;
return Character.toString(windVelocity);
}
return "-";
return prevision.map(p -> p.getWindVelocity()).orElse(UNDEF);
}

public String getWindDirection() {
if (prevision.isPresent()) {
int direction = prevision.get().zWindDirection;
return String.valueOf(direction);
}
return "-";
return prevision.map(p -> p.getWindDirection()).orElse(UNDEF);
}

public String getWindDirection2() {
if (prevision.isPresent()) {
int direction = prevision.get().zWindDirection2;
return String.valueOf(direction);
}
return "-";
return prevision.map(p -> p.getWindDirection2()).orElse(UNDEF);
}

public String getSagerCode() {
return prevision.map(p -> p.getSagerCode()).orElse(UNDEF);
}

public void setLatitude(double latitude) {
Expand All @@ -370,17 +359,31 @@ public void setLatitude(double latitude) {
}
}

private class Prevision {
public final char zForecast;
public final char zWindVelocity;
public final int zWindDirection;
public final int zWindDirection2;

public Prevision(String forecast) {
zForecast = forecast.charAt(0);
zWindVelocity = forecast.charAt(1);
zWindDirection = Character.getNumericValue(forecast.charAt(2));
zWindDirection2 = (forecast.length() > 3) ? Character.getNumericValue(forecast.charAt(3)) : -1;
public int getPredictedBeaufort() {
int result = currentBeaufort;
switch (getWindVelocity()) {
case "N":
result += 1;
break;
case "F":
result = 4;
break;
case "S":
result = 6;
break;
case "G":
result = 8;
break;
case "W":
result = 10;
break;
case "H":
result = 12;
break;
case "D":
result -= 1;
break;
}
return result;
}
}
Loading