forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[openwebnet] Replace gnu.io dependency with serial transport (openhab…
…#16376) Signed-off-by: Massimo Valla <[email protected]>
- Loading branch information
Showing
7 changed files
with
295 additions
and
19 deletions.
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
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
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
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
145 changes: 145 additions & 0 deletions
145
...ebnet/src/main/java/org/openhab/binding/openwebnet/internal/serial/SerialPortAdapter.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,145 @@ | ||
/** | ||
* Copyright (c) 2010-2024 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.openwebnet.internal.serial; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.TooManyListenersException; | ||
|
||
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.SerialPortEvent; | ||
import org.openhab.core.io.transport.serial.SerialPortEventListener; | ||
import org.openhab.core.io.transport.serial.SerialPortIdentifier; | ||
import org.openhab.core.io.transport.serial.UnsupportedCommOperationException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* openwebnet4j SerialPort implementation based on OH serial transport | ||
* | ||
* @author M. Valla - Initial contribution | ||
*/ | ||
|
||
@NonNullByDefault | ||
public class SerialPortAdapter implements org.openwebnet4j.communication.serial.spi.SerialPort { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(SerialPortAdapter.class); | ||
|
||
private static final int OPEN_TIMEOUT_MS = 200; | ||
|
||
private final SerialPortIdentifier spid; | ||
|
||
private @Nullable SerialPort sp = null; | ||
|
||
public SerialPortAdapter(final SerialPortIdentifier spid) { | ||
this.spid = spid; | ||
} | ||
|
||
@Override | ||
public boolean setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity) { | ||
@Nullable | ||
SerialPort lsp = sp; | ||
if (lsp != null) { | ||
try { | ||
lsp.setSerialPortParams(baudrate, dataBits, stopBits, parity); | ||
return true; | ||
} catch (UnsupportedCommOperationException e) { | ||
logger.error("UnsupportedCommOperationException while setting port params in setSerialPortParams: {}", | ||
e.getMessage()); | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean addEventListener(org.openwebnet4j.communication.serial.spi.SerialPortEventListener listener) { | ||
@Nullable | ||
SerialPort lsp = sp; | ||
if (lsp != null) { | ||
try { | ||
lsp.addEventListener(new SerialPortEventListener() { | ||
|
||
@Override | ||
public void serialEvent(SerialPortEvent event) { | ||
if (event != null) { | ||
listener.serialEvent(new SerialPortEventAdapter(event)); | ||
} | ||
} | ||
}); | ||
lsp.notifyOnDataAvailable(true); | ||
return true; | ||
} catch (TooManyListenersException e) { | ||
logger.error("TooManyListenersException while adding event listener: {}", e.getMessage()); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean open() { | ||
try { | ||
sp = spid.open(this.getClass().getName(), OPEN_TIMEOUT_MS); | ||
} catch (PortInUseException e) { | ||
logger.error("PortInUseException while opening serial port {}: {}", spid.getName(), e.getMessage()); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public @Nullable String getName() { | ||
@Nullable | ||
SerialPort lsp = sp; | ||
if (lsp != null) { | ||
return lsp.getName(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public @Nullable InputStream getInputStream() throws IOException { | ||
@Nullable | ||
SerialPort lsp = sp; | ||
if (lsp != null) { | ||
return lsp.getInputStream(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public @Nullable OutputStream getOutputStream() throws IOException { | ||
@Nullable | ||
SerialPort lsp = sp; | ||
if (lsp != null) { | ||
return lsp.getOutputStream(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
@Nullable | ||
SerialPort lsp = sp; | ||
if (lsp != null) { | ||
lsp.close(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
.../src/main/java/org/openhab/binding/openwebnet/internal/serial/SerialPortEventAdapter.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,48 @@ | ||
/** | ||
* Copyright (c) 2010-2024 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.openwebnet.internal.serial; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openwebnet4j.communication.serial.spi.SerialPortEvent; | ||
|
||
/** | ||
* openwebnet4j SerialPortEvent implementation based on OH serial transport | ||
* | ||
* @author M. Valla - Initial contribution | ||
*/ | ||
|
||
@NonNullByDefault | ||
public class SerialPortEventAdapter implements SerialPortEvent { | ||
|
||
private final org.openhab.core.io.transport.serial.SerialPortEvent event; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param event the underlying event implementation | ||
*/ | ||
public SerialPortEventAdapter(org.openhab.core.io.transport.serial.SerialPortEvent event) { | ||
this.event = event; | ||
} | ||
|
||
@Override | ||
public int getEventType() { | ||
if (event.getEventType() == org.openhab.core.io.transport.serial.SerialPortEvent.PORT_DISCONNECTED) { | ||
return SerialPortEvent.EVENT_PORT_DISCONNECTED; | ||
} else if (event.getEventType() == org.openhab.core.io.transport.serial.SerialPortEvent.DATA_AVAILABLE) { | ||
return SerialPortEvent.EVENT_DATA_AVAILABLE; | ||
} else { | ||
return event.getEventType(); | ||
} | ||
} | ||
} |
Oops, something went wrong.