Skip to content

Commit

Permalink
Astro: Added datetime offsets and channel group labels (openhab#1917)
Browse files Browse the repository at this point in the history
* Added range start/end date time offset
* Updated readme
* Added channel groups labels

Signed-off-by: Gerhard Riegler <[email protected]>
  • Loading branch information
gerrieg authored and kaikreuzer committed Feb 19, 2017
1 parent 58dfc4b commit 9408c81
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

<config-description uri="channel-type:astro:config">
<parameter name="offset" type="integer" min="-1440" max="1440">
<label>Event Offset</label>
<description>Moves an event forward or backward (in minutes)</description>
<label>Offset</label>
<description>Moves an event or datetime value forward or backward (in minutes)</description>
<default>0</default>
</parameter>
</config-description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@
<label>Start time</label>
<description>The start time of the event</description>
<state readOnly="true" pattern="%1$tF %1$tR" />
<config-description-ref uri="channel-type:astro:config" />
</channel-type>

<channel-type id="end">
<item-type>DateTime</item-type>
<label>End time</label>
<description>The end time of the event</description>
<state readOnly="true" pattern="%1$tF %1$tR" />
<config-description-ref uri="channel-type:astro:config" />
</channel-type>

<channel-type id="duration">
Expand Down
16 changes: 12 additions & 4 deletions addons/binding/org.openhab.binding.astro/ESH-INF/thing/moon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
<description>Provides astronomical data from the moon</description>

<channel-groups>
<channel-group id="rise" typeId="moonRange" />
<channel-group id="set" typeId="moonRange" />
<channel-group id="rise" typeId="moonRange">
<label>Moonrise</label>
</channel-group>
<channel-group id="set" typeId="moonRange">
<label>Moonset</label>
</channel-group>
<channel-group id="phase" typeId="moonPhase" />
<channel-group id="eclipse" typeId="moonEclipse" />
<channel-group id="distance" typeId="distance" />
<channel-group id="perigee" typeId="distance" />
<channel-group id="apogee" typeId="distance" />
<channel-group id="perigee" typeId="distance">
<label>Perigee</label>
</channel-group>
<channel-group id="apogee" typeId="distance">
<label>Apogee</label>
</channel-group>
<channel-group id="position" typeId="position" />
<channel-group id="zodiac" typeId="moonZodiac" />
</channel-groups>
Expand Down
52 changes: 39 additions & 13 deletions addons/binding/org.openhab.binding.astro/ESH-INF/thing/sun.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,45 @@
<description>Provides astronomical data from the sun</description>

<channel-groups>
<channel-group id="rise" typeId="sunRange" />
<channel-group id="set" typeId="sunRange" />
<channel-group id="noon" typeId="sunRange" />
<channel-group id="night" typeId="sunRange" />
<channel-group id="morningNight" typeId="sunRange" />
<channel-group id="astroDawn" typeId="sunRange" />
<channel-group id="nauticDawn" typeId="sunRange" />
<channel-group id="civilDawn" typeId="sunRange" />
<channel-group id="astroDusk" typeId="sunRange" />
<channel-group id="nauticDusk" typeId="sunRange" />
<channel-group id="civilDusk" typeId="sunRange" />
<channel-group id="eveningNight" typeId="sunRange" />
<channel-group id="daylight" typeId="sunRange" />
<channel-group id="rise" typeId="sunRange">
<label>Sunrise</label>
</channel-group>
<channel-group id="set" typeId="sunRange">
<label>Sunset</label>
</channel-group>
<channel-group id="noon" typeId="sunRange">
<label>Noon</label>
</channel-group>
<channel-group id="night" typeId="sunRange">
<label>Night</label>
</channel-group>
<channel-group id="morningNight" typeId="sunRange">
<label>Morning Night</label>
</channel-group>
<channel-group id="astroDawn" typeId="sunRange">
<label>Astro Dawn</label>
</channel-group>
<channel-group id="nauticDawn" typeId="sunRange">
<label>Nautic Dawn</label>
</channel-group>
<channel-group id="civilDawn" typeId="sunRange">
<label>Civil Dawn</label>
</channel-group>
<channel-group id="astroDusk" typeId="sunRange">
<label>Astro Dusk</label>
</channel-group>
<channel-group id="nauticDusk" typeId="sunRange">
<label>Nautic Dusk</label>
</channel-group>
<channel-group id="civilDusk" typeId="sunRange">
<label>Civil Dusk</label>
</channel-group>
<channel-group id="eveningNight" typeId="sunRange">
<label>Evening Night</label>
</channel-group>
<channel-group id="daylight" typeId="sunRange">
<label>Daylight</label>
</channel-group>
<channel-group id="position" typeId="position" />
<channel-group id="radiation" typeId="radiation" />
<channel-group id="zodiac" typeId="sunZodiac" />
Expand Down
16 changes: 15 additions & 1 deletion addons/binding/org.openhab.binding.astro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ]
astro:moon:home [ geolocation="xx.xxxxxx,xx.xxxxxx", interval=60 ]
```

or optionally with an offset
or optionally with an event offset

```
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ] {
Expand All @@ -126,6 +126,20 @@ astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ]
astro:moon:home [ geolocation="xx.xxxxxx,xx.xxxxxx", interval=60 ]
```

or a datetime offset

```
astro:sun:home [ geolocation="xx.xxxxxx,xx.xxxxxx", altitude=100, interval=60 ] {
Channels:
Type start : rise#start [
offset=5
]
Type end : rise#end [
offset=5
]
}
```

Items:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class AstroBindingConstants {
public static final String MOON = "moon";
public static final String LOCAL = "local";

public static final String EVENT_CONFIG_OFFSET = "offset";

// things
public static final ThingTypeUID THING_TYPE_SUN = new ThingTypeUID(BINDING_ID, SUN);
public static final ThingTypeUID THING_TYPE_MOON = new ThingTypeUID(BINDING_ID, MOON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.openhab.binding.astro.internal.config.AstroChannelConfig;
import org.openhab.binding.astro.internal.config.AstroThingConfig;
import org.openhab.binding.astro.internal.job.AbstractBaseJob;
import org.openhab.binding.astro.internal.job.AbstractDailyJob;
Expand Down Expand Up @@ -157,7 +158,9 @@ public void publishPlanet() {
public void publishChannelIfLinked(ChannelUID channelUID) {
if (isLinked(channelUID.getId()) && getPlanet() != null) {
try {
updateState(channelUID, PropertyUtils.getState(channelUID, getPlanet()));
AstroChannelConfig config = getThing().getChannel(channelUID.getId()).getConfiguration()
.as(AstroChannelConfig.class);
updateState(channelUID, PropertyUtils.getState(channelUID, config, getPlanet()));
} catch (Exception ex) {
logger.error("Can't update state for channel " + channelUID + ": " + ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2010-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.openhab.binding.astro.internal.config;

/**
* Channel configuration from openHab.
*
* @author Gerhard Riegler - Initial contribution
*/

public class AstroChannelConfig {
private Integer offset;

/**
* Returns the offset.
*/
public Integer getOffset() {
return offset;
}

/**
* Sets the offset.
*/
public void setOffset(Integer offset) {
this.offset = offset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.math.BigDecimal;
import java.util.Calendar;

import org.apache.commons.lang.time.DateFormatUtils;
import org.openhab.binding.astro.handler.AstroThingHandler;
import org.openhab.binding.astro.internal.AstroHandlerFactory;
import org.openhab.binding.astro.internal.config.AstroChannelConfig;
import org.openhab.binding.astro.internal.model.Planet;
import org.openhab.binding.astro.internal.model.Range;
import org.openhab.binding.astro.internal.model.SunPhaseName;
Expand Down Expand Up @@ -114,8 +114,8 @@ private void schedule(AstroThingHandler astroHandler, Class<? extends AbstractBa
*/
private int getEventOffset(AstroThingHandler astroHandler, String channelId) {
try {
BigDecimal delay = (BigDecimal) astroHandler.getThing().getChannel(channelId).getConfiguration()
.get(EVENT_CONFIG_OFFSET);
Integer delay = astroHandler.getThing().getChannel(channelId).getConfiguration()
.as(AstroChannelConfig.class).getOffset();
return delay.intValue();
} catch (Exception ex) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.astro.internal.config.AstroChannelConfig;

/**
* Methods to get the value from a property of an object.
*
*
* @author Gerhard Riegler - Initial contribution
*/
public class PropertyUtils {

/**
* Returns the state of the channel.
*/
public static State getState(ChannelUID channelUID, Object instance) throws Exception {
public static State getState(ChannelUID channelUID, AstroChannelConfig config, Object instance) throws Exception {
Object value = getPropertyValue(channelUID, instance);
if (value == null) {
return UnDefType.UNDEF;
} else if (value instanceof Calendar) {
return new DateTimeType((Calendar) value);
Calendar cal = (Calendar) value;
if (config.getOffset() != null) {
cal.add(Calendar.MINUTE, config.getOffset());
}
return new DateTimeType(cal);
} else if (value instanceof Number) {
BigDecimal decimalValue = new BigDecimal(value.toString()).setScale(2, RoundingMode.HALF_UP);
return new DecimalType(decimalValue);
Expand Down

0 comments on commit 9408c81

Please sign in to comment.