]> git.basschouten.com Git - openhab-addons.git/blob
acef67b57a44f246379728df41ae7cfa628a8e9f
[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.echonetlite.internal;
14
15 import static java.util.Objects.requireNonNull;
16 import static org.openhab.binding.echonetlite.internal.EchonetLiteBindingConstants.PROPERTY_NAME_INSTANCE_KEY;
17
18 import java.net.InetSocketAddress;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.binding.BaseThingHandler;
33 import org.openhab.core.thing.binding.builder.ChannelBuilder;
34 import org.openhab.core.thing.binding.builder.ThingBuilder;
35 import org.openhab.core.thing.type.ChannelTypeUID;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
38 import org.openhab.core.types.State;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * The {@link EchonetLiteHandler} is responsible for handling commands, which are
44  * sent to one of the channels.
45  *
46  * @author Michael Barker - Initial contribution
47  */
48 @NonNullByDefault
49 public class EchonetLiteHandler extends BaseThingHandler implements EchonetDeviceListener {
50     private final Logger logger = LoggerFactory.getLogger(EchonetLiteHandler.class);
51
52     private @Nullable InstanceKey instanceKey;
53     private final Map<String, State> stateByChannelId = new HashMap<>();
54
55     public EchonetLiteHandler(final Thing thing) {
56         super(thing);
57     }
58
59     @Nullable
60     private EchonetLiteBridgeHandler bridgeHandler() {
61         @Nullable
62         final Bridge bridge = getBridge();
63         if (null == bridge) {
64             return null;
65         }
66
67         @Nullable
68         final EchonetLiteBridgeHandler handler = (EchonetLiteBridgeHandler) bridge.getHandler();
69         return handler;
70     }
71
72     @Override
73     public void handleCommand(ChannelUID channelUID, Command command) {
74         @Nullable
75         final EchonetLiteBridgeHandler handler = bridgeHandler();
76         if (null == handler) {
77             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
78                     "@text/offline.conf-error.null-bridge-handler");
79             return;
80         }
81
82         if (command instanceof RefreshType) {
83             logger.debug("Refreshing: {}", channelUID);
84
85             final State currentState = stateByChannelId.get(channelUID.getId());
86             if (null == currentState) {
87                 handler.refreshDevice(requireNonNull(instanceKey), channelUID.getId());
88             } else {
89                 updateState(channelUID, currentState);
90             }
91         } else if (command instanceof State stateCommand) {
92             logger.debug("Updating: {} to {}", channelUID, command);
93
94             handler.updateDevice(requireNonNull(instanceKey), channelUID.getId(), stateCommand);
95         }
96     }
97
98     @Override
99     public void initialize() {
100         final EchonetDeviceConfig config = getConfigAs(EchonetDeviceConfig.class);
101
102         logger.debug("Initialising: {}", config);
103
104         updateStatus(ThingStatus.UNKNOWN);
105
106         @Nullable
107         final EchonetLiteBridgeHandler bridgeHandler = bridgeHandler();
108         if (null == bridgeHandler) {
109             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
110                     "@text/offline.conf-error.null-bridge-handler");
111             return;
112         }
113
114         try {
115             final InetSocketAddress address = new InetSocketAddress(requireNonNull(config.hostname), config.port);
116             final InstanceKey instanceKey = new InstanceKey(address,
117                     EchonetClass.resolve(config.groupCode, config.classCode), config.instance);
118             this.instanceKey = instanceKey;
119
120             updateProperty(PROPERTY_NAME_INSTANCE_KEY, instanceKey.representationProperty());
121             bridgeHandler.newDevice(instanceKey, config.pollIntervalMs, config.retryTimeoutMs, this);
122         } catch (Exception e) {
123             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
124         }
125     }
126
127     @Override
128     public void handleRemoval() {
129         @Nullable
130         final EchonetLiteBridgeHandler bridgeHandler = bridgeHandler();
131         if (null == bridgeHandler) {
132             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
133                     "@text/offline.conf-error.null-bridge-handler");
134             return;
135         }
136
137         bridgeHandler.removeDevice(requireNonNull(instanceKey));
138     }
139
140     @Override
141     public void onInitialised(String identifier, InstanceKey instanceKey, Map<String, String> channelIdAndType) {
142         logger.debug("Initialised Channels: {}", channelIdAndType);
143
144         final List<String> toAddChannelFor = new ArrayList<>();
145
146         for (String channelId : channelIdAndType.keySet()) {
147             if (null == thing.getChannel(channelId)) {
148                 toAddChannelFor.add(channelId);
149             }
150         }
151
152         logger.debug("Adding Channels: {}", toAddChannelFor);
153
154         if (!toAddChannelFor.isEmpty()) {
155             final ThingBuilder thingBuilder = editThing();
156
157             for (String channelId : toAddChannelFor) {
158                 final Channel channel = ChannelBuilder.create(new ChannelUID(thing.getUID(), channelId))
159                         .withAcceptedItemType(channelIdAndType.get(channelId))
160                         .withType(new ChannelTypeUID(thing.getThingTypeUID().getBindingId(), channelId)).build();
161                 thingBuilder.withChannel(channel);
162
163                 logger.debug("Added Channel: {}", channel);
164             }
165
166             updateThing(thingBuilder.build());
167         }
168
169         updateStatus(ThingStatus.ONLINE);
170     }
171
172     @Override
173     public void onUpdated(final String channelId, final State value) {
174         stateByChannelId.put(channelId, value);
175
176         if (ThingStatus.ONLINE != getThing().getStatus()) {
177             updateStatus(ThingStatus.ONLINE);
178         }
179         updateState(channelId, value);
180     }
181
182     @Override
183     public void onRemoved() {
184         updateStatus(ThingStatus.REMOVED);
185     }
186
187     @Override
188     public void onOffline() {
189         if (ThingStatus.OFFLINE != getThing().getStatus()) {
190             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
191         }
192     }
193 }