package com.basic.security.utils.socket; import android.os.SystemClock; import com.basic.security.utils.Constants; import org.apache.commons.io.IOUtils; import java.io.BufferedWriter; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class RelayServerUtil { public static final Lock lock = new ReentrantLock(); static ServerSocket serverSocket; static RelayServerUtilThread relayServerUtilThread = new RelayServerUtilThread(); static ExecutorService relayServerUtilExecutorService = Executors.newSingleThreadExecutor(); static { try { serverSocket = new ServerSocket(6000); } catch (Exception e) { e.printStackTrace(); } } public static String send(String instruction) throws Exception { String response = ""; try { Socket clientSocket = serverSocket.accept(); OutputStream outputStream = clientSocket.getOutputStream(); OutputStreamWriter opsw = new OutputStreamWriter(outputStream); BufferedWriter bw = new BufferedWriter(opsw); // bw.write("AT+STACH1=1\r\n"); // bw.write("AT+GANGCON=1,0\r\n"); // bw.write("AT+OCCH1=?\r\n"); // bw.write("AT+IP=?\r\n"); // bw.write("AT+gangcon=?\r\n"); // bw.write("AT+STACH1=?\r\n"); bw.write(instruction + "\r\n"); bw.flush(); Thread.sleep(100); clientSocket.shutdownOutput(); InputStream inputStream = clientSocket.getInputStream(); // Thread.sleep(1000); List readLines = IOUtils.readLines(inputStream); System.out.println(readLines); if (readLines != null && readLines.size() > 0) { response = readLines.get(0); } bw.close(); inputStream.close(); clientSocket.close(); } catch (Exception e) { e.printStackTrace(); throw e; } return response; } public static boolean getStatus() { try { Socket clientSocket = serverSocket.accept(); OutputStream outputStream = clientSocket.getOutputStream(); OutputStreamWriter opsw = new OutputStreamWriter(outputStream); BufferedWriter bw = new BufferedWriter(opsw); bw.write("AT+STACH1=?\r\n"); bw.flush(); Thread.sleep(100); clientSocket.shutdownOutput(); InputStream inputStream = clientSocket.getInputStream(); List readLines = IOUtils.readLines(inputStream); String line = readLines.get(0); String status = line.split(",", -1)[0].replace("+STACH1:", ""); if ("1".equals(status)) { return true; } System.out.println(status); bw.close(); inputStream.close(); clientSocket.close(); } catch (Exception e) { e.printStackTrace(); } return false; } public static void close() throws Exception { send("AT+STACH1=0"); } public static boolean openDoor() { lock.lock(); boolean success = false; try { if (getStatus()) { close(); } SystemClock.sleep(1000); send("AT+STACH1=1"); success = true; } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } return success; } public static void open() { try { if (!relayServerUtilThread.isRunning) { relayServerUtilThread.isRunning = true; relayServerUtilExecutorService.execute(relayServerUtilThread); } } catch (Exception e) { relayServerUtilThread.isRunning = false; e.printStackTrace(); } } public static class RelayServerUtilThread extends Thread { public boolean isRunning = false; public void run() { isRunning = true; try { openDoor(); } catch (Exception e) { e.printStackTrace(); } finally { isRunning = false; } } } }