]> git.basschouten.com Git - openhab-addons.git/blob
ad0298cbfefab8ff55251813b537ad22c7174205
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.orvibo.internal.handler;
14
15 import static org.openhab.binding.orvibo.internal.OrviboBindingConstants.CHANNEL_S20_SWITCH;
16
17 import java.net.SocketException;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.openhab.core.types.Command;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.github.tavalin.s20.S20Client;
32 import com.github.tavalin.s20.Socket;
33 import com.github.tavalin.s20.Socket.SocketStateListener;
34 import com.github.tavalin.s20.entities.Types.PowerState;
35
36 /**
37  * The {@link S20Handler} is responsible for handling commands, which are
38  * sent to one of the channels.
39  *
40  * @author Daniel Walters - Initial contribution
41  */
42 public class S20Handler extends BaseThingHandler implements SocketStateListener {
43
44     private final Logger logger = LoggerFactory.getLogger(S20Handler.class);
45     private Socket socket;
46     private S20Client client;
47     private ScheduledFuture<?> subscribeHandler;
48     private long refreshInterval = 15;
49     private Runnable subscribeTask = () -> {
50         if (socket != null) {
51             socket.subscribe();
52         }
53     };
54
55     public S20Handler(Thing thing) {
56         super(thing);
57     }
58
59     @Override
60     public void initialize() {
61         configure();
62     }
63
64     @Override
65     public void dispose() {
66         subscribeHandler.cancel(true);
67         socket.removeSocketStateListener(this);
68     }
69
70     @Override
71     public void handleCommand(ChannelUID channelUID, Command command) {
72         if (channelUID.getId().equals(CHANNEL_S20_SWITCH)) {
73             try {
74                 if (command == OnOffType.ON) {
75                     socket.on();
76                 } else if (command == OnOffType.OFF) {
77                     socket.off();
78                 }
79             } catch (SocketException e) {
80                 logger.error("Error issuing command {} to socket {}", command, channelUID.getId());
81             }
82         }
83     }
84
85     private void configure() {
86         try {
87             client = S20Client.getInstance();
88             String deviceId = thing.getUID().getId();
89             socket = client.socketWithDeviceId(deviceId);
90             socket.addSocketStateListener(this);
91             socket.findOnNetwork();
92             subscribeHandler = scheduler.scheduleWithFixedDelay(subscribeTask, 0, refreshInterval, TimeUnit.SECONDS);
93             updateStatus(ThingStatus.ONLINE);
94         } catch (SocketException ex) {
95             logger.error("Error occurred while initializing S20 handler: {}", ex.getMessage(), ex);
96         }
97     }
98
99     @Override
100     public void socketDidChangeLabel(Socket socket, String label) {
101         if (!StringUtils.isBlank(label)) {
102             logger.debug("Updating thing label to {}", label);
103             thing.setLabel(label);
104         }
105     }
106
107     @Override
108     public void socketDidChangePowerState(Socket socket, PowerState state) {
109         logger.debug("Received power state: {}", state);
110         if (socket.getDeviceId().equals(thing.getUID().getId())) {
111             if (state == PowerState.ON) {
112                 updateState(CHANNEL_S20_SWITCH, OnOffType.ON);
113             } else if (state == PowerState.OFF) {
114                 updateState(CHANNEL_S20_SWITCH, OnOffType.OFF);
115             }
116         }
117     }
118
119     @Override
120     public void socketDidInitialisation(Socket socket) {
121         if (thing.getStatus() != ThingStatus.ONLINE) {
122             updateStatus(ThingStatus.ONLINE);
123         }
124     }
125 }