Skip to content

Commit

Permalink
code formating and justify.
Browse files Browse the repository at this point in the history
Signed-off-by: lewei50 <service@lewei50.com>
  • Loading branch information
lewei50 committed Aug 11, 2020
1 parent 19ce9bc commit 30cf5bf
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 92 deletions.
17 changes: 17 additions & 0 deletions .project
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.openhab.addons.reactor</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Expand Up @@ -28,5 +28,4 @@ public class IammeterBindingConstants {

// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_POWERMETER = new ThingTypeUID(BINDING_ID, "powermeter");

}
Expand Up @@ -12,14 +12,17 @@
*/
package org.openhab.binding.iammeter.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link IammeterConfiguration} class contains fields mapping thing configuration parameters.
*
* @author yang bo - Initial contribution
* @author Yang Bo - Initial contribution
*/

@NonNullByDefault
public class IammeterConfiguration {
public String host;
public int port;
public int refreshInterval;
public String host = "127.0.0.1";
public int port = 80;
public int refreshInterval = 30;
}
Expand Up @@ -15,14 +15,16 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.library.types.DecimalType;
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,24 +51,28 @@
*
* @author yang bo - Initial contribution
*/

@NonNullByDefault
public class IammeterHandler extends BaseThingHandler {

private final Logger logger = LoggerFactory.getLogger(IammeterHandler.class);
private @Nullable ScheduledFuture<?> refreshJob;
private IammeterConfiguration config;

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{
try {
refresh();
} catch (Exception ex) {
logger.warn("refresh error {}" , ex.getMessage());
} catch (IOException | JsonSyntaxException ex) {
logger.warn("refresh error {}", ex.getMessage());
}
}
}
Expand All @@ -75,33 +81,28 @@ public void handleCommand(ChannelUID channelUID, Command command) {

@Override
public void initialize() {
IammeterConfiguration config = getConfiguration();

Runnable runnable = new Runnable() {
@Override
public void run() {
try{
refresh();
} catch (Exception ex) {
logger.warn("refresh error {}" , ex.getMessage());
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());
}
}
}
};
scheduler.scheduleWithFixedDelay(runnable, 0, config.refreshInterval, TimeUnit.SECONDS);

updateStatus(ThingStatus.UNKNOWN);
scheduler.execute(() -> {
try{
refresh();
} catch (Exception ex) {
logger.warn("refresh error {}" , ex.getMessage());
}
});
};
refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, config.refreshInterval, TimeUnit.SECONDS);
updateStatus(ThingStatus.UNKNOWN);
}
}

private void refresh() throws Exception {
private void refresh() throws IOException, JsonSyntaxException {
IammeterConfiguration config = this.config;
try {
IammeterConfiguration config = getConfiguration();
logger.trace("Starting refresh handler");
String httpMethod = "GET";
String url = "http://admin:admin@" + config.host + ":" + config.port + "/monitorjson";
Expand All @@ -121,7 +122,7 @@ private void refresh() throws Exception {
}
for (IammeterWEM3080Channel channelConfig : IammeterWEM3080Channel.values()) {
Channel channel = getThing().getChannel(channelConfig.getId());
if (channel != null){
if (channel != null) {
channelProfix = IammeterBindingConstants.THING_TYPE_POWERMETER + ":"
+ channel.getUID().getThingUID().getId();
State state = getDecimal(
Expand All @@ -133,7 +134,7 @@ private void refresh() throws Exception {
keyWord = "Datas";
for (IammeterWEM3080TChannel channelConfig : IammeterWEM3080TChannel.values()) {
Channel channel = getThing().getChannel(channelConfig.getId());
if (channel != null){
if (channel != null) {
State state = getDecimal(iammeterData.get(keyWord).getAsJsonArray().get(channelConfig.getRow())
.getAsJsonArray().get(channelConfig.getCol()).toString());
updateState(channel.getUID(), state);
Expand All @@ -145,15 +146,14 @@ private void refresh() throws Exception {
thingStructureChanged(channelProfix);
}
}
stream.close();
updateStatus(ThingStatus.ONLINE);
// Very rudimentary Exception differentiation
// Very rudimentary Exception differentiation
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Communication error with the device: " + e.getMessage());
} catch (JsonSyntaxException je) {
logger.warn("Invalid JSON when refreshing source {}: {}", getThing().getUID(), je.getMessage());
} catch (Exception e) {
logger.warn("Error refreshing source {}: {}", getThing().getUID(), e.getMessage(), e);
}
}

Expand Down Expand Up @@ -185,19 +185,24 @@ protected void thingStructureChanged(String channelProfix) {

private State getDecimal(String value) {
try {
return new DecimalType(new BigDecimal(value));
return QuantityType.valueOf(Float.parseFloat(value), SmartHomeUnits.VOLT);
} catch (NumberFormatException e) {
return UnDefType.UNDEF;
}
}

@Override
public void dispose() {
ScheduledFuture<?> refreshJob = this.refreshJob;
if (refreshJob != null && !refreshJob.isCancelled()) {
refreshJob.cancel(true);
refreshJob = null;
}
super.dispose();
}


@NonNullByDefault
public IammeterConfiguration getConfiguration() {
return this.getConfigAs(IammeterConfiguration.class);
}

}
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.iammeter.internal;

import javax.measure.Unit;

/**
* The {@link IammeterWEM3080Channel} Enum defines common constants, which are
* used across the whole binding.
Expand All @@ -28,10 +30,12 @@ public enum IammeterWEM3080Channel {

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

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

public String getId() {
Expand All @@ -41,4 +45,8 @@ public String getId() {
public int getIndex() {
return index;
}

public Unit<?> getUnit() {
return unit;
}
}
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.iammeter.internal;

import javax.measure.Unit;

/**
* The {@link IammeterWEM3080TChannel} Enum defines common constants, which are
* used across the whole binding.
Expand Down Expand Up @@ -45,11 +47,13 @@ public enum IammeterWEM3080TChannel {
private final String id;
private final int row;
private final int col;
private final Unit<?> unit;

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

public String getId() {
Expand All @@ -63,4 +67,8 @@ public int getRow() {
public int getCol() {
return col;
}

public Unit<?> getUnit() {
return unit;
}
}
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">

<name>Iammeter Binding</name>
<description>This is the binding for Iammeter.</description>
<author>yang bo</author>
<description>The Iammeter binding pull your Iammeter power meter data from LAN.</description>
<author>Yang Bo</author>

</binding:binding>

This file was deleted.

0 comments on commit 30cf5bf

Please sign in to comment.