ThirdController.java 2.4 KB
package com.jkdata.controller;

import com.alibaba.fastjson.JSONObject;
import com.jkdata.service.IThirdService;
import com.jkdata.tool.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/3d")
public class ThirdController {

    @Autowired
    private IThirdService thirdService;

    @PostMapping("/receivePosition")
    public Result<Map<String,Object>> save(HttpServletRequest request, HttpServletResponse response) {
        Result<Map<String,Object>> result = new Result<Map<String,Object>>();
        Map<String,Object> map = new HashMap<>();
        String resMsg = getPostData(request);
        JSONObject jsonObject = initData(resMsg);
        String type = jsonObject.getString("Type");
        //判断消息类型
        if("location_pos".equals(type)){
            map.put("message",resMsg);
            thirdService.savePosition(jsonObject);
            result.setResult(map);
            result.setSuccess(true);
        }else{
            result.error500("类型错误!");
        }
        return result;
    }

    //初始化数据
    private JSONObject initData(String resMsg) {
        resMsg = "{\"Gateway\":\"10030049\"," +
                "\"NodeId\":\"CB8A1DEB\"," +
                "\"SystemId\":\"10990145\"," +
                "\"Type\":\"location_pos\"," +
                "\"group\":\"5\"," +
                "\"groupNumber\":1," +
                "\"position\":{\"x\":214.44541185285127,\"y\":210.15474327170986}," +
                "\"scale\":0}";
        System.out.println("***"+System.currentTimeMillis()+"报文***"+resMsg);
        JSONObject jsonObject = new JSONObject();
        jsonObject = JSONObject.parseObject(resMsg);
        return jsonObject;
    }

    //解析推送报文
    private String getPostData(HttpServletRequest request) {
        StringBuffer data = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        try{
            reader = request.getReader();
            while (null!=(line = reader.readLine())){
                data.append(line);
            }
        }catch (Exception e){
        }
        return data.toString();

    }

}