]> git.basschouten.com Git - openhab-addons.git/blob
d257816d18eef5c94ecc025b70ec1c76c9b57190
[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.yeelight.internal.lib.device.connection;
14
15 import java.io.BufferedOutputStream;
16 import java.io.BufferedReader;
17 import java.io.InputStreamReader;
18 import java.net.Socket;
19
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;
25
26 /**
27  * Created by jiang on 16/10/25.
28  *
29  * @author Coaster Li - Initial contribution
30  */
31
32 public class WifiConnection implements ConnectionBase {
33
34     private final Logger logger = LoggerFactory.getLogger(WifiConnection.class);
35
36     private static final String TAG = WifiConnection.class.getSimpleName();
37
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;
44
45     public WifiConnection(DeviceBase device) {
46         mDevice = device;
47     }
48
49     @Override
50     public boolean invoke(DeviceMethod method) {
51         if (mWriter != null) {
52             try {
53                 mWriter.write(method.getParamsStr().getBytes());
54                 mWriter.flush();
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);
60                 return false;
61             }
62             return true;
63         }
64         return false;
65     }
66
67     @Override
68     public boolean invokeCustom(DeviceMethod method) {
69         if (mWriter != null) {
70             try {
71                 mWriter.write(method.getCustomParamsStr().getBytes());
72                 mWriter.flush();
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);
78                 return false;
79             }
80             return true;
81         }
82         return false;
83     }
84
85     @Override
86     public boolean connect() {
87         logger.debug("{}: connect() entering!", TAG);
88         if (mSocket != null && mSocket.isConnected()) {
89             logger.debug("{}: socket not null, return!", TAG);
90             return true;
91         }
92         mConnectThread = new Thread(() -> {
93             try {
94                 mCmdRun = true;
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);
101                 while (mCmdRun) {
102                     try {
103                         String value = mReader.readLine();
104                         logger.debug("{}: get response: {}", TAG, value);
105                         if (value == null) {
106                             mCmdRun = false;
107                         } else {
108                             mDevice.onNotify(value);
109                         }
110                     } catch (Exception e) {
111                         logger.debug("Exception", e);
112                         mCmdRun = false;
113                     }
114                 }
115                 mSocket.close();
116             } catch (Exception e) {
117                 logger.debug("{}: connect device! ERROR! {}", TAG, e.getMessage());
118                 logger.debug("Exception", e);
119             } finally {
120                 mDevice.setConnectionState(ConnectState.DISCONNECTED);
121                 mSocket = null;
122             }
123         });
124         mConnectThread.start();
125         return false;
126     }
127
128     @Override
129     public boolean disconnect() {
130         mDevice.setAutoConnect(false);
131         return false;
132     }
133 }