a
554325746@qq.com
2019-12-25 5ca83635d670aff1070f616a50d3fc43cccf2e29
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
package com.basic.security.utils;
 
import android.os.Handler;
 
import com.basic.security.activity.MainActivity;
import com.basic.security.base.BaseApplication;
import com.basic.security.manager.ClearBeginManager;
import com.basic.security.manager.RealTimeMetricsManager;
import com.basic.security.manager.TodayBeginManager;
import com.basic.security.model.RealTimeMetrics;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
 
public class MetricsAcceptedClient extends Thread {
 
    Socket mSocket;
    private static final int MAX_BUFFER = 15;
    public static final String CRLF = System.getProperty("line.separator");
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    public MetricsAcceptedClient(Socket client) {
        mSocket = client;
    }
 
    public static String getAck() {
        String ack = "";
        String statusLine = "HTTP/1.0 200 OK"+CRLF;
        String contentTypeLine = "Content-type: text/html"+CRLF ;
        String connectionClose = "Connection: Keep-Alive"+CRLF ;
        String contentLengthLine = "Content-Length: 0"+CRLF +CRLF;;
        ack = statusLine + contentTypeLine+connectionClose+contentLengthLine;
        return ack;
    }
 
    public static void parseXml(String xml) {
        try {
            Map<String, String> realTimeMetrics = new HashMap<>();
 
            xml = xml.substring(xml.indexOf("<?xml version"));
            Document doc = Jsoup.parse(xml);
            Elements rTReportElements = doc.getElementsByTag("RTReport");
            Elements rTCountElements = doc.getElementsByTag("RTCount");
            Elements transmitTimeElements = doc.getElementsByTag("TransmitTime");
 
            realTimeMetrics.put(RealTimeMetrics.TransmitTime, transmitTimeElements.first().text());
            String Date = rTReportElements.first().attr("Date");
            Date = Date.replace("T", " ");
            realTimeMetrics.put(RealTimeMetrics.Date, sdf.parse(Date).getTime()+"");
            realTimeMetrics.put(RealTimeMetrics.TotalEnters, rTCountElements.first().attr("TotalEnters"));
            realTimeMetrics.put(RealTimeMetrics.TotalExits, rTCountElements.first().attr("TotalExits"));
 
            RealTimeMetricsManager.saveRealTimeMetrics(realTimeMetrics);
            ClearBeginManager.initClearBegin();
            ClearBeginManager.reset();
            TodayBeginManager.initTodayBegin();
 
            ((MainActivity)BaseApplication.getApplication().activity).fragment_home.refresh();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public void run() {
        BufferedInputStream inputStream = null;
        BufferedOutputStream outputStream = null;
        try {
            byte[] byteBuff = new byte[4090];
            inputStream = new BufferedInputStream(mSocket.getInputStream());
            outputStream = new BufferedOutputStream(mSocket.getOutputStream());
            String ack = getAck();
            while (true) {
                try {
                    int len = inputStream.read(byteBuff);
                    String xml = new String(byteBuff, 0, len);
                    outputStream.write(ack.getBytes());
                    outputStream.flush();
                    parseXml(xml);
                    if (Thread.currentThread().isInterrupted())
                        break;
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (mSocket != null) {
                try {
                    mSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
}