]> git.basschouten.com Git - openhab-addons.git/blob
520ec612709a57ef6879b9edc8105838b8ddad6f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.webthing.internal.link;
14
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;
25
26 /**
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
29  *
30  * @author Gregor Roth - Initial contribution
31  */
32 @NonNullByDefault
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;
39
40     /**
41      * establish a upstream link from a Channel to a WebThing property
42      *
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
48      */
49     public static void establish(ChannelHandler channelHandler, Channel channel, ConsumedThing webthing,
50             String propertyName) throws UnknownPropertyException {
51         new ChannelToPropertyLink(channelHandler, channel, webthing, propertyName);
52     }
53
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);
63             } else {
64                 this.typeConverter = TypeConverters.create(acceptedType, propertyType);
65             }
66             this.propertyName = propertyName;
67             channelHandler.observeChannel(channel.getUID(), this);
68         } else {
69             throw new UnknownPropertyException("property " + propertyName + " does not exits");
70         }
71     }
72
73     @Override
74     public void onItemStateChanged(ChannelUID channelUID, State stateCommand) {
75         try {
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,
81                     pae.getMessage());
82         }
83     }
84 }