GpsManager.java 4.73 KB
package cn.com.glsx.entmicro.manager;


import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.model.collision.GopBaseResponse;
import cn.com.glsx.entmicro.model.collision.GpsRecordsResponse;
import cn.com.glsx.entmicro.util.GopUtils;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.DateUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.oreframework.util.http.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * gps 定位服务
 */
@Slf4j
@Component
public class GpsManager {

    @Autowired
    private Environment environment;

    private final static String APPKEY = "48e5e13229b82c1b4e6e8c96151f0637";

    private static final String VERSION = "1.0.0";

    private static final String FORMAT = "json";

    private static final String SECRET = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";

    public List<GpsRecordsResponse> getGpsRecords(String sn, Date collisionTime) {
        try {
            Calendar beforeTime=Calendar.getInstance();
            beforeTime.setTime(collisionTime);
            beforeTime.add(Calendar.MINUTE, -3);
            Calendar afterTime=Calendar.getInstance();
            afterTime.setTime(collisionTime);
            afterTime.add(Calendar.MINUTE, 3);
            MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
            form.add("sn", sn);
            form.add("startTime", DateUtils.formatDate(beforeTime.getTime(), DateUtils.DATE_FORMAT_DATE_TIME));
            form.add("endTime", DateUtils.formatDate(afterTime.getTime(), DateUtils.DATE_FORMAT_DATE_TIME));
            form.add("method", Constants.GPS_RECORDES);
            form.add("app_key", APPKEY);
            form.add("v", VERSION);
            form.add("format", FORMAT);
            form.add("source", "scrm-ent");
            form.add("timestamp", String.valueOf(System.currentTimeMillis()));
            String sign = GopUtils.sign(form.toSingleValueMap(), SECRET);
            form.add("sign", sign);
            String url = environment.getProperty("gop.open.url.prefix");
            url = url + gainJointGetParams(form);
            log.info("请求gps服务参数[{}]", url);
            String result = HttpUtil.get(url,null);
            if(result != null) {
                log.info("请求gps服务,返回信息[{}]", result);
                result = result.replaceAll(":null", ":\"\"");
                //去掉商户名称尾缀
                String merchantNameAffix = environment.getProperty("replace.merchant.name.affix");
                if(StringUtils.isNotBlank(merchantNameAffix)) {
                    String[] affixs = merchantNameAffix.split(",");
                    for(String affix:affixs){
                        String reg = "(\"merchantName\":\"([^\"]*?))" + affix + "(\")";
                        result = result.replaceAll(reg, "$1$3");
                    }
                }
                GopBaseResponse response = JSONObject.parseObject(result, GopBaseResponse.class);
                if(response != null && response.isSuccess()) {
                    return response.getList(GpsRecordsResponse.class);
                } else {
                    throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "获取设备轨迹数据失败");
                }
            } else {
                log.info("请求gps返回数据为空!");
                return null;
            }
        } catch (Exception e) {
            log.error("请求gps服务异常", e);
            throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
        }
    }

    private static String gainJointGetParams (MultiValueMap<String, String> form) {
        Iterator<String> iter = form.keySet().iterator();
        StringBuilder param = new StringBuilder("");
        while (iter.hasNext()) {
            Object key = iter.next();
            String val = form.get(key).toString();
            val = val.substring(1, val.length() - 1);
            try {
                param.append(key).append("=").append(URLEncoder.encode(val, Consts.UTF_8.name())).append("&");
            } catch (Exception e) {
                log.info(e.getMessage(), e);
            }
        }
        return param.substring(0, param.length() - 1);
    }
}