Skip to content

Commit

Permalink
add state format
Browse files Browse the repository at this point in the history
add disclaimer and javadoc to each class
add scheduler for refreshing the state and values of the thing
  • Loading branch information
fharni committed Dec 1, 2016
1 parent 2bd95ea commit 2a12218
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@
<channel-type id="current">
<item-type>Number</item-type>
<label>Current</label>
<description>Current of the Switch</description>
<description>Current of the Switch in ampere</description>
<state readOnly="true" pattern="%.3f A"/>
</channel-type>

<channel-type id="power">
<item-type>Number</item-type>
<label>Power</label>
<description>Power of the Switch</description>
<description>Power of the Switch in watt</description>
<state readOnly="true" pattern="%.2f W"/>
</channel-type>

</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<!--
Copyright (c) 2014-2016 by the respective copyright holders.
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* 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
Expand All @@ -10,7 +11,7 @@
import org.eclipse.smarthome.core.thing.ThingTypeUID;

/**
* The {@link EdimaxBinding} class defines common constants, which are
* The {@link EdimaxBindingConstants} class defines common constants, which are
* used across the whole binding.
*
* @author Falk Harnisch - Initial contribution
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,76 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* 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.edimax.configuration;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
* Holds the configuration for the thing.
*
* @author Falk Harnisch - Initial contribution
*/
public class EdimaxConfiguration {

private String ipAddress;
private String username;
private String password;

/**
* Returns the IP address of the thing.
*
* @return the IP address
*/
public String getIpAddress() {
return ipAddress;
}

/**
* Sets the IP address of the thing.
*
* @param The ipAddress IP address in format xxx.xxx.xxx.xxx
*/
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}

/**
* Returns the username of the thing.
*
* @return the username
*/
public String getUsername() {
return username;
}

/**
* Sets the username of the thing.
*
* @param username The Username. Default is 'admin'
*/
public void setUsername(String username) {
this.username = username;
}

/**
* Returns the password of the thing.
*
* @return The password
*/
public String getPassword() {
return password;
}

/**
* Sets the password of the thing.
*
* @param password The password. Default is '1234'
*/
public void setPassword(String password) {
this.password = password;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* 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.edimax.handler;

import org.eclipse.smarthome.core.thing.Thing;

/**
* The {@link Edimax1101Handler} is responsible for handling commands for the
* Edimax 1101, which are sent to one of the channels. It is equal to to base
* EdimaxHandler.
*
* @author Falk Harnisch - Initial contribution
*/
public class Edimax1101Handler extends EdimaxHandler {

/**
* Constructor the only calls the super constructor.
*
* @param thing The thing
*/
public Edimax1101Handler(Thing thing) {
super(thing);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* 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.edimax.handler;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.thing.ChannelUID;
Expand All @@ -16,10 +26,23 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link Edimax2101Handler} is responsible for handling commands for the
* Edimax 2101, which are sent to one of the channels.
*
* @author Falk Harnisch - Initial contribution
*/
public class Edimax2101Handler extends EdimaxHandler {

private Logger logger = LoggerFactory.getLogger(Edimax2101Handler.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Edimax2101Handler.class);

private ScheduledFuture<?> pollingJob;

/**
* Constructor the only calls the super constructor.
*
* @param thing The thing
*/
public Edimax2101Handler(Thing thing) {
super(thing);
}
Expand All @@ -31,13 +54,15 @@ public void handleCommand(ChannelUID channelUID, Command command) {
if (channelUID.getId().equals(EdimaxBindingConstants.CURRENT)) {
if (command instanceof RefreshType) {
final DecimalType current = new DecimalType(getCurrent());
logger.debug("Current: " + current);
LOGGER.debug("Current: " + current);
updateState(channelUID, current);
}
}
if (channelUID.getId().equals(EdimaxBindingConstants.POWER)) {
if (command instanceof RefreshType) {
final DecimalType power = new DecimalType(getPower());
logger.debug("Current: " + power);
LOGGER.debug("Power: " + power);
updateState(channelUID, power);
}
}
updateStatus(ThingStatus.ONLINE);
Expand All @@ -47,11 +72,31 @@ public void handleCommand(ChannelUID channelUID, Command command) {

}

@Override
public void initialize() {
super.initialize();
final Runnable runnable = () -> refreshValues();
pollingJob = scheduler.scheduleWithFixedDelay(runnable, 0, 30, TimeUnit.SECONDS);
}

@Override
public void dispose() {
pollingJob.cancel(true);
}

/**
* Sends command to the channel to refresh current and power.
*/
private void refreshValues() {
handleCommand(getThing().getChannel(EdimaxBindingConstants.CURRENT).getUID(), RefreshType.REFRESH);
handleCommand(getThing().getChannel(EdimaxBindingConstants.POWER).getUID(), RefreshType.REFRESH);
}

/**
* Returns the current.
*
* @return
* @throws IOException
* @return Current in Ampere
* @throws IOException if communication to device fails
*/
public BigDecimal getCurrent() throws IOException {
final GetCurrent getC = new GetCurrent();
Expand All @@ -61,8 +106,8 @@ public BigDecimal getCurrent() throws IOException {
/**
* Gets the actual power.
*
* @return
* @throws IOExceptionif
* @return Actual power in Watt
* @throws IOException if communication to device fails
*/
public BigDecimal getPower() throws IOException {
final GetPower getC = new GetPower();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* 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
Expand All @@ -8,6 +9,8 @@
package org.openhab.binding.edimax.handler;

import java.io.IOException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.thing.ChannelUID;
Expand All @@ -33,23 +36,33 @@
*/
public class EdimaxHandler extends BaseThingHandler {

private static final int PORT = 10000;
private static final Logger LOGGER = LoggerFactory.getLogger(EdimaxHandler.class);

private Logger logger = LoggerFactory.getLogger(EdimaxHandler.class);
private static final int PORT = 10000;

/**
* Connection Information to the device.
*/
protected ConnectionInformation ci;

private ScheduledFuture<?> pollingJob;

/**
* Constructor for the edimax things.
*
* @param thing The thing
*/
public EdimaxHandler(Thing thing) {
super(thing);
}

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("command for " + channelUID.getAsString() + ": " + command.toString());
LOGGER.debug("command for " + channelUID.getAsString() + ": " + command.toString());
try {
if (channelUID.getId().equals(EdimaxBindingConstants.SWITCH)) {
if (command instanceof RefreshType) {
logger.debug("State: " + getState().toString());
LOGGER.debug("State: " + getState().toString());
if (getState()) {
updateState(channelUID, OnOffType.ON);
} else {
Expand All @@ -72,22 +85,31 @@ public void handleCommand(ChannelUID channelUID, Command command) {

@Override
public void initialize() {
logger.debug("Initializing Things");
LOGGER.debug("Initializing Things");
final EdimaxConfiguration configuration = this.getConfigAs(EdimaxConfiguration.class);
final String completeUrl = completeURL(configuration.getIpAddress());
ci = new ConnectionInformation(configuration.getUsername(), configuration.getPassword(), completeUrl, PORT);
updateStatus(ThingStatus.ONLINE);

final Runnable runnable = () -> handleCommand(getThing().getChannel(EdimaxBindingConstants.SWITCH).getUID(),
RefreshType.REFRESH);
pollingJob = scheduler.scheduleWithFixedDelay(runnable, 30, 30, TimeUnit.SECONDS);
}

@Override
public void dispose() {
pollingJob.cancel(true);
}

private static String completeURL(String anIp) {
return "http://" + anIp;
}

/**
* Returns state for device with given IP.
* Returns state for device.
*
* @return
* @throws IOException
* @return The on/off state of the thing
* @throws IOException if the communication fails
*/
private Boolean getState() throws IOException {
final GetState getS = new GetState();
Expand All @@ -97,9 +119,9 @@ private Boolean getState() throws IOException {
/**
* Switch to.
*
* @param newState
* @return
* @throws IOException
* @param newState new state for the thing
* @return True if device is turned on. Otherwise false.
* @throws IOException if the communication fails
*/
public Boolean switchState(Boolean newState) throws IOException {
final SetState setS = new SetState(newState);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2010-2015, openHAB.org and others.
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -12,7 +12,6 @@
* Information about a connect.
*
* @author Heinz
*
*/
public class ConnectionInformation {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* 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
Expand Down Expand Up @@ -28,7 +29,7 @@
*/
public class EdimaxHandlerFactory extends BaseThingHandlerFactory {

private final static Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<ThingTypeUID>(
private final static Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>(
Arrays.asList(THING_TYPE_SP1101W, THING_TYPE_SP2101W));

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2010-2015, openHAB.org and others.
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down
Loading

0 comments on commit 2a12218

Please sign in to comment.