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

Refactor ZigBeeBaseChannelConverter to split initialisation #342

Merged
merged 2 commits into from Jan 7, 2019

Conversation

cdjackson
Copy link
Contributor

This implements the split in the ZigBeeBaseChannelConverter to separate device configuration and converter initialisation.

initializeDevice is used to perform the device configuration.
initializeConverter is used to perform tine converter initialisation.

Currently both are called at the same time, but as discussed this can be split in a later PR.

I've implemented the split in the OnOffConverter as an example.

Closes #338
Signed-off-by: Chris Jackson chris@cd-jackson.com

@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/zigbee-binding/15763/1782

@hsudbrock hsudbrock self-requested a review January 3, 2019 10:35
@@ -177,6 +177,16 @@ public void initialize(ZigBeeThingHandler thing, Channel channel, ZigBeeCoordina
this.coordinator = coordinator;
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the class-level documentation (which describes the life cycle of a converter) be updated to reflect this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep - thanks. I'll update.

Copy link
Contributor

@hsudbrock hsudbrock left a comment

Choose a reason for hiding this comment

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

Hi Chris - thanks for the PR, I left my comments inline.

public boolean initializeConverter() {
clusterOnOffClient = (ZclOnOffCluster) endpoint.getOutputCluster(ZclOnOffCluster.CLUSTER_ID);
clusterOnOffServer = (ZclOnOffCluster) endpoint.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
public boolean initializeDevice() {
Copy link
Contributor

Choose a reason for hiding this comment

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

If there is an error when binding to the cluster, or when setting reporting, then the polling period is adjusted to POLLING_PERIOD_HIGH. But adapting the polling period is converter initialisation, not device initialisation (and should, hence, be done in initializeConverter).

That would mean that if, in the future, as suggested in #338, the device is initialised only once, then the adjustment of the polling period would be lost when the converter is initialised a second time.

I don't see a straightforward 'fix' for this, though, because the information whether the polling period needs to be adjusted due to bind/reporting configuration issues is not persisted anywhere.

Do you have any idea on how to deal with this issue?

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 would hope (!!) that we can get a clean initialisation of the bind/reporting - even if it takes a few retries... In the event that this doesn't work (eg if the bind table is full - I know some Hue bulbs only have a single entry so it's only possible to bind one cluster!) then we probably should store that elsewhere (ie configuration).

I have another PR waiting for review (I think) which allows user configuration of polling period, so in the event that reporting is not initialised properly, the polling configuration can be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI: #344

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds like a viable solution; I will take a look at that other PR.

clusterOnOffClient = (ZclOnOffCluster) endpoint.getOutputCluster(ZclOnOffCluster.CLUSTER_ID);
clusterOnOffServer = (ZclOnOffCluster) endpoint.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
public boolean initializeDevice() {
ZclOnOffCluster clusterOnOffClient = (ZclOnOffCluster) endpoint.getOutputCluster(ZclOnOffCluster.CLUSTER_ID);
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor comment: Why use a local variable here, instead of assigning directly to the field clusterOnOffClient? (Same for the clusterOnOffServer in the following line.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My rational is that this method is transitory - ie it shouldn't retain state etc. Ok, I agree that we could use the field instead of the local variable, but to me at least the local variable should be initialised in the initializeConverter method, and disposed in the disposeConverter method, and used in the listeners. These methods should not (can not!) rely on the initializeDevice method initialising this field and I just felt it made it clearer not to use it....

I don't overly mind though if you think it's better to use the field instead for some reason?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, I think that it is often not a good idea to use the same name for a local variable and a class-level field, because it makes reading the code harder (and leads to confusion like mine in the review).

I, personally, would prefer to either use a different name for the local variables, or to simply add a one-line comment // Use local copy of the cluster objects, as the corresponding fields are only initialized in initializeConverter - here probably the comment, as it appears somewhat artificial to use a different name. But this is probably personal preference...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're right - it would be clearer if the name were different. I will implement one or both of your suggestions - thanks.

Signed-off-by: Chris Jackson <chris@cd-jackson.com>
@cdjackson
Copy link
Contributor Author

@hsudbrock I've updated this PR to improve the docs...

Copy link
Contributor

@hsudbrock hsudbrock left a comment

Choose a reason for hiding this comment

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

Thanks for updating the JavaDoc!

I think that we could merge this PR now, as (for now) initializeDevice is called on every converter initialization. Before introducing code that causes initializeDevice to not always be called, the initialization of the On/Off converter should be adapted using configurable polling periods as you suggested in the comments.

@cdjackson
Copy link
Contributor Author

cdjackson commented Jan 6, 2019 via email

@hsudbrock
Copy link
Contributor

I don’t see any option to change this now - it requires the other PR to be merged before this can be changed.

That's why I suggest to merge this now - and to simply take care that the OnOffConverter has been 'fixed' with the help of the other (not yet merged) PR before actually exploiting the split of the initialization introduced in this PR.

OK for you?

@cdjackson
Copy link
Contributor Author

Sure - that's fine.

@cdjackson cdjackson closed this Jan 7, 2019
@cdjackson cdjackson reopened this Jan 7, 2019
@cdjackson cdjackson merged commit 45f74b9 into openhab:master Jan 7, 2019
@cdjackson cdjackson deleted the converter_refactor branch January 7, 2019 08:28
@cdjackson cdjackson added this to the 2.5 milestone Dec 8, 2019
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.

RFC: Split the configuration out of the channel converter initialise method
3 participants