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.yeelight.internal.lib.device.connection;
15 import java.io.BufferedOutputStream;
16 import java.io.BufferedReader;
17 import java.io.InputStreamReader;
18 import java.net.Socket;
20 import org.openhab.binding.yeelight.internal.lib.device.ConnectState;
21 import org.openhab.binding.yeelight.internal.lib.device.DeviceBase;
22 import org.openhab.binding.yeelight.internal.lib.device.DeviceMethod;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Created by jiang on 16/10/25.
29 * @author Coaster Li - Initial contribution
32 public class WifiConnection implements ConnectionBase {
34 private final Logger logger = LoggerFactory.getLogger(WifiConnection.class);
36 private static final String TAG = WifiConnection.class.getSimpleName();
38 private Socket mSocket;
39 private BufferedReader mReader;
40 private BufferedOutputStream mWriter;
41 private Thread mConnectThread;
42 private DeviceBase mDevice;
43 private boolean mCmdRun = false;
45 public WifiConnection(DeviceBase device) {
50 public boolean invoke(DeviceMethod method) {
51 if (mWriter != null) {
53 mWriter.write(method.getParamsStr().getBytes());
55 logger.debug("{}: Write Success!", TAG);
56 } catch (Exception e) {
57 logger.debug("{}: write exception, set device to disconnected!", TAG);
58 logger.debug("Exception", e);
59 mDevice.setConnectionState(ConnectState.DISCONNECTED);
68 public boolean invokeCustom(DeviceMethod method) {
69 if (mWriter != null) {
71 mWriter.write(method.getCustomParamsStr().getBytes());
73 logger.debug("{}: Write Success!", TAG);
74 } catch (Exception e) {
75 logger.debug("{}: write exception, set device to disconnected!", TAG);
76 logger.debug("Exception", e);
77 mDevice.setConnectionState(ConnectState.DISCONNECTED);
86 public boolean connect() {
87 logger.debug("{}: connect() entering!", TAG);
88 if (mSocket != null && mSocket.isConnected()) {
89 logger.debug("{}: socket not null, return!", TAG);
92 mConnectThread = new Thread(() -> {
95 logger.debug("{}: connect device! {}, {}", TAG, mDevice.getAddress(), mDevice.getPort());
96 mSocket = new Socket(mDevice.getAddress(), mDevice.getPort());
97 mSocket.setKeepAlive(true);
98 mWriter = new BufferedOutputStream(mSocket.getOutputStream());
99 mReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
100 mDevice.setConnectionState(ConnectState.CONNECTED);
103 String value = mReader.readLine();
104 logger.debug("{}: get response: {}", TAG, value);
108 mDevice.onNotify(value);
110 } catch (Exception e) {
111 logger.debug("Exception", e);
116 } catch (Exception e) {
117 logger.debug("{}: connect device! ERROR! {}", TAG, e.getMessage());
118 logger.debug("Exception", e);
120 mDevice.setConnectionState(ConnectState.DISCONNECTED);
124 mConnectThread.start();
129 public boolean disconnect() {
130 mDevice.setAutoConnect(false);
133 if (mConnectThread != null) {
134 mConnectThread.interrupt();
136 if (mSocket != null) {
139 } catch (Exception e) {
140 logger.debug("Exception while terminating connection", e);
143 mDevice.setConnectionState(ConnectState.DISCONNECTED);