OutEntrance.java 7.73 KB
package com.bsth.data.zndd;
import com.bsth.common.ResponseCode;
import com.bsth.controller.station_signs.carMonitorThread;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.data.schedule.ScheduleComparator;
import com.bsth.data.adjg.domain.LargeGap;
import com.bsth.entity.logger.StationSignsLogger;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.zndd.StationPeopleLogger;
import com.bsth.service.logger.StationSignsLoggerService;
import com.bsth.service.zndd.ZnddLoggerService;
import com.bsth.websocket.handler.SendUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 对外接口
 * 与站牌、小程序对接接口
 * 调度预案通用接口
 */
@RestController
@RequestMapping("/out")
public class OutEntrance {

    @Autowired
    StationSignsLoggerService stationSignsLoggerService;

    @Autowired
    SendUtils sendUtils;
    @Autowired
    DayOfSchedule dayOfSchedule;

    @Autowired
    ZnddLoggerService znddLoggerService;
    @Autowired
    carMonitorThread carMonitorThread;


    @Value("${dc.imgurl}")
    private  String imgurl; //存储图片地址
    
    @Value("${dc.profile}")
    private  String profile; //存储图片地址
    
    static String url= "http://58.34.52.130:9777/xxfb/carMonitor?";  //信息发布地址
    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    
    
    /*
        调度屏小程序接口。
     */
    @RequestMapping(value = "/OutCar", method = RequestMethod.POST)
    public Map OutCarOutCar(@RequestParam Map m,@RequestBody Map<String, String> map) {


        Map rtn = new HashMap<>();
        try {
    
            ObjectMapper mapper = new ObjectMapper();
            m.put("image", uploadBase64Img(map.get("img").toString()));
            //map转换实体类
            StationSignsLogger ssLogger = mapper.convertValue(m, StationSignsLogger.class);
            //stationSignsLoggerService.save(ssLogger);
            
            //线调页面推送
            sendUtils.stationcf(m);
            
            //查询班次情况自动回复
            //当前日期
            //String rq = DateUtils.dqDate();
            List<Map> dzList = carMonitorThread.carMonitor(ssLogger.getLineCode(), ssLogger.getDir(), ssLogger.getStation());  //信息发布接口
            if (dzList.size() > 0){
                rtn.put("message","车辆还有"+dzList.get(0).get("sj")+"抵达,请耐心等待");
            }else {
                //筛选方向
                List<ScheduleRealInfo> rs = dayOfSchedule.findByLineCode(ssLogger.getLineCode());
                //排序
                Collections.sort(rs,new ScheduleComparator.FCSJ());
                SimpleDateFormat sdf= new SimpleDateFormat("HH:ss");
                String sjtext = "";
                LocalTime t1 = LocalTime.parse(sdf.format(new Date()), DateTimeFormatter.ofPattern("HH:mm"));
                for (ScheduleRealInfo sr:rs) {
                    LocalTime t2 = LocalTime.parse(sr.getFcsj(), DateTimeFormatter.ofPattern("HH:mm"));
                    //判断上下行 
                    if(t1.isAfter(t2)){
                        sjtext = sr.getFcsj();
                    }
                }
                
                rtn.put("message","航津路与杨高北路交叉口拥堵,请耐心等待");
                rtn.put("message_els","The intersection of Hangjin Road and Yanggao North Road is congested, please be patient and wait");
            }
            
            
            
            rtn.put("status",ResponseCode.SUCCESS);
        } catch (Exception e) {
            rtn.put("status", ResponseCode.ERROR);
            logger.info(e.getMessage());
        }
        
       return rtn;
    }
    /*
    智能调度接口--通用测试接口
     */
    @RequestMapping(value="/zjket",method = RequestMethod.GET)
    public Map Znddjk(@RequestParam Map m) {
        Map rtn = new HashMap<>();

        try {
            ObjectMapper mapper = new ObjectMapper();
            //线调页面推送
            sendUtils.sendzndd(m);

            rtn.put("status",ResponseCode.SUCCESS);
        } catch (Exception e) {
            rtn.put("status",ResponseCode.ERROR);
            logger.info("智能调度推送----",e);
        }

        return rtn;
    }

    /*
  智能调度接口-大客流
   */
    @RequestMapping(value="/dkl",method = RequestMethod.POST)
    public Map ddkl(@RequestParam Map m) {
        Map rtn = new HashMap<>();

        try {
            ObjectMapper mapper = new ObjectMapper();
            //map转换实体类
            StationPeopleLogger ssLogger = mapper.convertValue(m, StationPeopleLogger.class);
            
            ssLogger.setType("大客流");
       
            //保存记录
            znddLoggerService.save(ssLogger);
            //线调页面推送
            //sendUtils.sendzndd(ssLogger);

            rtn.put("status",ResponseCode.SUCCESS);
        } catch (Exception e) {
            rtn.put("status", ResponseCode.ERROR);
            Logger logger = LoggerFactory.getLogger(this.getClass());
        }
        return rtn;
    }




    /**
     * 保存base64图片
     * @param base64Str base64文件
     * @param  上传地址(示例:D:\\1.png)
     * @return
     */
    public  String uploadBase64Img(String base) {

        //获取年月日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //将年、月、日存入数组
        String[] time = sdf.format(new Date()).split("-");
        // 文件保存地址 默认地址为xxxx/年/月/日
        String url = "avat/"+time[0]+"/"+time[1]+"/"+time[2];
        String destDir = imgurl+"/"+url;
        
        
        base=base.replaceAll("data:image/png;base64,","");
        BASE64Decoder decoder =  new BASE64Decoder();
        byte[] imageByte = null;
        try{
            imageByte = decoder.decodeBuffer(base);
            for (int i = 0; i < imageByte.length; ++i) {
                if (imageByte[i] < 0) {// 调整异常数据
                    imageByte[i] += 256;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (imageByte.length>0) {
            try {
                String filepath = destDir;
                File destfile = new File(filepath);
                if (!destfile.exists()) {
                    destfile.mkdirs();
                }
                //文件新名称
                String fileNameNew = "666"+ ".png";
                File f = new File(destfile.getAbsoluteFile() + File.separator + fileNameNew);
                // 将字符串转换成二进制,用于显示图片
                // 将上面生成的图片格式字符串 imgStr,还原成图片显示
                InputStream in = new ByteArrayInputStream(imageByte);
                FileOutputStream fos = new FileOutputStream(f);
                byte[] buf = new byte[1024];
                int length;
                length = in.read(buf, 0, buf.length);

                while (length != -1) {
                    fos.write(buf, 0, length);
                    length = in.read(buf);
                }
                fos.flush();
                fos.close();
                in.close();
                return "/"+profile + "/" + url+"/"+fileNameNew;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }
        return null;
    }
    
    
    
}