-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[fronius] Added inverter power, battery state of charge and PV solar yield #10757
Changes from 3 commits
71d6345
50fa24b
73065c6
62c735b
01988c2
52d80ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
* | ||
* @author Gerrit Beine - Initial contribution | ||
* @author Thomas Rokohl - Refactoring to merge the concepts | ||
* @author Thomas Kordelle - Added inverter power, battery state of charge and PV solar yield | ||
*/ | ||
public abstract class FroniusBaseThingHandler extends BaseThingHandler { | ||
|
||
|
@@ -128,9 +129,9 @@ protected void updateChannel(String channelId) { | |
} else if (value instanceof ValueUnit) { | ||
state = new DecimalType(((ValueUnit) value).getValue()); | ||
} else if (value instanceof String) { | ||
state = new StringType((String) value); | ||
state = StringType.valueOf((String) value); | ||
} else if (value instanceof QuantityType) { | ||
state = (QuantityType) value; | ||
state = (QuantityType<?>) value; | ||
} else { | ||
logger.warn("Update channel {}: Unsupported value type {}", channelId, value.getClass().getSimpleName()); | ||
} | ||
|
@@ -198,12 +199,12 @@ protected <T extends BaseFroniusResponse> T collectDataFormUrl(Class<T> type, St | |
if (!resultOk) { | ||
logger.debug("Error in fronius response: {}", errorMsg); | ||
} | ||
} catch (JsonSyntaxException e) { | ||
} catch (JsonSyntaxException | NumberFormatException e) { | ||
errorMsg = "Invalid JSON data received"; | ||
logger.debug("Error running fronius request: {}", e.getMessage()); | ||
logger.error("Error running fronius request: {}", e.getMessage()); | ||
} catch (IOException | IllegalStateException e) { | ||
errorMsg = e.getMessage(); | ||
logger.debug("Error running fronius request: {}", errorMsg); | ||
logger.error("Error running fronius request: {}", errorMsg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bindings should only log to error if something severe happened, like the detection of a bug in your code. This could be debug as you catch an IOException. See this link for a description of the log levels: https://www.openhab.org/docs/developer/guidelines.html#f-logging |
||
} | ||
|
||
// Update the thing status | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,8 @@ | |
package org.openhab.binding.fronius.internal.handler; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.openhab.binding.fronius.internal.FroniusBaseDeviceConfiguration; | ||
import org.openhab.binding.fronius.internal.FroniusBindingConstants; | ||
import org.openhab.binding.fronius.internal.FroniusBridgeConfiguration; | ||
|
@@ -23,18 +23,16 @@ | |
import org.openhab.core.library.types.QuantityType; | ||
import org.openhab.core.library.unit.Units; | ||
import org.openhab.core.thing.Thing; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link FroniusMeterHandler} is responsible for updating the data, which are | ||
* sent to one of the channels. | ||
* | ||
* @author Jimmy Tanagra - Initial contribution | ||
* @author Thomas Kordelle - Actually constants should be all upper case. | ||
*/ | ||
public class FroniusMeterHandler extends FroniusBaseThingHandler { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(FroniusMeterHandler.class); | ||
private MeterRealtimeBodyDataDTO meterRealtimeBodyData; | ||
private FroniusBaseDeviceConfiguration config; | ||
|
||
|
@@ -72,42 +70,47 @@ protected Object getValue(String channelId) { | |
return null; | ||
} | ||
|
||
String[] fields = StringUtils.split(channelId, "#"); | ||
String fieldName = fields[0]; | ||
final String[] fields = channelId.split("#"); | ||
if (fields.length < 1) { | ||
return null; | ||
} | ||
final String fieldName = fields[0]; | ||
|
||
switch (fieldName) { | ||
case FroniusBindingConstants.MeterEnable: | ||
case FroniusBindingConstants.METER_ENABLE: | ||
return meterRealtimeBodyData.getEnable(); | ||
case FroniusBindingConstants.MeterLocation: | ||
case FroniusBindingConstants.METER_LOCATION: | ||
return meterRealtimeBodyData.getMeterLocationCurrent(); | ||
case FroniusBindingConstants.MeterCurrentAcPhase1: | ||
return new QuantityType(meterRealtimeBodyData.getCurrentACPhase1(), Units.AMPERE); | ||
case FroniusBindingConstants.MeterCurrentAcPhase2: | ||
return new QuantityType(meterRealtimeBodyData.getCurrentACPhase2(), Units.AMPERE); | ||
case FroniusBindingConstants.MeterCurrentAcPhase3: | ||
return new QuantityType(meterRealtimeBodyData.getCurrentACPhase3(), Units.AMPERE); | ||
case FroniusBindingConstants.MeterVoltageAcPhase1: | ||
return new QuantityType(meterRealtimeBodyData.getVoltageACPhase1(), Units.VOLT); | ||
case FroniusBindingConstants.MeterVoltageAcPhase2: | ||
return new QuantityType(meterRealtimeBodyData.getVoltageACPhase2(), Units.VOLT); | ||
case FroniusBindingConstants.MeterVoltageAcPhase3: | ||
return new QuantityType(meterRealtimeBodyData.getVoltageACPhase3(), Units.VOLT); | ||
case FroniusBindingConstants.MeterPowerPhase1: | ||
return new QuantityType(meterRealtimeBodyData.getPowerRealPPhase1(), Units.WATT); | ||
case FroniusBindingConstants.MeterPowerPhase2: | ||
return new QuantityType(meterRealtimeBodyData.getPowerRealPPhase2(), Units.WATT); | ||
case FroniusBindingConstants.MeterPowerPhase3: | ||
return new QuantityType(meterRealtimeBodyData.getPowerRealPPhase3(), Units.WATT); | ||
case FroniusBindingConstants.MeterPowerFactorPhase1: | ||
case FroniusBindingConstants.METER_CURRENT_AC_PHASE_1: | ||
return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase1(), Units.AMPERE); | ||
case FroniusBindingConstants.METER_CURRENT_AC_PHASE_2: | ||
return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase2(), Units.AMPERE); | ||
case FroniusBindingConstants.METER_CURRENT_AC_PHASE_3: | ||
return new QuantityType<>(meterRealtimeBodyData.getCurrentACPhase3(), Units.AMPERE); | ||
case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_1: | ||
return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase1(), Units.VOLT); | ||
case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_2: | ||
return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase2(), Units.VOLT); | ||
case FroniusBindingConstants.METER_VOLTAGE_AC_PHASE_3: | ||
return new QuantityType<>(meterRealtimeBodyData.getVoltageACPhase3(), Units.VOLT); | ||
case FroniusBindingConstants.METER_POWER_PHASE_1: | ||
return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase1(), Units.WATT); | ||
case FroniusBindingConstants.METER_POWER_PHASE_2: | ||
return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase2(), Units.WATT); | ||
case FroniusBindingConstants.METER_POWER_PHASE_3: | ||
return new QuantityType<>(meterRealtimeBodyData.getPowerRealPPhase3(), Units.WATT); | ||
case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_1: | ||
return meterRealtimeBodyData.getPowerFactorPhase1(); | ||
case FroniusBindingConstants.MeterPowerFactorPhase2: | ||
case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_2: | ||
return meterRealtimeBodyData.getPowerFactorPhase2(); | ||
case FroniusBindingConstants.MeterPowerFactorPhase3: | ||
case FroniusBindingConstants.METER_POWER_FACTOR_PHASE_3: | ||
return meterRealtimeBodyData.getPowerFactorPhase3(); | ||
case FroniusBindingConstants.MeterEnergyRealSumConsumed: | ||
return new QuantityType(meterRealtimeBodyData.getEnergyRealWACSumConsumed(), Units.WATT_HOUR); | ||
case FroniusBindingConstants.MeterEnergyRealSumProduced: | ||
return new QuantityType(meterRealtimeBodyData.getEnergyRealWACSumProduced(), Units.WATT_HOUR); | ||
case FroniusBindingConstants.METER_ENERGY_REAL_SUM_CONSUMED: | ||
return new QuantityType<>(meterRealtimeBodyData.getEnergyRealWACSumConsumed(), Units.WATT_HOUR); | ||
case FroniusBindingConstants.METER_ENERGY_REAL_SUM_PRODUCED: | ||
return new QuantityType<>(meterRealtimeBodyData.getEnergyRealWACSumProduced(), Units.WATT_HOUR); | ||
default: | ||
break; | ||
} | ||
|
||
return null; | ||
|
@@ -118,10 +121,10 @@ private void updateProperties() { | |
return; | ||
} | ||
|
||
Map<String, String> properties = editProperties(); | ||
final Map<String, String> properties = editProperties(); | ||
|
||
properties.put(FroniusBindingConstants.MeterModel, meterRealtimeBodyData.getDetails().getModel()); | ||
properties.put(FroniusBindingConstants.MeterSerial, meterRealtimeBodyData.getDetails().getSerial()); | ||
properties.put(FroniusBindingConstants.METER_MODEL, meterRealtimeBodyData.getDetails().getModel()); | ||
properties.put(FroniusBindingConstants.METER_SERIAL, meterRealtimeBodyData.getDetails().getSerial()); | ||
|
||
updateProperties(properties); | ||
} | ||
|
@@ -146,8 +149,11 @@ private void updateData(FroniusBridgeConfiguration bridgeConfiguration, FroniusB | |
* @param deviceId of the device | ||
* @return {MeterRealtimeResponse} the object representation of the json response | ||
*/ | ||
private MeterRealtimeResponseDTO getMeterRealtimeData(String ip, int deviceId) { | ||
String location = FroniusBindingConstants.METER_REALTIME_DATA_URL.replace("%IP%", StringUtils.trimToEmpty(ip)); | ||
private MeterRealtimeResponseDTO getMeterRealtimeData(final String ip, final Integer deviceId) { | ||
Objects.requireNonNull(ip, "IP address must be set in the configuration."); | ||
Objects.requireNonNull(deviceId, "Device ID must be set in the configuration."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will throw an unchecked exception, which will crash your binding. You could throw a custom exception and catch it in the calling method. |
||
|
||
String location = FroniusBindingConstants.METER_REALTIME_DATA_URL.replace("%IP%", ip.trim()); | ||
location = location.replace("%DEVICEID%", Integer.toString(deviceId)); | ||
return collectDataFormUrl(MeterRealtimeResponseDTO.class, location); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Syntactical sugar. Please check all.