package com.basic.security.utils.socket;
|
|
import android.os.SystemClock;
|
|
import com.basic.security.base.BaseApplication;
|
import com.basic.security.manager.BaseSettingManager;
|
import com.basic.security.utils.Constants;
|
import com.basic.security.utils.IOUtil;
|
|
import java.util.LinkedList;
|
|
public class RelayServerUtil2 {
|
|
public static LinkedList<String> openDoorThreadQueue = new LinkedList<>();
|
public static long lastOpenDoorThread = System.currentTimeMillis();
|
public static long lastCloseDoorThread = System.currentTimeMillis();
|
|
public static boolean threadStarted = false;
|
|
public static void startOpenDoorThread() {
|
if (threadStarted) {
|
return;
|
}
|
threadStarted = true;
|
BaseApplication.getApplication().executorService.execute(() -> {
|
while (true) {
|
boolean normally_closed = BaseSettingManager.getNormallyClosed();
|
synchronized (openDoorThreadQueue) {
|
if (openDoorThreadQueue.size() > 0) {
|
openDoorThreadQueue.clear();
|
lastOpenDoorThread = System.currentTimeMillis();
|
if (normally_closed) {
|
IOUtil.initIOUtil().closeUNLOCK();
|
// System.out.println("RelayServerUtil2.startOpenDoorThread IOUtil.initIOUtil().closeUNLOCK(); " +normally_closed);
|
} else {
|
IOUtil.initIOUtil().openUNLOCK();
|
// System.out.println("RelayServerUtil2.startOpenDoorThread IOUtil.initIOUtil().openUNLOCK(); " + normally_closed);
|
}
|
// System.out.println("RelayServerUtil2.run 开门");
|
}
|
}
|
long pass = (System.currentTimeMillis() - lastOpenDoorThread);
|
long getOpenDoorDurationSeconds = BaseSettingManager.getOpenDoorDurationSeconds() * 1000;
|
// System.out.println("RelayServerUtil2.run pass="+pass+", " + getOpenDoorDurationSeconds+" " + (pass > getOpenDoorDurationSeconds));
|
if (pass > getOpenDoorDurationSeconds) {
|
lastCloseDoorThread = System.currentTimeMillis();
|
if (normally_closed) {
|
IOUtil.initIOUtil().openUNLOCK();
|
// System.out.println("RelayServerUtil2.startOpenDoorThread IOUtil.initIOUtil().openUNLOCK(); " + normally_closed);
|
} else {
|
IOUtil.initIOUtil().closeUNLOCK();
|
// System.out.println("RelayServerUtil2.startOpenDoorThread IOUtil.initIOUtil().closeUNLOCK(); " + normally_closed);
|
}
|
// System.out.println("RelayServerUtil2.run 关门");
|
}
|
SystemClock.sleep(1000);
|
}
|
});
|
}
|
|
public static boolean open() {
|
if (Constants.useAlf) {
|
synchronized (openDoorThreadQueue) {
|
openDoorThreadQueue.add(System.currentTimeMillis() + "");
|
}
|
// System.out.println("RelayServerUtil2.open");
|
// IOUtil.initIOUtil().openUNLOCK();
|
return true;
|
} else {
|
System.out.println("RelayServerUtil2.open");
|
return true;
|
}
|
|
// boolean success = false;
|
// BufferedInputStream inputStream = null;
|
// BufferedOutputStream outputStream = null;
|
// Socket socket = null;
|
// ByteArrayOutputStream byteArray = null;
|
// try {
|
// socket = new Socket("192.168.1.100", 6722);
|
// socket.setSoTimeout(1000);
|
// inputStream = new BufferedInputStream(socket.getInputStream());
|
// outputStream = new BufferedOutputStream(socket.getOutputStream());
|
//
|
// outputStream.write("00".getBytes()); // 查询
|
//// outputStream.write("21".getBytes()); // ch1 释放 返回00011000
|
//// outputStream.write("11".getBytes()); // ch1 吸合 返回10011000
|
//// outputStream.write("11:2".getBytes()); // CH1 吸合,30 秒后自动释放 返回10011000
|
// outputStream.flush();
|
//
|
// byte[] buff = new byte[256];
|
// int len = 0;
|
//
|
// String ret = "";
|
//
|
// while ((len = inputStream.read(buff)) != -1) {
|
// ret = new String(buff, "UTF-8");
|
// System.out.println(ret);
|
// Log.e("test111", ret);
|
// break;
|
// }
|
// if ("1".equals(ret.charAt(0)+"")) {
|
// outputStream.write("21".getBytes()); // ch1 释放 返回00011000
|
// } else {
|
// outputStream.write("11".getBytes()); // ch1 吸合 返回10011000
|
// }
|
// outputStream.flush();
|
//
|
// while ((len = inputStream.read(buff)) != -1) {
|
// ret = new String(buff, "ISO8859-1");
|
//// System.out.println(ret);
|
// Log.e("test222", ret);
|
// break;
|
// }
|
//
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// } finally {
|
// try {
|
// if (outputStream != null) {
|
// outputStream.close();
|
// outputStream = null;
|
// }
|
//
|
// if (inputStream != null) {
|
// inputStream.close();
|
// inputStream = null;
|
// }
|
//
|
// if (socket != null) {
|
// socket.close();
|
// socket = null;
|
// }
|
//
|
// if (byteArray != null) {
|
// byteArray.close();
|
// }
|
//
|
// } catch (IOException e) {
|
//
|
// }
|
// }
|
// return success;
|
}
|
}
|