liuxiaolong
2019-11-02 bcffe049acc4d80b21d923f5c002496c280d5668
src/main/java/com/cloud/count/listener/CountListener.java
@@ -2,6 +2,7 @@
import com.cloud.count.dao.CountDao;
import com.cloud.count.model.CountState;
import com.cloud.count.model.People;
import com.cloud.count.service.CountService;
import com.cloud.count.service.serviceImpl.CountServiceImpl;
import com.cloud.count.utils.XmlUtil;
import org.slf4j.Logger;
@@ -62,29 +63,38 @@
                        byte[] b=new byte[1024];
                        is.read(b);
                        String str = new String(b);
//                        logger.info(str);
                        sendAck(iWriter);
                        try{
                            String xmlStr = XmlUtil.toXmlStr(str);
                            // 如果存在TotalEnters。则证明返回的是实时数据的xml
                            People people = null;
                            if(xmlStr.indexOf("TotalEnters")>-1){
                                people = XmlUtil.realTimeXmlToPeople(xmlStr);
                                dao.savePeople(people);
                                CountServiceImpl.totalEnters = people.getEnters();
                                CountServiceImpl.totalExits = people.getExits();
                                int inCreEnter = CountServiceImpl.addEnter(people.getEnters());//进入人数增量
                                int inCreExit = CountServiceImpl.addExit(people.getExits());//出去人数增量
                                System.out.println("TotalEnters:"+people.getEnters());
                                CountServiceImpl.totalEnters += inCreEnter;
                                CountServiceImpl.totalExits += inCreExit;
                                if( CountServiceImpl.totalEnters < CountServiceImpl.baseEnters){
                                    //表示统计设备重新启动了,统计参数归零
                                    CountServiceImpl.initCountArgs(people);
                                }
                                people.setEnters(CountServiceImpl.totalEnters);//将累加后的数量存到数据库中
                                people.setExits(CountServiceImpl.totalExits);
                                boolean resultB = dao.savePeople(people);
                                //总进入数量或者总出去数量,不一定哪一个先到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))){
                                    if(!sdf.format(nowDate).equals(sdf.format(BaseDate))){//历史数据不等于当天
                                        CountServiceImpl.baseEnters  = people.getEnters();
                                        CountServiceImpl.baseExits = people.getExits();
                                        CountServiceImpl.realtimeBaseEnters = people.getEnters();
@@ -92,6 +102,7 @@
                                        CountServiceImpl.errorCount = 0;
                                        break op;
                                    }
                                    //还原状态
                                    CountServiceImpl.baseEnters  = state.getBaseEnters();
                                    CountServiceImpl.baseExits = state.getBaseExits();
                                    CountServiceImpl.totalEnters = state.getTotalEnters();
@@ -117,6 +128,8 @@
                                currentState.setErrorCount(CountServiceImpl.errorCount);
                                currentState.setBaseTime(people.getTimestamp());
                                dao.updateCountState(currentState);//更新当前统计状态
                            } else {
                                System.out.println("noTotalEners");
                            }
                            // 如果存在ReportData。则证明返回的是统计数据的xml
                            if(xmlStr.indexOf("ReportData")>-1){
@@ -124,7 +137,7 @@
                                dao.savePeople(people);
                            }
                        }catch (Exception e){
                            System.out.println(e.getMessage());
                        }
                    }catch (IOException ex){