2 * Copyright (c) 2010-2023 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.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.github.tavalin.s20.S20Client;
31 import com.github.tavalin.s20.Socket;
32 import com.github.tavalin.s20.Socket.SocketStateListener;
33 import com.github.tavalin.s20.entities.Types.PowerState;
36 * The {@link S20Handler} is responsible for handling commands, which are
37 * sent to one of the channels.
39 * @author Daniel Walters - Initial contribution
41 public class S20Handler extends BaseThingHandler implements SocketStateListener {
43 private final Logger logger = LoggerFactory.getLogger(S20Handler.class);
44 private Socket socket;
45 private S20Client client;
46 private ScheduledFuture<?> subscribeHandler;
47 private long refreshInterval = 15;
48 private Runnable subscribeTask = () -> {
54 public S20Handler(Thing thing) {
59 public void initialize() {
64 public void dispose() {
65 subscribeHandler.cancel(true);
66 socket.removeSocketStateListener(this);
70 public void handleCommand(ChannelUID channelUID, Command command) {
71 if (channelUID.getId().equals(CHANNEL_S20_SWITCH)) {
73 if (command == OnOffType.ON) {
75 } else if (command == OnOffType.OFF) {
78 } catch (SocketException e) {
79 logger.error("Error issuing command {} to socket {}", command, channelUID.getId());
84 private void configure() {
86 client = S20Client.getInstance();
87 String deviceId = thing.getUID().getId();
88 socket = client.socketWithDeviceId(deviceId);
89 socket.addSocketStateListener(this);
90 socket.findOnNetwork();
91 subscribeHandler = scheduler.scheduleWithFixedDelay(subscribeTask, 0, refreshInterval, TimeUnit.SECONDS);
92 updateStatus(ThingStatus.ONLINE);
93 } catch (SocketException ex) {
94 logger.error("Error occurred while initializing S20 handler: {}", ex.getMessage(), ex);
99 public void socketDidChangeLabel(Socket socket, String label) {
100 if (label != null && !label.isBlank()) {
101 logger.debug("Updating thing label to {}", label);
102 thing.setLabel(label);
107 public void socketDidChangePowerState(Socket socket, PowerState state) {
108 logger.debug("Received power state: {}", state);
109 if (socket.getDeviceId().equals(thing.getUID().getId())) {
110 if (state == PowerState.ON) {
111 updateState(CHANNEL_S20_SWITCH, OnOffType.ON);
112 } else if (state == PowerState.OFF) {
113 updateState(CHANNEL_S20_SWITCH, OnOffType.OFF);
119 public void socketDidInitialisation(Socket socket) {
120 if (thing.getStatus() != ThingStatus.ONLINE) {
121 updateStatus(ThingStatus.ONLINE);