2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.webthing.internal.link;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.webthing.internal.ChannelHandler;
17 import org.openhab.binding.webthing.internal.WebThingHandler;
18 import org.openhab.binding.webthing.internal.client.ConsumedThing;
19 import org.openhab.binding.webthing.internal.client.PropertyAccessException;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.types.State;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * The {@link ChannelToPropertyLink} represents an upstream link from a Channel to a WebThing property.
28 * This link is used to update a the value of a property
30 * @author Gregor Roth - Initial contribution
33 public class ChannelToPropertyLink implements WebThingHandler.ItemChangedListener {
34 private final Logger logger = LoggerFactory.getLogger(ChannelToPropertyLink.class);
35 private final String propertyName;
36 private final String propertyType;
37 private final ConsumedThing webThing;
38 private final TypeConverter typeConverter;
41 * establish a upstream link from a Channel to a WebThing property
43 * @param channelHandler the channel handler that provides registering an ItemChangedListener
44 * @param channel the channel to be linked
45 * @param webthing the WebThing to be linked
46 * @param propertyName the property name
47 * @throws UnknownPropertyException if the a WebThing property should be link that does not exist
49 public static void establish(ChannelHandler channelHandler, Channel channel, ConsumedThing webthing,
50 String propertyName) throws UnknownPropertyException {
51 new ChannelToPropertyLink(channelHandler, channel, webthing, propertyName);
54 private ChannelToPropertyLink(ChannelHandler channelHandler, Channel channel, ConsumedThing webThing,
55 String propertyName) throws UnknownPropertyException {
56 this.webThing = webThing;
57 var optionalProperty = webThing.getThingDescription().getProperty(propertyName);
58 if (optionalProperty.isPresent()) {
59 this.propertyType = optionalProperty.get().type;
60 var acceptedType = channel.getAcceptedItemType();
61 if (acceptedType == null) {
62 this.typeConverter = TypeConverters.create("String", propertyType);
64 this.typeConverter = TypeConverters.create(acceptedType, propertyType);
66 this.propertyName = propertyName;
67 channelHandler.observeChannel(channel.getUID(), this);
69 throw new UnknownPropertyException("property " + propertyName + " does not exits");
74 public void onItemStateChanged(ChannelUID channelUID, State stateCommand) {
76 var propertyValue = typeConverter.toPropertyValue(stateCommand);
77 webThing.writeProperty(propertyName, typeConverter.toPropertyValue((State) stateCommand));
78 logger.debug("property {} updated with {} ({}) ", propertyName, propertyValue, this.propertyType);
79 } catch (PropertyAccessException pae) {
80 logger.warn("could not write WebThing property {} with new channel value. {}", propertyName,