liuxiaolong
2019-11-25 cf7d6f0ee626527abd97f2845d4491452cf3a8ea
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.cloud.count.listener;
 
import com.cloud.count.dao.CountDao;
import com.cloud.count.model.CountState;
import com.cloud.count.model.People;
import com.cloud.count.service.serviceImpl.CountServiceImpl;
import com.cloud.count.utils.XmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class CountThread implements Runnable {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
 
    private CountDao dao;
 
    static final String CRLF = System.getProperty("line.separator");
 
    private Socket client;
 
    public CountThread(Socket socket) {
        this.dao = BeanContext.getBean("countDao");
        this.client = socket;
    }
 
    String encoding = "UTF-8";
    BufferedReader iReader = null;
    BufferedWriter iWriter = null;
 
    @Override
    public void run() {
        try{
            iReader = new BufferedReader(new InputStreamReader(client.getInputStream(), encoding));
            iWriter = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), encoding));
            String msgStr = "";
            String xmlStr = "";
            People people = null;
            InputStream is = client.getInputStream();
            byte[] b;
            while (true) {
                b = new byte[1024];
                is.read(b);
                msgStr = new String(b);
                System.out.println("msgStr:"+msgStr);
                sendAck(iWriter);
 
                xmlStr = XmlUtil.toXmlStr(msgStr);
 
                // 如果存在TotalEnters。则证明返回的是实时数据的xml
                if(xmlStr.indexOf("TotalEnters")>-1){
                    people = XmlUtil.realTimeXmlToPeople(xmlStr);
                    int inCreEnter = CountServiceImpl.addEnter(people.getEnters());//进入人数增量
                    int inCreExit = CountServiceImpl.addExit(people.getExits());//出去人数增量
                    CountServiceImpl.totalEnters += inCreEnter;
                    CountServiceImpl.totalExits += inCreExit;
 
                    people.setEnters(CountServiceImpl.totalEnters);//将累加后的数量存到数据库中
                    people.setExits(CountServiceImpl.totalExits);
                    boolean resultB = dao.savePeople(people);
 
                    //总进入数量或者总出去数量,不一定哪一个先到2字节int的上限65535
//                                if( CountServiceImpl.totalEnters < CountServiceImpl.baseEnters){
//                                    //表示统计设备重新启动了,统计参数归零
//                                    CountServiceImpl.initCountArgs(people);
//                                }
 
                    //总进入人数小于总出去的人数,表示统计设备在放学时间(出去的人数大于进入的人数)重启了
                    if(CountServiceImpl.totalEnters < CountServiceImpl.totalExits){
                        CountServiceImpl.errorCount = CountServiceImpl.totalExits - CountServiceImpl.totalEnters;
                    }else
                        CountServiceImpl.errorCount = 0;
 
                    CountState currentState = new CountState();
                    currentState.setBaseEnters(CountServiceImpl.baseEnters);
                    currentState.setBaseExits(CountServiceImpl.baseExits);
                    currentState.setTotalEnters(CountServiceImpl.totalEnters);
                    currentState.setTotalExits(CountServiceImpl.totalExits);
                    currentState.setRealtimeBaseEnters(CountServiceImpl.realtimeBaseEnters);
                    currentState.setRealtimeBaseExits(CountServiceImpl.realtimebBaseExits);
                    currentState.setErrorCount(CountServiceImpl.errorCount);
                    currentState.setBaseTime(people.getTimestamp());
                    dao.updateCountState(currentState);//更新当前统计状态
                } else {
                    System.out.println("noTotalEners");
                }
                // 如果存在ReportData。则证明返回的是统计数据的xml
                if(xmlStr.indexOf("ReportData")>-1){
                    people = XmlUtil.xmlToPeople(xmlStr);
                    dao.savePeople(people);
                }
            }
 
        }catch (Exception e){
            System.out.println("exception:"+e.getMessage());
        }
        finally {
            try {
                if(iWriter !=null)
                    iWriter.close();
                if(iReader !=null)
                    iReader.close();
                if(client !=null)
                    client.close();
 
            }catch (IOException e){
                System.out.println("finally exception:"+e.getMessage());
            }
        }
    }
 
    public static void sendAck(BufferedWriter iWriter) {
        String statusLine = "HTTP/1.0 200 OK"+CRLF;
        String contentTypeLine = "Content-type: text/html"+CRLF ;
        String connectionAlive = "Connection: Keep-Alive"+CRLF ;
        String contentLengthLine = "Content-Length: 0"+CRLF +CRLF;
 
        try {
            iWriter.write(statusLine);
            iWriter.write(contentTypeLine);
            iWriter.write(connectionAlive);
            iWriter.write(contentLengthLine);
            iWriter.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
 
    // Send nack
    public static void sendNack(BufferedWriter iWriter) {
        String statusLine = "HTTP/1.1 400 Bad Request" + CRLF;
        String contentTypeLine = "Content-type: text/html" + CRLF;
        String connectionClose = "Connection: close" + CRLF;
        String content = "<ErrorList><Error>error</Error></ErrorList>";
        String contentLengthLine = "Content-Length: "
                + (new Integer(content.length())).toString() + CRLF + CRLF;
 
        try {
            iWriter.write(statusLine);
            iWriter.write(contentTypeLine);
            iWriter.write(connectionClose);
            iWriter.write(contentLengthLine);
            iWriter.write(content);
            iWriter.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}