IThirdServiceImpl.java 9.2 KB
package com.jkdata.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.jkdata.tool.CommonConstant;
import com.jkdata.entity.ThirdPosition;
import com.jkdata.mapper.ThirdMapper;
import com.jkdata.service.IThirdService;
import com.jkdata.tool.MD5Util;
import com.jkdata.tool.RedisUtil;
import com.jkdata.tool.UUIDGenerator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IThirdServiceImpl implements IThirdService {

    @Autowired
    private ThirdMapper thirdMapper;

    private static DecimalFormat df = new DecimalFormat("######0.0000");
    private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void savePosition(JSONObject jsonObject) {
        Date t = new Date();
        boolean ok = addMsgLog(jsonObject,sf.format(t));//添加接收报文日志
        if(ok){
            ThirdPosition thirdPosition = new ThirdPosition();
            thirdPosition.setNodeId(jsonObject.getString("NodeId"));
            Map xyMap = jsonObject.getObject("position",Map.class);
            thirdPosition.setPositionx(xyMap.get("x").toString());
            thirdPosition.setPositiony(xyMap.get("y").toString());
            thirdPosition.setSystemId(jsonObject.getString("SystemId"));
            thirdPosition.setType(jsonObject.getString("Type"));
            thirdPosition.setGroup(jsonObject.getString("group"));
            thirdPosition.setGroupNumber(jsonObject.getInteger("groupNumber"));
            thirdPosition.setCreateTime(t);
            //判断是B1、B2
            changCoordinateB1(thirdPosition);
//            changCoordinateB2(thirdPosition);
        }


    }

    /**
     * 收报文日志
     * @param jsonObject
     */
    private boolean addMsgLog(JSONObject jsonObject, String format) {
        String sql = "INSERT INTO sys_msg_log  ( mag_log,create_by,create_time )  VALUES  " +
                "( '"+jsonObject.toJSONString()+"','"+CommonConstant.CMCC+"','"+format+"' )";
        System.out.println("**********"+sql);
        boolean ok = addDataToDataBase(sql);
        return ok;
    }

    //SQL插入数据库
    private boolean addDataToDataBase(String sql) {
        boolean ok = thirdMapper.addDataToDataBase(sql);
        return ok;
    }

    public static void main(String[] args) {
        String x1 = "463.7121276855469";
        String y1 = "142.59878540039062";

        double fd_x = CommonConstant.BLC1*(Double.valueOf(x1)-CommonConstant.LT_B1_X)+CommonConstant.LT_B1_X;
        double fd_y = CommonConstant.BLC1*(Double.valueOf(y1)+CommonConstant.LT_B1_Y)-CommonConstant.LT_B1_Y;
        System.out.println("放大后的坐标为:X:"+fd_x+",Y:"+fd_y);

        double a = CommonConstant.MQ_B1_X - CommonConstant.LT_B1_X;//原点偏移x
        double b = CommonConstant.MQ_B1_Y - CommonConstant.LT_B1_Y;//原点偏移y
        System.out.println("偏移a:"+a+",b:"+b);

        double x = Double.valueOf(fd_x) +  a;
        double y = Double.valueOf(fd_y) +  b;
        System.out.println("新坐标为:X:"+x+",Y:-"+y);
    }

    //B1定位坐标
    private void changCoordinateB1(ThirdPosition thirdPosition) {
        String x1 = thirdPosition.getPositionx();
        String y1 = thirdPosition.getPositiony();

        double fd_x = CommonConstant.BLC1*(Double.valueOf(x1)-CommonConstant.LT_B1_X)+CommonConstant.LT_B1_X;
        double fd_y = CommonConstant.BLC1*(Double.valueOf(y1)+CommonConstant.LT_B1_Y)-CommonConstant.LT_B1_Y;
        System.out.println("放大后的坐标为:X:"+fd_x+",Y:"+fd_y);

        double a = CommonConstant.MQ_B1_X - CommonConstant.LT_B1_X;//原点偏移x
        double b = CommonConstant.MQ_B1_Y - CommonConstant.LT_B1_Y;//原点偏移y
        System.out.println("偏移a:"+a+",b:"+b);

        double x = Double.valueOf(fd_x) +  a;
        double y = Double.valueOf(fd_y) +  b;
        System.out.println("新坐标为:X:"+x+",Y:-"+y);

        //原点偏移
//        double new_fd_x = 10*(Double.valueOf(x)-CommonConstant.MQ_B1_X)+CommonConstant.MQ_B1_X;
//        double new_fd_y = 10*(Double.valueOf(y)+CommonConstant.MQ_B1_Y)-CommonConstant.MQ_B1_Y;
//        System.out.println("新放大后的坐标为:X:"+new_fd_x+",Y:"+new_fd_y);
        thirdPosition.setPositionx(String.valueOf(x));
        thirdPosition.setPositiony(String.valueOf(y));
        addPositionInfo(thirdPosition);//添加转换后坐标信息
    }

    //B2定位坐标
    private void changCoordinateB2(ThirdPosition thirdPosition) {
        String x1 = thirdPosition.getPositionx();
        String y1 = thirdPosition.getPositiony();

        double fd_x = CommonConstant.BLC2*(Double.valueOf(x1)-CommonConstant.LT_B2_X)+CommonConstant.LT_B2_X;
        double fd_y = CommonConstant.BLC2*(Double.valueOf(y1)+CommonConstant.LT_B2_Y)-CommonConstant.LT_B2_Y;
        System.out.println("放大后的坐标为:X:"+fd_x+",Y:"+fd_y);

        double a = CommonConstant.MQ_B2_X - CommonConstant.LT_B2_X;//原点偏移x
        double b = CommonConstant.MQ_B2_Y - CommonConstant.LT_B2_Y;//原点偏移y
        System.out.println("偏移a:"+a+",b:"+b);

        double x = Double.valueOf(fd_x) +  a;
        double y = Double.valueOf(fd_y) +  b;
        System.out.println("新坐标为:X:"+x+",Y:-"+y);

        thirdPosition.setPositionx(String.valueOf(x));
        thirdPosition.setPositiony(String.valueOf(y));
        addPositionInfo(thirdPosition);//添加转换后坐标信息
    }

    private boolean addPositionInfo(ThirdPosition thirdPosition) {
        String uuid = UUIDGenerator.creatUUID();
        String sql = "INSERT INTO sys_cmcc_position ( `position_id`,`node_id`,`system_id`,`type`,`group`," +
                "`group_number`,`position_x`,`position_y`,`create_by`,`create_time` )  VALUES " +
                " ( '"+uuid+"','"+thirdPosition.getNodeId()+"','"+thirdPosition.getSystemId()+"','"+thirdPosition.getType()+"','"+thirdPosition.getGroup()+"'," +
                ""+thirdPosition.getGroupNumber()+",'"+thirdPosition.getPositionx()+"','"+thirdPosition.getPositiony()+"','"+CommonConstant.CMCC+"','"+sf.format(thirdPosition.getCreateTime())+"' )";
        System.out.println("**********"+sql);
        boolean ok = addDataToDataBase(sql);
        return ok;
    }

    @Override
    public String getBuildingAllInfo() {
        HashMap<String,Object> map = new HashMap<>();
        String tmsp = String.valueOf(System.currentTimeMillis()/1000);
        map.put("bldId",CommonConstant.BLD_ID);
        map.put("ts",tmsp);
        String str = StringUtils.join(Arrays.asList(new String[]{CommonConstant.BLD_ID,tmsp,CommonConstant.CMCC_KEY}),",")
                .replace(",","");
        System.out.println("sign参数:"+str);
        String strMd5 = MD5Util.getMD5LowerCase(str);
        System.out.println("加密后参数:"+str);
        map.put("sgn",strMd5);
        String post = HttpUtil.post(CommonConstant.BUILD_URL,map);
        System.out.println("请求结果:"+post);
        return post;
    }

    @Override
    public String getPositionByDeviceId(String deviceId,String startTime,String endTime) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("buildingId",CommonConstant.BLD_ID);
        map.put("deviceId",deviceId);
        map.put("page","0");
        map.put("count","200");
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        String post = HttpUtil.post(CommonConstant.HISTORY_URL,map);
        return post;
    }

    @Override
    public String getAlarmListByCount() {
        HashMap<String,Object> map = new HashMap<>();
        String tmsp = String.valueOf(System.currentTimeMillis()/1000);
        map.put("bldId",CommonConstant.BLD_ID);
        map.put("ts",tmsp);
        map.put("count","10");
        String str = StringUtils.join(Arrays.asList(new String[]{CommonConstant.BLD_ID,"10",tmsp,CommonConstant.CMCC_KEY}),",")
                .replace(",","");
        String strMd5 = MD5Util.getMD5LowerCase(str);
        map.put("sgn",strMd5);
        String post = HttpUtil.post(CommonConstant.ALARM_URL,map);
        return post;
    }

    @Override
    public String getWeekAlarmState() {
        HashMap<String,Object> map = new HashMap<>();
        String tmsp = String.valueOf(System.currentTimeMillis()/1000);
        map.put("bldId",CommonConstant.BLD_ID);
        map.put("ts",tmsp);
        String str = StringUtils.join(Arrays.asList(new String[]{CommonConstant.BLD_ID,tmsp,CommonConstant.CMCC_KEY}),",")
                .replace(",","");
        String strMd5 = MD5Util.getMD5LowerCase(str);
        map.put("sgn",strMd5);
        String post = HttpUtil.post(CommonConstant.WEEK_ALARM_URL,map);
        return post;
    }

    @Override
    public ThirdPosition getRealTimeLocation(String deviceId) {
        ThirdPosition thirdPosition = new ThirdPosition();
        thirdPosition = thirdMapper.getRealTimeLocation(deviceId);
        return thirdPosition;
    }

    @Override
    public String getSafeArea() {
        String get = HttpUtil.get(CommonConstant.SAFE_AREA_URL+"?buildingId=10");
        return get;
    }


}