-
-
Notifications
You must be signed in to change notification settings - Fork 427
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
Pure Java Comm - Initial Contribution #3632
Open
kgoderis
wants to merge
1
commit into
openhab:main
Choose a base branch
from
kgoderis:purejavacomm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
bundles/org.openhab.core.io.transport.serial.purejavacomm/.classpath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
<attribute name="optional" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
<attribute name="test" value="true"/> | ||
<attribute name="optional" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
<attribute name="test" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" path="target/generated-sources/annotations"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="test" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
34 changes: 34 additions & 0 deletions
34
bundles/org.openhab.core.io.transport.serial.purejavacomm/.project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>org.openhab.core.io.transport.serial.javacomm</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
<filteredResources> | ||
<filter> | ||
<id>1684860371393</id> | ||
<name></name> | ||
<type>30</type> | ||
<matcher> | ||
<id>org.eclipse.core.resources.regexFilterMatcher</id> | ||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments> | ||
</matcher> | ||
</filter> | ||
</filteredResources> | ||
</projectDescription> |
14 changes: 14 additions & 0 deletions
14
bundles/org.openhab.core.io.transport.serial.purejavacomm/NOTICE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
This content is produced and maintained by the openHAB project. | ||
|
||
* Project home: https://www.openhab.org | ||
|
||
== Declared Project Licenses | ||
|
||
This program and the accompanying materials are made available under the terms | ||
of the Eclipse Public License 2.0 which is available at | ||
https://www.eclipse.org/legal/epl-2.0/. | ||
|
||
== Source Code | ||
|
||
https://github.com/openhab/openhab-core | ||
|
28 changes: 28 additions & 0 deletions
28
bundles/org.openhab.core.io.transport.serial.purejavacomm/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.openhab.core.bundles</groupId> | ||
<artifactId>org.openhab.core.reactor.bundles</artifactId> | ||
<version>4.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>org.openhab.core.io.transport.serial.purejavacomm</artifactId> | ||
|
||
<name>openHAB Core :: Bundles :: Serial Transport for Pure Java Communications API</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.openhab.core.bundles</groupId> | ||
<artifactId>org.openhab.core.io.transport.serial</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.opensmarthouse</groupId> | ||
<artifactId>purejavacomm</artifactId> | ||
<version>1.0.5</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
99 changes: 99 additions & 0 deletions
99
...src/main/java/org/openhab/core/io/transport/serial/internal/PureJavaCommPortProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.core.io.transport.serial.internal; | ||
|
||
import java.net.URI; | ||
import java.util.Enumeration; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.io.transport.serial.ProtocolType; | ||
import org.openhab.core.io.transport.serial.ProtocolType.PathType; | ||
import org.openhab.core.io.transport.serial.SerialPortIdentifier; | ||
import org.openhab.core.io.transport.serial.SerialPortProvider; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import purejavacomm.CommPortIdentifier; | ||
import purejavacomm.NoSuchPortException; | ||
|
||
/** | ||
* | ||
* @author Łukasz Dywicki - Initial contribution | ||
* @author Karel Goderis - added further methods | ||
*/ | ||
@NonNullByDefault | ||
@Component(service = SerialPortProvider.class) | ||
public class PureJavaCommPortProvider implements SerialPortProvider { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(PureJavaCommPortProvider.class); | ||
|
||
@Override | ||
public @Nullable SerialPortIdentifier getPortIdentifier(URI port) { | ||
CommPortIdentifier ident = null; | ||
try { | ||
ident = CommPortIdentifier.getPortIdentifier(port.getPath()); | ||
} catch (NoSuchPortException e) { | ||
logger.debug("No SerialPortIdentifier found for: {}", port.getPath()); | ||
return null; | ||
} | ||
return new SerialPortIdentifierImpl(ident); | ||
} | ||
|
||
@Override | ||
public Stream<ProtocolType> getAcceptedProtocols() { | ||
return Stream.of(new ProtocolType(PathType.LOCAL, "purejavacomm")); | ||
} | ||
|
||
@Override | ||
public Stream<SerialPortIdentifier> getSerialPortIdentifiers() { | ||
@SuppressWarnings("unchecked") | ||
final Enumeration<CommPortIdentifier> ids = CommPortIdentifier.getPortIdentifiers(); | ||
return StreamSupport.stream(new SplitIteratorForEnumeration<>(ids), false) | ||
.filter(id -> id.getPortType() == CommPortIdentifier.PORT_SERIAL) | ||
.map(sid -> new SerialPortIdentifierImpl(sid)); | ||
} | ||
|
||
private static class SplitIteratorForEnumeration<T> extends Spliterators.AbstractSpliterator<T> { | ||
private final Enumeration<T> e; | ||
|
||
public SplitIteratorForEnumeration(final Enumeration<T> e) { | ||
super(Long.MAX_VALUE, Spliterator.ORDERED); | ||
this.e = e; | ||
} | ||
|
||
@Override | ||
@NonNullByDefault({}) | ||
public boolean tryAdvance(Consumer<? super T> action) { | ||
if (e.hasMoreElements()) { | ||
action.accept(e.nextElement()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
@NonNullByDefault({}) | ||
public void forEachRemaining(Consumer<? super T> action) { | ||
while (e.hasMoreElements()) { | ||
action.accept(e.nextElement()); | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...comm/src/main/java/org/openhab/core/io/transport/serial/internal/SerialPortEventImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.core.io.transport.serial.internal; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.io.transport.serial.SerialPortEvent; | ||
|
||
/** | ||
* Specific serial port event implementation. | ||
* | ||
* @author Łukasz Dywicki - Initial contribution | ||
* @author Karel Goderis - added further methods | ||
*/ | ||
@NonNullByDefault | ||
public class SerialPortEventImpl implements SerialPortEvent { | ||
|
||
private final purejavacomm.SerialPortEvent event; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param event the underlying event implementation | ||
*/ | ||
public SerialPortEventImpl(final purejavacomm.SerialPortEvent event) { | ||
this.event = event; | ||
} | ||
|
||
@Override | ||
public int getEventType() { | ||
return event.getEventType(); | ||
} | ||
|
||
@Override | ||
public boolean getNewValue() { | ||
return event.getNewValue(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...src/main/java/org/openhab/core/io/transport/serial/internal/SerialPortIdentifierImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.core.io.transport.serial.internal; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.io.transport.serial.PortInUseException; | ||
import org.openhab.core.io.transport.serial.SerialPort; | ||
import org.openhab.core.io.transport.serial.SerialPortIdentifier; | ||
|
||
import purejavacomm.CommPortIdentifier; | ||
|
||
/** | ||
* Specific serial port identifier implementation. | ||
* | ||
* @author Łukasz Dywicki - Initial contribution | ||
* @author Karel Goderis - added further methods | ||
*/ | ||
@NonNullByDefault | ||
public class SerialPortIdentifierImpl implements SerialPortIdentifier { | ||
|
||
final CommPortIdentifier id; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param id the underlying comm port identifier implementation | ||
*/ | ||
public SerialPortIdentifierImpl(final CommPortIdentifier id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
final String name = id.getName(); | ||
return name != null ? name : ""; | ||
} | ||
|
||
@Override | ||
public SerialPort open(String owner, int timeout) throws PortInUseException { | ||
|
||
try { | ||
return new SerialPortImpl((purejavacomm.SerialPort) id.open(owner, timeout)); | ||
} catch (purejavacomm.PortInUseException e) { | ||
String message = e.getMessage(); | ||
if (message != null) { | ||
throw new PortInUseException(message, e); | ||
} else { | ||
throw new PortInUseException(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isCurrentlyOwned() { | ||
return id.isCurrentlyOwned(); | ||
} | ||
|
||
@Override | ||
public @Nullable String getCurrentOwner() { | ||
return id.getCurrentOwner(); | ||
} | ||
|
||
public String toString() { | ||
return getName() + " (pure java comm)"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.