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,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.onewiregpio-${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-onewiregpio" description="OneWireGPIO Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.onewiregpio/${project.version}</bundle>
</feature>
</features>

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.onewiregpio.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;
/**
* The {@link OneWireGPIOBindingConstants} class defines common constants, which are
* used across the whole binding.
*
* @author Anatol Ogorek - Initial contribution
*/
@NonNullByDefault
public class OneWireGPIOBindingConstants {
public static final String BINDING_ID = "onewiregpio";
public static final ThingTypeUID THING_TYPE = new ThingTypeUID(BINDING_ID, "sensor");
public static final String TEMPERATURE = "temperature";
/**
* The default auto refresh time in seconds.
*/
public static final Integer DEFAULT_REFRESH_TIME = Integer.valueOf(120);
public static final int MAX_PRECISION_VALUE = 3;
public static final String FILE_TEMP_MARKER = "t=";
}

View File

@@ -0,0 +1,54 @@
/**
* 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.onewiregpio.internal;
import static org.openhab.binding.onewiregpio.internal.OneWireGPIOBindingConstants.THING_TYPE;
import java.util.Collections;
import java.util.Set;
import org.openhab.binding.onewiregpio.internal.handler.OneWireGPIOHandler;
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.Component;
/**
* The {@link OneWireGPIOHandlerFactory} is responsible for creating things and thing
* handlers.
*
* @author Anatol Ogorek - Initial contribution
*/
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.onewiregpio")
public class OneWireGPIOHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE);
@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
}
@Override
protected ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(THING_TYPE)) {
return new OneWireGPIOHandler(thing);
}
return null;
}
}

View File

@@ -0,0 +1,28 @@
/**
* 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.onewiregpio.internal;
import java.math.BigDecimal;
/**
* The {@link OneWireGpioConfiguration} Configuration class for easier configuration read through the reflection method
* in
* the framework.
*
* @author Konstantin Polihronov - Initial contribution
*/
public class OneWireGpioConfiguration {
public String gpio_bus_file;
public Integer refresh_time;
public BigDecimal precision;
}

View File

@@ -0,0 +1,194 @@
/**
* 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.onewiregpio.internal.handler;
import static org.openhab.binding.onewiregpio.internal.OneWireGPIOBindingConstants.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import org.apache.commons.lang.StringUtils;
import org.openhab.binding.onewiregpio.internal.OneWireGPIOBindingConstants;
import org.openhab.binding.onewiregpio.internal.OneWireGpioConfiguration;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.thing.Channel;
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.types.Command;
import org.openhab.core.types.RefreshType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link OneWireGPIOHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Anatol Ogorek - Initial contribution
* @author Konstantin Polihronov - Changed configuration handling and added new parameter - precision
*/
public class OneWireGPIOHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(OneWireGPIOHandler.class);
private String gpioBusFile;
private Integer refreshTime;
private Integer precision;
private ScheduledFuture<?> sensorRefreshJob;
public OneWireGPIOHandler(Thing thing) {
super(thing);
}
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
if (channelUID.getId().equals(TEMPERATURE)) {
if (command instanceof RefreshType) {
publishSensorValue(channelUID);
} else {
logger.debug("Command {} is not supported for channel: {}. Supported command: REFRESH", command,
channelUID.getId());
}
}
}
@Override
public void initialize() {
OneWireGpioConfiguration configuration = getConfigAs(OneWireGpioConfiguration.class);
gpioBusFile = configuration.gpio_bus_file;
refreshTime = configuration.refresh_time;
precision = configuration.precision.intValue();
logger.debug("GPIO Busfile={}, RefreshTime={}, precision={}", gpioBusFile, refreshTime, precision);
if (checkConfiguration()) {
startAutomaticRefresh();
updateStatus(ThingStatus.ONLINE);
}
}
/**
* This method checks if the provided configuration is valid.
* When invalid parameter is found, default value is assigned.
*/
private boolean checkConfiguration() {
if (StringUtils.isEmpty(gpioBusFile)) {
logger.debug("GPIO_BUS_FILE not set. Please check configuration, and set proper path to w1_slave file.");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"The path to the w1_slave sensor data file is missing.");
return false;
}
if (refreshTime <= 0) {
logger.debug("Refresh time [{}] is not valid. Falling back to default value: {}.", refreshTime,
DEFAULT_REFRESH_TIME);
refreshTime = DEFAULT_REFRESH_TIME;
}
if (precision < 0 || precision > MAX_PRECISION_VALUE) {
logger.debug(
"Precision value {} is outside allowed values [0 - {}]. Falling back to maximum precision value.",
precision, MAX_PRECISION_VALUE);
precision = MAX_PRECISION_VALUE;
}
return true;
}
private void startAutomaticRefresh() {
Runnable refresher = () -> {
List<Channel> channels = getThing().getChannels();
for (Channel channel : channels) {
if (isLinked(channel.getUID().getId())) {
publishSensorValue(channel.getUID());
}
}
};
sensorRefreshJob = scheduler.scheduleWithFixedDelay(refresher, 0, refreshTime.intValue(), TimeUnit.SECONDS);
logger.debug("Start automatic refresh every {} seconds", refreshTime.intValue());
}
private void publishSensorValue(ChannelUID channelUID) {
String channelID = channelUID.getId();
switch (channelID) {
case TEMPERATURE:
publishTemperatureSensorState(channelUID);
break;
default:
logger.debug("Can not update channel with ID : {} - channel name might be wrong!", channelID);
break;
}
}
private void publishTemperatureSensorState(ChannelUID channelUID) {
BigDecimal temp = readSensorTemperature(gpioBusFile);
if (temp != null) {
updateState(channelUID, new QuantityType<>(temp, SIUnits.CELSIUS));
}
}
private BigDecimal readSensorTemperature(String gpioFile) {
try (Stream<String> stream = Files.lines(Paths.get(gpioFile))) {
Optional<String> temperatureLine = stream
.filter(s -> s.contains(OneWireGPIOBindingConstants.FILE_TEMP_MARKER)).findFirst();
if (temperatureLine.isPresent()) {
String line = temperatureLine.get();
String tempString = line.substring(line.indexOf(OneWireGPIOBindingConstants.FILE_TEMP_MARKER)
+ OneWireGPIOBindingConstants.FILE_TEMP_MARKER.length());
Integer intTemp = Integer.parseInt(tempString);
if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);
}
return calculateValue(intTemp);
} else {
logger.debug(
"GPIO file didn't contain line with 't=' where temperature value should be available. Check if configuration points to the proper file");
return null;
}
} catch (IOException | InvalidPathException e) {
logger.debug("error reading GPIO bus file. File path is: {}. Check if path is proper.", gpioFile, e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error reading GPIO bus file.");
return null;
}
}
private BigDecimal calculateValue(Integer intTemp) {
BigDecimal result = BigDecimal.valueOf(intTemp).movePointLeft(3);
if (precision != MAX_PRECISION_VALUE) {
result = result.setScale(precision, RoundingMode.HALF_UP);
}
logger.debug("Thing = {}, temperature value = {}.", getThing().getUID(), result);
return result;
}
@Override
public void dispose() {
if (sensorRefreshJob != null) {
sensorRefreshJob.cancel(true);
}
super.dispose();
}
}

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<binding:binding id="onewiregpio" 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>OneWireGPIO Binding</name>
<description>Use GPIO in various devices like RaspberryPi to communicate the OneWire protocol</description>
<author>Anatol Ogorek</author>
</binding:binding>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="onewiregpio"
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">
<thing-type id="sensor">
<label>Temperature Sensor</label>
<description>OneWire GPIO Temperature sensor</description>
<channels>
<channel id="temperature" typeId="temperature"/>
</channels>
<config-description>
<parameter name="gpio_bus_file" type="text" required="true">
<label>Device Path</label>
<description>device id in format: /sys/bus/w1/devices/DEVICE_ID_TO_SET/w1_slave</description>
</parameter>
<parameter name="refresh_time" type="integer">
<label>Refresh Time Interval</label>
<description>Refresh time interval in seconds.</description>
<default>600</default>
</parameter>
<parameter name="precision" type="integer" min="0" max="3">
<label>Sensor Precision</label>
<description>Sensor precision after floating point.</description>
<required>false</required>
<default>3</default>
</parameter>
</config-description>
</thing-type>
<channel-type id="temperature">
<item-type>Number:Temperature</item-type>
<label>Temperature</label>
<description>Indicates the temperature read from one wire gpio sensor</description>
<state readOnly="true" pattern="%.3f %unit%"/>
</channel-type>
</thing:thing-descriptions>