Search in sources :

Example 1 with DmxCommand

use of org.openhab.binding.dmx.internal.cmd.DmxCommand in project openhab1-addons by openhab.

the class DmxSwitchItemTest method canHaveCustomCommandConfiguration.

@SuppressWarnings("unchecked")
@Test
public void canHaveCustomCommandConfiguration() throws BindingConfigParseException {
    // test valid configurations
    DmxItem item = getItemInstance("CHANNEL[7/3:1000] ON[FADE|0:255,255,255:30000|5000:0,0,0:-1]");
    DmxCommand cmd = ((Map<String, DmxCommand>) Whitebox.getInternalState(item, "customCommands")).get("ON");
    assertTrue(cmd instanceof DmxFadeCommand);
    getItemInstance("CHANNEL[7/4] ,ON[SFADE|0:255,255,255:30000|5000:0,0,0:-1]");
    cmd = ((Map<String, DmxCommand>) Whitebox.getInternalState(item, "customCommands")).get("ON");
    assertTrue(cmd instanceof DmxFadeCommand);
    item = getItemInstance("CHANNEL[1/18], ON[FADE|0:255,255,255:125|0:0,0,255:125|0:255,255,255:125|0:0,0,255:125|0:255,255,255:125|0:0,0,255:125|0:255,255,255:125|0:0,0,255:125|0:255,255,255:125|0:0,0,255:125|0:0,0,255:-1]");
    cmd = ((Map<String, DmxCommand>) Whitebox.getInternalState(item, "customCommands")).get("ON");
    assertTrue(cmd instanceof DmxFadeCommand);
    item = getItemInstance("CHANNEL[1/18], ON[SFADE|0:255,255,255:125|0:0,0,255:125|0:255,255,255:125|0:0,0,255:125|0:255,255,255:125|0:0,0,255:125|0:255,255,255:125|0:0,0,255:125|0:255,255,255:125|0:0,0,255:125|0:0,0,255:-1]");
    cmd = ((Map<String, DmxCommand>) Whitebox.getInternalState(item, "customCommands")).get("ON");
    assertTrue(cmd instanceof DmxSuspendingFadeCommand);
    item = getItemInstance("CHANNEL[13/3], 0[FADE|2000:127,36,127:0|2000:0,0,127:0|2000:127,0,0:0], 1[SFADE|500:127,36,127:0|500:0,0,127:0|500:127,0,0:0], 2[FADE|200:127,36,127:300|200:0,0,127:300|200:127,0,0:300]");
    cmd = ((Map<String, DmxCommand>) Whitebox.getInternalState(item, "customCommands")).get("0");
    assertTrue(cmd instanceof DmxFadeCommand);
    cmd = ((Map<String, DmxCommand>) Whitebox.getInternalState(item, "customCommands")).get("1");
    assertTrue(cmd instanceof DmxSuspendingFadeCommand);
    // test invalid configurations
    try {
        item = getItemInstance("CHANNEL[7:1000], ON[FADE|1,2,5]");
        fail("Missing exception");
    } catch (BindingConfigParseException e) {
        e.printStackTrace();
    }
    try {
        item = getItemInstance("CHANNEL[7:1000] ON[FADE|1,2,5");
        fail("Missing exception");
    } catch (BindingConfigParseException e) {
        e.printStackTrace();
    }
    try {
        item = getItemInstance("CHANNEL[7:1000]ONFADE|1,2,5]");
        fail("Missing exception");
    } catch (BindingConfigParseException e) {
        e.printStackTrace();
    }
    try {
        item = getItemInstance("CHANNEL[7:1000] ON[FADE|1,2,5]");
        fail("Missing exception");
    } catch (BindingConfigParseException e) {
        e.printStackTrace();
    }
    try {
        item = getItemInstance("CHANNEL[7/3:1000], OR[FADE|0:255,255,255|5000:0,0,0:-1]");
        fail("Missing exception");
    } catch (BindingConfigParseException e) {
        e.printStackTrace();
    }
}
Also used : DmxFadeCommand(org.openhab.binding.dmx.internal.cmd.DmxFadeCommand) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DmxCommand(org.openhab.binding.dmx.internal.cmd.DmxCommand) Map(java.util.Map) DmxSuspendingFadeCommand(org.openhab.binding.dmx.internal.cmd.DmxSuspendingFadeCommand) Test(org.junit.Test)

Example 2 with DmxCommand

use of org.openhab.binding.dmx.internal.cmd.DmxCommand in project openhab1-addons by openhab.

the class DmxSwitchItem method processCommand.

/**
     * {@inheritDoc}
     */
@Override
public void processCommand(DmxService service, Command command) {
    // process regular on/off switch
    if (command instanceof OnOffType && !isRedefinedByCustomCommand(command)) {
        if (OnOffType.ON.equals(command)) {
            boolean hasValue = false;
            for (int channelId : channels) {
                service.enableChannel(channelId);
                if (service.getChannelValue(channelId) > 0) {
                    hasValue = true;
                }
            }
            if (!hasValue) {
                // switch to max value if there is no light..
                for (int channelId : channels) {
                    service.setChannelValue(channelId, DmxChannel.DMX_MAX_VALUE);
                }
            }
        } else {
            for (int channelId : channels) {
                service.disableChannel(channelId);
            }
        }
        return;
    }
    // process custom commands if they are available
    if (isRedefinedByCustomCommand(command)) {
        DmxCommand dmxCommand = customCommands.get(command.toString());
        dmxCommand.execute(service);
        return;
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) DmxCommand(org.openhab.binding.dmx.internal.cmd.DmxCommand)

Example 3 with DmxCommand

use of org.openhab.binding.dmx.internal.cmd.DmxCommand in project openhab1-addons by openhab.

the class DmxSwitchItemTest method canCallCustomCommand.

@Test
@SuppressWarnings("unchecked")
public void canCallCustomCommand() throws BindingConfigParseException {
    DmxItem item = getValidInstance();
    DmxService service = Mockito.mock(DmxService.class);
    DmxCommand cmd = Mockito.mock(DmxCommand.class);
    Map<String, DmxCommand> commands = (Map<String, DmxCommand>) Whitebox.getInternalState(item, "customCommands");
    commands.put("ON", cmd);
    item.processCommand(service, OnOffType.ON);
    Mockito.verify(cmd).execute(service);
}
Also used : DmxService(org.openhab.binding.dmx.DmxService) DmxCommand(org.openhab.binding.dmx.internal.cmd.DmxCommand) Map(java.util.Map) Test(org.junit.Test)

Aggregations

DmxCommand (org.openhab.binding.dmx.internal.cmd.DmxCommand)3 Map (java.util.Map)2 Test (org.junit.Test)2 DmxService (org.openhab.binding.dmx.DmxService)1 DmxFadeCommand (org.openhab.binding.dmx.internal.cmd.DmxFadeCommand)1 DmxSuspendingFadeCommand (org.openhab.binding.dmx.internal.cmd.DmxSuspendingFadeCommand)1 OnOffType (org.openhab.core.library.types.OnOffType)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1