XmlInfoPublishController.java 10.6 KB
package com.bsth.controller;

import com.bsth.controller.dto.CarMonitorEntity;
import com.bsth.controller.dto.DispatchScreen;
import com.bsth.data.BasicCacheData;
import com.bsth.data.charts_data.AccuracyDataHandler;
import com.bsth.data.geo.GeoCacheData;
import com.bsth.data.gps.GpsCacheData;
import com.bsth.data.history.HistoryConsumeTimeDataHandler;
import com.bsth.data.schedule.entity.ScheduleRealInfo;
import com.bsth.entity.GpsEntity;
import com.bsth.entity.Line;
import com.bsth.entity.StationRoute;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 信息发布,xml 格式
 */
@RestController
@RequestMapping("xxfb")
public class XmlInfoPublishController {


    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    AccuracyDataHandler accuracyDataHandler;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    static DecimalFormat df = new DecimalFormat("#.00");

    @RequestMapping("getLineInfoByName")
    public String getLineInfoByName(@RequestParam String linename, @RequestParam String t) {
        StringBuilder sb = new StringBuilder();
        try {
            if (StringUtils.isBlank(linename))
                return "";

            Collection<Line> vs = BasicCacheData.code2LineMap.values();

            Line line = null;
            for (Line v : vs) {
                if (linename.equals(v.getName())) {
                    line = v;
                    break;
                }
            }

            if (null == line)
                return "";

            //to xml
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.append("<linedetail>");

            sb.append("<end_earlytime>" + line.getEndStationFirstTime() + "</end_earlytime>");
            sb.append("<end_latetime>" + line.getEndStationEndTime() + "</end_latetime>");
            sb.append("<end_stop>" + line.getEndStationName() + "</end_stop>");
            sb.append("<line_id>" + line.getLineCode() + "</line_id>");
            sb.append("<line_name>" + line.getName() + "</line_name>");
            sb.append("<start_earlytime>" + line.getStartStationFirstTime() + "</start_earlytime>");
            sb.append("<start_latetime>" + line.getStartStationEndTime() + "</start_latetime>");
            sb.append("<start_stop>" + line.getStartStationName() + "</start_stop>");

            sb.append("</linedetail>");
        } catch (Exception e) {
            logger.error("", e);
        }
        return sb.toString();
    }

    @RequestMapping("getLine")
    public String getLine(@RequestParam String lineid, @RequestParam String t) {
        StringBuilder sb = new StringBuilder();
        try {

            List<StationRoute>[] listArray = GeoCacheData.find(lineid);

            List<StationRoute> srs0 = listArray[0];
            List<StationRoute> srs1 = listArray[1];

            //to xml

            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.append("<lineInfoDetails>");

            if (null != srs0) {
                //上行
                sb.append("<lineResults0>");
                sb.append("<direction>true</direction>");
                sb.append("<stops>");
                for (StationRoute s : srs0) {
                    sb.append("<stop>");
                    sb.append("<zdmc>" + s.getName() + "</zdmc>");
                    sb.append("<id>" + s.getStationCode() + "</id>");
                    sb.append("</stop>");
                }
                sb.append("</stops>");
                sb.append("</lineResults0>");
            }


            if (null != srs1) {
                //下行
                sb.append("<lineResults1>");
                sb.append("<direction>false</direction>");
                sb.append("<stops>");
                for (StationRoute s : srs1) {
                    sb.append("<stop>");
                    sb.append("<zdmc>" + s.getName() + "</zdmc>");
                    sb.append("<id>" + s.getStationCode() + "</id>");
                    sb.append("</stop>");
                }
                sb.append("</stops>");
                sb.append("</lineResults1>");
            }

            sb.append("</lineInfoDetails>");
        } catch (Exception e) {
            logger.error("", e);
        }
        return sb.toString();
    }

