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

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.jkdata.entity.ThirdPosition;
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();

    }

    /**
     * 获取人员实时定位
     * @return
     */
    @GetMapping("/realTimeLocation")
    public Result<ThirdPosition> getRealTimeLocation(String deviceId) {
        Result<ThirdPosition> result = new Result<ThirdPosition>();
        ThirdPosition thirdPosition = thirdService.getRealTimeLocation(deviceId);
        result.setSuccess(true);
        result.setResult(thirdPosition);
        return result;
    }

    /**
     * 获取静态资源信息
     * @return
     */
    @GetMapping("/buildingAllInfo")
    public Result<JSONObject> getBuildingAllInfo() {
        Result<JSONObject> result = new Result<JSONObject>();
        String res = thirdService.getBuildingAllInfo();
        JSONObject json = JSONObject.parseObject(res);
        result.setSuccess(true);
        result.setResult(json);
        return result;
    }

    /**
     * 获取历史轨迹
     * @param deviceId
     * @return
     */
    @GetMapping("/positionByDeviceId")
    public Result<JSONObject> getPositionByDeviceId(String deviceId,String startTime,String endTime) {
        Result<JSONObject> result = new Result<JSONObject>();
        String res = thirdService.getPositionByDeviceId(deviceId, startTime, endTime);
        JSONObject json = JSONObject.parseObject(res);
        result.setSuccess(true);
        result.setResult(json);
        return result;
    }

    /**
     * 获取告警信息-最新 n 条
     * @return
     */
    @GetMapping("/alarmListByCount")
    public Result<JSONObject> getAlarmListByCount() {
        Result<JSONObject> result = new Result<JSONObject>();
        String res = thirdService.getAlarmListByCount();
        JSONObject json = JSONObject.parseObject(res);
        result.setSuccess(true);
        result.setResult(json);
        return result;
    }

    /**
     * 获取最近7天的告警统计数据
     * @return
     */
    @GetMapping("/weekAlarmState")
    public Result<JSONObject> getWeekAlarmState() {
        Result<JSONObject> result = new Result<JSONObject>();
        String res = thirdService.getWeekAlarmState();
        JSONObject json = JSONObject.parseObject(res);
        result.setSuccess(true);
        result.setResult(json);
        return result;
    }
}