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

[pilight] Pilight Binding initial contribution + add discovery #9744

Merged
merged 5 commits into from
Feb 17, 2021

Conversation

niklasdoerfler
Copy link
Member

@niklasdoerfler niklasdoerfler commented Jan 8, 2021

Migrate the already existing pilight binding from openhab1-addons (https://www.openhab.org/addons/bindings/pilight1/) to openHAB 3. Also some discovery logic to auto discover the pilight daemon as a bridge and the configured devices from the pilight daemon is added.

This PR is based on the still unmerged PR #8469, which could be closed afterwards, since it was used as a basis for this PR.

Built .jar of the binding is available here: https://openhab.jfrog.io/artifactory/libs-pullrequest-local/org/openhab/addons/bundles/org.openhab.binding.pilight/3.1.0-SNAPSHOT/org.openhab.binding.pilight-3.1.0-SNAPSHOT.jar

@niklasdoerfler niklasdoerfler requested a review from a team as a code owner January 8, 2021 16:55
@fwolter fwolter added new binding If someone has started to work on a binding. For a new binding PR. oh1 migration Relates to migrating an openHAB 1 addon to openHAB 2 labels Jan 8, 2021
@wborn wborn added rebuild Triggers Jenkins PR build and removed rebuild Triggers Jenkins PR build labels Jan 10, 2021
@stefanroellin
Copy link
Contributor

Thanks for taking care of my pull request. Since I have not yet switched to openHAB 3, I didn't continue with it.
However I use the binding in my openHAB 2 installation since about four months without any issues.

@wborn wborn added rebuild Triggers Jenkins PR build and removed rebuild Triggers Jenkins PR build labels Jan 19, 2021
@niklasdoerfler
Copy link
Member Author

niklasdoerfler commented Jan 20, 2021

I don't know why, but for some reason the last line break at the end of file bundles/pom.xml was accidentally removed, so the build failed in the spotless check. I have fixed that now.

@wborn could you please trigger a new build again? Thanks!
This time jenkins detected the changes automatically.

@niklasdoerfler
Copy link
Member Author

Could someone please set the rebuild label again? Thanks!

Jenkins complained about the following in the last build, which has nothing to do with this binding from my point of view...

[ERROR] /home/jenkins/jenkins-agent1/workspace/PR-openHAB-Addons@4/bundles/org.openhab.binding.dsmr/src/test/java/org/openhab/binding/dsmr/internal/device/DSMRSerialAutoDeviceTest.java:[136,23] The constructor org.openhab.core.io.transport.serial.PortInUseException() is undefined

@fwolter fwolter self-assigned this Jan 28, 2021
Copy link
Member

@fwolter fwolter 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 your contribution! I reviewed your code and here is my feedback.

You code doesn't build, because the license header's year parameter isn't updated, yet.

To make the binding compile, you need to rebase your branch.

Here are the commands for rebasing your branch:

If not already done, add the upstream openHAB addon repo as a remote to your local repo and fetch it:

git remote add upstream https://github.com/openhab/openhab-addons.git
git fetch upstream

The remotes (git remote --verbose) should look like this, now:

origin  https://github.com/[your name]/openhab-addons.git (fetch)
origin  https://github.com/[your name]/openhab-addons.git (push)
upstream        https://github.com/openhab/openhab-addons.git (fetch)
upstream        https://github.com/openhab/openhab-addons.git (push)

Then, you can rebase your PR's branch onto main:

git rebase upstream/main

Finally force-push the rebased branch to this PR's branch:

git push origin [your branch name of this PR] --force-with-lease

DON'T use merge! This can clutter the Git history and make the PR unusable.

bundles/org.openhab.binding.pilight/README.md Outdated Show resolved Hide resolved
bundles/org.openhab.binding.pilight/README.md Outdated Show resolved Hide resolved
bundles/org.openhab.binding.pilight/README.md Outdated Show resolved Hide resolved
bundles/org.openhab.binding.pilight/README.md Outdated Show resolved Hide resolved
Comment on lines 17 to 33
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.4</version>
</dependency>
</dependencies>
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why you don't use the default libraries for JSON processing? See https://www.openhab.org/docs/developer/guidelines.html#default-libraries

Copy link
Member Author

Choose a reason for hiding this comment

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

I continue using jackson, because most of the code has been adopted from the Pilight binding for openHAB v1 and some functions are based on Jackson for which there is no direct equivalent in Gson. Maybe we can try to replace Jackson by Gson later.

Also-by: Stefan Roellin stefan@roellin-baumann.ch
Signed-off-by: Niklas Dörfler <niklas@doerfler-el.de>
@niklasdoerfler
Copy link
Member Author

Thank you @fwolter for your detailed comments. I'll take a look at the points and get back here as soon as I'm done with them.
I have already made the rebase and the Jenkins build is now passing successfully (link to the built can be found in the first comment).

Signed-off-by: Niklas Dörfler <niklas@doerfler-el.de>
@cpmeister cpmeister self-assigned this Feb 9, 2021
Signed-off-by: Niklas Dörfler <niklas@doerfler-el.de>
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.

Can you also try to address the compiler warnings that show up when building your project?


final Scanner scanner = new Scanner(new ByteArrayInputStream(recvData),
StandardCharsets.UTF_8);
scanner.findAll("Location:([0-9.]+):(.*)").forEach(matchResult -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can avoid making loop an AtomicBoolean by using Stream.peek().count() instead of foreach. You can use the returned count value to set your loop value.

Suggested change
scanner.findAll("Location:([0-9.]+):(.*)").forEach(matchResult -> {
scanner.findAll("Location:([0-9.]+):(.*)").peek(matchResult -> {

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, but is it good practise to use peek for this? According to the documentation, it is mainly intended for debugging.

This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline.

(see https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-)

@niklasdoerfler
Copy link
Member Author

Can you also try to address the compiler warnings that show up when building your project?

@cpmeister: Yes, I already tried to get rid of the compiler warnings, but to be honest I'm not quite sure, why some of the warnings occur and how to fix them, e.g.:

@Nullable Object test;

if (test != null) {
   test.doSomething();
}

This leads to a Potential null pointer access: this expression has a '@Nullable' type warning at the method call "test.doSomething()". Why does the compiler see a null pointer access here and how should I fix this?

@cpmeister
Copy link
Contributor

In order to resolve most of the compiler warnings you need to cache the @Nullable field to a local variable and then perform all of your logic on the local variable instead of on the field directly. The reason that it is issuing this warning is that the system will make the assumption that a nullable field can become null at any time due to multithreading. The warnings can be a little overzealous but they do help cut down almost all of the NPEs that we might otherwise see.

@niklasdoerfler
Copy link
Member Author

I resolved most of the compiler warnings, except for two, that I'm not sure how to solve:

[WARNING] pom.xml [0:0]: Unused Export-Package instructions: [org.openhab.*]
[WARNING] pom.xml [0:0]: Unused Import-Package instructions: [io.swagger.v3.oas.annotations.*, javax.annotation.security.*, org.eclipse.jdt.annotation.*,
org.openhab.core.automation.annotation.*, com.google.common.*]

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.

You did a fantastic job on addressing the warnings. The remaining warnings don't need to be addressed.
I think there is only this last change then we are good to go.

Comment on lines 18 to 32
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.4</version>
</dependency>
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these libraries are already available to you from the core. They are version 2.10.1 though. Would it be alright to use those instead? If it doesn't work to remove these dependencies from the pom file then you can just manually specify them as a provided dependency like so:

Suggested change
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.1</version>
<scope>provided</scope>
</dependency>

If you are forced to add it as a provided dependency then you will also need to add those dependencies to the feature.xml file as well. Not terribly difficult but I would rather have you use an existing dependency than to bring in a new one.

Copy link
Member Author

@niklasdoerfler niklasdoerfler Feb 16, 2021

Choose a reason for hiding this comment

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

In the first step, I removed the dependencies from the .pom and my local build was successful. Let's see what Jenkins says. Looks like Jenkins is happy too.

Comment on lines 241 to 244
if (delayedActionWorkerFuture != null) {
delayedActionWorkerFuture.cancel(false);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This would cancel the already canceled future.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not quite sure, what you mean. This call takes place in the lambda function of the fixed delay scheduler. In each run of it, I check if there are remaining elements in the delayedActionQueue and poll one of them to call doSendAction(). If the queue is empty I need to cancel the scheduler. I think it is a valid case here or am I wrong?
Maybe you can explain in more detail what you are thinking?

Copy link
Contributor

@cpmeister cpmeister Feb 16, 2021

Choose a reason for hiding this comment

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

I'm saying that the old code referenced the delayedActionWorkerFuture field, but now it is referencing the delayedActionWorkerFuture local variable. The local variable has already been determined to be cancelled at this point in the code. It clear that your intent is to cancel the future field rather than the local variable. You need to make a second local variable inside the lambda to grab a fresh reference of the field.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah sure, now I get your point!

Signed-off-by: Niklas Dörfler <niklas@doerfler-el.de>
@cpmeister cpmeister added rebuild Triggers Jenkins PR build and removed rebuild Triggers Jenkins PR build labels Feb 16, 2021
@cpmeister cpmeister added the cre Coordinated Review Effort label Feb 17, 2021
…thing/bridge.xml


Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
@fwolter fwolter merged commit c0cec80 into openhab:main Feb 17, 2021
Copy link
Member

@fwolter fwolter left a comment

Choose a reason for hiding this comment

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

LGTM

@fwolter fwolter added this to the 3.1 milestone Feb 17, 2021
@fwolter fwolter removed the cre Coordinated Review Effort label Feb 17, 2021
themillhousegroup pushed a commit to themillhousegroup/openhab2-addons that referenced this pull request May 10, 2021
Signed-off-by: Niklas Dörfler <niklas@doerfler-el.de>
Signed-off-by: John Marshall <john.marshall.au@gmail.com>
thinkingstone pushed a commit to thinkingstone/openhab-addons that referenced this pull request Nov 7, 2021
Signed-off-by: Niklas Dörfler <niklas@doerfler-el.de>
marcfischerboschio pushed a commit to bosch-io/openhab-addons that referenced this pull request May 5, 2022
Signed-off-by: Niklas Dörfler <niklas@doerfler-el.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new binding If someone has started to work on a binding. For a new binding PR. oh1 migration Relates to migrating an openHAB 1 addon to openHAB 2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants