liuxiaolong
2019-11-07 2f3e3ec2c0cad87eab8a6d170caffc06a4e11c2d
src/main/java/com/cloud/count/listener/CountThread.java
@@ -14,7 +14,7 @@
public class CountThread implements Runnable {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    static boolean isServerReboot = false;
    private CountDao dao;
@@ -36,15 +36,16 @@
        try{
            iReader = new BufferedReader(new InputStreamReader(client.getInputStream(), encoding));
            iWriter = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), encoding));
            String msgStr = "";
            String xmlStr = "";
            People people = null;
            while (true) {
                String str = iReader.readLine();
                msgStr = iReader.readLine();
                sendAck(iWriter);
                String xmlStr = XmlUtil.toXmlStr(str);
                xmlStr = XmlUtil.toXmlStr(msgStr);
                // 如果存在TotalEnters。则证明返回的是实时数据的xml
                People people = null;
                if(xmlStr.indexOf("TotalEnters")>-1){
                    people = XmlUtil.realTimeXmlToPeople(xmlStr);
                    int inCreEnter = CountServiceImpl.addEnter(people.getEnters());//进入人数增量
@@ -55,36 +56,12 @@
                    people.setEnters(CountServiceImpl.totalEnters);//将累加后的数量存到数据库中
                    people.setExits(CountServiceImpl.totalExits);
                    boolean resultB = dao.savePeople(people);
                    System.out.println("totalEnters:"+CountServiceImpl.totalEnters);
                    //总进入数量或者总出去数量,不一定哪一个先到2字节int的上限65535
//                                if( CountServiceImpl.totalEnters < CountServiceImpl.baseEnters){
//                                    //表示统计设备重新启动了,统计参数归零
//                                    CountServiceImpl.initCountArgs(people);
//                                }
                    op: if(!isServerReboot){
                        isServerReboot = true;
                        CountState state = dao.getCountState();//恢复统计状态
                        Date nowDate = new Date();
                        Date BaseDate = new Date(state.getBaseTime()*1000L);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        if(!sdf.format(nowDate).equals(sdf.format(BaseDate))){//历史数据不等于当天
                            CountServiceImpl.baseEnters  = people.getEnters();
                            CountServiceImpl.baseExits = people.getExits();
                            CountServiceImpl.realtimeBaseEnters = people.getEnters();
                            CountServiceImpl.realtimebBaseExits =  people.getExits();
                            CountServiceImpl.errorCount = 0;
                            break op;
                        }
                        //还原状态
                        CountServiceImpl.baseEnters  = state.getBaseEnters();
                        CountServiceImpl.baseExits = state.getBaseExits();
                        CountServiceImpl.totalEnters = state.getTotalEnters();
                        CountServiceImpl.totalExits = state.getTotalExits();
                        CountServiceImpl.realtimeBaseEnters = state.getRealtimeBaseEnters();
                        CountServiceImpl.realtimebBaseExits = state.getRealtimeBaseExits();
                        CountServiceImpl.errorCount = state.getErrorCount();
                    }
                    //总进入人数小于总出去的人数,表示统计设备在放学时间(出去的人数大于进入的人数)重启了
                    if(CountServiceImpl.totalEnters < CountServiceImpl.totalExits){
@@ -112,8 +89,6 @@
                }
            }
        }catch (IOException ex){
        }catch (Exception e){
            System.out.println("exception:"+e.getMessage());
        }
@@ -135,13 +110,13 @@
    public static void sendAck(BufferedWriter iWriter) {
        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;;
        String connectionAlive = "Connection: Keep-Alive"+CRLF ;
        String contentLengthLine = "Content-Length: 0"+CRLF +CRLF;
        try {
            iWriter.write(statusLine);
            iWriter.write(contentTypeLine);
            iWriter.write(connectionClose);
            iWriter.write(connectionAlive);
            iWriter.write(contentLengthLine);
            iWriter.flush();
        } catch (IOException ex) {