这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @author Andreas Brenk
* @author Thomas.Eichstaedt-Engelen
* @author Gaël L'hopital
* @author Rob Nielsen
* @since 1.4.0
*/
public class NetatmoBinding extends
Expand All @@ -63,6 +64,8 @@ public class NetatmoBinding extends
protected static final String CONFIG_CLIENT_SECRET = "clientsecret";
protected static final String CONFIG_REFRESH = "refresh";
protected static final String CONFIG_REFRESH_TOKEN = "refreshtoken";
protected static final String CONFIG_PRESSURE_UNIT = "pressureunit";
protected static final String CONFIG_UNIT_SYSTEM = "unitsystem";

/**
* The refresh interval which is used to poll values from the Netatmo server
Expand All @@ -74,6 +77,10 @@ public class NetatmoBinding extends

private Map<String, OAuthCredentials> credentialsCache = new HashMap<String, OAuthCredentials>();

private NetatmoPressureUnit pressureUnit = NetatmoPressureUnit.DEFAULT_PRESSURE_UNIT;

private NetatmoUnitSystem unitSystem = NetatmoUnitSystem.DEFAULT_UNIT_SYSTEM;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -152,11 +159,19 @@ protected void execute() {
case RAIN:
final String requestKey = createKey(deviceId,
moduleId);
final BigDecimal value = deviceMeasureValueMap.get(
BigDecimal value = deviceMeasureValueMap.get(
requestKey).get(measureType.getMeasure());
// Protect that sometimes Netatmo returns null where
// numeric value is awaited (issue #1848)
if (value != null) {
if (measureType == NetatmoMeasureType.TEMPERATURE) {
value = unitSystem.convertTemp(value);
} else if (measureType == NetatmoMeasureType.RAIN) {
value = unitSystem.convertRain(value);
} else if (measureType == NetatmoMeasureType.PRESSURE) {
value = pressureUnit.convertPressure(value);
}

state = new DecimalType(value);
}
break;
Expand Down Expand Up @@ -196,8 +211,8 @@ protected void execute() {
device.getLatitude()),
new DecimalType(device
.getLongitude()),
new DecimalType(device
.getAltitude()));
new DecimalType(Math.round(unitSystem.
convertAltitude(device.getAltitude()))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should convertAltitude work in BigDecimal instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to keep the code as similar as before

}
if (device.getId().equals(deviceId)) {
switch (measureType) {
Expand Down Expand Up @@ -524,6 +539,24 @@ public void updated(final Dictionary<String, ?> config)
credentials.clientSecret = value;
} else if (CONFIG_REFRESH_TOKEN.equals(configKeyTail)) {
credentials.refreshToken = value;
} else if (CONFIG_PRESSURE_UNIT.equals(configKeyTail)) {
try {
pressureUnit = NetatmoPressureUnit.fromString(value);
} catch (IllegalArgumentException e) {
throw new ConfigurationException(configKey,
"the value '" + value
+ "' is not valid for the configKey '"
+ configKey +"'");
}
} else if (CONFIG_UNIT_SYSTEM.equals(configKeyTail)) {
try {
unitSystem = NetatmoUnitSystem.fromString(value);
} catch (IllegalArgumentException e) {
throw new ConfigurationException(configKey,
"the value '" + value
+ "' is not valid for the configKey '"
+ configKey +"'");
}
} else {
throw new ConfigurationException(configKey,
"the given configKey '" + configKey
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2010-2015, openHAB.org and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.netatmo.internal;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;

/**
* @author Rob Nielsen
* @since 1.8.0
*
* This enum holds all the different pressure units for the Netatmo
* binding, along with conversion methods
*/
public enum NetatmoPressureUnit {
MBAR("mbar"), INHG("inHg"), MMHG("mmHg");

public static final NetatmoPressureUnit DEFAULT_PRESSURE_UNIT = NetatmoPressureUnit.MBAR;
private static final BigDecimal MBAR_TO_INHG = new BigDecimal(0.02952998751);

private static final BigDecimal MBAR_TO_MMHG = new BigDecimal(0.750061683);

String pressureUnit;

private NetatmoPressureUnit(String pressureUnit) {
this.pressureUnit = pressureUnit;
}

public String getpressureUnit() {
return pressureUnit;
}

public static NetatmoPressureUnit fromString(String pressureUnit) {
if (!StringUtils.isEmpty(pressureUnit)) {
for (NetatmoPressureUnit pressureUnitType : NetatmoPressureUnit
.values()) {
if (pressureUnitType.toString().equalsIgnoreCase(pressureUnit)) {
return pressureUnitType;
}
}
}
throw new IllegalArgumentException("Invalid pressureUnit: "
+ pressureUnit);
}

public BigDecimal convertPressure(BigDecimal value) {
if (this == DEFAULT_PRESSURE_UNIT) {
return value;
} else if (this == INHG) {
return value.multiply(MBAR_TO_INHG);
} else { // MMGH
return value.multiply(MBAR_TO_MMHG);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2010-2015, openHAB.org and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.netatmo.internal;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;

/**
* @author Rob Nielsen
* @since 1.8.0
*
* This enum holds all the different unit systems for the Netatmo binding,
* along with conversion methods
*/
public enum NetatmoUnitSystem {
M("Metric"), US("US");

public static final NetatmoUnitSystem DEFAULT_UNIT_SYSTEM = NetatmoUnitSystem.M;

private static final double METERS_TO_FEET = 3.2808399;

private static final BigDecimal MM_TO_INCHES = new BigDecimal(0.0393700787);

private static final BigDecimal ONE_POINT_EIGHT = new BigDecimal(1.8);

private static final BigDecimal THIRTY_TWO = new BigDecimal(32);

String unitSystem;

private NetatmoUnitSystem(String unitSystem) {
this.unitSystem = unitSystem;
}

public String getunitSystem() {
return unitSystem;
}

public static NetatmoUnitSystem fromString(String unitSystem) {
if (!StringUtils.isEmpty(unitSystem)) {
for (NetatmoUnitSystem unitSystemType : NetatmoUnitSystem.values()) {
if (unitSystemType.toString().equalsIgnoreCase(unitSystem)) {
return unitSystemType;
}
}
}
throw new IllegalArgumentException("Invalid unitSystem: " + unitSystem);
}

public double convertAltitude(double value) {
if (this == DEFAULT_UNIT_SYSTEM) {
return value;
}

return value * METERS_TO_FEET;
}

public BigDecimal convertRain(BigDecimal value) {
if (this == DEFAULT_UNIT_SYSTEM) {
return value;
}

return value.multiply(MM_TO_INCHES);
}

public BigDecimal convertTemp(BigDecimal value) {
if (this == DEFAULT_UNIT_SYSTEM) {
return value;
}

return value.multiply(ONE_POINT_EIGHT).add(THIRTY_TWO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* modules.
*
* @author Andreas Brenk
* @author Rob Nielsen
* @since 1.4.0
* @see <a href="http://dev.netatmo.com/doc/restapi/devicelist">devicelist</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ else if (level < WIFI_STATUS_THRESHOLD_0)
return result;
}

public Integer getAltitude() {
public Double getAltitude() {
return this.place.altitude;
}

Expand Down Expand Up @@ -566,7 +566,7 @@ public String toString() {
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Place extends AbstractMessagePart {

private Integer altitude;
private Double altitude;
private String country;
private List<Double> location;
private String timezone;
Expand All @@ -576,7 +576,7 @@ public static class Place extends AbstractMessagePart {
* "altitude": 33
*/
@JsonProperty("altitude")
public Integer getAltitude() {
public Double getAltitude() {
return this.altitude;
}

Expand Down
7 changes: 7 additions & 0 deletions distribution/openhabhome/configurations/openhab_default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,13 @@ tcp:refreshinterval=250
# The Netatmo refresh token (see http://dev.netatmo.com/doc/authentication/usercred)
#netatmo:refreshtoken=

# The Netatmo unit system where M=Metric system - celsius/meters/millimeters,
# US=US System - fahrenheit/feet/inches (optional, defaults to M)
#netatmo:unitsystem=

# The Netatmo pressure unit values: mbar, inHg, mmHg (optional, defaults to mbar)
#netatmo:pressureunit=

########################### HDanywhere Binding ########################################
#
# Optional, specify the number of input and output ports for a given HDanywhere matrix
Expand Down