554325746@qq.com
2019-08-07 2539f53391765abb74b6fe63f46e5a2c701e950f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.basic.security.utils.socket;
 
import android.os.SystemClock;
import android.util.Log;
 
import com.basic.security.manager.impl.cblite.BaseSettingManager;
import com.basic.security.utils.Constants;
import com.basic.security.utils.IOUtil;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
 
public class RelayServerUtil2 {
 
    public static LinkedList<String> openDoorThreadQueue = new LinkedList<>();
    public static long lastOpenDoorThread = System.currentTimeMillis();
    public static void startOpenDoorThread() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    synchronized (openDoorThreadQueue) {
                        if (openDoorThreadQueue.size() > 0) {
                            openDoorThreadQueue.clear();
                            lastOpenDoorThread = System.currentTimeMillis();
                            IOUtil.initIOUtil().openUNLOCK();
                            System.out.println("RelayServerUtil2.run 开门");
                        }
                    }
                    if (System.currentTimeMillis() - lastOpenDoorThread > BaseSettingManager.getOpenDoorDurationSeconds() * 1000) {
                        IOUtil.initIOUtil().closeUNLOCK();
                        System.out.println("RelayServerUtil2.run 关门");
                    }
                    SystemClock.sleep(1000);
                }
            }
        }.start();
    }
 
    public static boolean open() {
        if (Constants.useAef) {
            synchronized (openDoorThreadQueue) {
                openDoorThreadQueue.add(System.currentTimeMillis()+"");
            }
//            System.out.println("RelayServerUtil2.open");
//            IOUtil.initIOUtil().openUNLOCK();
            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;
    }
}