]> git.basschouten.com Git - openhab-addons.git/blob
04f0e0563c787319c62b36cf18c3d9829d11e2ce
[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.netatmo.internal.handler.capability;
14
15 import java.util.List;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
20 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
21 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.types.State;
25
26 /**
27  * {@link ChannelHelperCapability} give the capability to dispatch incoming data across the channel helpers.
28  *
29  * @author GaĆ«l L'hopital - Initial contribution
30  *
31  */
32 @NonNullByDefault
33 public class ChannelHelperCapability extends Capability {
34     private final List<ChannelHelper> channelHelpers;
35
36     public ChannelHelperCapability(CommonInterface handler, List<ChannelHelper> channelHelpers) {
37         super(handler);
38         this.channelHelpers = channelHelpers;
39     }
40
41     @Override
42     public void afterNewData(@Nullable NAObject newData) {
43         super.afterNewData(newData);
44         channelHelpers.forEach(helper -> helper.setNewData(newData));
45         handler.getActiveChannels().forEach(channel -> {
46             ChannelUID channelUID = channel.getUID();
47             String channelID = channelUID.getIdWithoutGroup();
48             String groupId = channelUID.getGroupId();
49             Configuration channelConfig = channel.getConfiguration();
50             for (ChannelHelper helper : channelHelpers) {
51                 State state = helper.getChannelState(channelID, groupId, channelConfig);
52                 if (state != null) {
53                     handler.updateState(channelUID, state);
54                     break;
55                 }
56             }
57         });
58     }
59 }