Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve DMX binding (#4455)
* refactor Channel to DmxChannel to avoid confusion
* make FadeAction complete on infinite hold, add tests for FadeAction

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
  • Loading branch information
J-N-K authored and sjsf committed Nov 1, 2017
1 parent ebd2a3a commit c567375
Show file tree
Hide file tree
Showing 19 changed files with 342 additions and 162 deletions.
Expand Up @@ -17,7 +17,7 @@

import org.eclipse.smarthome.binding.dmx.handler.ArtnetBridgeHandler;
import org.eclipse.smarthome.binding.dmx.internal.DmxBridgeHandler;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseChannel;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ManagedThingProvider;
Expand Down Expand Up @@ -118,8 +118,8 @@ public void retrievingOfChannels() {
managedThingProvider.add(thing);
DmxBridgeHandler bridgeHandler = (DmxBridgeHandler) bridge.getHandler();

BaseChannel channel = new BaseChannel(TEST_UNIVERSE, TEST_CHANNEL);
BaseChannel returnedChannel = bridgeHandler.getDmxChannel(channel, thing);
BaseDmxChannel channel = new BaseDmxChannel(TEST_UNIVERSE, TEST_CHANNEL);
BaseDmxChannel returnedChannel = bridgeHandler.getDmxChannel(channel, thing);

Integer channelId = returnedChannel.getChannelId();
assertThat(channelId, is(TEST_CHANNEL));
Expand Down
Expand Up @@ -17,7 +17,7 @@

import org.eclipse.smarthome.binding.dmx.handler.Lib485BridgeHandler;
import org.eclipse.smarthome.binding.dmx.internal.DmxBridgeHandler;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseChannel;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ManagedThingProvider;
Expand Down Expand Up @@ -101,8 +101,8 @@ public void retrievingOfChannels() {
managedThingProvider.add(thing);
DmxBridgeHandler bridgeHandler = (DmxBridgeHandler) bridge.getHandler();

BaseChannel channel = new BaseChannel(0, TEST_CHANNEL);
BaseChannel returnedChannel = bridgeHandler.getDmxChannel(channel, thing);
BaseDmxChannel channel = new BaseDmxChannel(0, TEST_CHANNEL);
BaseDmxChannel returnedChannel = bridgeHandler.getDmxChannel(channel, thing);

Integer channelId = returnedChannel.getChannelId();
assertThat(channelId, is(TEST_CHANNEL));
Expand Down
Expand Up @@ -17,7 +17,7 @@

import org.eclipse.smarthome.binding.dmx.handler.SacnBridgeHandler;
import org.eclipse.smarthome.binding.dmx.internal.DmxBridgeHandler;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseChannel;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ManagedThingProvider;
Expand Down Expand Up @@ -118,8 +118,8 @@ public void retrievingOfChannels() {
managedThingProvider.add(thing);
DmxBridgeHandler bridgeHandler = (DmxBridgeHandler) bridge.getHandler();

BaseChannel channel = new BaseChannel(TEST_UNIVERSE, TEST_CHANNEL);
BaseChannel returnedChannel = bridgeHandler.getDmxChannel(channel, thing);
BaseDmxChannel channel = new BaseDmxChannel(TEST_UNIVERSE, TEST_CHANNEL);
BaseDmxChannel returnedChannel = bridgeHandler.getDmxChannel(channel, thing);

Integer channelId = returnedChannel.getChannelId();
assertThat(channelId, is(TEST_CHANNEL));
Expand Down
@@ -0,0 +1,143 @@
/**
* Copyright (c) 2014-2017 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.eclipse.smarthome.binding.dmx.internal;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.eclipse.smarthome.binding.dmx.internal.action.ActionState;
import org.eclipse.smarthome.binding.dmx.internal.action.FadeAction;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.DmxChannel;
import org.junit.Test;

/**
* Tests cases FadeAction
*
* @author Jan N. Klug - Initial contribution
*/
public class FadeActionTest {

private static final int testValue = 200;
private static final int testFadeTime = 1000;
private static final int testHoldTime = 1000;

@Test
public void checkWithFadingWithoutHold() {
FadeAction fadeAction = new FadeAction(testFadeTime, testValue, 0);
DmxChannel testChannel = new DmxChannel(0, 1);
testChannel.setValue(0);

long startTime = System.currentTimeMillis();

assertThat(fadeAction.getState(), is(ActionState.WAITING));
assertThat(fadeAction.getNewValue(testChannel, startTime), is(0));
assertThat(fadeAction.getState(), is(ActionState.RUNNING));
assertThat(fadeAction.getNewValue(testChannel, startTime + testFadeTime / 2), is(256 * testValue / 2));
assertThat(fadeAction.getNewValue(testChannel, startTime + 1000), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.COMPLETED));

fadeAction.reset();
assertThat(fadeAction.getState(), is(ActionState.WAITING));
}

@Test
public void checkWithFadingWithHold() {
FadeAction fadeAction = new FadeAction(testFadeTime, testValue, testHoldTime);
DmxChannel testChannel = new DmxChannel(0, 1);
testChannel.setValue(0);

long startTime = System.currentTimeMillis();

assertThat(fadeAction.getState(), is(ActionState.WAITING));
assertThat(fadeAction.getNewValue(testChannel, startTime), is(0));
assertThat(fadeAction.getState(), is(ActionState.RUNNING));
assertThat(fadeAction.getNewValue(testChannel, startTime + testFadeTime / 2), is(256 * testValue / 2));
assertThat(fadeAction.getNewValue(testChannel, startTime + testFadeTime), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.RUNNING));
assertThat(fadeAction.getNewValue(testChannel, startTime + testFadeTime + testHoldTime / 2),
is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.RUNNING));
assertThat(fadeAction.getNewValue(testChannel, startTime + testFadeTime + testHoldTime), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.COMPLETED));

fadeAction.reset();
assertThat(fadeAction.getState(), is(ActionState.WAITING));
}

@Test
public void checkWithFadingWithInfiniteHold() {
FadeAction fadeAction = new FadeAction(testFadeTime, testValue, -1);
DmxChannel testChannel = new DmxChannel(0, 1);
testChannel.setValue(0);

long startTime = System.currentTimeMillis();

assertThat(fadeAction.getState(), is(ActionState.WAITING));
assertThat(fadeAction.getNewValue(testChannel, startTime), is(0));
assertThat(fadeAction.getState(), is(ActionState.RUNNING));
assertThat(fadeAction.getNewValue(testChannel, startTime + testFadeTime / 2), is(256 * testValue / 2));
assertThat(fadeAction.getNewValue(testChannel, startTime + testFadeTime), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.COMPLETEDFINAL));

fadeAction.reset();
assertThat(fadeAction.getState(), is(ActionState.WAITING));
}

