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

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.jkdata.entity.HistoryPoint;
import com.jkdata.entity.SafeArea;
import com.jkdata.entity.ThirdPosition;
import com.jkdata.service.IThirdService;
import com.jkdata.tool.Result;
import io.swagger.annotations.ApiOperation;
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.List;
import java.util.Map;

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

    @Autowired
    private IThirdService thirdService;

    @ApiOperation(value="接收定位数据", notes="接收定位数据")
    @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
     */
    @ApiOperation(value="获取人员实时定位", notes="获取人员实时定位")
    @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
     */
    @ApiOperation(value="获取静态资源信息", notes="获取静态资源信息")
    @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
     */
    @ApiOperation(value="获取历史轨迹", notes="获取历史轨迹")
    @GetMapping("/positionByDeviceId")
    public Result<List<HistoryPoint>> getPositionByDeviceId(String deviceId, String startTime, String endTime) {
        Result<List<HistoryPoint>> result = new Result<List<HistoryPoint>>();
        List<HistoryPoint> list = thirdService.getPositionByDeviceId(deviceId, startTime, endTime);
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

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

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

    /**
     * 获取安全区域
     * @return
     */
    @ApiOperation(value="获取安全区域", notes="获取安全区域")
    @GetMapping("/safeArea")
    public Result<List<SafeArea>> getSafeArea() {
        Result<List<SafeArea>> result = new Result<List<SafeArea>>();
        List<SafeArea> list = thirdService.getSafeAreaNew();
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }
}