Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ntp] Clean up #7834

Merged
merged 6 commits into from
Jun 19, 2020
Merged

[ntp] Clean up #7834

merged 6 commits into from
Jun 19, 2020

Conversation

lolodomo
Copy link
Contributor

@lolodomo lolodomo commented Jun 1, 2020

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

Signed-off-by: Laurent Garnier [email protected]

@lolodomo lolodomo requested a review from marcelrv as a code owner June 1, 2020 23:18
@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 1, 2020

Binding fully retested with these changes.

@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 2, 2020

I need to adjust the integration tests.

Add null annotations
Suppress one unused configutation setting (locale)
Get the default timezone from TimeZoneProvider
Map thing configuration and channel configuration to classes

Signed-off-by: Laurent Garnier <[email protected]>
@lolodomo lolodomo force-pushed the ntp_nullannotations branch from 6f89095 to 04af195 Compare June 2, 2020 07:20
@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 2, 2020

Integration tests now ok again.

private static final int DEFAULT_REFRESH_NTP = 30;
private static final int DEFAULT_SERVER_PORT = 123;

public @Nullable String hostname;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you use something like

Suggested change
public @Nullable String hostname;
public String hostname = "0.pool.ntp.org";

You can skip the whole logic below then and directly access the fields in the code. If the user sets set parameter, the default is overwritten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, fixed.

private static final int DEFAULT_SERVER_PORT = 123;

public @Nullable String hostname;
public @Nullable Integer refreshInterval;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public @Nullable Integer refreshInterval;
public int refreshInterval = 60;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, fixed.

@@ -44,12 +48,12 @@ public NtpDiscovery() throws IllegalArgumentException {
}

@Override
protected void activate(Map<String, Object> configProperties) {
protected void activate(@Nullable Map<String, @Nullable Object> configProperties) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed at all? It only calls the method it overrides

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should refactor this call and rather use injection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructor injection done.

@@ -54,217 +54,183 @@
* to one of the channels.
*
* @author Marcel Verpaalen - Initial contribution OH2 ntp binding
* @author Thomas.Eichstaedt-Engelen OH1 ntp binding (getTime routine)
* @author Thomas.Eichstaedt - Engelen OH1 ntp binding (getTime routine)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. Thomas's last name is "Eichstaedt-Engelen", so the spaces arounf the dash are wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a warning on this line but my change was stupid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


/** for publish purposes */
private DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ);

private final LocaleProvider localeProvider;
private @Nullable NtpThingConfiguration configuration;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all values have reasonable defaults, you can skip null checks by using

Suggested change
private @Nullable NtpThingConfiguration configuration;
private NtpThingConfiguration configuration = new NtpThingConfiguration();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done.

Comment on lines 106 to 107
NtpThingConfiguration config = getConfigAs(NtpThingConfiguration.class);
configuration = config;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use

Suggested change
NtpThingConfiguration config = getConfigAs(NtpThingConfiguration.class);
configuration = config;
configuration = getConfigAs(NtpThingConfiguration.class);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was to avoid null check warnings due to configuration which was nullable.
Now that configuration is non null, the local variable can be avoided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local variable removed.


String timeZoneConfigValue = config.getTimeZone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use the defaults directly, you can access the fields here and omit most of the null checks/function calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't define a default for the configuration because the expected default is the timezone provided by the TimeZoneProvider.

logger.debug("{} using default locale '{}', because configuration property '{}' is null.",
getThing().getUID(), locale, PROPERTY_LOCALE);
}
timeZoneId = TimeZone.getTimeZone(timeZoneConfigValue).toZoneId();
} catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to catch Exception here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it is not necessary because it returns GMT if the string is invalid.
Fixed.

@@ -37,14 +37,5 @@
public static final String CHANNEL_DATE_TIME = "dateTime";
public static final String CHANNEL_STRING = "string";

// Custom Properties
public static final String PROPERTY_NTP_SERVER_HOST = "hostname";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep the constants here, as that is what is most common in other bindings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I often saw the constants defined in th econfiguration class too.
But ok, I revert.

