]> git.basschouten.com Git - openhab-addons.git/blob
deed510bb4df19be086a206b5f5c73f70f4f4b94
[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.NAError;
20 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
21 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
22 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.State;
26
27 /**
28  * {@link ChannelHelperCapability} give the capability to dispatch incoming data across the channel helpers.
29  * This capability is common to all things
30  *
31  * @author GaĆ«l L'hopital - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public class ChannelHelperCapability extends Capability {
36     private final List<ChannelHelper> channelHelpers;
37
38     public ChannelHelperCapability(CommonInterface handler, List<ChannelHelper> channelHelpers) {
39         super(handler);
40         this.channelHelpers = channelHelpers;
41     }
42
43     @Override
44     public void afterNewData(@Nullable NAObject newData) {
45         super.afterNewData(newData);
46         channelHelpers.forEach(helper -> helper.setNewData(newData));
47         handler.getActiveChannels().forEach(channel -> {
48             ChannelUID channelUID = channel.getUID();
49             String channelID = channelUID.getIdWithoutGroup();
50             String groupId = channelUID.getGroupId();
51             Configuration channelConfig = channel.getConfiguration();
52             for (ChannelHelper helper : channelHelpers) {
53                 State state = helper.getChannelState(channelID, groupId, channelConfig);
54                 if (state != null) {
55                     handler.updateState(channelUID, state);
56                     break;
57                 }
58             }
59         });
60     }
61
62     @Override
63     protected void updateErrors(NAError error) {
64         if (error.getId().equals(handler.getId())) {
65             statusReason = "@text/%s".formatted(error.getCode().message);
66         }
67     }
68 }