]> git.basschouten.com Git - openhab-addons.git/blob
9bab4cebe99032582b08633c3f593ea0caf4e051
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.somfytahoma.internal.handler;
14
15 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.STATUS;
16 import static org.openhab.core.thing.Thing.PROPERTY_FIRMWARE_VERSION;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaStatus;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26
27 /**
28  * The {@link SomfyTahomaGatewayHandler} is responsible for handling commands,
29  * which are sent to one of the channels of the gateway thing.
30  *
31  * @author Ondrej Pecta - Initial contribution
32  */
33 @NonNullByDefault
34 public class SomfyTahomaGatewayHandler extends SomfyTahomaBaseThingHandler {
35
36     public SomfyTahomaGatewayHandler(Thing thing) {
37         super(thing);
38     }
39
40     @Override
41     public void initializeThing(@Nullable ThingStatus bridgeStatus) {
42         if (bridgeStatus != null) {
43             if (bridgeStatus == ThingStatus.ONLINE) {
44                 refresh(STATUS);
45             } else {
46                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
47             }
48         } else {
49             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
50         }
51     }
52
53     @Override
54     public void refresh(String channel) {
55         String id = getGateWayId();
56         SomfyTahomaStatus status = getTahomaStatus(id);
57         String tahomaStatus = status.getStatus();
58         Channel ch = thing.getChannel(channel);
59         if (ch != null) {
60             updateState(ch.getUID(), new StringType(tahomaStatus));
61         }
62         // update the firmware property
63         String fw = status.getProtocolVersion();
64         updateProperty(PROPERTY_FIRMWARE_VERSION, fw);
65
66         updateStatus("DISCONNECTED".equals(tahomaStatus) ? ThingStatus.OFFLINE : ThingStatus.ONLINE);
67     }
68
69     public String getGateWayId() {
70         return getThing().getConfiguration().get("id").toString();
71     }
72 }