Skip to content

Commit

Permalink
fix unit display.
Browse files Browse the repository at this point in the history
Signed-off-by: lewei50 <service@lewei50.com>
  • Loading branch information
lewei50 committed Aug 12, 2020
1 parent 30cf5bf commit afe0135
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 92 deletions.
Expand Up @@ -21,10 +21,11 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.measure.Unit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.library.types.QuantityType;
import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
Expand All @@ -49,7 +50,7 @@
* The {@link IammeterHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author yang bo - Initial contribution
* @author Yang Bo - Initial contribution
*/

@NonNullByDefault
Expand All @@ -58,22 +59,18 @@ public class IammeterHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(IammeterHandler.class);
private @Nullable ScheduledFuture<?> refreshJob;
private IammeterConfiguration config;
private static final int TIMEOUT_MS = 5000;

public IammeterHandler(Thing thing) {
super(thing);
config = getConfiguration();
}

private final int timeout = 5000;

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
if (command instanceof RefreshType) {
try {
refresh();
} catch (IOException | JsonSyntaxException ex) {
logger.warn("refresh error {}", ex.getMessage());
}
refresh();
}
}

Expand All @@ -82,39 +79,27 @@ public void handleCommand(ChannelUID channelUID, Command command) {
@Override
public void initialize() {
ScheduledFuture<?> refreshJob = this.refreshJob;
IammeterConfiguration config = this.config;
config = getConfiguration();
if (refreshJob == null || refreshJob.isCancelled()) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
refresh();
} catch (IOException | JsonSyntaxException ex) {
logger.warn("refresh error {}", ex.getMessage());
}
}
};
refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, config.refreshInterval, TimeUnit.SECONDS);
if (refreshJob == null) {
refreshJob = scheduler.scheduleWithFixedDelay(this::refresh, 0, config.refreshInterval, TimeUnit.SECONDS);
updateStatus(ThingStatus.UNKNOWN);
}
}

private void refresh() throws IOException, JsonSyntaxException {
private void refresh() {
IammeterConfiguration config = this.config;
try {
logger.trace("Starting refresh handler");
String httpMethod = "GET";
String url = "http://admin:admin@" + config.host + ":" + config.port + "/monitorjson";
String content = "";
InputStream stream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));

String response = HttpUtil.executeUrl(httpMethod, url, stream, null, timeout);
String response = HttpUtil.executeUrl(httpMethod, url, stream, null, TIMEOUT_MS);
JsonElement iammeterDataElement = new JsonParser().parse(response);
JsonObject iammeterData = iammeterDataElement.getAsJsonObject();
String keyWord = "Data";
boolean bRemoveChannels = false;
String channelProfix = "";
String channelPrefix = "";
if (iammeterData.has("data") || (iammeterData.has("Data") && iammeterData.has("SN"))) {
bRemoveChannels = true;
if (iammeterData.has("data")) {
Expand All @@ -123,10 +108,10 @@ private void refresh() throws IOException, JsonSyntaxException {
for (IammeterWEM3080Channel channelConfig : IammeterWEM3080Channel.values()) {
Channel channel = getThing().getChannel(channelConfig.getId());
if (channel != null) {
channelProfix = IammeterBindingConstants.THING_TYPE_POWERMETER + ":"
channelPrefix = IammeterBindingConstants.THING_TYPE_POWERMETER + ":"
+ channel.getUID().getThingUID().getId();
State state = getDecimal(
iammeterData.get(keyWord).getAsJsonArray().get(channelConfig.getIndex()).toString());
iammeterData.get(keyWord).getAsJsonArray().get(channelConfig.getIndex()).toString(), channelConfig.getUnit());
updateState(channel.getUID(), state);
}
}
Expand All @@ -136,14 +121,14 @@ private void refresh() throws IOException, JsonSyntaxException {
Channel channel = getThing().getChannel(channelConfig.getId());
if (channel != null) {
State state = getDecimal(iammeterData.get(keyWord).getAsJsonArray().get(channelConfig.getRow())
.getAsJsonArray().get(channelConfig.getCol()).toString());
.getAsJsonArray().get(channelConfig.getCol()).toString(), channelConfig.getUnit());
updateState(channel.getUID(), state);
}
}
}
if (bRemoveChannels) {
if (!bExtraChannelRemoved) {
thingStructureChanged(channelProfix);
thingStructureChanged(channelPrefix);
}
}
stream.close();
Expand All @@ -157,24 +142,24 @@ private void refresh() throws IOException, JsonSyntaxException {
}
}