    @RequestMapping("carMonitor")
    public String carMonitor(@RequestParam String lineid, @RequestParam String stopid
            , @RequestParam String direction, String t, HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();

        try {
            long ct = System.currentTimeMillis();
            int upDown = Integer.parseInt(direction);
            List<GpsEntity> list = GpsCacheData.findList(lineid, upDown);

            Map<String, Double> disMap = new HashMap<>();
            Map<String, Integer> snMap = new HashMap<>();
            List<StationRoute> srs = GeoCacheData.find(lineid, upDown);

            StationRoute es = GeoCacheData.findByCode(lineid, upDown, stopid), s = null;

            List<GpsEntity> vs = new ArrayList<>();
            ScheduleRealInfo sch = null;
            double sum;
            int sn;

            int serialNo = es.getSerialNo();
            for (GpsEntity gps : list) {
                if (ct - gps.getTimestamp() > 1000 * 60 * 10)
                    continue;//掉线超过10分钟,不发布

                //短时间掉线,平滑过渡
                gps.smoothTransition(ct);

                s = GeoCacheData.findByCode(gps);
                if (null == s || s.getSerialNo() > serialNo)
                    continue;

                if ("B".equals(s.getMark()) && gps.getInOut() == 1)
                    continue;//起点站 站内车辆

                sch = gps.getSch();
                if (null != sch) {//有班次
                    if ("ldks".equals(sch.getBcType())
                            || "venting".equals(sch.getBcType())
                            || "in".equals(sch.getBcType())
                            || "out".equals(sch.getBcType()))
                        continue;//非营运班次

                    if (serialNo > sch.getQdzNo() && serialNo < sch.getZdzNo())
                        vs.add(gps);
                } else {
                    vs.add(gps);
                }

                sum = 0;
                sn = 1;

                for (StationRoute sr : srs) {
                    if (sr.getSerialNo() < serialNo
                            && sr.getSerialNo() > gps.getStationNo()) {
                        sum += sr.getLength();
                        sn++;
                    }
                }

                disMap.put(gps.getDeviceId(), sum);
                snMap.put(gps.getDeviceId(), sn);
            }


            Map<String, Integer> forecast = HistoryConsumeTimeDataHandler.forecastEnd(vs, stopid);
            List<CarMonitorEntity> reals = new ArrayList<>();
            CarMonitorEntity cme;
            for (GpsEntity v : vs) {
                if (v.getStationCode().equals(stopid)
                        && v.getInOut() != 1)
                    continue; //已出当前站

                cme = new CarMonitorEntity();
                cme.setGps(v);
                cme.setPlate(BasicCacheData.device2plateMap.get(v.getDeviceId()));
                if (v.getStationCode().equals(stopid)
                        && v.getInOut() == 1
                        && (null != v.getInStationTime() && ct - v.getInStationTime() < 40)) {

                    cme.setStopdis(0);
                    cme.setDistance("0");
                    cme.setTime(0);
                } else {
                    cme.setStopdis(snMap.get(v.getDeviceId()));
                    cme.setDistance(df.format((disMap.get(v.getDeviceId()) + v.getDistance())));
                    cme.setTime(forecast.get(v.getDeviceId()));
                }

                reals.add(cme);
            }
            //to xml

            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.append("<result>");
            sb.append("<cars lineid=\"" + lineid + "\">");

            for (CarMonitorEntity obj : reals) {
                sb.append("<car>");
                sb.append("<terminal>" + obj.getPlate() + "</terminal>");
                sb.append("<stopdis>" + obj.getStopdis() + "</stopdis>");
                sb.append("<distance>" + obj.getDistance() + "</distance>");
                sb.append("<time>" + obj.getTime() + "</time>");
                sb.append("</car>");
            }

            //预测准确性分析
            accuracyDataHandler.mark(reals, stopid, request);

            sb.append("</cars>");
            sb.append("</result>");
        } catch (Exception e) {
            logger.error("", e);
        }
        return sb.toString();
    }

    @RequestMapping("getdispatchScreen")
    public String getdispatchScreen(@RequestParam String lineid, @RequestParam String direction, String t) {
        StringBuilder sb = new StringBuilder();

        try {
            String sql = "select cph as plate,dfsj as time,line_code from bsth_t_clfcxxb where line_code='" + lineid + "' and updown=" + direction;

            List<DispatchScreen> list = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(DispatchScreen.class));

            //to xml

            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.append("<result>");
            sb.append("<cars lineid=\"" + lineid + "\">");

            char[] cs;
            for (DispatchScreen d : list) {
                cs = d.getTime().toCharArray();
                sb.append("<car>");
                sb.append("<vehicle>" + d.getPlate() + "</vehicle>");
                sb.append("<time>" + (cs[0] + "" + cs[1] + ":" + cs[2] + "" + cs[3] + ":00") + "</time>");
                sb.append("</car>");
            }

            sb.append("</cars>");
            sb.append("</result>");
        } catch (Exception e) {
            logger.error("", e);
        }
        return sb.toString();
    }
}