|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2025 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.core.model.yaml.internal.items; |
| 14 | + |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | + |
| 22 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 23 | +import org.openhab.core.common.registry.AbstractProvider; |
| 24 | +import org.openhab.core.config.core.Configuration; |
| 25 | +import org.openhab.core.items.ItemProvider; |
| 26 | +import org.openhab.core.model.yaml.internal.util.YamlElementUtils; |
| 27 | +import org.openhab.core.thing.ChannelUID; |
| 28 | +import org.openhab.core.thing.link.ItemChannelLink; |
| 29 | +import org.openhab.core.thing.link.ItemChannelLinkProvider; |
| 30 | +import org.openhab.core.thing.profiles.ProfileTypeUID; |
| 31 | +import org.osgi.service.component.annotations.Component; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +/** |
| 36 | + * This class serves as a provider for all item channel links that is found within YAML files. |
| 37 | + * It is filled with content by the {@link YamlItemProvider}, which cannot itself implement the |
| 38 | + * {@link ItemChannelLinkProvider} interface as it already implements {@link ItemProvider}, |
| 39 | + * which would lead to duplicate methods. |
| 40 | + * |
| 41 | + * @author Laurent Garnier - Initial contribution |
| 42 | + */ |
| 43 | +@NonNullByDefault |
| 44 | +@Component(immediate = true, service = { ItemChannelLinkProvider.class, YamlChannelLinkProvider.class }) |
| 45 | +public class YamlChannelLinkProvider extends AbstractProvider<ItemChannelLink> implements ItemChannelLinkProvider { |
| 46 | + |
| 47 | + private final Logger logger = LoggerFactory.getLogger(YamlChannelLinkProvider.class); |
| 48 | + |
| 49 | + // Map the channel links to each channel UID and then to each item name and finally to each model name |
| 50 | + private Map<String, Map<String, Map<ChannelUID, ItemChannelLink>>> itemsChannelLinksMap = new ConcurrentHashMap<>(); |
| 51 | + |
| 52 | + @Override |
| 53 | + public Collection<ItemChannelLink> getAll() { |
| 54 | + return itemsChannelLinksMap.values().stream().flatMap(m -> m.values().stream()) |
| 55 | + .flatMap(m -> m.values().stream()).toList(); |
| 56 | + } |
| 57 | + |
| 58 | + public Collection<ItemChannelLink> getAllFromModel(String modelName) { |
| 59 | + return itemsChannelLinksMap.getOrDefault(modelName, Map.of()).values().stream() |
| 60 | + .flatMap(m -> m.values().stream()).toList(); |
| 61 | + } |
| 62 | + |
| 63 | + public void updateItemChannelLinks(String modelName, String itemName, Map<String, Configuration> channelLinks) { |
| 64 | + Map<String, Map<ChannelUID, ItemChannelLink>> channelLinksMap = Objects |
| 65 | + .requireNonNull(itemsChannelLinksMap.computeIfAbsent(modelName, k -> new ConcurrentHashMap<>())); |
| 66 | + // Create a HashMap with an initial capacity of 2 (the default is 16) to save memory because most items have |
| 67 | + // only one channel. A capacity of 2 is enough to avoid resizing the HashMap in most cases, whereas 1 would |
| 68 | + // trigger a resize as soon as one element is added. |
| 69 | + Map<ChannelUID, ItemChannelLink> links = Objects |
| 70 | + .requireNonNull(channelLinksMap.computeIfAbsent(itemName, k -> new ConcurrentHashMap<>(2))); |
| 71 | + |
| 72 | + Set<ChannelUID> linksToBeRemoved = new HashSet<>(links.keySet()); |
| 73 | + |
| 74 | + for (Map.Entry<String, Configuration> entry : channelLinks.entrySet()) { |
| 75 | + String channelUID = entry.getKey(); |
| 76 | + Configuration configuration = entry.getValue(); |
| 77 | + |
| 78 | + ChannelUID channelUIDObject; |
| 79 | + try { |
| 80 | + channelUIDObject = new ChannelUID(channelUID); |
| 81 | + } catch (IllegalArgumentException e) { |
| 82 | + logger.warn("Invalid channel UID '{}' in channel link for item '{}'!", channelUID, itemName, e); |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + // Fix the configuration in case a profile is defined without any scope |
| 87 | + if (configuration.containsKey("profile") && configuration.get("profile") instanceof String profile |
| 88 | + && profile.indexOf(":") == -1) { |
| 89 | + String fullProfile = ProfileTypeUID.SYSTEM_SCOPE + ":" + profile; |
| 90 | + configuration.put("profile", fullProfile); |
| 91 | + logger.info( |
| 92 | + "Profile '{}' for channel '{}' is missing the scope prefix, assuming the correct UID is '{}'. Check your configuration.", |
| 93 | + profile, channelUID, fullProfile); |
| 94 | + } |
| 95 | + |
| 96 | + ItemChannelLink itemChannelLink = new ItemChannelLink(itemName, channelUIDObject, configuration); |
| 97 | + |
| 98 | + linksToBeRemoved.remove(channelUIDObject); |
| 99 | + ItemChannelLink oldLink = links.get(channelUIDObject); |
| 100 | + if (oldLink == null) { |
| 101 | + links.put(channelUIDObject, itemChannelLink); |
| 102 | + logger.debug("notify added item channel link {}", itemChannelLink.getUID()); |
| 103 | + notifyListenersAboutAddedElement(itemChannelLink); |
| 104 | + } else if (!YamlElementUtils.equalsConfig(configuration.getProperties(), |
| 105 | + oldLink.getConfiguration().getProperties())) { |
| 106 | + links.put(channelUIDObject, itemChannelLink); |
| 107 | + logger.debug("notify updated item channel link {}", itemChannelLink.getUID()); |
| 108 | + notifyListenersAboutUpdatedElement(oldLink, itemChannelLink); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + linksToBeRemoved.forEach(uid -> { |
| 113 | + ItemChannelLink link = links.remove(uid); |
| 114 | + if (link != null) { |
| 115 | + logger.debug("notify removed item channel link {}", link.getUID()); |
| 116 | + notifyListenersAboutRemovedElement(link); |
| 117 | + } |
| 118 | + }); |
| 119 | + if (links.isEmpty()) { |
| 120 | + channelLinksMap.remove(itemName); |
| 121 | + } |
| 122 | + if (channelLinksMap.isEmpty()) { |
| 123 | + itemsChannelLinksMap.remove(modelName); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments