added migrated 2.x add-ons

Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
Kai Kreuzer
2020-09-21 01:58:32 +02:00
parent bbf1a7fd29
commit 6df6783b60
11662 changed files with 1302875 additions and 11 deletions

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.urtsi-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>
<feature name="openhab-binding-urtsi" description="Somfy URTSI II Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<feature>openhab-transport-serial</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.urtsi/${project.version}</bundle>
</feature>
</features>

View File

@@ -0,0 +1,44 @@
/**
* Copyright (c) 2010-2020 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.urtsi.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;
/**
* The {@link UrtsiBinding} class defines common constants, which are
* used across the whole binding.
*
* @author Oliver Libutzki - Initial contribution
*/
@NonNullByDefault
public class UrtsiBindingConstants {
public static final String BINDING_ID = "urtsi";
// List of all Thing Type UIDs
/**
* URTSI II box
*/
public static final ThingTypeUID URTSI_DEVICE_THING_TYPE = new ThingTypeUID(BINDING_ID, "urtsidevice");
/**
* RTS Device (e.g. rollershutter)
*/
public static final ThingTypeUID RTS_DEVICE_THING_TYPE = new ThingTypeUID(BINDING_ID, "rtsdevice");
/**
* Rollershutter's position
*/
public static final String POSITION = "position";
}

View File

@@ -0,0 +1,72 @@
/**
* Copyright (c) 2010-2020 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.urtsi.internal;
import static org.openhab.binding.urtsi.internal.UrtsiBindingConstants.*;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.urtsi.internal.handler.RtsDeviceHandler;
import org.openhab.binding.urtsi.internal.handler.UrtsiDeviceHandler;
import org.openhab.core.io.transport.serial.SerialPortManager;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* The {@link UrtsiHandlerFactory} is responsible for creating things and thing
* handlers.
*
* @author Oliver Libutzki - Initial contribution
*/
@NonNullByDefault
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.urtsi")
public class UrtsiHandlerFactory extends BaseThingHandlerFactory {
private static final List<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Arrays.asList(URTSI_DEVICE_THING_TYPE,
RTS_DEVICE_THING_TYPE);
private final SerialPortManager serialPortManager;
@Activate
public UrtsiHandlerFactory(final @Reference SerialPortManager serialPortManager) {
this.serialPortManager = serialPortManager;
}
@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
}
@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(URTSI_DEVICE_THING_TYPE) && thing instanceof Bridge) {
return new UrtsiDeviceHandler((Bridge) thing, serialPortManager);
} else if (thingTypeUID.equals(RTS_DEVICE_THING_TYPE)) {
return new RtsDeviceHandler(thing);
}
return null;
}
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (c) 2010-2020 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.urtsi.internal.config;
/**
* Configuration of a RTS Device (e.g. rollershutter)
*
* @author Oliver Libutzki - Initial contribution
*
*/
public class RtsDeviceConfig {
public String channel;
}

View File

@@ -0,0 +1,25 @@
/**
* Copyright (c) 2010-2020 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.urtsi.internal.config;
/**
* Configuration of a URTSI II Device
*
* @author Oliver Libutzki - Initial contribution
*
*/
public class UrtsiDeviceConfig {
public String port;
public int commandInterval;
}

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) 2010-2020 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.urtsi.internal.handler;
/**
* The {@code RtsCommand} provides the available commands due to Somfy's RTS protocol.
*
* @author Oliver Libutzki - Initial contribution
*
*/
public enum RtsCommand {
UP("U"),
DOWN("D"),
STOP("S");
private String actionKey;
private RtsCommand(String actionKey) {
this.actionKey = actionKey;
}
/**
* Returns the action key which is used for communicating with the URTSI II device.
*
* @return the action key
*/
public String getActionKey() {
return actionKey;
}
}

View File

@@ -0,0 +1,94 @@
/**
* Copyright (c) 2010-2020 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.urtsi.internal.handler;
import org.openhab.binding.urtsi.internal.UrtsiBindingConstants;
import org.openhab.binding.urtsi.internal.config.RtsDeviceConfig;
import org.openhab.binding.urtsi.internal.mapping.UrtsiChannelMapping;
import org.openhab.core.library.types.StopMoveType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
/**
* The {@link RtsDeviceHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Oliver Libutzki - Initial contribution
*/
public class RtsDeviceHandler extends BaseThingHandler {
public RtsDeviceHandler(Thing thing) {
super(thing);
}
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
if (channelUID.getId().equals(UrtsiBindingConstants.POSITION)) {
RtsCommand rtsCommand = null;
if (command instanceof UpDownType) {
switch ((UpDownType) command) {
case UP:
rtsCommand = RtsCommand.UP;
break;
case DOWN:
rtsCommand = RtsCommand.DOWN;
break;
default:
break;
}
} else if (command instanceof StopMoveType) {
switch ((StopMoveType) command) {
case STOP:
rtsCommand = RtsCommand.STOP;
break;
default:
break;
}
}
if (rtsCommand != null) {
// We delegate the execution to the bridge handler
ThingHandler bridgeHandler = getBridge().getHandler();
if (bridgeHandler instanceof UrtsiDeviceHandler) {
boolean executedSuccessfully = ((UrtsiDeviceHandler) bridgeHandler).executeRtsCommand(getThing(),
rtsCommand);
if (executedSuccessfully && command instanceof State) {
updateState(channelUID, (State) command);
}
}
}
}
}
@Override
public void initialize() {
RtsDeviceConfig rtsDeviceConfig = getConfigAs(RtsDeviceConfig.class);
String mappedChannel = UrtsiChannelMapping.getMappedChannel(rtsDeviceConfig.channel);
if (mappedChannel == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"The channel '" + rtsDeviceConfig.channel + "' is invalid.");
} else {
// Just use the status of the bridge as we do not have any information if there a RTS device listening at
// the configured channel
ThingStatus bridgeStatus = getBridge().getStatus();
updateStatus(bridgeStatus);
}
}
}

View File

@@ -0,0 +1,231 @@
/**
* Copyright (c) 2010-2020 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.urtsi.internal.handler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.TooManyListenersException;
import java.util.stream.Collectors;
import org.openhab.binding.urtsi.internal.config.RtsDeviceConfig;
import org.openhab.binding.urtsi.internal.config.UrtsiDeviceConfig;
import org.openhab.binding.urtsi.internal.mapping.UrtsiChannelMapping;
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.SerialPortManager;
import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseBridgeHandler;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link UrtsiDeviceHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Oliver Libutzki - Initial contribution
*/
public class UrtsiDeviceHandler extends BaseBridgeHandler {
private final Logger logger = LoggerFactory.getLogger(UrtsiDeviceHandler.class);
private static final int BAUD = 9600;
private static final int DATABITS = SerialPort.DATABITS_8;
private static final int STOPBIT = SerialPort.STOPBITS_1;
private static final int PARITY = SerialPort.PARITY_NONE;
private int commandInterval;
private String address;
private long lastCommandTime;
private SerialPortIdentifier portId;
private SerialPort serialPort;
private final SerialPortManager serialPortManager;
private OutputStream outputStream;
private InputStream inputStream;
public UrtsiDeviceHandler(Bridge bridge, SerialPortManager serialPortManager) {
super(bridge);
this.serialPortManager = serialPortManager;
}
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
// the bridge does not have any channels
}
/**
* Executes the given {@link RtsCommand} for the given {@link Thing} (RTS Device).
*
* @param rtsDevice the RTS Device which is the receiver of the command.
* @param rtsCommand the command to be executed
* @return
*/
public boolean executeRtsCommand(Thing rtsDevice, RtsCommand rtsCommand) {
RtsDeviceConfig rtsDeviceConfig = rtsDevice.getConfiguration().as(RtsDeviceConfig.class);
String mappedChannel = UrtsiChannelMapping.getMappedChannel(rtsDeviceConfig.channel);
if (mappedChannel == null) {
return false;
}
String urtsiCommand = new StringBuilder(address).append(mappedChannel).append(rtsCommand.getActionKey())
.toString();
boolean executedSuccessfully = writeString(urtsiCommand);
return executedSuccessfully;
}
/**
* Sends a string to the serial port of this device.
* The writing of the msg is executed synchronized, so it's guaranteed that the device doesn't get
* multiple messages concurrently.
*
* @param msg the string to send
* @return true, if the message has been transmitted successfully, otherwise false.
*/
protected synchronized boolean writeString(final String msg) {
logger.debug("Writing '{}' to serial port {}", msg, portId.getName());
final long earliestNextExecution = lastCommandTime + commandInterval;
while (earliestNextExecution > System.currentTimeMillis()) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
return false;
}
}
try {
final List<Boolean> listenerResult = new ArrayList<>();
serialPort.addEventListener(new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
// we get here if data has been received
final StringBuilder sb = new StringBuilder();
final byte[] readBuffer = new byte[20];
try {
do {
// read data from serial device
while (inputStream.available() > 0) {
final int bytes = inputStream.read(readBuffer);
sb.append(new String(readBuffer, 0, bytes));
}
try {
// add wait states around reading the stream, so that interrupted transmissions are
// merged
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore interruption
}
} while (inputStream.available() > 0);
final String result = sb.toString();
if (result.equals(msg)) {
listenerResult.add(true);
}
} catch (IOException e) {
logger.debug("Error receiving data on serial port {}: {}", portId.getName(),
e.getMessage());
}
}
}
});
serialPort.notifyOnDataAvailable(true);
outputStream.write(msg.getBytes());
outputStream.flush();
lastCommandTime = System.currentTimeMillis();
final long timeout = lastCommandTime + 1000;
while (listenerResult.isEmpty() && System.currentTimeMillis() < timeout) {
// Waiting for response
Thread.sleep(100);
}
return !listenerResult.isEmpty();
} catch (IOException | TooManyListenersException | InterruptedException e) {
logger.error("Error writing '{}' to serial port {}: {}", msg, portId.getName(), e.getMessage());
} finally {
serialPort.removeEventListener();
}
return false;
}
@Override
public void initialize() {
address = getThing().getProperties().get("address");
UrtsiDeviceConfig urtsiDeviceConfig = getConfigAs(UrtsiDeviceConfig.class);
commandInterval = urtsiDeviceConfig.commandInterval;
String port = urtsiDeviceConfig.port;
portId = serialPortManager.getIdentifier(port);
if (portId == null) {
String availablePorts = serialPortManager.getIdentifiers().map(id -> id.getName())
.collect(Collectors.joining(System.lineSeparator()));
String description = String.format("Serial port '%s' could not be found. Available ports are:%n%s", port,
availablePorts);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, description);
return;
}
try {
serialPort = portId.open("openHAB", 2000);
serialPort.setSerialPortParams(BAUD, DATABITS, STOPBIT, PARITY);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
updateStatus(ThingStatus.ONLINE);
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error: " + e.getMessage());
} catch (PortInUseException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Port already used: " + port);
} catch (UnsupportedCommOperationException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Unsupported operation on port '" + port + "': " + e.getMessage());
}
}
@Override
public void dispose() {
super.dispose();
if (serialPort != null) {
serialPort.removeEventListener();
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
logger.debug("Error while closing the output stream: {}", e.getMessage());
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.debug("Error while closing the input stream: {}", e.getMessage());
}
}
if (serialPort != null) {
serialPort.close();
}
outputStream = null;
inputStream = null;
serialPort = null;
}
}

View File

@@ -0,0 +1,41 @@
/**
* Copyright (c) 2010-2020 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.urtsi.internal.mapping;
/**
* The {@code UrtsiChannelMapping} is responsible for mapping the channel you select at the URTSI II device to the
* channel which is transmitted via the serial port.
*
* @author Oliver Libutzki - Initial contribution
*
*/
public class UrtsiChannelMapping {
/**
* Returns the mapped channel which is used to communicate with the URTSI II device. Returns null if the given
* channel is not valid.
*
* @param configuredChannel the channel selected at the URTSI II device
* @return returns the mapped channel, returns null is the given channel is not valid.
*/
public static String getMappedChannel(String configuredChannel) {
int channel = Integer.parseInt(configuredChannel, 16);
if (channel == 0) {
channel = 16;
}
if (channel < 1 || channel > 16) {
return null;
}
return String.format("%02d", channel);
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<binding:binding id="urtsi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:binding="https://openhab.org/schemas/binding/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">
<name>Somfy URTSI II Binding</name>
<description>@text/bindingDescription</description>
<author>Oliver Libutzki</author>
</binding:binding>

View File

@@ -0,0 +1,18 @@
# binding
bindingDescription = Dies ist das Binding für Somfy Universal RTS Interface II (URTSI II).
# thing types
urtsiDeviceLabel = Somfy URTSI II Gerät
urtsiDeviceDescription = Dies ist die Somfy URTSI II Box.
urtsiDevicePortLabel = Port
urtsiDevicePortDescription = Der Port, über den das Gerät angesprochen wird (z.B. /dev/ttyUSB0)
urtsiDeviceCommandIntervalLabel = Befehlausführungsintervall
urtsiDeviceCommandIntervalDescription = Die Zeit (in ms), die das Binding zwischen dem Absenden von zwei Befehlen warten soll
rtsDeviceLabel = Somfy RTS Gerät
rtsDeviceDescription = Dies ist ein Gerät, das über RTS kommuniziert (z.B. ein Rolladenmotor).
rtsDeviceChannelLabel = Kanal
rtsDeviceChannelDescription = Der Kanel, der dem RTS Gerät am URTSI II zugewiesen wurde
positionChannelLabel = Position des RTS Geräts
positionChannelDescription = Ändern der Position des RTS Gerätes

View File

@@ -0,0 +1,18 @@
# binding
bindingDescription = This is the binding for Somfy Universal RTS Interface II (URTSI II).
# thing types
urtsiDeviceLabel = Somfy URTSI II Device
urtsiDeviceDescription = This is the Somfy URTSI II box.
urtsiDevicePortLabel = Port
urtsiDevicePortDescription = The port which is used to access the device (e.g. /dev/ttyUSB0)
urtsiDeviceCommandIntervalLabel = Command execution interval
urtsiDeviceCommandIntervalDescription = The time (in ms) the binding should wait between sending commands to the device
rtsDeviceLabel = Somfy RTS Device
rtsDeviceDescription = This is the RTS device (e.g. a rollershutter).
rtsDeviceChannelLabel = Channel
rtsDeviceChannelDescription = The URTSI II channel the RTS device is assigned to at URTSI II.
positionChannelLabel = RTS Device position
positionChannelDescription = Change the position of your device

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="urtsi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">
<bridge-type id="urtsidevice">
<label>@text/urtsiDeviceLabel</label>
<description>@text/urtsiDeviceDescription</description>
<properties>
<property name="address">01</property>
</properties>
<config-description>
<parameter name="port" type="text" required="true">
<label>@text/urtsiDevicePortLabel</label>
<context>serial-port</context>
<limitToOptions>false</limitToOptions>
<description>@text/urtsiDevicePortDescription</description>
</parameter>
<parameter name="commandInterval" type="integer" required="false" min="50" max="5000">
<label>@text/urtsiDeviceCommandIntervalLabel</label>
<description>@text/urtsiDeviceCommandIntervalDescription</description>
<default>100</default>
</parameter>
</config-description>
</bridge-type>
<thing-type id="rtsdevice">
<supported-bridge-type-refs>
<bridge-type-ref id="urtsidevice"/>
</supported-bridge-type-refs>
<label>@text/rtsDeviceLabel</label>
<description>@text/rtsDeviceDescription</description>
<channels>
<channel id="position" typeId="position"/>
</channels>
<config-description>
<parameter name="channel" type="text" pattern="([0-9]|[A-F]|[a-f])">
<label>@text/rtsDeviceChannelLabel</label>
<description>@text/rtsDeviceChannelDescription</description>
</parameter>
</config-description>
</thing-type>
<channel-type id="position">
<item-type>Rollershutter</item-type>
<label>@text/positionChannelLabel</label>
<description>@text/positionChannelDescription</description>
</channel-type>
</thing:thing-descriptions>