protected void thingStructureChanged(String channelProfix) {
protected void thingStructureChanged(String channelPrefix) {
List<ChannelUID> noUsedItems = new ArrayList<>();
noUsedItems.add(new ChannelUID(channelProfix + ":frequency_a"));
noUsedItems.add(new ChannelUID(channelProfix + ":pf_a"));
noUsedItems.add(new ChannelUID(channelProfix + ":voltage_b"));
noUsedItems.add(new ChannelUID(channelProfix + ":current_b"));
noUsedItems.add(new ChannelUID(channelProfix + ":power_b"));
noUsedItems.add(new ChannelUID(channelProfix + ":importenergy_b"));
noUsedItems.add(new ChannelUID(channelProfix + ":exportgrid_b"));
noUsedItems.add(new ChannelUID(channelProfix + ":frequency_b"));
noUsedItems.add(new ChannelUID(channelProfix + ":pf_b"));
noUsedItems.add(new ChannelUID(channelProfix + ":voltage_c"));
noUsedItems.add(new ChannelUID(channelProfix + ":current_c"));
noUsedItems.add(new ChannelUID(channelProfix + ":power_c"));
noUsedItems.add(new ChannelUID(channelProfix + ":importenergy_c"));
noUsedItems.add(new ChannelUID(channelProfix + ":exportgrid_c"));
noUsedItems.add(new ChannelUID(channelProfix + ":frequency_c"));
noUsedItems.add(new ChannelUID(channelProfix + ":pf_c"));
noUsedItems.add(new ChannelUID(channelPrefix + ":frequency_a"));
noUsedItems.add(new ChannelUID(channelPrefix + ":pf_a"));
noUsedItems.add(new ChannelUID(channelPrefix + ":voltage_b"));
noUsedItems.add(new ChannelUID(channelPrefix + ":current_b"));
noUsedItems.add(new ChannelUID(channelPrefix + ":power_b"));
noUsedItems.add(new ChannelUID(channelPrefix + ":importenergy_b"));
noUsedItems.add(new ChannelUID(channelPrefix + ":exportgrid_b"));
noUsedItems.add(new ChannelUID(channelPrefix + ":frequency_b"));
noUsedItems.add(new ChannelUID(channelPrefix + ":pf_b"));
noUsedItems.add(new ChannelUID(channelPrefix + ":voltage_c"));
noUsedItems.add(new ChannelUID(channelPrefix + ":current_c"));
noUsedItems.add(new ChannelUID(channelPrefix + ":power_c"));
noUsedItems.add(new ChannelUID(channelPrefix + ":importenergy_c"));
noUsedItems.add(new ChannelUID(channelPrefix + ":exportgrid_c"));
noUsedItems.add(new ChannelUID(channelPrefix + ":frequency_c"));
noUsedItems.add(new ChannelUID(channelPrefix + ":pf_c"));
ThingBuilder thingBuilder = editThing();
for (ChannelUID chl : noUsedItems) {
thingBuilder.withoutChannel(chl);
Expand All @@ -183,9 +168,9 @@ protected void thingStructureChanged(String channelProfix) {
bExtraChannelRemoved = true;
}

private State getDecimal(String value) {
private State getDecimal(String value, Unit<?> unit) {
try {
return QuantityType.valueOf(Float.parseFloat(value), SmartHomeUnits.VOLT);
return QuantityType.valueOf(Float.parseFloat(value), unit);
} catch (NumberFormatException e) {
return UnDefType.UNDEF;
}
Expand All @@ -201,7 +186,7 @@ public void dispose() {
super.dispose();
}

@NonNullByDefault

public IammeterConfiguration getConfiguration() {
return this.getConfigAs(IammeterConfiguration.class);
}
Expand Down
Expand Up @@ -11,31 +11,31 @@
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.iammeter.internal;

import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
import javax.measure.Unit;

/**
* The {@link IammeterWEM3080Channel} Enum defines common constants, which are
* used across the whole binding.
*
* @author yang bo - Initial contribution
* @author Yang Bo - Initial contribution
*/
public enum IammeterWEM3080Channel {

CHANNEL_VOLTAGE("voltage_a", 0),
CHANNEL_CURRENT("current_a", 1),
CHANNEL_POWER("power_a", 2),
CHANNEL_IMPORTENERGY("importenergy_a", 3),
CHANNEL_EXPORTGRID("exportgrid_a", 4);
CHANNEL_VOLTAGE("voltage_a", 0, SmartHomeUnits.VOLT),
CHANNEL_CURRENT("current_a", 1, SmartHomeUnits.AMPERE),
CHANNEL_POWER("power_a", 2, SmartHomeUnits.WATT),
CHANNEL_IMPORTENERGY("importenergy_a", 3, SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_EXPORTGRID("exportgrid_a", 4, SmartHomeUnits.KILOWATT_HOUR);

private final String id;
private final int index;
private final Unit<?> unit;

IammeterWEM3080Channel(String id, int index) {
IammeterWEM3080Channel(String id, int index, Unit<?> unit) {
this.id = id;
this.index = index;
this.unit = null;
this.unit = unit;
}

public String getId() {
Expand Down
Expand Up @@ -14,46 +14,48 @@

import javax.measure.Unit;

import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;

/**
* The {@link IammeterWEM3080TChannel} Enum defines common constants, which are
* used across the whole binding.
*
* @author Johann Richard - Initial contribution
* @author Yang Bo - Initial contribution
*/
public enum IammeterWEM3080TChannel {

CHANNEL_VOLTAGE_A("voltage_a", 0, 0),
CHANNEL_CURRENT_A("current_a", 0, 1),
CHANNEL_POWER_A("power_a", 0, 2),
CHANNEL_IMPORTENERGY_A("importenergy_a", 0, 3),
CHANNEL_EXPORTGRID_A("exportgrid_a", 0, 4),
CHANNEL_FREQUENCY_A("frequency_a", 0, 5),
CHANNEL_PF_A("pf_a", 0, 6),
CHANNEL_VOLTAGE_B("voltage_b", 1, 0),
CHANNEL_CURRENT_B("current_b", 1, 1),
CHANNEL_POWER_B("power_b", 1, 2),
CHANNEL_IMPORTENERGY_B("importenergy_b", 1, 3),
CHANNEL_EXPORTGRID_B("exportgrid_b", 1, 4),
CHANNEL_FREQUENCY_B("frequency_b", 1, 5),
CHANNEL_PF_B("pf_b", 1, 6),
CHANNEL_VOLTAGE_C("voltage_c", 2, 0),
CHANNEL_CURRENT_C("current_c", 2, 1),
CHANNEL_POWER_C("power_c", 2, 2),
CHANNEL_IMPORTENERGY_C("importenergy_c", 2, 3),
CHANNEL_EXPORTGRID_C("exportgrid_c", 2, 4),
CHANNEL_FREQUENCY_C("frequency_c", 2, 5),
CHANNEL_PF_C("pf_c", 2, 6);
CHANNEL_VOLTAGE_A("voltage_a", 0, 0, SmartHomeUnits.VOLT),
CHANNEL_CURRENT_A("current_a", 0, 1, SmartHomeUnits.AMPERE),
CHANNEL_POWER_A("power_a", 0, 2, SmartHomeUnits.WATT),
CHANNEL_IMPORTENERGY_A("importenergy_a", 0, 3, SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_EXPORTGRID_A("exportgrid_a", 0, 4, SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_FREQUENCY_A("frequency_a", 0, 5, SmartHomeUnits.HERTZ),
CHANNEL_PF_A("pf_a", 0, 6, SmartHomeUnits.HERTZ),
CHANNEL_VOLTAGE_B("voltage_b", 1, 0, SmartHomeUnits.VOLT),
CHANNEL_CURRENT_B("current_b", 1, 1, SmartHomeUnits.AMPERE),
CHANNEL_POWER_B("power_b", 1, 2, SmartHomeUnits.WATT),
CHANNEL_IMPORTENERGY_B("importenergy_b", 1, 3, SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_EXPORTGRID_B("exportgrid_b", 1, 4, SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_FREQUENCY_B("frequency_b", 1, 5, SmartHomeUnits.HERTZ),
CHANNEL_PF_B("pf_b", 1, 6, SmartHomeUnits.HERTZ),
CHANNEL_VOLTAGE_C("voltage_c", 2, 0, SmartHomeUnits.VOLT),
CHANNEL_CURRENT_C("current_c", 2, 1, SmartHomeUnits.AMPERE),
CHANNEL_POWER_C("power_c", 2, 2, SmartHomeUnits.WATT),
CHANNEL_IMPORTENERGY_C("importenergy_c", 2, 3, SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_EXPORTGRID_C("exportgrid_c", 2, 4, SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_FREQUENCY_C("frequency_c", 2, 5, SmartHomeUnits.HERTZ),
CHANNEL_PF_C("pf_c", 2, 6, SmartHomeUnits.HERTZ);

private final String id;
private final int row;
private final int col;
private final Unit<?> unit;

IammeterWEM3080TChannel(String id, int r, int c) {
IammeterWEM3080TChannel(String id, int r, int c, Unit<?> unit) {
this.id = id;
this.row = r;
this.col = c;
this.unit = null;
this.unit = unit;
}

public String getId() {
Expand Down
Expand Up @@ -43,7 +43,7 @@
<default>80</default>
</parameter>
<parameter name="refreshInterval" type="text" required="false">
<label>Refresh interval in seconds.</label>
<label>Refresh interval in seconds</label>
<default>60</default>
</parameter>
</config-description>
Expand All @@ -69,19 +69,19 @@
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="importenergy_a">
<item-type>Number:kWh</item-type>
<item-type>Number:Energy</item-type>
<label>ImportEnergy A</label>
<description>importenergy for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="exportgrid_a">
<item-type>Number:kWh</item-type>
<item-type>Number:Energy</item-type>
<label>Exportgrid A</label>
<description>exportgrid for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="frequency_a">
<item-type>Number:Hz</item-type>
<item-type>Number:Frequency</item-type>
<label>Frequency A</label>
<description>frequency for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
Expand Down Expand Up @@ -111,19 +111,19 @@
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="importenergy_b">
<item-type>Number:kWh</item-type>
<item-type>Number:Energy</item-type>
<label>ImportEnergy B</label>
<description>importenergy for phase B</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="exportgrid_b">
<item-type>Number:kWh</item-type>
<item-type>Number:Energy</item-type>
<label>Exportgrid B</label>
<description>exportgrid for phase B</description>
<state pattern="%.2f %unit%"></state>
</channel-type>
<channel-type id="frequency_b">
<item-type>Number:Hz</item-type>
<item-type>Number:Frequency</item-type>
<label>Frequency B</label>
<description>frequency for phase B</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
Expand Down Expand Up @@ -153,19 +153,19 @@
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="importenergy_c">
<item-type>Number:kWh</item-type>
<item-type>Number:Energy</item-type>
<label>ImportEnergy C</label>
<description>importenergy for phase C</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="exportgrid_c">
<item-type>Number:kWh</item-type>
<item-type>Number:Energy</item-type>
<label>Exportgrid C</label>
<description>exportgrid for phase C</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="frequency_c">
<item-type>Number:Hz</item-type>
<item-type>Number:Frequency</item-type>
<label>Frequency C</label>
<description>frequency for phase C</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
Expand Down

0 comments on commit afe0135

Please sign in to comment.