@lolodomo lolodomo force-pushed the ntp_nullannotations branch from 888c605 to 18f34f2 Compare June 2, 2020 23:18
@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 2, 2020

java.util.TimeZone is no more used.
java.time.ZoneId is rather used.

Use constructor injection for the discovery service

Signed-off-by: Laurent Garnier <[email protected]>
@lolodomo lolodomo force-pushed the ntp_nullannotations branch from 18f34f2 to c0e2029 Compare June 2, 2020 23:27
@openhab openhab deleted a comment from TravisBuddy Jun 2, 2020
@openhab openhab deleted a comment from TravisBuddy Jun 2, 2020
@TravisBuddy
Copy link

Travis tests have failed

Hey @lolodomo,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 3, 2020

java.util.TimeZone is no more used.
java.time.ZoneId is rather used.

I have to test again but it is possible that parsing of the timezone configuration setting using ZoneId.of(configuration.timeZone) is now failing in particular cases like for example "GMT+1:30" when there is no existing zone id. In that case, I will revert to TimeZone.getTimeZone(configuration.timeZone).

@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 3, 2020

New timezone parsing is working well. In fact GMT+1:00 is an invalid value while GMT+01:30 is valid and correctly parsed by the binding.

@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 3, 2020

And the full build is green.

@openhab openhab deleted a comment from TravisBuddy Jun 6, 2020
@openhab openhab deleted a comment from TravisBuddy Jun 6, 2020
@TravisBuddy
Copy link

Travis tests have failed

Hey @lolodomo,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

1 similar comment
@TravisBuddy
Copy link

Travis tests have failed

Hey @lolodomo,
please read the following log in order to understand the failure reason. There might also be some helpful tips along the way.
It will be awesome if you fix what is wrong and commit the changes.

@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 9, 2020

This is not yet good as TimeZoneProvider is considered only at thing handler initialization.
I will change that and test.

@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 9, 2020

I tested how the binding is behaving with these changes when no timezone is set on the thing side but the user changes the openHAB system timezone. And this is working well, the openHAB system timezone is correctly taken into accound when the date/time is updated by the binding.

@lolodomo
Copy link
Contributor Author

lolodomo commented Jun 9, 2020

@J-N-K : could you please finish your review or more exactly review my changes after your first review ?

Copy link
Contributor

@cpmeister cpmeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cpmeister
Copy link
Contributor

@J-N-K can you take a final look?

@lolodomo
Copy link
Contributor Author

Can we have this PR merged before 2.5.6 is released?

@cpmeister
Copy link
Contributor

@J-N-K friendly ping

@cpmeister cpmeister merged commit abc926d into openhab:2.5.x Jun 19, 2020
@cpmeister cpmeister added this to the 2.5.6 milestone Jun 19, 2020
@lolodomo lolodomo deleted the ntp_nullannotations branch June 19, 2020 21:28
knikhilwiz pushed a commit to knikhilwiz/openhab2-addons that referenced this pull request Jul 12, 2020
* [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]>
CSchlipp pushed a commit to CSchlipp/openhab-addons that referenced this pull request Jul 26, 2020
* [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]>
Signed-off-by: CSchlipp <[email protected]>
MPH80 pushed a commit to MPH80/openhab-addons that referenced this pull request Aug 3, 2020
* [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]>
Signed-off-by: MPH80 <[email protected]>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
* [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]>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
* [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]>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
* [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]>
andrewfg pushed a commit to andrewfg/openhab-addons that referenced this pull request Aug 31, 2020
* [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]>
DaanMeijer pushed a commit to DaanMeijer/openhab-addons that referenced this pull request Sep 1, 2020
* [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]>
Signed-off-by: Daan Meijer <[email protected]>
markus7017 pushed a commit to markus7017/openhab-addons that referenced this pull request Sep 19, 2020
* [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]>
This was referenced Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants