LutronHomeWorksCommandBuilder implements CommandBuilder { @Override public Command build(Element element) { String addressAsString = null; String commandAsString = null; String sceneAsString = null; String keyAsString = null; String levelAsString = null; LutronHomeWorksAddress address = null; Integer scene = null; Integer key = null; Integer level = null; @SuppressWarnings("unchecked") List<Element> propertyElements = element.getChildren(XML_ELEMENT_PROPERTY, element.getNamespace()); for (Element el : propertyElements) { String propertyName = el.getAttributeValue(XML_ATTRIBUTENAME_NAME); String propertyValue = el.getAttributeValue(XML_ATTRIBUTENAME_VALUE); if (LUTRON_XMLPROPERTY_ADDRESS.equalsIgnoreCase(propertyName)) { addressAsString = propertyValue; } else if (LUTRON_XMLPROPERTY_COMMAND.equalsIgnoreCase(propertyName)) { commandAsString = propertyValue; } else if (LUTRON_XMLPROPERTY_KEY.equalsIgnoreCase(propertyName)) { keyAsString = propertyValue; } else if (LUTRON_XMLPROPERTY_SCENE.equalsIgnoreCase(propertyName)) { sceneAsString = propertyValue; } else if (LUTRON_XMLPROPERTY_LEVEL.equalsIgnoreCase(propertyName)) { levelAsString = propertyValue; } else { log.warn("Unknown Lutron HomeWorks property '<" + XML_ELEMENT_PROPERTY + " " + XML_ATTRIBUTENAME_NAME + " = \"" + propertyName + "\" " + XML_ATTRIBUTENAME_VALUE + " = \"" + propertyValue + "\"/>'."); } } if (commandAsString == null || "".equals(commandAsString)) { throw new NoSuchCommandException("Lutron HomeWorks command must have a '" + LUTRON_XMLPROPERTY_COMMAND + "' property."); } if (addressAsString != null && !"".equals(addressAsString)) { log.info("Will attemp to build address"); try { address = new LutronHomeWorksAddress(addressAsString.trim()); } catch (InvalidLutronHomeWorksAddressException e) { log.error("Invalid Lutron HomeWorks address", e); throw new NoSuchCommandException(e.getMessage(), e); } } if (sceneAsString != null && !"".equals(sceneAsString)) { try { scene = Integer.parseInt(sceneAsString); } catch (NumberFormatException e) { log.error("Invalid scene number", e); throw new NoSuchCommandException(e.getMessage(), e); } } if (keyAsString != null && !"".equals(keyAsString)) { try { key = Integer.parseInt(keyAsString); } catch (NumberFormatException e) { log.error("Invalid key number", e); throw new NoSuchCommandException(e.getMessage(), e); } } if (levelAsString != null && !"".equals(levelAsString)) { try { level = Integer.parseInt(levelAsString); } catch (NumberFormatException e) { log.error("Invalid level number", e); throw new NoSuchCommandException(e.getMessage(), e); } } if (level == null) { String paramValue = element.getAttributeValue(Command.DYNAMIC_VALUE_ATTR_NAME); if (paramValue != null && !paramValue.equals("")) { try { level = Integer.parseInt(paramValue); } catch (NumberFormatException e) { log.error("Invalid param value", e); throw new NoSuchCommandException(e.getMessage(), e); } } } Command cmd = LutronHomeWorksCommand.createCommand(commandAsString, gateway, address, scene, key, level); log.info("Created Lutron Command " + cmd + " for address '" + address + "'"); return cmd; } LutronHomeWorksCommandBuilder(); @Override Command build(Element element); LutronHomeWorksGateway getGateway(); void setGateway(LutronHomeWorksGateway gateway); final static String LUTRON_LOG_CATEGORY; final static String LUTRON_XMLPROPERTY_ADDRESS; final static String LUTRON_XMLPROPERTY_COMMAND; final static String LUTRON_XMLPROPERTY_KEY; final static String LUTRON_XMLPROPERTY_SCENE; final static String LUTRON_XMLPROPERTY_LEVEL; }
@Test public void testDimmerCommands() { CommandBuilder cb = new LutronHomeWorksCommandBuilder(); Assert.assertTrue(cb.build(getCommandElement("RAISE", "1:4:1:1:1", null, null, null)) instanceof DimmerCommand); Assert.assertTrue(cb.build(getCommandElement("LOWER", "1:4:1:1:1", null, null, null)) instanceof DimmerCommand); Assert.assertTrue(cb.build(getCommandElement("STOP", "1:4:1:1:1", null, null, null)) instanceof DimmerCommand); Assert.assertTrue(cb.build(getCommandElement("FADE", "1:4:1:1:1", null, null, "10")) instanceof DimmerCommand); }
@Test(expected = NoSuchCommandException.class) public void testDimmerCommandInvalidAddress() { new LutronHomeWorksCommandBuilder().build(getCommandElement("RAISE", "1:8:1", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testDimmerCommandMissingAddress() { new LutronHomeWorksCommandBuilder().build(getCommandElement("RAISE", null, null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testDimmerFadeCommandMissingLevel() { new LutronHomeWorksCommandBuilder().build(getCommandElement("FADE", "1:4:1:1:1", null, null, null)); }
@Test public void testKeypadCommands() { CommandBuilder cb = new LutronHomeWorksCommandBuilder(); Assert.assertTrue(cb.build(getCommandElement("PRESS", "1:4:1", null, "2", null)) instanceof KeypadCommand); Assert.assertTrue(cb.build(getCommandElement("RELEASE", "1:4:1", null, "2", null)) instanceof KeypadCommand); Assert.assertTrue(cb.build(getCommandElement("HOLD", "1:4:1", null, "2", null)) instanceof KeypadCommand); Assert.assertTrue(cb.build(getCommandElement("DOUBLE_TAP", "1:4:1", null, "2", null)) instanceof KeypadCommand); Assert.assertTrue(cb.build(getCommandElement("STATUS_KEYPADLED", "1:4:1", null, "2", null)) instanceof KeypadCommand); }
@Test(expected = NoSuchCommandException.class) public void testKeypadCommandMissingAddress() { new LutronHomeWorksCommandBuilder().build(getCommandElement("PRESS", null, null, "2", null)); }
@Test(expected = NoSuchCommandException.class) public void testKeypadCommandInvalidAddress() { new LutronHomeWorksCommandBuilder().build(getCommandElement("PRESS", "1:4:1:1", null, "2", null)); }
@Test(expected = NoSuchCommandException.class) public void testKeypadCommandMissingKey() { new LutronHomeWorksCommandBuilder().build(getCommandElement("PRESS", "1:4:1:1:1", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testKeypadCommandInvalidKey() { new LutronHomeWorksCommandBuilder().build(getCommandElement("PRESS", "1:4:1:1:1", null, "hhj", null)); }
@Test public void testSceneCommands() { CommandBuilder cb = new LutronHomeWorksCommandBuilder(); Assert.assertTrue(cb.build(getCommandElement("SCENE", "1:4:1", "1", null, null)) instanceof GrafikEyeCommand); Assert.assertTrue(cb.build(getCommandElement("STATUS_SCENE", "1:4:1", "1", null, null)) instanceof GrafikEyeCommand); }
@Test(expected = NoSuchCommandException.class) public void testSceneCommandMissingAddress() { new LutronHomeWorksCommandBuilder().build(getCommandElement("SCENE", null, "3", null, null)); }
@Test(expected = NoSuchCommandException.class) public void testSceneCommandInvalidAddress() { new LutronHomeWorksCommandBuilder().build(getCommandElement("SCENE", "1:4:1:1", "3", null, null)); }
@Test(expected = NoSuchCommandException.class) public void testSceneCommandMissingScene() { new LutronHomeWorksCommandBuilder().build(getCommandElement("SCENE", "1:4:1", null, null, null)); }
@Test(expected = NoSuchCommandException.class) public void testSceneCommandInvalidScene() { new LutronHomeWorksCommandBuilder().build(getCommandElement("SCENE", "1:4:1", "hjhjj", null, null)); }
@Test(expected = NoSuchCommandException.class) public void testUnknwonCommand() { new LutronHomeWorksCommandBuilder().build(getCommandElement("DIMMER", null, null, null, null)); }