Skip to content

Commit

Permalink
[ntp] Clean up (openhab#7834)
Browse files Browse the repository at this point in the history
* [ntp] Clean up

Add null annotations
Suppress one unused configutation setting (locale)
Get the default timezone from TimeZoneProvider
Map thing configuration and channel configuration to classes
Use constructor injection for the discovery service
* Use TimeZoneProvider each time the timezone is required
* Call activate in the discovery service constructor

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and andrewfg committed Aug 31, 2020
1 parent ae240ed commit 501dc15
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.LocaleProvider;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
Expand All @@ -38,11 +38,11 @@
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.ntp")
public class NtpHandlerFactory extends BaseThingHandlerFactory {

private final LocaleProvider localeProvider;
private final TimeZoneProvider timeZoneProvider;

@Activate
public NtpHandlerFactory(final @Reference LocaleProvider localeProvider) {
this.localeProvider = localeProvider;
public NtpHandlerFactory(final @Reference TimeZoneProvider timeZoneProvider) {
this.timeZoneProvider = timeZoneProvider;
}

@Override
Expand All @@ -55,7 +55,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();

if (THING_TYPE_NTP.equals(thingTypeUID)) {
return new NtpHandler(thing, localeProvider);
return new NtpHandler(thing, timeZoneProvider);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.ntp.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link NtpStringChannelConfiguration} is responsible for holding
* the configuration settings for the channel "string"
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class NtpStringChannelConfiguration {

public String DateTimeFormat = "yyyy-MM-dd HH:mm:ss z";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.ntp.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* The {@link NtpThingConfiguration} is responsible for holding
* the thing configuration settings
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class NtpThingConfiguration {

public String hostname = "0.pool.ntp.org";
public int refreshInterval = 60;
public int refreshNtp = 30;
public int serverPort = 123;
public @Nullable String timeZone;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@

import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.i18n.LocaleProvider;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -36,21 +39,21 @@
*
* @author Marcel Verpaalen - Initial contribution
*/
@NonNullByDefault
@Component(service = DiscoveryService.class, immediate = true, configurationPid = "discovery.ntp")
public class NtpDiscovery extends AbstractDiscoveryService {

public NtpDiscovery() throws IllegalArgumentException {
super(SUPPORTED_THING_TYPES_UIDS, 2);
}

@Override
protected void activate(Map<String, Object> configProperties) {
super.activate(configProperties);
}
private final TimeZoneProvider timeZoneProvider;

@Override
protected void modified(Map<String, Object> configProperties) {
super.modified(configProperties);
@Activate
public NtpDiscovery(final @Reference LocaleProvider localeProvider,
final @Reference TranslationProvider i18nProvider, final @Reference TimeZoneProvider timeZoneProvider,
@Nullable Map<String, @Nullable Object> configProperties) throws IllegalArgumentException {
super(SUPPORTED_THING_TYPES_UIDS, 2);
this.localeProvider = localeProvider;
this.i18nProvider = i18nProvider;
this.timeZoneProvider = timeZoneProvider;
activate(configProperties);
}

@Override
Expand All @@ -70,28 +73,10 @@ protected void startScan() {
*/
private void discoverNtp() {
Map<String, Object> properties = new HashMap<>(4);
properties.put(PROPERTY_TIMEZONE, TimeZone.getDefault().getID());
properties.put(PROPERTY_TIMEZONE, timeZoneProvider.getTimeZone().getId());
ThingUID uid = new ThingUID(THING_TYPE_NTP, "local");
DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("Local Time")
.build();
thingDiscovered(result);
}

@Reference
protected void setLocaleProvider(final LocaleProvider localeProvider) {
this.localeProvider = localeProvider;
}

protected void unsetLocaleProvider(final LocaleProvider localeProvider) {
this.localeProvider = null;
}

@Reference
protected void setTranslationProvider(TranslationProvider i18nProvider) {
this.i18nProvider = i18nProvider;
}

protected void unsetTranslationProvider(TranslationProvider i18nProvider) {
this.i18nProvider = null;
}
}
Loading

0 comments on commit 501dc15

Please sign in to comment.