DateTimeCommandBuilder implements CommandBuilder { @SuppressWarnings("unchecked") @Override public Command build(Element element) { logger.debug("Building DateTime command"); List<Element> propertyEles = element.getChildren("property", element.getNamespace()); String latitude = null; String longitude = null; String timezone = null; String command = null; String format = null; String interval = null; Integer intervalInMillis = null; for (Element ele : propertyEles) { String elementName = ele.getAttributeValue(CommandBuilder.XML_ATTRIBUTENAME_NAME); String elementValue = ele.getAttributeValue(CommandBuilder.XML_ATTRIBUTENAME_VALUE); if (STR_ATTRIBUTE_NAME_LATITUDE.equals(elementName)) { latitude = elementValue; logger.debug("DateTime command: latitude = " + latitude); } else if (STR_ATTRIBUTE_NAME_LONGITUDE.equals(elementName)) { longitude = elementValue; logger.debug("DateTime command: longitude = " + longitude); } else if (STR_ATTRIBUTE_NAME_TIMEZONE.equals(elementName)) { timezone = elementValue; logger.debug("DateTime command: timezone = " + timezone); } else if (STR_ATTRIBUTE_NAME_COMMAND.equals(elementName)) { command = elementValue; logger.debug("DateTime command: command = " + command); } else if (STR_ATTRIBUTE_NAME_FORMAT.equals(elementName)) { format = elementValue; logger.debug("DateTime command: format = " + format); } else if (STR_ATTRIBUTE_NAME_POLLINGINTERVAL.equals(elementName)) { interval = elementValue; logger.debug("ShellExe Command: pollingInterval = " + interval); } } try { if (interval != null) { intervalInMillis = Integer.valueOf(Strings.convertPollingIntervalString(interval)); } else { intervalInMillis = 60000; } } catch (Exception e) { throw new NoSuchCommandException("Unable to create DateTime command, pollingInterval could not be converted into milliseconds", e); } if (command.equals("sunrise") || command.equals("sunset") || command.equals("minutesUntilSunrise") || command.equals("minutesUntilSunset") || command.equals("isDay") || command.equals("isNight")) { if (null == longitude || null == latitude) { throw new NoSuchCommandException("Unable to create DateTime command for sunrise/sunset related command, " + "missing configuration parameter(s)"); } } logger.debug("DateTime command created successfully"); return new DateTimeCommand(latitude, longitude, timezone, command, format, intervalInMillis); }  @SuppressWarnings("unchecked") @Override Command build(Element element); final static String DATETIME_PROTOCOL_LOG_CATEGORY; }
@Test public void testCommandBuilder() { Element ele = new Element("command"); ele.setAttribute("id", "1"); ele.setAttribute("protocol", "datetime"); ele.setAttribute(Command.DYNAMIC_VALUE_ATTR_NAME, "++"); Element nameProp = new Element("property"); nameProp.setAttribute("name", "command"); nameProp.setAttribute("value", "date"); Element cmdProp = new Element("property"); cmdProp.setAttribute("name", "timezone"); cmdProp.setAttribute("value", "Europe/Berlin"); ele.addContent(nameProp); ele.addContent(cmdProp); DateTimeCommand cmd = (DateTimeCommand)builder.build(ele); Assert.assertEquals("date", cmd.getCommand()); Assert.assertEquals("Europe/Berlin", cmd.getTimezone().getID()); }
@Test public void testCommandBuilderUnknownTimezone() { Element ele = new Element("command"); ele.setAttribute("id", "1"); ele.setAttribute("protocol", "datetime"); ele.setAttribute(Command.DYNAMIC_VALUE_ATTR_NAME, "++"); Element nameProp = new Element("property"); nameProp.setAttribute("name", "command"); nameProp.setAttribute("value", "date"); Element cmdProp = new Element("property"); cmdProp.setAttribute("name", "timezone"); cmdProp.setAttribute("value", "Europe/Bielefeld"); ele.addContent(nameProp); ele.addContent(cmdProp); DateTimeCommand cmd = (DateTimeCommand)builder.build(ele); Assert.assertEquals("date", cmd.getCommand()); Assert.assertEquals("GMT", cmd.getTimezone().getID()); }
@Test public void testCommandBuilderSunriseParams() { Element ele = new Element("command"); ele.setAttribute("id", "1"); ele.setAttribute("protocol", "datetim"); ele.setAttribute(Command.DYNAMIC_VALUE_ATTR_NAME, "++"); Element prop = new Element("property"); prop.setAttribute("name", "command"); prop.setAttribute("value", "sunrise"); ele.addContent(prop); prop = new Element("property"); prop.setAttribute("name", "latitude"); prop.setAttribute("value", "11.111"); ele.addContent(prop); prop = new Element("property"); prop.setAttribute("name", "longitude"); prop.setAttribute("value", "11.111"); ele.addContent(prop); prop = new Element("property"); prop.setAttribute("name", "timezone"); prop.setAttribute("value", "Europe/Berlin"); ele.addContent(prop); DateTimeCommand cmd = (DateTimeCommand)builder.build(ele); Assert.assertEquals("sunrise", cmd.getCommand()); Assert.assertEquals("Europe/Berlin", cmd.getTimezone().getID()); }
@Test (expected=NoSuchCommandException.class) public void testCommandBuilderSunriseMissingParam() { Element ele = new Element("command"); ele.setAttribute("id", "1"); ele.setAttribute("protocol", "datetim"); ele.setAttribute(Command.DYNAMIC_VALUE_ATTR_NAME, "++"); Element prop = new Element("property"); prop.setAttribute("name", "command"); prop.setAttribute("value", "sunrise"); ele.addContent(prop); prop = new Element("property"); prop.setAttribute("name", "latitude"); prop.setAttribute("value", "11.111"); ele.addContent(prop); prop = new Element("property"); prop.setAttribute("name", "timezone"); prop.setAttribute("value", "Europe/Berlin"); ele.addContent(prop); builder.build(ele); }