]> git.basschouten.com Git - openhab-addons.git/blob
a11ede40b6ce1b9b51ffe65d036d66c66c3c93f9
[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.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.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;
29
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;
34
35 /**
36  * The {@link S20Handler} is responsible for handling commands, which are
37  * sent to one of the channels.
38  *
39  * @author Daniel Walters - Initial contribution
40  */
41 public class S20Handler extends BaseThingHandler implements SocketStateListener {
42
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 = () -> {
49         if (socket != null) {
50             socket.subscribe();
51         }
52     };
53
54     public S20Handler(Thing thing) {
55         super(thing);
56     }
57
58     @Override
59     public void initialize() {
60         configure();
61     }
62
63     @Override
64     public void dispose() {
65         subscribeHandler.cancel(true);
66         socket.removeSocketStateListener(this);
67     }
68
69     @Override
70     public void handleCommand(ChannelUID channelUID, Command command) {
71         if (channelUID.getId().equals(CHANNEL_S20_SWITCH)) {
72             try {
73                 if (command == OnOffType.ON) {
74                     socket.on();
75                 } else if (command == OnOffType.OFF) {
76                     socket.off();
77                 }
78             } catch (SocketException e) {
79                 logger.error("Error issuing command {} to socket {}", command, channelUID.getId());
80             }
81         }
82     }
83
84     private void configure() {
85         try {
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);
95         }
96     }
97
98     @Override
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);
103         }
104     }
105
106     @Override
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);
114             }
115         }
116     }
117
118     @Override
119     public void socketDidInitialisation(Socket socket) {
120         if (thing.getStatus() != ThingStatus.ONLINE) {
121             updateStatus(ThingStatus.ONLINE);
122         }
123     }
124 }