2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.orvibo.internal.handler;
15 import static org.openhab.binding.orvibo.internal.OrviboBindingConstants.CHANNEL_S20_SWITCH;
17 import java.net.SocketException;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
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;
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;
37 * The {@link S20Handler} is responsible for handling commands, which are
38 * sent to one of the channels.
40 * @author Daniel Walters - Initial contribution
42 public class S20Handler extends BaseThingHandler implements SocketStateListener {
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 = () -> {
55 public S20Handler(Thing thing) {
60 public void initialize() {
65 public void dispose() {
66 subscribeHandler.cancel(true);
67 socket.removeSocketStateListener(this);
71 public void handleCommand(ChannelUID channelUID, Command command) {
72 if (channelUID.getId().equals(CHANNEL_S20_SWITCH)) {
74 if (command == OnOffType.ON) {
76 } else if (command == OnOffType.OFF) {
79 } catch (SocketException e) {
80 logger.error("Error issuing command {} to socket {}", command, channelUID.getId());
85 private void configure() {
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);
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);
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);
120 public void socketDidInitialisation(Socket socket) {
121 if (thing.getStatus() != ThingStatus.ONLINE) {
122 updateStatus(ThingStatus.ONLINE);