@Test
public void checkWithoutFadingWithHold() {
FadeAction fadeAction = new FadeAction(0, testValue, testHoldTime);
DmxChannel testChannel = new DmxChannel(0, 1);
testChannel.setValue(0);

long startTime = System.currentTimeMillis();

assertThat(fadeAction.getState(), is(ActionState.WAITING));
assertThat(fadeAction.getNewValue(testChannel, startTime), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.RUNNING));
assertThat(fadeAction.getNewValue(testChannel, startTime + testHoldTime / 2), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.RUNNING));
assertThat(fadeAction.getNewValue(testChannel, startTime + testHoldTime), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.COMPLETED));

fadeAction.reset();
assertThat(fadeAction.getState(), is(ActionState.WAITING));
}

@Test
public void checkWithoutFadingWithoutHold() {
FadeAction fadeAction = new FadeAction(0, testValue, 0);
DmxChannel testChannel = new DmxChannel(0, 1);
testChannel.setValue(0);

long startTime = System.currentTimeMillis();

assertThat(fadeAction.getState(), is(ActionState.WAITING));
assertThat(fadeAction.getNewValue(testChannel, startTime), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.COMPLETED));

fadeAction.reset();
assertThat(fadeAction.getState(), is(ActionState.WAITING));
}

@Test
public void checkWithoutFadingWithInfiniteHold() {
FadeAction fadeAction = new FadeAction(0, testValue, -1);
DmxChannel testChannel = new DmxChannel(0, 1);
testChannel.setValue(0);

long startTime = System.currentTimeMillis();

assertThat(fadeAction.getState(), is(ActionState.WAITING));
assertThat(fadeAction.getNewValue(testChannel, startTime), is(256 * testValue));
assertThat(fadeAction.getState(), is(ActionState.COMPLETEDFINAL));

fadeAction.reset();
assertThat(fadeAction.getState(), is(ActionState.WAITING));
}

}
Expand Up @@ -11,7 +11,7 @@
import static org.junit.Assert.assertThat;

import org.eclipse.smarthome.binding.dmx.internal.Util;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.Channel;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.DmxChannel;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.junit.Test;

