Skip to content

Commit

Permalink
[robonect] Get Timezone from config on each request
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Triller <[email protected]>
  • Loading branch information
t2000 committed Jun 14, 2020
1 parent beb09bf commit 82a950f
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class RobonectHandler extends BaseThingHandler {
private HttpClient httpClient;
private TimeZoneProvider timeZoneProvider;

private ZoneId timeZone = ZoneId.of("Europe/Berlin");
private RobonectConfig robonectConfig;

private RobonectClient robonectClient;

Expand Down Expand Up @@ -283,13 +283,33 @@ private void updateNextTimer(MowerInfo info) {
updateState(CHANNEL_TIMER_NEXT_TIMER, dateTime);
}

private ZoneId getTimeZoneForThing() {
ZoneId timeZone = ZoneId.of("Europe/Berlin");

String timeZoneString = robonectConfig.getTimezone();
try {
if (timeZoneString != null) {
timeZone = ZoneId.of(timeZoneString);
} else {
logger.warn("No timezone provided, falling back to the default timezone configured in openHAB: '{}'",
timeZoneProvider.getTimeZone());
timeZone = timeZoneProvider.getTimeZone();
}
} catch (DateTimeException e) {
logger.warn("Error setting timezone '{}', falling back to the default timezone configured in openHAB: '{}'",
timeZoneString, timeZoneProvider.getTimeZone(), e);
timeZone = timeZoneProvider.getTimeZone();
}
return timeZone;
}

private State convertUnixToDateTimeType(String unixTimeSec) {
// the value in unixTimeSec represents the time on the robot in its configured timezone. However, it does not
// provide which zone this is. Thus we have to add the zone information from the Thing configuration in order to
// provide correct results.
Instant rawInstant = Instant.ofEpochMilli(Long.valueOf(unixTimeSec) * 1000);

ZoneOffset offsetToConfiguredZone = timeZone.getRules().getOffset(rawInstant);
ZoneOffset offsetToConfiguredZone = getTimeZoneForThing().getRules().getOffset(rawInstant);
long adjustedTime = rawInstant.getEpochSecond() - offsetToConfiguredZone.getTotalSeconds();
Instant adjustedInstant = Instant.ofEpochMilli(adjustedTime * 1000);

Expand Down Expand Up @@ -342,25 +362,10 @@ private void updateLastErrorChannels(ErrorEntry error) {

@Override
public void initialize() {
RobonectConfig robonectConfig = getConfigAs(RobonectConfig.class);
robonectConfig = getConfigAs(RobonectConfig.class);
RobonectEndpoint endpoint = new RobonectEndpoint(robonectConfig.getHost(), robonectConfig.getUser(),
robonectConfig.getPassword());

String timeZoneString = robonectConfig.getTimezone();
try {
if (timeZoneString != null) {
timeZone = ZoneId.of(timeZoneString);
} else {
logger.warn("No timezone provided, falling back to the default timezone configured in openHAB: '{}'",
timeZoneProvider.getTimeZone());
timeZone = timeZoneProvider.getTimeZone();
}
} catch (DateTimeException e) {
logger.warn("Error setting timezone '{}', falling back to the default timezone configured in openHAB: '{}'",
timeZoneString, timeZoneProvider.getTimeZone(), e);
timeZone = timeZoneProvider.getTimeZone();
}

try {
httpClient.start();
robonectClient = new RobonectClient(httpClient, endpoint);
Expand Down

0 comments on commit 82a950f

Please sign in to comment.