Permalink
Browse files

Twilight plugin: switch from Joda to Java 8 time

Fixes #417
  • Loading branch information...
nwholloway authored and mcicolella committed Oct 14, 2018
1 parent ccc1f4a commit d57e6d01a81feb6099220b156a641629302a1428
@@ -59,11 +59,6 @@
<version>${project.parent.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
<build>
@@ -27,7 +27,7 @@
import com.freedomotic.plugins.devices.twilight.providers.EarthToolsWI;
import com.freedomotic.plugins.devices.twilight.providers.OpenWeatherMapWI;
import java.io.IOException;
import org.joda.time.DateTime;
import java.time.ZonedDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -81,7 +81,7 @@ protected void onHideGui() {
@Override
protected void onRun() {
EventTemplate ev = TLU.prepareEvent(DateTime.now());
EventTemplate ev = TLU.prepareEvent(ZonedDateTime.now());
LOG.info(ev.getPayload().toString().replace("\n", " "));
notifyEvent(ev);
@@ -20,8 +20,8 @@
package com.freedomotic.plugins.devices.twilight;
import com.freedomotic.events.GenericEvent;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import java.time.Duration;
import java.time.ZonedDateTime;
/**
*
@@ -47,9 +47,9 @@ public TwilightUtils(int pw, WeatherInfo provider) {
* @param ref
* @return
*/
public GenericEvent prepareEvent(DateTime ref) {
DateTime sunsetTime = provider.getNextSunset();
DateTime sunriseTime = provider.getNextSunrise();
public GenericEvent prepareEvent(ZonedDateTime ref) {
ZonedDateTime sunsetTime = provider.getNextSunset();
ZonedDateTime sunriseTime = provider.getNextSunrise();
while (sunsetTime.isBefore(ref) && sunriseTime.isBefore(ref)) {
if (sunsetTime.isBefore(sunriseTime)) {
@@ -61,33 +61,33 @@ public GenericEvent prepareEvent(DateTime ref) {
}
}
Duration toSunset = sunsetTime.isAfter(ref) ? new Duration(ref, sunsetTime) : new Duration(sunsetTime, ref);
Duration toSunrise = sunriseTime.isAfter(ref) ? new Duration(ref, sunriseTime) : new Duration(sunriseTime, ref);
Duration toSunset = sunsetTime.isAfter(ref) ? Duration.between(ref, sunsetTime) : Duration.between(sunsetTime, ref);
Duration toSunrise = sunriseTime.isAfter(ref) ? Duration.between(ref, sunriseTime) : Duration.between(sunriseTime, ref);
// create the event
GenericEvent ev = new GenericEvent(getClass());
ev.setDestination("app.event.sensor.calendar.event.twilight");
if (toSunset.getMillis() < POLLING_WAIT / 2) {
if (toSunset.toMillis() < POLLING_WAIT / 2) {
// it's sunset
ev.addProperty("isSunset", "true");
} else if (toSunrise.getMillis() < POLLING_WAIT / 2) {
} else if (toSunrise.toMillis() < POLLING_WAIT / 2) {
// it's sunrise
ev.addProperty("isSunrise", "true");
}
if (ref.isBefore(sunriseTime)) {
// before sunrise
ev.addProperty("beforeSunrise", Long.toString(toSunrise.getStandardMinutes()));
ev.addProperty("beforeSunrise", Long.toString(toSunrise.toMinutes()));
} else if (ref.isAfter(sunriseTime)) {
// after sunrise
ev.addProperty("afterSunrise", Long.toString(toSunrise.getStandardMinutes()));
ev.addProperty("afterSunrise", Long.toString(toSunrise.toMinutes()));
}
if (ref.isBefore(sunsetTime)) {
// before sunset
ev.addProperty("beforeSunset", Long.toString(toSunset.getStandardMinutes()));
ev.addProperty("beforeSunset", Long.toString(toSunset.toMinutes()));
} else if (ref.isAfter(sunsetTime)) {
// after sunset
ev.addProperty("afterSunset", Long.toString(toSunset.getStandardMinutes()));
ev.addProperty("afterSunset", Long.toString(toSunset.toMinutes()));
}
return ev;
}
@@ -19,7 +19,7 @@
*/
package com.freedomotic.plugins.devices.twilight;
import org.joda.time.DateTime;
import java.time.ZonedDateTime;
/**
@@ -32,13 +32,13 @@
*
* @return
*/
DateTime getNextSunset();
ZonedDateTime getNextSunset();
/**
*
* @return
*/
DateTime getNextSunrise();
ZonedDateTime getNextSunrise();
/**
*
@@ -50,12 +50,12 @@
*
* @param sunset
*/
void setNextSunset(DateTime sunset);
void setNextSunset(ZonedDateTime sunset);
/**
*
* @param sunrise
*/
void setNextSunrise(DateTime sunrise);
void setNextSunrise(ZonedDateTime sunrise);
}
@@ -23,10 +23,12 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
@@ -42,8 +44,8 @@
private static final Logger LOG = LoggerFactory.getLogger(EarthToolsWI.class.getCanonicalName());
private final String latitude;
private final String longitude;
private DateTime nextSunrise;
private DateTime nextSunset;
private ZonedDateTime nextSunrise;
private ZonedDateTime nextSunset;
/**
*
@@ -60,7 +62,7 @@ public EarthToolsWI(String latitude, String longitude) {
* @return
*/
@Override
public DateTime getNextSunset() {
public ZonedDateTime getNextSunset() {
return nextSunset;
}
@@ -69,7 +71,7 @@ public DateTime getNextSunset() {
* @return
*/
@Override
public DateTime getNextSunrise() {
public ZonedDateTime getNextSunrise() {
return nextSunrise;
}
@@ -100,23 +102,21 @@ private Document getXMLStatusFile(int dom, int moy, int zone, int dst) throws Ma
*/
@Override
public boolean updateData() throws Exception {
DateTime dt = new DateTime();
int dst = dt.getZone().isStandardOffset(dt.getMillis()) ? 0 : 1;
int offset = dt.getZone().getStandardOffset(dt.getMillis()) / 3600000;
Document doc = getXMLStatusFile(dt.getDayOfMonth(), dt.getMonthOfYear(), offset, dst);
ZonedDateTime dt = ZonedDateTime.now();
int dst = dt.getZone().getRules().isDaylightSavings(dt.toInstant()) ? 1 : 0;
int offset = dt.getZone().getRules().getStandardOffset(dt.toInstant()).getTotalSeconds() / 3600;
Document doc = getXMLStatusFile(dt.getDayOfMonth(), dt.getMonthValue(), offset, dst);
//parse xml
if (doc != null) {
Node sunriseNode = doc.getElementsByTagName("sunrise").item(0);
Node sunsetNode = doc.getElementsByTagName("sunset").item(0);
// compare with the current time
String srTime[] = sunriseNode.getFirstChild().getNodeValue().split(":");
String ssTime[] = sunsetNode.getFirstChild().getNodeValue().split(":");
nextSunrise = new DateTime(dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(),
Integer.parseInt(srTime[0]), Integer.parseInt(srTime[1]), Integer.parseInt(srTime[2]));
nextSunset = new DateTime(dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(),
Integer.parseInt(ssTime[0]), Integer.parseInt(ssTime[1]), Integer.parseInt(ssTime[2]));
LocalTime srTime = LocalTime.parse(sunriseNode.getFirstChild().getNodeValue());
LocalTime ssTime = LocalTime.parse(sunsetNode.getFirstChild().getNodeValue());
nextSunrise = dt.with(srTime);
nextSunset = dt.with(ssTime);
LOG.info("Sunrise at: {} Sunset at: {}", nextSunrise, nextSunset);
return true;
} else {
@@ -129,7 +129,7 @@ public boolean updateData() throws Exception {
* @param sunset
*/
@Override
public void setNextSunset(DateTime sunset) {
public void setNextSunset(ZonedDateTime sunset) {
nextSunset = sunset;
}
@@ -138,7 +138,7 @@ public void setNextSunset(DateTime sunset) {
* @param sunrise
*/
@Override
public void setNextSunrise(DateTime sunrise) {
public void setNextSunrise(ZonedDateTime sunrise) {
nextSunrise = sunrise;
}
@@ -24,10 +24,12 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
@@ -43,8 +45,8 @@
private static final Logger LOG = LoggerFactory.getLogger(OpenWeatherMapWI.class.getCanonicalName());
private final String latitude;
private final String longitude;
private DateTime nextSunrise;
private DateTime nextSunset;
private ZonedDateTime nextSunrise;
private ZonedDateTime nextSunset;
/**
*
@@ -61,7 +63,7 @@ public OpenWeatherMapWI(String latitude, String longitude) {
* @return
*/
@Override
public DateTime getNextSunset() {
public ZonedDateTime getNextSunset() {
return nextSunset;
}
@@ -70,7 +72,7 @@ public DateTime getNextSunset() {
* @return
*/
@Override
public DateTime getNextSunrise() {
public ZonedDateTime getNextSunrise() {
return nextSunrise;
}
@@ -98,19 +100,19 @@ private Document getXMLStatusFile(int dom, int moy, int zone, int dst) throws Ma
*/
@Override
public boolean updateData() throws Exception {
DateTime dt = new DateTime();
int dst = dt.getZone().isStandardOffset(dt.getMillis()) ? 0 : 1;
int offset = dt.getZone().getStandardOffset(dt.getMillis()) / 3600000;
Document doc = getXMLStatusFile(dt.getDayOfMonth(), dt.getMonthOfYear(), offset, dst);
ZonedDateTime dt = ZonedDateTime.now();
int dst = dt.getZone().getRules().isDaylightSavings(dt.toInstant()) ? 1 : 0;
int offset = dt.getZone().getRules().getOffset(dt.toInstant()).getTotalSeconds() / 3600;
Document doc = getXMLStatusFile(dt.getDayOfMonth(), dt.getMonthValue(), offset, dst);
//parse xml
if (doc != null) {
Node sunriseNode = doc.getElementsByTagName("sun").item(0).getAttributes().getNamedItem("rise");
Node sunsetNode = doc.getElementsByTagName("sun").item(0).getAttributes().getNamedItem("set");
// compare with the current time
nextSunrise = new DateTime(sunriseNode.getNodeValue()).plusHours(offset);
nextSunset = new DateTime(sunsetNode.getNodeValue()).plusHours(offset);
nextSunrise = dt.with(LocalDateTime.parse(sunriseNode.getNodeValue())).plusHours(offset);
nextSunset = dt.with(LocalDateTime.parse(sunsetNode.getNodeValue())).plusHours(offset);
LOG.info("Sunrise at: {} Sunset at: {}", nextSunrise, nextSunset);
return true;
} else {
@@ -123,7 +125,7 @@ public boolean updateData() throws Exception {
* @param sunset
*/
@Override
public void setNextSunset(DateTime sunset) {
public void setNextSunset(ZonedDateTime sunset) {
nextSunset = sunset;
}
@@ -132,7 +134,7 @@ public void setNextSunset(DateTime sunset) {
* @param sunrise
*/
@Override
public void setNextSunrise(DateTime sunrise) {
public void setNextSunrise(ZonedDateTime sunrise) {
nextSunrise = sunrise;
}
}
Oops, something went wrong.

0 comments on commit d57e6d0

Please sign in to comment.