Expand All @@ -26,11 +26,11 @@ public class UtilTest {
public void coercingOfDmxValues() {
// overrange
int value = Util.toDmxValue(300);
assertThat(value, is(Channel.MAX_VALUE));
assertThat(value, is(DmxChannel.MAX_VALUE));

// underrange
value = Util.toDmxValue(-1);
assertThat(value, is(Channel.MIN_VALUE));
assertThat(value, is(DmxChannel.MIN_VALUE));

// inrange
value = Util.toDmxValue(100);
Expand Down
Expand Up @@ -12,7 +12,7 @@

import java.util.List;

import org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseChannel;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel;
import org.junit.Test;

/**
Expand All @@ -25,15 +25,15 @@ public class BaseChannelTest {
@Test
public void creatingBaseChannelFromIntegers() {
// overrange
BaseChannel channel = new BaseChannel(0, 600);
assertThat(channel.getChannelId(), is(BaseChannel.MAX_CHANNEL_ID));
BaseDmxChannel channel = new BaseDmxChannel(0, 600);
assertThat(channel.getChannelId(), is(BaseDmxChannel.MAX_CHANNEL_ID));

// underrange
channel = new BaseChannel(0, -1);
assertThat(channel.getChannelId(), is(BaseChannel.MIN_CHANNEL_ID));
channel = new BaseDmxChannel(0, -1);
assertThat(channel.getChannelId(), is(BaseDmxChannel.MIN_CHANNEL_ID));

// inrange & universe
channel = new BaseChannel(5, 100);
channel = new BaseDmxChannel(5, 100);
assertThat(channel.getChannelId(), is(100));
assertThat(channel.getUniverseId(), is(5));

Expand All @@ -44,17 +44,17 @@ public void creatingBaseChannelFromIntegers() {

@Test
public void creatingBaseChannelfromBaseChannel() {
BaseChannel baseChannel = new BaseChannel(5, 100);
BaseChannel copyChannel = new BaseChannel(baseChannel);
BaseDmxChannel baseChannel = new BaseDmxChannel(5, 100);
BaseDmxChannel copyChannel = new BaseDmxChannel(baseChannel);

assertThat(copyChannel.getChannelId(), is(100));
assertThat(copyChannel.getUniverseId(), is(5));
}

@Test
public void comparingChannels() {
BaseChannel channel1 = new BaseChannel(5, 100);
BaseChannel channel2 = new BaseChannel(7, 140);
BaseDmxChannel channel1 = new BaseDmxChannel(5, 100);
BaseDmxChannel channel2 = new BaseDmxChannel(7, 140);

assertThat(channel1.compareTo(channel2), is(-1));
assertThat(channel2.compareTo(channel1), is(1));
Expand All @@ -64,31 +64,31 @@ public void comparingChannels() {
@Test
public void stringConversion() {
// to string
BaseChannel baseChannel = new BaseChannel(5, 100);
BaseDmxChannel baseChannel = new BaseDmxChannel(5, 100);
assertThat(baseChannel.toString(), is(equalTo("5:100")));

// single channel from string with universe
String parseString = new String("2:100");
List<BaseChannel> channelList = BaseChannel.fromString(parseString, 0);
List<BaseDmxChannel> channelList = BaseDmxChannel.fromString(parseString, 0);
assertThat(channelList.size(), is(1));
assertThat(channelList.get(0).toString(), is(equalTo("2:100")));

// single channel from string without universe
parseString = new String("100");
channelList = BaseChannel.fromString(parseString, 2);
channelList = BaseDmxChannel.fromString(parseString, 2);
assertThat(channelList.size(), is(1));
assertThat(channelList.get(0).toString(), is(equalTo("2:100")));

// two channels with channel width
parseString = new String("100/2");
channelList = BaseChannel.fromString(parseString, 2);
channelList = BaseDmxChannel.fromString(parseString, 2);
assertThat(channelList.size(), is(2));
assertThat(channelList.get(0).toString(), is(equalTo("2:100")));
assertThat(channelList.get(1).toString(), is(equalTo("2:101")));

// to channels with comma
parseString = new String("100,102");
channelList = BaseChannel.fromString(parseString, 2);
channelList = BaseDmxChannel.fromString(parseString, 2);
assertThat(channelList.size(), is(2));
assertThat(channelList.get(0).toString(), is(equalTo("2:100")));
assertThat(channelList.get(1).toString(), is(equalTo("2:102")));
Expand Down
Expand Up @@ -12,7 +12,7 @@

import org.eclipse.smarthome.binding.dmx.internal.action.BaseAction;
import org.eclipse.smarthome.binding.dmx.internal.action.FadeAction;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.Channel;
import org.eclipse.smarthome.binding.dmx.internal.multiverse.DmxChannel;
import org.junit.Test;

/**
Expand All @@ -24,23 +24,23 @@ public class ChannelTest {

@Test
public void setAndGetValues() {
Channel channel = new Channel(0, 1);
DmxChannel channel = new DmxChannel(0, 1);

// value is set
channel.setValue(100);
assertThat(channel.getValue(), is(100));

// limits are observed
channel.setValue(300);
assertThat(channel.getValue(), is(Channel.MAX_VALUE));
assertThat(channel.getValue(), is(DmxChannel.MAX_VALUE));

channel.setValue(-1);
assertThat(channel.getValue(), is(Channel.MIN_VALUE));
assertThat(channel.getValue(), is(DmxChannel.MIN_VALUE));
}

@Test
public void setAndClearAction() {
Channel channel = new Channel(0, 1);
DmxChannel channel = new DmxChannel(0, 1);
BaseAction action = new FadeAction(0, 100, -1);

// has action
Expand All @@ -54,7 +54,7 @@ public void setAndClearAction() {

@Test
public void suspendAndResumeAction() {
Channel channel = new Channel(0, 1);
DmxChannel channel = new DmxChannel(0, 1);
BaseAction action = new FadeAction(0, 100, -1);

// has action
Expand Down

0 comments on commit c567375

Please sign in to comment.